GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-magic-int.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2020-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_ov_magic_int_h)
27 #define octave_ov_magic_int_h 1
28 
29 #include "octave-config.h"
30 
31 #include <iosfwd>
32 #include <string>
33 
34 #include "oct-inttypes-fwd.h"
35 
36 #include "ov-base.h"
37 #include "ov-re-mat.h"
38 #include "ov-base-scalar.h"
39 #include "ov-typeinfo.h"
40 
41 class octave_value_list;
42 
43 // Large integer scalar values. The uint64 or int64 value they contain may be
44 // accessed without loss of precision when needed (for example, when
45 // directly converted to a uint64 or int64 value). Otherwise, they
46 // behave like real scalars, so any operation on them will result in
47 // type conversion.
48 
49 template <typename T>
50 class
52 {
53 public:
54 
56  : octave_base_scalar<T> (0) { }
57 
58  octave_base_magic_int (const T& val)
59  : octave_base_scalar<T> (val) { }
60 
61  ~octave_base_magic_int () = default;
62 
63  // We return an octave_matrix here instead of an octave_scalar so
64  // that in expressions like A(2,2,2) = 2 (for A previously
65  // undefined), A will be empty instead of a 1x1 object.
66  octave_base_value * empty_clone () const { return new octave_matrix (); }
67 
68  // Although SCALAR is a protected member of the base class, it is not
69  // directly visible here without the explicit octave_base_slalar<T>::
70  // qualification. Why not?
71 
72  const T& scalar_ref () const { return octave_base_scalar<T>::scalar; }
73 
75 
76  octave_value do_index_op (const octave_value_list& idx,
77  bool resize_ok = false);
78 
79  octave::idx_vector index_vector (bool require_integers = false) const;
80 
81  octave_value any (int = 0) const { return scalar_ref () != T (0); }
82 
84 
85  bool is_storable () const { return false; }
86 
87  bool is_magic_int () const { return true; }
88 
89  bool vm_need_storable_call () const { return true; }
90 
91  bool is_real_scalar () const { return true; }
92 
93  bool isreal () const { return true; }
94 
95  bool is_double_type () const { return true; }
96 
97  bool isfloat () const { return true; }
98 
100  { return int8NDArray (dim_vector (1, 1), double_value ()); }
101 
103  { return int16NDArray (dim_vector (1, 1), double_value ()); }
104 
106  { return int32NDArray (dim_vector (1, 1), double_value ()); }
107 
109  { return int64NDArray (dim_vector (1, 1), double_value ()); }
110 
112  { return uint8NDArray (dim_vector (1, 1), double_value ()); }
113 
115  { return uint16NDArray (dim_vector (1, 1), double_value ()); }
116 
118  { return uint32NDArray (dim_vector (1, 1), double_value ()); }
119 
121  { return uint64NDArray (dim_vector (1, 1), double_value ()); }
122 
124  { return octave_int8 (double_value ()); }
125 
127  { return octave_int16 (double_value ()); }
128 
130  { return octave_int32 (double_value ()); }
131 
133  { return octave_int64 (double_value ()); }
134 
136  { return octave_uint8 (double_value ()); }
137 
139  { return octave_uint16 (double_value ()); }
140 
142  { return octave_uint32 (double_value ()); }
143 
145  { return octave_uint64 (double_value ()); }
146 
147  double double_value (bool = false) const
148  {
149  return scalar_ref ().double_value ();
150  }
151 
152  float float_value (bool = false) const
153  { return static_cast<float> (double_value ()); }
154 
155  double scalar_value (bool = false) const
156  { return double_value (); }
157 
158  float float_scalar_value (bool = false) const
159  { return float_value (); }
160 
161  Matrix matrix_value (bool = false) const
162  { return Matrix (1, 1, double_value ()); }
163 
164  FloatMatrix float_matrix_value (bool = false) const
165  { return FloatMatrix (1, 1, float_value ()); }
166 
167  NDArray array_value (bool = false) const
168  { return NDArray (dim_vector (1, 1), double_value ()); }
169 
170  FloatNDArray float_array_value (bool = false) const
171  { return FloatNDArray (dim_vector (1, 1), float_value ()); }
172 
173  SparseMatrix sparse_matrix_value (bool = false) const
174  { return SparseMatrix (Matrix (1, 1, double_value ())); }
175 
176  // FIXME: Need SparseComplexMatrix (Matrix) constructor!
178  { return SparseComplexMatrix (sparse_matrix_value ()); }
179 
180  octave_value resize (const dim_vector& dv, bool fill = false) const;
181 
182  Complex complex_value (bool = false) const { return double_value (); }
183 
184  FloatComplex float_complex_value (bool = false) const
185  { return FloatComplex (float_value ()); }
186 
187  ComplexMatrix complex_matrix_value (bool = false) const
188  { return ComplexMatrix (1, 1, Complex (double_value ())); }
189 
191  { return FloatComplexMatrix (1, 1, FloatComplex (float_value ())); }
192 
193  ComplexNDArray complex_array_value (bool = false) const
194  { return ComplexNDArray (dim_vector (1, 1), Complex (double_value ())); }
195 
197  {
198  return FloatComplexNDArray (dim_vector (1, 1),
199  FloatComplex (float_value ()));
200  }
201 
203  char_array_value (bool = false) const
204  {
205  charNDArray retval (dim_vector (1, 1));
206  retval(0) = static_cast<char> (double_value ());
207  return retval;
208  }
209 
210  bool bool_value (bool warn = false) const
211  {
212  if (warn && scalar_ref () != T (0) && scalar_ref () != T (1))
214 
215  return double_value ();
216  }
217 
218  boolNDArray bool_array_value (bool warn = false) const
219  {
220  if (warn && scalar_ref () != T (0) && scalar_ref () != T (1))
222 
223  return boolNDArray (dim_vector (1, 1), double_value ());
224  }
225 
226  octave_value as_double () const;
227  octave_value as_single () const;
228 
229  octave_value as_int8 () const;
230  octave_value as_int16 () const;
231  octave_value as_int32 () const;
232  octave_value as_int64 () const;
233 
234  octave_value as_uint8 () const;
235  octave_value as_uint16 () const;
236  octave_value as_uint32 () const;
237  octave_value as_uint64 () const;
238 
239  // We don't need to override both forms of the diag method. The using
240  // declaration will avoid warnings about partially-overloaded virtual
241  // functions.
243 
245 
246  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
247 
248  void increment () { scalar_ref () += T (1); }
249 
250  void decrement () { scalar_ref () -= T (1); }
251 
252  bool save_ascii (std::ostream& os);
253 
254  bool load_ascii (std::istream& is);
255 
256  bool save_binary (std::ostream& os, bool save_as_floats);
257 
258  bool load_binary (std::istream& is, bool swap,
260 
261  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
262 
263  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
264 
265  int write (octave::stream& os, int block_size,
266  oct_data_conv::data_type output_type, int skip,
267  octave::mach_info::float_format flt_fmt) const
268  {
269  return os.write (array_value (), block_size, output_type,
270  skip, flt_fmt);
271  }
272 
273  mxArray * as_mxArray (bool interleaved) const;
274 
276 };
277 
278 class
279 OCTINTERP_API
281 {
282 public:
283 
286 
289 
290  ~octave_magic_uint () = default;
291 
293  {
294  return new octave_magic_uint (*this);
295  }
296 
297  type_conv_info numeric_conversion_function () const;
298 
299 private:
300 
302 };
303 
304 class
305 OCTINTERP_API
307 {
308 public:
309 
312 
315 
316  ~octave_magic_int () = default;
317 
319  {
320  return new octave_magic_int (*this);
321  }
322 
323  type_conv_info numeric_conversion_function () const;
324 
325 private:
326 
328 };
329 
330 #endif
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
uint64NDArray uint64_array_value() const
Definition: ov-magic-int.h:120
bool is_double_type() const
Definition: ov-magic-int.h:95
octave_uint8 uint8_scalar_value() const
Definition: ov-magic-int.h:135
Complex complex_value(bool=false) const
Definition: ov-magic-int.h:182
boolNDArray bool_array_value(bool warn=false) const
Definition: ov-magic-int.h:218
octave_base_value * empty_clone() const
Definition: ov-magic-int.h:66
ComplexMatrix complex_matrix_value(bool=false) const
Definition: ov-magic-int.h:187
int16NDArray int16_array_value() const
Definition: ov-magic-int.h:102
double scalar_value(bool=false) const
Definition: ov-magic-int.h:155
uint16NDArray uint16_array_value() const
Definition: ov-magic-int.h:114
int64NDArray int64_array_value() const
Definition: ov-magic-int.h:108
ComplexNDArray complex_array_value(bool=false) const
Definition: ov-magic-int.h:193
uint32NDArray uint32_array_value() const
Definition: ov-magic-int.h:117
FloatNDArray float_array_value(bool=false) const
Definition: ov-magic-int.h:170
bool is_storable() const
Definition: ov-magic-int.h:85
bool is_magic_int() const
Definition: ov-magic-int.h:87
octave_int8 int8_scalar_value() const
Definition: ov-magic-int.h:123
bool isfloat() const
Definition: ov-magic-int.h:97
FloatComplexNDArray float_complex_array_value(bool=false) const
Definition: ov-magic-int.h:196
float float_scalar_value(bool=false) const
Definition: ov-magic-int.h:158
bool bool_value(bool warn=false) const
Definition: ov-magic-int.h:210
FloatComplex float_complex_value(bool=false) const
Definition: ov-magic-int.h:184
octave_int32 int32_scalar_value() const
Definition: ov-magic-int.h:129
bool vm_need_storable_call() const
Definition: ov-magic-int.h:89
charNDArray char_array_value(bool=false) const
Definition: ov-magic-int.h:203
octave_base_magic_int(const T &val)
Definition: ov-magic-int.h:58
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-magic-int.h:177
octave_uint16 uint16_scalar_value() const
Definition: ov-magic-int.h:138
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-magic-int.h:164
builtin_type_t builtin_type() const
Definition: ov-magic-int.h:83
bool is_real_scalar() const
Definition: ov-magic-int.h:91
octave_value any(int=0) const
Definition: ov-magic-int.h:81
octave_int16 int16_scalar_value() const
Definition: ov-magic-int.h:126
float float_value(bool=false) const
Definition: ov-magic-int.h:152
octave_uint64 uint64_scalar_value() const
Definition: ov-magic-int.h:144
int8NDArray int8_array_value() const
Definition: ov-magic-int.h:99
NDArray array_value(bool=false) const
Definition: ov-magic-int.h:167
bool isreal() const
Definition: ov-magic-int.h:93
int32NDArray int32_array_value() const
Definition: ov-magic-int.h:105
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-magic-int.h:173
const T & scalar_ref() const
Definition: ov-magic-int.h:72
FloatComplexMatrix float_complex_matrix_value(bool=false) const
Definition: ov-magic-int.h:190
Matrix matrix_value(bool=false) const
Definition: ov-magic-int.h:161
double double_value(bool=false) const
Definition: ov-magic-int.h:147
uint8NDArray uint8_array_value() const
Definition: ov-magic-int.h:111
int write(octave::stream &os, int block_size, oct_data_conv::data_type output_type, int skip, octave::mach_info::float_format flt_fmt) const
Definition: ov-magic-int.h:265
~octave_base_magic_int()=default
octave_int64 int64_scalar_value() const
Definition: ov-magic-int.h:132
octave_uint32 uint32_scalar_value() const
Definition: ov-magic-int.h:141
octave_magic_int(const octave_int64 &val)
Definition: ov-magic-int.h:313
octave_base_value * clone() const
Definition: ov-magic-int.h:318
~octave_magic_int()=default
octave_base_value * clone() const
Definition: ov-magic-int.h:292
~octave_magic_uint()=default
octave_magic_uint(const octave_uint64 &val)
Definition: ov-magic-int.h:287
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
Definition: ovl.h:117
Array< octave_value > array_value() const
Definition: ovl.h:90
void warn_logical_conversion()
Definition: errwarn.cc:365
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
intNDArray< octave_int16 > int16NDArray
Definition: int16NDArray.h:36
intNDArray< octave_int32 > int32NDArray
Definition: int32NDArray.h:36
intNDArray< octave_int64 > int64NDArray
Definition: int64NDArray.h:36
intNDArray< octave_int8 > int8NDArray
Definition: int8NDArray.h:36
float_format
Definition: mach-info.h:38
T octave_idx_type m
Definition: mx-inlines.cc:781
octave_idx_type n
Definition: mx-inlines.cc:761
std::complex< double > Complex
Definition: oct-cmplx.h:33
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
int64_t octave_hdf5_id
octave_int< uint32_t > octave_uint32
octave_int< int32_t > octave_int32
octave_int< int16_t > octave_int16
octave_int< int8_t > octave_int8
octave_int< int64_t > octave_int64
octave_int< uint64_t > octave_uint64
octave_int< uint16_t > octave_uint16
octave_int< uint8_t > octave_uint8
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:181
builtin_type_t
Definition: ov-base.h:83
@ btyp_double
Definition: ov-base.h:84
intNDArray< octave_uint16 > uint16NDArray
Definition: uint16NDArray.h:36
intNDArray< octave_uint32 > uint32NDArray
Definition: uint32NDArray.h:36
intNDArray< octave_uint64 > uint64NDArray
Definition: uint64NDArray.h:36
intNDArray< octave_uint8 > uint8NDArray
Definition: uint8NDArray.h:36