GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-magic-int.cc
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 (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <istream>
31 #include <ostream>
32 
33 #include "oct-inttypes-fwd.h"
34 
35 #include "data-conv.h"
36 #include "mach-info.h"
37 #include "lo-specfun.h"
38 #include "lo-mappers.h"
39 
40 #include "defun.h"
41 #include "errwarn.h"
42 #include "mxarray.h"
43 #include "ovl.h"
44 #include "oct-hdf5.h"
45 #include "oct-stream.h"
46 #include "ov-scalar.h"
47 #include "ov-float.h"
48 #include "ov-base.h"
49 #include "ov-magic-int.h"
50 #include "ov-base-scalar.h"
51 #include "ov-re-mat.h"
52 #include "ov-typeinfo.h"
53 #include "pr-output.h"
54 #include "xdiv.h"
55 #include "xpow.h"
56 #include "ops.h"
57 
58 #include "ls-oct-text.h"
59 #include "ls-hdf5.h"
60 
61 // NOTE: Although there is some additional overhead, for all but the
62 // simplest data type extraction operations, we convert to an
63 // octave_scalar object and forward the operation to avoid code
64 // duplication and ensure that operations on magic_int objects are
65 // identical to operations on octave_scalar objects. We could also
66 // avoid code duplication by deriving octave_magic_int from
67 // octave_scalar, but then we would need to store both the double and
68 // octave_uint64 or octave_int64 values, doubling the storage
69 // requirement.
70 
71 static octave_base_value *
72 default_numeric_conv_fcn (const octave_base_value& a)
73 {
74  return new octave_scalar (a.double_value ());
75 }
76 
77 template <typename T>
80  bool resize_ok)
81 {
82  octave_value tmp (double_value ());
83 
84  return tmp.index_op (idx, resize_ok);
85 }
86 
87 template <typename T>
89 octave_base_magic_int<T>::index_vector (bool require_integers) const
90 {
91  octave_value tmp (double_value ());
92 
93  return tmp.index_vector (require_integers);
94 }
95 
96 template <typename T>
98 octave_base_magic_int<T>::resize (const dim_vector& dv, bool fill) const
99 {
100  octave_value tmp (double_value ());
101 
102  return tmp.resize (dv, fill);
103 }
104 
105 template <typename T>
108 {
109  return static_cast<double> (scalar_ref ());
110 }
111 
112 template <typename T>
115 {
116  return static_cast<float> (scalar_ref ());
117 }
118 
119 template <typename T>
122 {
123  return octave_int8 (scalar_ref ());
124 }
125 
126 template <typename T>
129 {
130  return octave_int16 (scalar_ref ());
131 }
132 
133 template <typename T>
136 {
137  return octave_int32 (scalar_ref ());
138 }
139 
140 template <typename T>
143 {
144  return octave_int64 (scalar_ref ());
145 }
146 
147 template <typename T>
150 {
151  return octave_uint8 (scalar_ref ());
152 }
153 
154 template <typename T>
157 {
158  return octave_uint16 (scalar_ref ());
159 }
160 
161 template <typename T>
164 {
165  return octave_uint32 (scalar_ref ());
166 }
167 
168 template <typename T>
171 {
172  return octave_uint64 (scalar_ref ());
173 }
174 
175 template <typename T>
178 {
179  octave_value tmp (double_value ());
180 
181  return tmp.diag (m, n);
182 }
183 
184 template <typename T>
187 {
188  octave_value retval;
189 
190  int ival;
191 
192  if (scalar_ref ().value () > std::numeric_limits<unsigned char>::max ())
193  {
194  // FIXME: is there something better we could do?
195 
196  ival = 0;
197 
198  ::warning ("range error for conversion to character value");
199  }
200  else
201  ival = scalar_ref ().value ();
202 
203  retval = octave_value (std::string (1, static_cast<char> (ival)), type);
204 
205  return retval;
206 }
207 
208 
209 template <typename T>
210 bool
212 {
213  octave_value tmp (double_value ());
214 
215  return tmp.save_ascii (os);
216 }
217 
218 template <typename T>
219 OCTAVE_NORETURN bool
221 {
222  error ("octave_base_magic_int<T>::load_ascii: internal error");
223 }
224 
225 template <typename T>
226 bool
227 octave_base_magic_int<T>::save_binary (std::ostream& os, bool save_as_floats)
228 {
229  octave_value tmp (double_value ());
230 
231  return tmp.save_binary (os, save_as_floats);
232 }
233 
234 template <typename T>
235 OCTAVE_NORETURN bool
238 {
239  error ("octave_base_magic_int<T>::load_binary: internal error");
240 }
241 
242 template <typename T>
243 bool
245  bool save_as_floats)
246 {
247  bool retval = false;
248 
249 #if defined (HAVE_HDF5)
250 
251  octave_value tmp (double_value ());
252 
253  return tmp.save_hdf5 (loc_id, name, save_as_floats);
254 
255 #else
256 
257  octave_unused_parameter (loc_id);
258  octave_unused_parameter (name);
259  octave_unused_parameter (save_as_floats);
260 
262 
263 #endif
264 
265  return retval;
266 }
267 
268 template <typename T>
269 bool
271 {
272 #if defined (HAVE_HDF5)
273 
274  error ("octave_base_magic_int<T>::load_binary: internal error");
275 
276  return false;
277 
278 #else
279 
281 
282  return false;
283 
284 #endif
285 }
286 
287 template <typename T>
288 mxArray *
289 octave_base_magic_int<T>::as_mxArray (bool interleaved) const
290 {
291  octave_value tmp (double_value ());
292 
293  return tmp.as_mxArray (interleaved);
294 }
295 
296 template <typename T>
299 {
300  octave_value tmp (double_value ());
301 
302  return tmp.map (umap);
303 }
304 
306  "double");
307 
310 {
311  return octave_base_value::type_conv_info (default_numeric_conv_fcn,
313 }
314 
316  "double");
317 
320 {
321  return octave_base_value::type_conv_info (default_numeric_conv_fcn,
323 }
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:230
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_value as_int64() const
octave_value convert_to_str_internal(bool pad, bool force, char type) const
octave_value map(octave_base_value::unary_mapper_t umap) const
octave::idx_vector index_vector(bool require_integers=false) const
Definition: ov-magic-int.cc:89
octave_value as_single() const
octave_value as_uint32() const
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
octave_value as_uint16() const
octave_value as_uint64() const
octave_value as_int16() const
octave_value as_uint8() const
octave_value as_double() const
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-magic-int.cc:79
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
Definition: ov-magic-int.cc:98
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
bool load_ascii(std::istream &is)
octave_value diag(octave_idx_type m, octave_idx_type n) const
mxArray * as_mxArray(bool interleaved) const
octave_value as_int32() const
void warn_load(const char *type) const
Definition: ov-base.cc:1157
virtual double double_value(bool=false) const
Definition: ov-base.cc:582
void warn_save(const char *type) const
Definition: ov-base.cc:1166
type_conv_info numeric_conversion_function() const
type_conv_info numeric_conversion_function() const
static int static_type_id()
Definition: ov-scalar.h:291
octave_value index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov.h:504
octave::idx_vector index_vector(bool require_integers=false) const
Definition: ov.h:534
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition: ov.h:1376
bool save_binary(std::ostream &os, bool save_as_floats)
Definition: ov.h:1370
bool save_ascii(std::ostream &os)
Definition: ov.h:1366
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1513
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov.h:580
mxArray * as_mxArray(bool interleaved=false) const
Definition: ov.h:1407
octave_value diag(octave_idx_type k=0) const
Definition: ov.h:1410
void warning(const char *fmt,...)
Definition: error.cc:1063
void() error(const char *fmt,...)
Definition: error.cc:988
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
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
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
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:235