GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-range.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 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_range_h)
27 #define octave_ov_range_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cstdlib>
32 
33 #include <iosfwd>
34 #include <string>
35 
36 #include "Range.h"
37 
38 #include "lo-mappers.h"
39 #include "lo-utils.h"
40 #include "mx-base.h"
41 #include "str-vec.h"
42 
43 #include "error.h"
44 #include "oct-stream.h"
45 #include "ov-base.h"
46 #include "ov-re-mat.h"
47 #include "ov-typeinfo.h"
48 
49 class octave_value_list;
50 
51 // Range values.
52 
53 class
55 {
56 public:
57 
58  octave_range (void)
59  : octave_base_value (), range (), idx_cache () { }
60 
61  octave_range (double base, double limit, double inc)
62  : octave_base_value (), range (base, limit, inc), idx_cache ()
63  {
64  if (range.numel () < 0)
65  error ("invalid range");
66  }
67 
68  octave_range (const Range& r)
69  : octave_base_value (), range (r), idx_cache ()
70  {
71  if (range.numel () < 0 && range.numel () != -2)
72  error ("invalid range");
73  }
74 
76  : octave_base_value (), range (r.range),
77  idx_cache (r.idx_cache ? new idx_vector (*r.idx_cache) : nullptr)
78  { }
79 
80  octave_range (const Range& r, const idx_vector& cache)
81  : octave_base_value (), range (r), idx_cache ()
82  {
83  set_idx_cache (cache);
84  }
85 
86  ~octave_range (void) { clear_cached_info (); }
87 
88  octave_base_value * clone (void) const { return new octave_range (*this); }
89 
90  // A range is really just a special kind of real matrix object. In
91  // the places where we need to call empty_clone, it makes more sense
92  // to create an empty matrix (0x0) instead of an empty range (1x0).
93  octave_base_value * empty_clone (void) const { return new octave_matrix (); }
94 
95  type_conv_info numeric_conversion_function (void) const;
96 
97  octave_base_value * try_narrowing_conversion (void);
98 
99  // We don't need to override all three forms of subsref. The using
100  // declaration will avoid warnings about partially-overloaded virtual
101  // functions.
103 
104  octave_value subsref (const std::string& type,
105  const std::list<octave_value_list>& idx);
106 
107  octave_value_list subsref (const std::string& type,
108  const std::list<octave_value_list>& idx, int)
109  { return subsref (type, idx); }
110 
111  octave_value do_index_op (const octave_value_list& idx,
112  bool resize_ok = false);
113 
114  idx_vector index_vector (bool require_integers = false) const;
115 
116  dim_vector dims (void) const
117  {
118  octave_idx_type n = range.numel ();
119  return dim_vector (n > 0, n);
120  }
121 
122  octave_idx_type nnz (void) const { return range.nnz (); }
123 
124  octave_value resize (const dim_vector& dv, bool fill = false) const;
125 
126  size_t byte_size (void) const { return 3 * sizeof (double); }
127 
128  octave_value reshape (const dim_vector& new_dims) const
129  { return NDArray (array_value ().reshape (new_dims)); }
130 
131  octave_value permute (const Array<int>& vec, bool inv = false) const
132  { return NDArray (array_value ().permute (vec, inv)); }
133 
134  octave_value squeeze (void) const { return range; }
135 
136  octave_value full_value (void) const { return range.matrix_value (); }
137 
138  bool is_defined (void) const { return true; }
139 
140  bool is_constant (void) const { return true; }
141 
142  bool is_range (void) const { return true; }
143 
144  octave_value all (int dim = 0) const;
145 
146  octave_value any (int dim = 0) const;
147 
148  octave_value diag (octave_idx_type k = 0) const;
149 
151 
153  { return range.sort (dim, mode); }
154 
156  sortmode mode = ASCENDING) const
157  { return range.sort (sidx, dim, mode); }
158 
160  { return range.issorted (mode); }
161 
163  { return Array<octave_idx_type> (dim_vector (1, 0)); }
164 
166  { return (mode == UNSORTED) ? ASCENDING : mode; }
167 
168  builtin_type_t builtin_type (void) const { return btyp_double; }
169 
170  bool isreal (void) const { return true; }
171 
172  bool is_double_type (void) const { return true; }
173 
174  bool isfloat (void) const { return true; }
175 
176  bool isnumeric (void) const { return true; }
177 
178  bool is_true (void) const;
179 
180  double double_value (bool = false) const;
181 
182  float float_value (bool = false) const;
183 
184  double scalar_value (bool frc_str_conv = false) const
185  { return double_value (frc_str_conv); }
186 
187  float float_scalar_value (bool frc_str_conv = false) const
188  { return float_value (frc_str_conv); }
189 
190  Matrix matrix_value (bool = false) const
191  { return range.matrix_value (); }
192 
193  FloatMatrix float_matrix_value (bool = false) const
194  { return range.matrix_value (); }
195 
196  NDArray array_value (bool = false) const
197  { return range.matrix_value (); }
198 
199  FloatNDArray float_array_value (bool = false) const
200  { return FloatMatrix (range.matrix_value ()); }
201 
202  charNDArray char_array_value (bool = false) const;
203 
204  // FIXME: it would be better to have Range::intXNDArray_value
205  // functions to avoid the intermediate conversion to a matrix
206  // object.
207 
209  int8_array_value (void) const { return int8NDArray (array_value ()); }
210 
212  int16_array_value (void) const { return int16NDArray (array_value ()); }
213 
215  int32_array_value (void) const { return int32NDArray (array_value ()); }
216 
218  int64_array_value (void) const { return int64NDArray (array_value ()); }
219 
221  uint8_array_value (void) const { return uint8NDArray (array_value ()); }
222 
224  uint16_array_value (void) const { return uint16NDArray (array_value ()); }
225 
227  uint32_array_value (void) const { return uint32NDArray (array_value ()); }
228 
230  uint64_array_value (void) const { return uint64NDArray (array_value ()); }
231 
232  SparseMatrix sparse_matrix_value (bool = false) const
233  { return SparseMatrix (range.matrix_value ()); }
234 
236  { return SparseComplexMatrix (sparse_matrix_value ()); }
237 
238  Complex complex_value (bool = false) const;
239 
240  FloatComplex float_complex_value (bool = false) const;
241 
242  boolNDArray bool_array_value (bool warn = false) const;
243 
244  ComplexMatrix complex_matrix_value (bool = false) const
245  { return ComplexMatrix (range.matrix_value ()); }
246 
248  { return FloatComplexMatrix (range.matrix_value ()); }
249 
250  ComplexNDArray complex_array_value (bool = false) const
251  { return ComplexMatrix (range.matrix_value ()); }
252 
254  { return FloatComplexMatrix (range.matrix_value ()); }
255 
256  Range range_value (void) const { return range; }
257 
258  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
259 
260  octave_value as_double (void) const;
261  octave_value as_single (void) const;
262 
263  octave_value as_int8 (void) const;
264  octave_value as_int16 (void) const;
265  octave_value as_int32 (void) const;
266  octave_value as_int64 (void) const;
267 
268  octave_value as_uint8 (void) const;
269  octave_value as_uint16 (void) const;
270  octave_value as_uint32 (void) const;
271  octave_value as_uint64 (void) const;
272 
273  void print (std::ostream& os, bool pr_as_read_syntax = false);
274 
275  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
276 
277  bool print_name_tag (std::ostream& os, const std::string& name) const;
278 
279  void short_disp (std::ostream& os) const;
280 
282 
283  std::string edit_display (const float_display_format& fmt,
284  octave_idx_type i, octave_idx_type j) const;
285 
286  bool save_ascii (std::ostream& os);
287 
288  bool load_ascii (std::istream& is);
289 
290  bool save_binary (std::ostream& os, bool save_as_floats);
291 
292  bool load_binary (std::istream& is, bool swap,
294 
295  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
296 
297  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
298 
299  int write (octave::stream& os, int block_size,
300  oct_data_conv::data_type output_type, int skip,
301  octave::mach_info::float_format flt_fmt) const
302  {
303  // FIXME: could be more memory efficient by having a
304  // special case of the octave::stream::write method for ranges.
305 
306  return os.write (matrix_value (), block_size, output_type, skip, flt_fmt);
307  }
308 
309  mxArray * as_mxArray (void) const;
310 
312  {
313  octave_matrix m (matrix_value ());
314  return m.map (umap);
315  }
316 
317  octave_value fast_elem_extract (octave_idx_type n) const;
318 
319 private:
320 
322 
324  {
325  delete idx_cache;
326  idx_cache = (idx ? new idx_vector (idx) : nullptr);
327  return idx;
328  }
329 
330  void clear_cached_info (void) const
331  {
332  delete idx_cache; idx_cache = nullptr;
333  }
334 
336 
337  // No assignment.
338 
340 
342 };
343 
344 #endif
template class OCTAVE_API Array< octave_idx_type >
Definition: dMatrix.h:42
Definition: Range.h:40
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
octave_idx_type write(const octave_value &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt)
Definition: oct-stream.cc:6777
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:201
double scalar_value(bool frc_str_conv=false) const
Definition: ov-range.h:184
octave_value map(unary_mapper_t umap) const
Definition: ov-range.h:311
FloatNDArray float_array_value(bool=false) const
Definition: ov-range.h:199
bool is_defined(void) const
Definition: ov-range.h:138
idx_vector * idx_cache
Definition: ov-range.h:335
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-range.h:128
~octave_range(void)
Definition: ov-range.h:86
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-range.h:165
dim_vector dims(void) const
Definition: ov-range.h:116
octave_value squeeze(void) const
Definition: ov-range.h:134
octave_value sort(Array< octave_idx_type > &sidx, octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-range.h:155
uint8NDArray uint8_array_value(void) const
Definition: ov-range.h:221
int8NDArray int8_array_value(void) const
Definition: ov-range.h:209
builtin_type_t builtin_type(void) const
Definition: ov-range.h:168
bool isfloat(void) const
Definition: ov-range.h:174
octave_value full_value(void) const
Definition: ov-range.h:136
octave_range(void)
Definition: ov-range.h:58
int64NDArray int64_array_value(void) const
Definition: ov-range.h:218
size_t byte_size(void) const
Definition: ov-range.h:126
int32NDArray int32_array_value(void) const
Definition: ov-range.h:215
octave_range(double base, double limit, double inc)
Definition: ov-range.h:61
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-range.h:232
bool is_constant(void) const
Definition: ov-range.h:140
octave_base_value * empty_clone(void) const
Definition: ov-range.h:93
void clear_cached_info(void) const
Definition: ov-range.h:330
uint32NDArray uint32_array_value(void) const
Definition: ov-range.h:227
ComplexMatrix complex_matrix_value(bool=false) const
Definition: ov-range.h:244
uint64NDArray uint64_array_value(void) const
Definition: ov-range.h:230
int16NDArray int16_array_value(void) const
Definition: ov-range.h:212
uint16NDArray uint16_array_value(void) const
Definition: ov-range.h:224
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov-range.h:159
idx_vector set_idx_cache(const idx_vector &idx) const
Definition: ov-range.h:323
octave_range(const Range &r)
Definition: ov-range.h:68
float float_scalar_value(bool frc_str_conv=false) const
Definition: ov-range.h:187
NDArray array_value(bool=false) const
Definition: ov-range.h:196
octave_range(const Range &r, const idx_vector &cache)
Definition: ov-range.h:80
Range range
Definition: ov-range.h:321
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-range.h:152
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-range.h:235
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-range.h:299
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
Definition: ov-range.h:107
bool isnumeric(void) const
Definition: ov-range.h:176
bool is_double_type(void) const
Definition: ov-range.h:172
bool is_range(void) const
Definition: ov-range.h:142
Matrix matrix_value(bool=false) const
Definition: ov-range.h:190
octave_base_value * clone(void) const
Definition: ov-range.h:88
FloatComplexNDArray float_complex_array_value(bool=false) const
Definition: ov-range.h:253
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-range.h:193
bool isreal(void) const
Definition: ov-range.h:170
Array< octave_idx_type > sort_rows_idx(sortmode) const
Definition: ov-range.h:162
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-range.h:131
Range range_value(void) const
Definition: ov-range.h:256
octave_idx_type nnz(void) const
Definition: ov-range.h:122
octave_range(const octave_range &r)
Definition: ov-range.h:75
ComplexNDArray complex_array_value(bool=false) const
Definition: ov-range.h:250
FloatComplexMatrix float_complex_matrix_value(bool=false) const
Definition: ov-range.h:247
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
octave_value_list & operator=(const octave_value_list &obj)=default
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov.h:1339
Matrix matrix_value(bool frc_str_conv=false) const
Definition: ov.h:806
void error(const char *fmt,...)
Definition: error.cc:968
QString name
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
bool is_true(const std::string &s)
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753
T * r
Definition: mx-inlines.cc:773
static float_display_format get_edit_display_format(const octave_value &val)
std::complex< double > Complex
Definition: oct-cmplx.h:33
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
int64_t octave_hdf5_id
sortmode
Definition: oct-sort.h:95
@ UNSORTED
Definition: oct-sort.h:95
@ ASCENDING
Definition: oct-sort.h:95
static double as_double(time_t sec, long usec)
Definition: oct-time.h:37
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:158
builtin_type_t
Definition: ov-base.h:72
@ btyp_double
Definition: ov-base.h:73
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