GNU Octave  8.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-2023 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 (void) = 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 (void) 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 (void) 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 
83  builtin_type_t builtin_type (void) const { return btyp_double; }
84 
85  bool is_storable (void) const { return false; }
86 
87  bool is_magic_int (void) const { return true; }
88 
89  bool is_real_scalar (void) const { return true; }
90 
91  bool isreal (void) const { return true; }
92 
93  bool is_double_type (void) const { return true; }
94 
95  bool isfloat (void) const { return true; }
96 
98  { return int8NDArray (dim_vector (1, 1), double_value ()); }
99 
101  { return int16NDArray (dim_vector (1, 1), double_value ()); }
102 
104  { return int32NDArray (dim_vector (1, 1), double_value ()); }
105 
107  { return int64NDArray (dim_vector (1, 1), double_value ()); }
108 
110  { return uint8NDArray (dim_vector (1, 1), double_value ()); }
111 
113  { return uint16NDArray (dim_vector (1, 1), double_value ()); }
114 
116  { return uint32NDArray (dim_vector (1, 1), double_value ()); }
117 
119  { return uint64NDArray (dim_vector (1, 1), double_value ()); }
120 
122  { return octave_int8 (double_value ()); }
123 
125  { return octave_int16 (double_value ()); }
126 
128  { return octave_int32 (double_value ()); }
129 
131  { return octave_int64 (double_value ()); }
132 
134  { return octave_uint8 (double_value ()); }
135 
137  { return octave_uint16 (double_value ()); }
138 
140  { return octave_uint32 (double_value ()); }
141 
143  { return octave_uint64 (double_value ()); }
144 
145  double double_value (bool = false) const
146  {
147  return scalar_ref ().double_value ();
148  }
149 
150  float float_value (bool = false) const
151  { return static_cast<float> (double_value ()); }
152 
153  double scalar_value (bool = false) const
154  { return double_value (); }
155 
156  float float_scalar_value (bool = false) const
157  { return float_value (); }
158 
159  Matrix matrix_value (bool = false) const
160  { return Matrix (1, 1, double_value ()); }
161 
162  FloatMatrix float_matrix_value (bool = false) const
163  { return FloatMatrix (1, 1, float_value ()); }
164 
165  NDArray array_value (bool = false) const
166  { return NDArray (dim_vector (1, 1), double_value ()); }
167 
168  FloatNDArray float_array_value (bool = false) const
169  { return FloatNDArray (dim_vector (1, 1), float_value ()); }
170 
171  SparseMatrix sparse_matrix_value (bool = false) const
172  { return SparseMatrix (Matrix (1, 1, double_value ())); }
173 
174  // FIXME: Need SparseComplexMatrix (Matrix) constructor!
176  { return SparseComplexMatrix (sparse_matrix_value ()); }
177 
178  octave_value resize (const dim_vector& dv, bool fill = false) const;
179 
180  Complex complex_value (bool = false) const { return double_value (); }
181 
182  FloatComplex float_complex_value (bool = false) const
183  { return FloatComplex (float_value ()); }
184 
185  ComplexMatrix complex_matrix_value (bool = false) const
186  { return ComplexMatrix (1, 1, Complex (double_value ())); }
187 
189  { return FloatComplexMatrix (1, 1, FloatComplex (float_value ())); }
190 
191  ComplexNDArray complex_array_value (bool = false) const
192  { return ComplexNDArray (dim_vector (1, 1), Complex (double_value ())); }
193 
195  {
196  return FloatComplexNDArray (dim_vector (1, 1),
197  FloatComplex (float_value ()));
198  }
199 
201  char_array_value (bool = false) const
202  {
203  charNDArray retval (dim_vector (1, 1));
204  retval(0) = static_cast<char> (double_value ());
205  return retval;
206  }
207 
208  bool bool_value (bool warn = false) const
209  {
210  if (warn && scalar_ref () != T (0) && scalar_ref () != T (1))
212 
213  return double_value ();
214  }
215 
216  boolNDArray bool_array_value (bool warn = false) const
217  {
218  if (warn && scalar_ref () != T (0) && scalar_ref () != T (1))
220 
221  return boolNDArray (dim_vector (1, 1), double_value ());
222  }
223 
224  octave_value as_double (void) const;
225  octave_value as_single (void) const;
226 
227  octave_value as_int8 (void) const;
228  octave_value as_int16 (void) const;
229  octave_value as_int32 (void) const;
230  octave_value as_int64 (void) const;
231 
232  octave_value as_uint8 (void) const;
233  octave_value as_uint16 (void) const;
234  octave_value as_uint32 (void) const;
235  octave_value as_uint64 (void) const;
236 
237  // We don't need to override both forms of the diag method. The using
238  // declaration will avoid warnings about partially-overloaded virtual
239  // functions.
241 
243 
244  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
245 
246  void increment (void) { scalar_ref () += T (1); }
247 
248  void decrement (void) { scalar_ref () -= T (1); }
249 
250  bool save_ascii (std::ostream& os);
251 
252  bool load_ascii (std::istream& is);
253 
254  bool save_binary (std::ostream& os, bool save_as_floats);
255 
256  bool load_binary (std::istream& is, bool swap,
258 
259  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
260 
261  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
262 
263  int write (octave::stream& os, int block_size,
264  oct_data_conv::data_type output_type, int skip,
265  octave::mach_info::float_format flt_fmt) const
266  {
267  return os.write (array_value (), block_size, output_type,
268  skip, flt_fmt);
269  }
270 
271  mxArray * as_mxArray (bool interleaved) const;
272 
274 };
275 
276 class
277 OCTINTERP_API
279 {
280 public:
281 
284 
287 
288  ~octave_magic_uint (void) = default;
289 
290  octave_base_value * clone (void) const
291  {
292  return new octave_magic_uint (*this);
293  }
294 
295  type_conv_info numeric_conversion_function (void) const;
296 
297 private:
298 
300 };
301 
302 class
303 OCTINTERP_API
305 {
306 public:
307 
310 
313 
314  ~octave_magic_int (void) = default;
315 
316  octave_base_value * clone (void) const
317  {
318  return new octave_magic_int (*this);
319  }
320 
321  type_conv_info numeric_conversion_function (void) const;
322 
323 private:
324 
326 };
327 
328 #endif
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
Complex complex_value(bool=false) const
Definition: ov-magic-int.h:180
boolNDArray bool_array_value(bool warn=false) const
Definition: ov-magic-int.h:216
ComplexMatrix complex_matrix_value(bool=false) const
Definition: ov-magic-int.h:185
octave_uint64 uint64_scalar_value(void) const
Definition: ov-magic-int.h:142
double scalar_value(bool=false) const
Definition: ov-magic-int.h:153
builtin_type_t builtin_type(void) const
Definition: ov-magic-int.h:83
ComplexNDArray complex_array_value(bool=false) const
Definition: ov-magic-int.h:191
bool isreal(void) const
Definition: ov-magic-int.h:91
const T & scalar_ref(void) const
Definition: ov-magic-int.h:72
bool is_magic_int(void) const
Definition: ov-magic-int.h:87
~octave_base_magic_int(void)=default
octave_int8 int8_scalar_value(void) const
Definition: ov-magic-int.h:121
FloatNDArray float_array_value(bool=false) const
Definition: ov-magic-int.h:168
int8NDArray int8_array_value(void) const
Definition: ov-magic-int.h:97
octave_int16 int16_scalar_value(void) const
Definition: ov-magic-int.h:124
FloatComplexNDArray float_complex_array_value(bool=false) const
Definition: ov-magic-int.h:194
int16NDArray int16_array_value(void) const
Definition: ov-magic-int.h:100
float float_scalar_value(bool=false) const
Definition: ov-magic-int.h:156
int32NDArray int32_array_value(void) const
Definition: ov-magic-int.h:103
bool bool_value(bool warn=false) const
Definition: ov-magic-int.h:208
FloatComplex float_complex_value(bool=false) const
Definition: ov-magic-int.h:182
uint64NDArray uint64_array_value(void) const
Definition: ov-magic-int.h:118
uint8NDArray uint8_array_value(void) const
Definition: ov-magic-int.h:109
bool is_real_scalar(void) const
Definition: ov-magic-int.h:89
charNDArray char_array_value(bool=false) const
Definition: ov-magic-int.h:201
uint16NDArray uint16_array_value(void) const
Definition: ov-magic-int.h:112
octave_base_value * empty_clone(void) const
Definition: ov-magic-int.h:66
octave_base_magic_int(const T &val)
Definition: ov-magic-int.h:58
int64NDArray int64_array_value(void) const
Definition: ov-magic-int.h:106
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-magic-int.h:175
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-magic-int.h:162
octave_int64 int64_scalar_value(void) const
Definition: ov-magic-int.h:130
bool is_storable(void) const
Definition: ov-magic-int.h:85
octave_value any(int=0) const
Definition: ov-magic-int.h:81
bool is_double_type(void) const
Definition: ov-magic-int.h:93
octave_uint32 uint32_scalar_value(void) const
Definition: ov-magic-int.h:139
float float_value(bool=false) const
Definition: ov-magic-int.h:150
bool isfloat(void) const
Definition: ov-magic-int.h:95
NDArray array_value(bool=false) const
Definition: ov-magic-int.h:165
octave_uint8 uint8_scalar_value(void) const
Definition: ov-magic-int.h:133
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-magic-int.h:171
FloatComplexMatrix float_complex_matrix_value(bool=false) const
Definition: ov-magic-int.h:188
Matrix matrix_value(bool=false) const
Definition: ov-magic-int.h:159
double double_value(bool=false) const
Definition: ov-magic-int.h:145
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:263
octave_int32 int32_scalar_value(void) const
Definition: ov-magic-int.h:127
uint32NDArray uint32_array_value(void) const
Definition: ov-magic-int.h:115
octave_uint16 uint16_scalar_value(void) const
Definition: ov-magic-int.h:136
octave_magic_int(const octave_int64 &val)
Definition: ov-magic-int.h:311
~octave_magic_int(void)=default
octave_base_value * clone(void) const
Definition: ov-magic-int.h:316
~octave_magic_uint(void)=default
octave_magic_uint(const octave_uint64 &val)
Definition: ov-magic-int.h:285
octave_base_value * clone(void) const
Definition: ov-magic-int.h:290
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
Definition: ovl.h:117
Array< octave_value > array_value(void) const
Definition: ovl.h:90
void warn_logical_conversion(void)
Definition: errwarn.cc:365
octave::idx_vector idx_vector
Definition: idx-vector.h:1039
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
class OCTAVE_API NDArray
Definition: mx-fwd.h:38
class OCTAVE_API boolNDArray
Definition: mx-fwd.h:42
class OCTAVE_API Matrix
Definition: mx-fwd.h:31
class OCTAVE_API ComplexMatrix
Definition: mx-fwd.h:32
class OCTAVE_API SparseMatrix
Definition: mx-fwd.h:55
class OCTAVE_API FloatComplexMatrix
Definition: mx-fwd.h:34
class OCTAVE_API FloatMatrix
Definition: mx-fwd.h:33
class OCTAVE_API ComplexNDArray
Definition: mx-fwd.h:39
class OCTAVE_API SparseComplexMatrix
Definition: mx-fwd.h:56
class OCTAVE_API FloatComplexNDArray
Definition: mx-fwd.h:41
class OCTAVE_API FloatNDArray
Definition: mx-fwd.h:40
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753
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
static double as_double(OCTAVE_TIME_T sec, long usec)
Definition: oct-time.h:35
#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