GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-magic-int.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2020-2025 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
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
49template <typename T>
51{
52public:
53
56
57 octave_base_magic_int (const T& val)
58 : octave_base_scalar<T> (val) { }
59
61
62 // We return an octave_matrix here instead of an octave_scalar so
63 // that in expressions like A(2,2,2) = 2 (for A previously
64 // undefined), A will be empty instead of a 1x1 object.
65 octave_base_value * empty_clone () const { return new octave_matrix (); }
66
67 // Although SCALAR is a protected member of the base class, it is not
68 // directly visible here without the explicit octave_base_slalar<T>::
69 // qualification. Why not?
70
71 const T& scalar_ref () const { return octave_base_scalar<T>::scalar; }
72
74
76 bool resize_ok = false);
77
78 octave::idx_vector index_vector (bool require_integers = false) const;
79
80 octave_value any (int = 0) const { return scalar_ref () != T (0); }
81
83
84 bool is_storable () const { return false; }
85
86 bool is_magic_int () const { return true; }
87
88 bool vm_need_storable_call () const { return true; }
89
90 bool is_real_scalar () const { return true; }
91
92 bool isreal () const { return true; }
93
94 bool is_double_type () const { return true; }
95
96 bool isfloat () const { return true; }
97
99 { return int8NDArray (dim_vector (1, 1), double_value ()); }
100
103
106
109
112
115
118
121
124
127
130
133
136
139
142
145
146 double double_value (bool = false) const
147 {
148 return scalar_ref ().double_value ();
149 }
150
151 float float_value (bool = false) const
152 { return static_cast<float> (double_value ()); }
153
154 double scalar_value (bool = false) const
155 { return double_value (); }
156
157 float float_scalar_value (bool = false) const
158 { return float_value (); }
159
160 Matrix matrix_value (bool = false) const
161 { return Matrix (1, 1, double_value ()); }
162
163 FloatMatrix float_matrix_value (bool = false) const
164 { return FloatMatrix (1, 1, float_value ()); }
165
166 NDArray array_value (bool = false) const
167 { return NDArray (dim_vector (1, 1), double_value ()); }
168
169 FloatNDArray float_array_value (bool = false) const
170 { return FloatNDArray (dim_vector (1, 1), float_value ()); }
171
173 { return SparseMatrix (Matrix (1, 1, double_value ())); }
174
175 // FIXME: Need SparseComplexMatrix (Matrix) constructor!
178
179 octave_value resize (const dim_vector& dv, bool fill = false) const;
180
181 Complex complex_value (bool = false) const { return double_value (); }
182
184 { return FloatComplex (float_value ()); }
185
187 { return ComplexMatrix (1, 1, Complex (double_value ())); }
188
191
193 { return ComplexNDArray (dim_vector (1, 1), Complex (double_value ())); }
194
196 {
197 return FloatComplexNDArray (dim_vector (1, 1),
199 }
200
202 char_array_value (bool = false) const
203 {
204 charNDArray retval (dim_vector (1, 1));
205 retval(0) = static_cast<char> (double_value ());
206 return retval;
207 }
208
209 bool bool_value (bool warn = false) const
210 {
211 if (warn && scalar_ref () != T (0) && scalar_ref () != T (1))
213
214 return double_value ();
215 }
216
217 boolNDArray bool_array_value (bool warn = false) const
218 {
219 if (warn && scalar_ref () != T (0) && scalar_ref () != T (1))
221
222 return boolNDArray (dim_vector (1, 1), double_value ());
223 }
224
225 octave_value as_double () const;
226 octave_value as_single () const;
227
228 octave_value as_int8 () const;
229 octave_value as_int16 () const;
230 octave_value as_int32 () const;
231 octave_value as_int64 () const;
232
233 octave_value as_uint8 () const;
234 octave_value as_uint16 () const;
235 octave_value as_uint32 () const;
236 octave_value as_uint64 () const;
237
238 // We don't need to override both forms of the diag method. The using
239 // declaration will avoid warnings about partially-overloaded virtual
240 // functions.
242
244
245 octave_value convert_to_str_internal (bool pad, bool force, char type) const;
246
247 void increment () { scalar_ref () += T (1); }
248
249 void decrement () { scalar_ref () -= T (1); }
250
251 bool save_ascii (std::ostream& os);
252
253 bool load_ascii (std::istream& is);
254
255 bool save_binary (std::ostream& os, bool save_as_floats);
256
257 bool load_binary (std::istream& is, bool swap,
258 octave::mach_info::float_format fmt);
259
260 bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
261
262 bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
263
264 int write (octave::stream& os, int block_size,
265 oct_data_conv::data_type output_type, int skip,
266 octave::mach_info::float_format flt_fmt) const
267 {
268 return os.write (array_value (), block_size, output_type,
269 skip, flt_fmt);
270 }
271
272 mxArray * as_mxArray (bool interleaved) const;
273
275};
276
277class OCTINTERP_API octave_magic_uint : public octave_base_magic_int<octave_uint64>
278{
279public:
280
283
286
287 ~octave_magic_uint () = default;
288
290 {
291 return new octave_magic_uint (*this);
292 }
293
294 type_conv_info numeric_conversion_function () const;
295
296private:
297
299};
300
301class OCTINTERP_API octave_magic_int : public octave_base_magic_int<octave_int64>
302{
303public:
304
307
310
311 ~octave_magic_int () = default;
312
314 {
315 return new octave_magic_int (*this);
316 }
317
318 type_conv_info numeric_conversion_function () const;
319
320private:
321
323};
324
325#endif
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
uint64NDArray uint64_array_value() const
bool is_double_type() const
octave_uint8 uint8_scalar_value() const
Complex complex_value(bool=false) const
boolNDArray bool_array_value(bool warn=false) const
ComplexMatrix complex_matrix_value(bool=false) const
octave_value as_int64() const
int16NDArray int16_array_value() const
double scalar_value(bool=false) const
uint16NDArray uint16_array_value() const
int64NDArray int64_array_value() const
ComplexNDArray complex_array_value(bool=false) const
octave_value convert_to_str_internal(bool pad, bool force, char type) const
uint32NDArray uint32_array_value() const
FloatNDArray float_array_value(bool=false) const
bool is_storable() const
bool is_magic_int() const
octave_int8 int8_scalar_value() const
FloatComplexNDArray float_complex_array_value(bool=false) const
octave_value map(octave_base_value::unary_mapper_t umap) const
float float_scalar_value(bool=false) const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
bool bool_value(bool warn=false) const
octave::idx_vector index_vector(bool require_integers=false) const
octave_value as_single() const
octave_value as_uint32() const
FloatComplex float_complex_value(bool=false) const
octave_int32 int32_scalar_value() const
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
bool vm_need_storable_call() const
octave_value as_uint16() const
charNDArray char_array_value(bool=false) const
octave_base_magic_int(const T &val)
octave_base_value * empty_clone() const
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
octave_value as_uint64() const
octave_uint16 uint16_scalar_value() const
FloatMatrix float_matrix_value(bool=false) const
octave_value as_int16() const
builtin_type_t builtin_type() const
bool is_real_scalar() const
bool load_ascii(std::istream &is)
octave_value as_uint8() const
octave_value any(int=0) const
octave_int16 int16_scalar_value() const
const T & scalar_ref() const
float float_value(bool=false) const
octave_value as_double() const
octave_uint64 uint64_scalar_value() const
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
int8NDArray int8_array_value() const
NDArray array_value(bool=false) const
int32NDArray int32_array_value() const
bool save_binary(std::ostream &os, bool save_as_floats)
octave_value as_int8() const
bool save_ascii(std::ostream &os)
octave_value resize(const dim_vector &dv, bool fill=false) const
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
SparseMatrix sparse_matrix_value(bool=false) const
FloatComplexMatrix float_complex_matrix_value(bool=false) const
Matrix matrix_value(bool=false) const
double double_value(bool=false) const
uint8NDArray uint8_array_value() const
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
~octave_base_magic_int()=default
octave_int64 int64_scalar_value() const
octave_value diag(octave_idx_type m, octave_idx_type n) const
mxArray * as_mxArray(bool interleaved) const
octave_value as_int32() const
octave_uint32 uint32_scalar_value() const
virtual type_conv_info numeric_conversion_function() const
Definition ov-base.h:303
octave_magic_int(const octave_int64 &val)
octave_base_value * clone() const
~octave_magic_int()=default
~octave_magic_uint()=default
octave_magic_uint(const octave_uint64 &val)
octave_base_value * clone() const
void warn_logical_conversion()
Definition errwarn.cc:365
intNDArray< octave_int16 > int16NDArray
intNDArray< octave_int32 > int32NDArray
intNDArray< octave_int64 > int64NDArray
intNDArray< octave_int8 > int8NDArray
Definition int8NDArray.h:36
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_API(API)
Definition ov-base.h:185
builtin_type_t
Definition ov-base.h:83
@ btyp_double
Definition ov-base.h:84
intNDArray< octave_uint16 > uint16NDArray
intNDArray< octave_uint32 > uint32NDArray
intNDArray< octave_uint64 > uint64NDArray
intNDArray< octave_uint8 > uint8NDArray