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.cc
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 (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
71static octave_base_value *
72default_numeric_conv_fcn (const octave_base_value& a)
73{
74 return new octave_scalar (a.double_value ());
76
77template <typename T>
80 bool resize_ok)
81{
82 octave_value tmp (double_value ());
83
84 return tmp.index_op (idx, resize_ok);
85}
86
87template <typename T>
88octave::idx_vector
89octave_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
96template <typename T>
98octave_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
105template <typename T>
108{
109 return static_cast<double> (scalar_ref ());
110}
111
112template <typename T>
115{
116 return static_cast<float> (scalar_ref ());
117}
118
119template <typename T>
122{
123 return octave_int8 (scalar_ref ());
124}
125
126template <typename T>
129{
130 return octave_int16 (scalar_ref ());
131}
132
133template <typename T>
136{
137 return octave_int32 (scalar_ref ());
138}
139
140template <typename T>
143{
144 return octave_int64 (scalar_ref ());
145}
146
147template <typename T>
150{
151 return octave_uint8 (scalar_ref ());
152}
153
154template <typename T>
157{
158 return octave_uint16 (scalar_ref ());
159}
160
161template <typename T>
164{
165 return octave_uint32 (scalar_ref ());
166}
167
168template <typename T>
171{
172 return octave_uint64 (scalar_ref ());
173}
174
175template <typename T>
178{
179 octave_value tmp (double_value ());
180
181 return tmp.diag (m, n);
182}
183
184template <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
209template <typename T>
210bool
212{
213 octave_value tmp (double_value ());
214
215 return tmp.save_ascii (os);
216}
217
218template <typename T>
219bool
221{
222 error ("octave_base_magic_int<T>::load_ascii: internal error");
223}
224
225template <typename T>
226bool
227octave_base_magic_int<T>::save_binary (std::ostream& os, bool save_as_floats)
229 octave_value tmp (double_value ());
231 return tmp.save_binary (os, save_as_floats);
232}
234template <typename T>
235bool
237 octave::mach_info::float_format)
238{
239 error ("octave_base_magic_int<T>::load_binary: internal error");
240}
241
242template <typename T>
243bool
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);
263#endif
264
265 return retval;
266}
267
268template <typename T>
269bool
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
287template <typename T>
288mxArray *
290{
291 octave_value tmp (double_value ());
292
293 return tmp.as_mxArray (interleaved);
294}
295
296template <typename T>
299{
300 octave_value tmp (double_value ());
301
302 return tmp.map (umap);
303}
304
306 "double");
307
314
316 "double");
317
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
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
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
octave::idx_vector index_vector(bool require_integers=false) const
octave_value as_single() const
octave_value as_uint32() const
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
bool load_ascii(std::istream &is)
octave_value as_uint8() const
octave_value as_double() const
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
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)
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:1152
virtual double double_value(bool=false) const
Definition ov-base.cc:579
void warn_save(const char *type) const
Definition ov-base.cc:1161
type_conv_info numeric_conversion_function() const
type_conv_info numeric_conversion_function() const
static int static_type_id()
Definition ov-scalar.h:290
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_ascii(std::ostream &os)
Definition ov.h:1381
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition ov.h:1528
mxArray * as_mxArray(bool interleaved=false) const
Definition ov.h:1422
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition ov.h:580
void warning(const char *fmt,...)
Definition error.cc:1078
void error(const char *fmt,...)
Definition error.cc:1003
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 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition ov-base.h:246