GNU Octave  9.1.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-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 (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 #include <type_traits>
36 
37 #include "Array-fwd.h"
38 #include "Range.h"
39 
40 #include "lo-mappers.h"
41 #include "lo-utils.h"
42 #include "mx-base.h"
43 #include "str-vec.h"
44 
45 #include "error.h"
46 #include "oct-stream.h"
47 #include "ov-base.h"
48 #include "ov-range-traits.h"
49 #include "ov-re-mat.h"
50 #include "ov-typeinfo.h"
51 
52 class octave_value_list;
53 
54 
56 {
57 public:
58 
60  : m_numel (numel), m_base (base), m_increment(incr) { }
61 
63 
65 
66  OCTINTERP_API octave_value
68  {
69  if (i < m_numel - 1)
70  return m_base + static_cast<int> (i) * m_increment;
71  return m_base + (m_numel - 1) * m_increment;
72  }
73 
74  double
76  {
77  if (i < m_numel - 1)
78  return m_base + static_cast<int> (i) * m_increment;
79  return m_base + (m_numel - 1) * m_increment;
80  }
81 
82  bool is_trivial_range () const { return true; };
83 
84 private:
85  int m_numel = 0;
86  int m_base = 0;
87  int m_increment = 0;
88 
90 };
91 
92 // For now, we only need ov_range<double> but we don't attempt to
93 // enforce that restriction.
94 
95 template <typename T>
96 class
98 {
99 public:
100 
102  : octave_base_value (), m_range (), m_idx_cache () { }
103 
104  ov_range (const octave::range<T>& r)
105  : octave_base_value (), m_range (r), m_idx_cache ()
106  {
107  if (numel () < 0 && numel () != -2)
108  error ("invalid range");
109  }
110 
112  : octave_base_value (), m_range (r.m_range),
113  m_idx_cache (r.m_idx_cache
114  ? new octave::idx_vector (*r.m_idx_cache) : nullptr)
115  { }
116 
117  ov_range (const octave::range<T>& r, const octave::idx_vector& cache)
118  : octave_base_value (), m_range (r), m_idx_cache ()
119  {
120  set_idx_cache (cache);
121  }
122 
123  // No assignment.
124  ov_range& operator = (const ov_range&) = delete;
125 
126  ~ov_range () { clear_cached_info (); }
127 
129  {
130  return new ov_range (*this);
131  }
132 
133  // A range is really just a special kind of real matrix object. In
134  // the places where we need to call empty_clone, it makes more sense
135  // to create an empty matrix (0x0) instead of an empty range (1x0).
136 
138  {
139  return new typename octave_value_range_traits<T>::matrix_type ();
140  }
141 
142  OCTINTERP_API type_conv_info numeric_conversion_function () const;
143 
144  OCTINTERP_API octave_base_value * try_narrowing_conversion ();
145 
147 
148  // We don't need to override all three forms of subsref. The using
149  // declaration will avoid warnings about partially-overloaded virtual
150  // functions.
152 
153  octave_value subsref (const std::string& type,
154  const std::list<octave_value_list>& idx);
155 
156  octave_value_list subsref (const std::string& type,
157  const std::list<octave_value_list>& idx, int)
158  { return subsref (type, idx); }
159 
160  OCTINTERP_API octave_value
161  do_index_op (const octave_value_list& idx, bool resize_ok = false);
162 
163  OCTINTERP_API octave::idx_vector index_vector (bool require_integers = false) const;
164 
165  dim_vector dims () const
166  {
167  octave_idx_type n = numel ();
168  return dim_vector (n > 0, n);
169  }
170 
171  OCTINTERP_API octave_value as_trivial_range ();
172  OCTINTERP_API bool could_be_trivial_range ();
173 
174  OCTINTERP_API octave_value
175  vm_extract_forloop_value (octave_idx_type idx);
176 
177  octave_idx_type numel () const { return m_range.numel (); }
178 
180  {
181  // FIXME: this is a potential waste of memory.
182 
183  octave_value tmp (raw_array_value ());
184  return tmp.nnz ();
185  }
186 
187  OCTINTERP_API octave_value
188  resize (const dim_vector& dv, bool fill = false) const;
189 
190  std::size_t byte_size () const { return 3 * sizeof (T); }
191 
192  octave_value reshape (const dim_vector& new_dims) const
193  {
194  return raw_array_value ().reshape (new_dims);
195  }
196 
197  octave_value permute (const Array<int>& vec, bool inv = false) const
198  {
199  return raw_array_value ().permute (vec, inv);
200  }
201 
202  octave_value squeeze () const { return m_range; }
203 
204  octave_value full_value () const { return raw_array_value (); }
205 
206  bool is_defined () const { return true; }
207 
208  bool is_storable () const { return m_range.is_storable (); }
209 
210  bool is_constant () const { return true; }
211 
212  bool is_range () const { return true; }
213 
214  bool vm_need_storable_call () const { return true; }
215 
216  bool is_double_type () const { return builtin_type () == btyp_double; }
217 
218  bool is_single_type () const { return builtin_type () == btyp_float; }
219 
220  bool isfloat () const { return btyp_isfloat (builtin_type ()); }
221 
222  bool is_int8_type () const { return builtin_type () == btyp_int8; }
223 
224  bool is_int16_type () const { return builtin_type () == btyp_int16; }
225 
226  bool is_int32_type () const { return builtin_type () == btyp_int32; }
227 
228  bool is_int64_type () const { return builtin_type () == btyp_int64; }
229 
230  bool is_uint8_type () const { return builtin_type () == btyp_uint8; }
231 
232  bool is_uint16_type () const { return builtin_type () == btyp_uint16; }
233 
234  bool is_uint32_type () const { return builtin_type () == btyp_uint32; }
235 
236  bool is_uint64_type () const { return builtin_type () == btyp_uint64; }
237 
238  bool isinteger () const
239  {
240  return btyp_isinteger (builtin_type ());
241  }
242 
243  bool isreal () const { return true; }
244 
245  bool isnumeric () const
246  {
247  return btyp_isnumeric (builtin_type ());
248  }
249 
250  bool is_true () const { return nnz () == numel (); }
251 
252  octave_value all (int dim = 0) const
253  {
254  // FIXME: this is a potential waste of memory.
255 
256  typedef typename octave_value_range_traits<T>::matrix_type ov_mx_type;
257  typename ov_mx_type::object_type m (raw_array_value ());
258 
259  return m.all (dim);
260  }
261 
262  octave_value any (int dim = 0) const
263  {
264  // FIXME: this is a potential waste of memory.
265 
266  typedef typename octave_value_range_traits<T>::matrix_type ov_mx_type;
267  typename ov_mx_type::object_type m (raw_array_value ());
268 
269  return m.any (dim);
270  }
271 
273  {
274  // FIXME: this is a potential waste of memory.
275 
276  return m_range.diag (k);
277  }
278 
280  {
281  // FIXME: this is a potential waste of memory.
282 
283  typedef typename octave_value_range_traits<T>::matrix_type ov_mx_type;
284  typename ov_mx_type::object_type m (raw_array_value ());
285 
286  return m.diag (nr, nc);
287  }
288 
290  {
291  Array<T> tmp = raw_array_value ();
292  return tmp.sort (dim, mode);
293  }
294 
296  sortmode mode = ASCENDING) const
297  {
298  Array<T> tmp = raw_array_value ();
299  return tmp.sort (sidx, dim, mode);
300  }
301 
303  {
304  return m_range.issorted (mode);
305  }
306 
308  {
309  return Array<octave_idx_type> (dim_vector (1, 0));
310  }
311 
313  {
314  return (mode == UNSORTED) ? ASCENDING : mode;
315  }
316 
317  Array<T> raw_array_value () const { return m_range.array_value (); }
318 
319  OCTINTERP_API double double_value (bool = false) const;
320 
321  OCTINTERP_API float float_value (bool = false) const;
322 
323  double scalar_value (bool frc_str_conv = false) const
324  {
325  return double_value (frc_str_conv);
326  }
327 
328  float float_scalar_value (bool frc_str_conv = false) const
329  {
330  return float_value (frc_str_conv);
331  }
332 
333  Matrix matrix_value (bool = false) const
334  {
335  return raw_array_value ();
336  }
337 
338  FloatMatrix float_matrix_value (bool = false) const
339  {
340  return raw_array_value ();
341  }
342 
343  NDArray array_value (bool = false) const
344  {
345  return raw_array_value ();
346  }
347 
348  FloatNDArray float_array_value (bool = false) const
349  {
350  return raw_array_value ();
351  }
352 
353  OCTINTERP_API charNDArray char_array_value (bool = false) const;
354 
355  // FIXME: it would be better to have Range::intXNDArray_value
356  // functions to avoid the intermediate conversion to a matrix
357  // object.
358 
360  {
361  return raw_array_value ();
362  }
363 
365  {
366  return raw_array_value ();
367  }
368 
370  {
371  return raw_array_value ();
372  }
373 
375  {
376  return raw_array_value ();
377  }
378 
380  {
381  return raw_array_value ();
382  }
383 
385  {
386  return raw_array_value ();
387  }
388 
390  {
391  return raw_array_value ();
392  }
393 
395  {
396  return raw_array_value ();
397  }
398 
399  SparseMatrix sparse_matrix_value (bool = false) const
400  {
401  return SparseMatrix (matrix_value ());
402  }
403 
405  {
406  return SparseComplexMatrix (complex_matrix_value ());
407  }
408 
409  OCTINTERP_API Complex complex_value (bool = false) const;
410 
411  OCTINTERP_API FloatComplex float_complex_value (bool = false) const;
412 
413  OCTINTERP_API boolNDArray bool_array_value (bool warn = false) const;
414 
415  ComplexMatrix complex_matrix_value (bool = false) const
416  {
417  return raw_array_value ();
418  }
419 
421  {
422  return raw_array_value ();
423  }
424 
425  ComplexNDArray complex_array_value (bool = false) const
426  {
427  return raw_array_value ();
428  }
429 
431  {
432  return raw_array_value ();
433  }
434 
435  OCTINTERP_API octave::range<double> range_value () const;
436 
437 // For now, enable only ov_range<double>.
438 
439  OCTINTERP_API octave_value
440  convert_to_str_internal (bool pad, bool force, char type) const;
441 
442  OCTINTERP_API octave_value as_double () const;
443  OCTINTERP_API octave_value as_single () const;
444 
445  OCTINTERP_API octave_value as_int8 () const;
446  OCTINTERP_API octave_value as_int16 () const;
447  OCTINTERP_API octave_value as_int32 () const;
448  OCTINTERP_API octave_value as_int64 () const;
449 
450  OCTINTERP_API octave_value as_uint8 () const;
451  OCTINTERP_API octave_value as_uint16 () const;
452  OCTINTERP_API octave_value as_uint32 () const;
453  OCTINTERP_API octave_value as_uint64 () const;
454 
455  OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
456 
457  OCTINTERP_API void
458  print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
459 
460  OCTINTERP_API bool
461  print_name_tag (std::ostream& os, const std::string& name) const;
462 
463  OCTINTERP_API void short_disp (std::ostream& os) const;
464 
465  OCTINTERP_API float_display_format get_edit_display_format () const;
466 
467  OCTINTERP_API std::string
468  edit_display (const float_display_format& fmt,
469  octave_idx_type i, octave_idx_type j) const;
470 
471  OCTINTERP_API bool save_ascii (std::ostream& os);
472 
473  OCTINTERP_API bool load_ascii (std::istream& is);
474 
475  OCTINTERP_API bool save_binary (std::ostream& os, bool save_as_floats);
476 
477  OCTINTERP_API bool
478  load_binary (std::istream& is, bool swap,
480 
481  OCTINTERP_API bool
482  save_hdf5 (octave_hdf5_id loc_id, const char *name, bool flag);
483 
484  OCTINTERP_API bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
485 
486  int write (octave::stream& os, int block_size,
487  oct_data_conv::data_type output_type, int skip,
488  octave::mach_info::float_format flt_fmt) const
489  {
490  // FIXME: could be more memory efficient by having a
491  // special case of the octave::stream::write method for ranges.
492 
493  return os.write (matrix_value (), block_size, output_type, skip, flt_fmt);
494  }
495 
496  OCTINTERP_API mxArray * as_mxArray (bool interleaved) const;
497 
499  {
500  octave_value tmp (raw_array_value ());
501  return tmp.map (umap);
502  }
503 
504  OCTINTERP_API octave_value fast_elem_extract (octave_idx_type n) const;
505 
506 protected:
507 
508  octave::range<T> m_range;
509 
511  {
512  delete m_idx_cache;
513  m_idx_cache = new octave::idx_vector (idx);
514  return idx;
515  }
516 
517  void clear_cached_info () const
518  {
519  delete m_idx_cache; m_idx_cache = nullptr;
520  }
521 
523 
525 
527 };
528 
530 
531 // For now, enable only ov_range<double>.
532 
533 // Specializations.
534 
535 template <>
536 OCTINTERP_API octave::range<double>
538 
539 // For now, enable only ov_range<double>.
540 
541 // The following specializations are here to preserve previous Range
542 // performance until solutions can be generalized for other types.
543 
544 template <>
545 OCTINTERP_API octave::idx_vector
546 ov_range<double>::index_vector (bool require_integers) const;
547 
548 template <>
549 OCTINTERP_API octave_idx_type
551 
552 // The following specialization is also historical baggage. For double
553 // ranges, we can produce special double-valued diagnoal matrix objects
554 // but Octave currently provides only double and Complex diagonal matrix
555 // objects.
556 
557 template <>
558 OCTINTERP_API octave_value
560 
561 template <>
562 OCTINTERP_API octave_value
564 
565 template <>
566 OCTINTERP_API void
567 ov_range<double>::print_raw (std::ostream& os, bool pr_as_read_syntax) const;
568 
570 
571 // For now, enable only ov_range<double>.
572 
574 
575 #endif
Array< T, Alloc > sort(int dim=0, sortmode mode=ASCENDING) const
Size of the specified dimension.
Definition: Array-base.cc:1781
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:248
virtual octave_idx_type numel() const
Definition: ov-base.h:391
octave_trivial_range(const octave_trivial_range &)=default
bool is_trivial_range() const
Definition: ov-range.h:82
double vm_extract_forloop_double(octave_idx_type i)
Definition: ov-range.h:75
octave_trivial_range(octave_idx_type numel, int base, int incr)
Definition: ov-range.h:59
octave_value vm_extract_forloop_value(octave_idx_type i)
Definition: ov-range.h:67
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov.h:574
octave_value reshape(const dim_vector &dv) const
Definition: ov.h:571
octave_idx_type nnz() const
Definition: ov.h:565
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1513
octave_value diag(octave_idx_type k=0) const
Definition: ov.h:1410
ComplexMatrix complex_matrix_value(bool=false) const
Definition: ov-range.h:415
double scalar_value(bool frc_str_conv=false) const
Definition: ov-range.h:323
octave::idx_vector set_idx_cache(const octave::idx_vector &idx) const
Definition: ov-range.h:510
bool isreal() const
Definition: ov-range.h:243
bool is_defined() const
Definition: ov-range.h:206
octave_value map(unary_mapper_t umap) const
Definition: ov-range.h:498
bool is_uint8_type() const
Definition: ov-range.h:230
ov_range(const ov_range< T > &r)
Definition: ov-range.h:111
octave_base_value * clone() const
Definition: ov-range.h:128
float float_scalar_value(bool frc_str_conv=false) const
Definition: ov-range.h:328
octave_idx_type numel() const
Definition: ov-range.h:177
int32NDArray int32_array_value() const
Definition: ov-range.h:369
bool is_range() const
Definition: ov-range.h:212
bool is_uint16_type() const
Definition: ov-range.h:232
bool is_int64_type() const
Definition: ov-range.h:228
bool is_double_type() const
Definition: ov-range.h:216
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-range.h:312
octave_value diag(octave_idx_type k=0) const
Definition: ov-range.h:272
ov_range(const octave::range< T > &r)
Definition: ov-range.h:104
builtin_type_t builtin_type() const
Definition: ov-range.h:146
static octave_hdf5_id hdf5_save_type
Definition: ov-range.h:524
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-range.h:289
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-range.h:399
dim_vector dims() const
Definition: ov-range.h:165
FloatNDArray float_array_value(bool=false) const
Definition: ov-range.h:348
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov-range.h:302
FloatComplexMatrix float_complex_matrix_value(bool=false) const
Definition: ov-range.h:420
uint8NDArray uint8_array_value() const
Definition: ov-range.h:379
bool isinteger() const
Definition: ov-range.h:238
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
Definition: ov-range.cc:422
NDArray array_value(bool=false) const
Definition: ov-range.h:343
bool is_int8_type() const
Definition: ov-range.h:222
octave_value full_value() const
Definition: ov-range.h:204
octave_value all(int dim=0) const
Definition: ov-range.h:252
Array< T > raw_array_value() const
Definition: ov-range.h:317
bool vm_need_storable_call() const
Definition: ov-range.h:214
std::size_t byte_size() const
Definition: ov-range.h:190
~ov_range()
Definition: ov-range.h:126
octave_idx_type nnz() const
Definition: ov-range.h:179
bool isfloat() const
Definition: ov-range.h:220
bool is_int16_type() const
Definition: ov-range.h:224
bool is_uint32_type() const
Definition: ov-range.h:234
octave_value squeeze() const
Definition: ov-range.h:202
octave::range< T > m_range
Definition: ov-range.h:508
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
Definition: ov-range.h:156
bool is_constant() const
Definition: ov-range.h:210
bool is_storable() const
Definition: ov-range.h:208
octave_value any(int dim=0) const
Definition: ov-range.h:262
int8NDArray int8_array_value() const
Definition: ov-range.h:359
bool is_single_type() const
Definition: ov-range.h:218
ComplexNDArray complex_array_value(bool=false) const
Definition: ov-range.h:425
bool is_true() const
Definition: ov-range.h:250
octave_value diag(octave_idx_type nr, octave_idx_type nc) const
Definition: ov-range.h:279
octave::idx_vector * m_idx_cache
Definition: ov-range.h:522
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-range.h:338
octave_base_value * empty_clone() const
Definition: ov-range.h:137
octave::range< double > range_value() const
Definition: ov-range.cc:325
ov_range(const octave::range< T > &r, const octave::idx_vector &cache)
Definition: ov-range.h:117
Matrix matrix_value(bool=false) const
Definition: ov-range.h:333
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-range.h:404
int16NDArray int16_array_value() const
Definition: ov-range.h:364
Array< octave_idx_type > sort_rows_idx(sortmode) const
Definition: ov-range.h:307
void clear_cached_info() const
Definition: ov-range.h:517
bool is_uint64_type() const
Definition: ov-range.h:236
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-range.h:192
uint16NDArray uint16_array_value() const
Definition: ov-range.h:384
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:486
ov_range()
Definition: ov-range.h:101
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-range.h:197
FloatComplexNDArray float_complex_array_value(bool=false) const
Definition: ov-range.h:430
bool is_int32_type() const
Definition: ov-range.h:226
bool isnumeric() const
Definition: ov-range.h:245
octave_value sort(Array< octave_idx_type > &sidx, octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-range.h:295
uint32NDArray uint32_array_value() const
Definition: ov-range.h:389
uint64NDArray uint64_array_value() const
Definition: ov-range.h:394
octave::idx_vector index_vector(bool require_integers=false) const
Definition: ov-range.cc:212
int64NDArray int64_array_value() const
Definition: ov-range.h:374
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
T * r
Definition: mx-inlines.cc:781
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:97
@ UNSORTED
Definition: oct-sort.h:97
@ ASCENDING
Definition: oct-sort.h:97
T::size_type numel(const T &str)
Definition: oct-string.cc:74
bool btyp_isnumeric(builtin_type_t btyp)
Definition: ov-base.h:107
bool btyp_isinteger(builtin_type_t btyp)
Definition: ov-base.h:110
#define DECLARE_TEMPLATE_OV_TYPEID_SPECIALIZATIONS(cls, type)
Definition: ov-base.h:203
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:181
bool btyp_isfloat(builtin_type_t btyp)
Definition: ov-base.h:113
builtin_type_t
Definition: ov-base.h:83
@ btyp_double
Definition: ov-base.h:84
@ btyp_int32
Definition: ov-base.h:90
@ btyp_float
Definition: ov-base.h:85
@ btyp_uint16
Definition: ov-base.h:93
@ btyp_int64
Definition: ov-base.h:91
@ btyp_uint64
Definition: ov-base.h:95
@ btyp_int16
Definition: ov-base.h:89
@ btyp_uint32
Definition: ov-base.h:94
@ btyp_uint8
Definition: ov-base.h:92
@ btyp_int8
Definition: ov-base.h:88
ov_range< double > octave_double_range
Definition: ov-range.h:569
octave_double_range octave_range
Definition: ov-range.h:573