GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-lazy-idx.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2010-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_lazy_idx_h)
27 #define octave_ov_lazy_idx_h 1
28 
29 #include "octave-config.h"
30 
31 #include "ov-re-mat.h"
32 
33 // Lazy indices that stay in idx_vector form until the conversion to NDArray is
34 // actually needed.
35 
36 class
37 OCTINTERP_API
39 {
40 public:
41 
43  : octave_base_value (), m_index (), m_value () { }
44 
46  : octave_base_value (), m_index (idx), m_value () { }
47 
49  : octave_base_value (), m_index (i.m_index), m_value (i.m_value) { }
50 
51  ~octave_lazy_index () = default;
52 
54  { return new octave_lazy_index (*this); }
55  octave_base_value * empty_clone () const { return new octave_matrix (); }
56 
57  type_conv_info numeric_conversion_function () const;
58 
59  octave_base_value * try_narrowing_conversion ();
60 
61  octave_value fast_elem_extract (octave_idx_type n) const;
62 
63  std::size_t byte_size () const
64  { return numel () * sizeof (octave_idx_type); }
65 
66  octave_value squeeze () const;
67 
68  octave_value full_value () const { return make_value (); }
69 
70  octave::idx_vector index_vector (bool /* require_integers */ = false) const
71  { return m_index; }
72 
74 
75  bool is_real_matrix () const { return true; }
76 
77  bool isreal () const { return true; }
78 
79  bool is_double_type () const { return true; }
80 
81  bool isfloat () const { return true; }
82 
83  // We don't need to override all three forms of subsref. The using
84  // declaration will avoid warnings about partially-overloaded virtual
85  // functions.
87 
88  octave_value subsref (const std::string& type,
89  const std::list<octave_value_list>& idx)
90  { return make_value ().subsref (type, idx); }
91 
92  octave_value_list subsref (const std::string& type,
93  const std::list<octave_value_list>& idx, int)
94  { return subsref (type, idx); }
95 
97  bool resize_ok = false)
98  { return make_value ().index_op (idx, resize_ok); }
99 
100  dim_vector dims () const { return m_index.orig_dimensions (); }
101 
102  octave_idx_type numel () const { return m_index.length (0); }
103 
104  octave_idx_type nnz () const { return numel (); }
105 
106  octave_value reshape (const dim_vector& new_dims) const;
107 
108  octave_value permute (const Array<int>& vec, bool inv = false) const;
109 
110  octave_value resize (const dim_vector& dv, bool fill = false) const
111  { return make_value ().resize (dv, fill); }
112 
113  octave_value all (int dim = 0) const { return make_value ().all (dim); }
114  octave_value any (int dim = 0) const { return make_value ().any (dim); }
115 
116  MatrixType matrix_type () const { return make_value ().matrix_type (); }
117  MatrixType matrix_type (const MatrixType& _typ) const
118  { return make_value ().matrix_type (_typ); }
119 
120  octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
121 
123  sortmode mode = ASCENDING) const;
124 
125  sortmode issorted (sortmode mode = UNSORTED) const;
126 
127  Array<octave_idx_type> sort_rows_idx (sortmode mode = ASCENDING) const;
128 
129  sortmode is_sorted_rows (sortmode mode = UNSORTED) const;
130 
131  bool is_matrix_type () const { return true; }
132 
133  bool isnumeric () const { return true; }
134 
135  bool is_defined () const { return true; }
136 
137  bool is_constant () const { return true; }
138 
139  bool is_true () const
140  { return make_value ().is_true (); }
141 
142  bool print_as_scalar () const
143  { return make_value ().print_as_scalar (); }
144 
145  void print (std::ostream& os, bool pr_as_read_syntax = false)
146  { make_value ().print (os, pr_as_read_syntax); }
147 
148  void print_info (std::ostream& os, const std::string& prefix) const
149  { make_value ().print_info (os, prefix); }
150 
151 #define FORWARD_VALUE_QUERY(TYPE, NAME) \
152  TYPE NAME () const \
153  { \
154  return make_value ().NAME (); \
155  }
156 
157  FORWARD_VALUE_QUERY (int8NDArray, int8_array_value)
158  FORWARD_VALUE_QUERY (int16NDArray, int16_array_value)
159  FORWARD_VALUE_QUERY (int32NDArray, int32_array_value)
160  FORWARD_VALUE_QUERY (int64NDArray, int64_array_value)
161  FORWARD_VALUE_QUERY (uint8NDArray, uint8_array_value)
162  FORWARD_VALUE_QUERY (uint16NDArray, uint16_array_value)
163  FORWARD_VALUE_QUERY (uint32NDArray, uint32_array_value)
164  FORWARD_VALUE_QUERY (uint64NDArray, uint64_array_value)
165 
166 #define FORWARD_VALUE_QUERY1(TYPE, NAME) \
167  TYPE NAME (bool flag = false) const \
168  { \
169  return make_value ().NAME (flag); \
170  }
171 
172  FORWARD_VALUE_QUERY1 (double, double_value)
173  FORWARD_VALUE_QUERY1 (float, float_value)
174  FORWARD_VALUE_QUERY1 (double, scalar_value)
175  FORWARD_VALUE_QUERY1 (Matrix, matrix_value)
176  FORWARD_VALUE_QUERY1 (FloatMatrix, float_matrix_value)
177  FORWARD_VALUE_QUERY1 (Complex, complex_value)
178  FORWARD_VALUE_QUERY1 (FloatComplex, float_complex_value)
179  FORWARD_VALUE_QUERY1 (ComplexMatrix, complex_matrix_value)
180  FORWARD_VALUE_QUERY1 (FloatComplexMatrix, float_complex_matrix_value)
181  FORWARD_VALUE_QUERY1 (ComplexNDArray, complex_array_value)
182  FORWARD_VALUE_QUERY1 (FloatComplexNDArray, float_complex_array_value)
183  FORWARD_VALUE_QUERY1 (boolNDArray, bool_array_value)
184  FORWARD_VALUE_QUERY1 (charNDArray, char_array_value)
186  FORWARD_VALUE_QUERY1 (FloatNDArray, float_array_value)
187  FORWARD_VALUE_QUERY1 (SparseMatrix, sparse_matrix_value)
188  FORWARD_VALUE_QUERY1 (SparseComplexMatrix, sparse_complex_matrix_value)
189 
190  // We don't need to override both forms of the diag method. The using
191  // declaration will avoid warnings about partially-overloaded virtual
192  // functions.
193  using octave_base_value::diag;
194 
195  octave_value diag (octave_idx_type k = 0) const
196  {
197  return make_value ().diag (k);
198  }
199 
200  octave_value convert_to_str_internal (bool pad, bool force, char type) const
201  {
202  return make_value ().convert_to_str_internal (pad, force, type);
203  }
204 
205  octave_value as_double () const;
206  octave_value as_single () const;
207 
208  octave_value as_int8 () const;
209  octave_value as_int16 () const;
210  octave_value as_int32 () const;
211  octave_value as_int64 () const;
212 
213  octave_value as_uint8 () const;
214  octave_value as_uint16 () const;
215  octave_value as_uint32 () const;
216  octave_value as_uint64 () const;
217 
218  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const
219  {
220  return make_value ().print_raw (os, pr_as_read_syntax);
221  }
222 
223  bool save_ascii (std::ostream& os);
224 
225  bool load_ascii (std::istream& is);
226 
227  bool save_binary (std::ostream& os, bool save_as_floats);
228 
229  bool load_binary (std::istream& is, bool swap,
231 
232  int write (octave::stream& os, int block_size,
233  oct_data_conv::data_type output_type, int skip,
234  octave::mach_info::float_format flt_fmt) const
235  {
236  return make_value ().write (os, block_size, output_type, skip, flt_fmt);
237  }
238 
239  // This function exists to support the MEX interface.
240  // You should not use it anywhere else.
241  const void * mex_get_data () const
242  {
243  return make_value ().mex_get_data ();
244  }
245 
246  mxArray * as_mxArray (bool interleaved) const
247  {
248  return make_value ().as_mxArray (interleaved);
249  }
250 
252  {
253  return make_value ().map (umap);
254  }
255 
256 private:
257 
258  const octave_value& make_value () const
259  {
260  if (m_value.is_undefined ())
261  m_value = octave_value (m_index, false);
262 
263  return m_value;
264  }
265 
266  octave_value& make_value ()
267  {
268  if (m_value.is_undefined ())
269  m_value = octave_value (m_index, false);
270 
271  return m_value;
272  }
273 
274  octave::idx_vector m_index;
275  mutable octave_value m_value;
276 
277  static octave_base_value *
278  numeric_conversion_function (const octave_base_value&);
279 
281 };
282 
283 #endif
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
bool isnumeric() const
Definition: ov-lazy-idx.h:133
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-lazy-idx.h:110
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-lazy-idx.h:96
const void * mex_get_data() const
Definition: ov-lazy-idx.h:241
bool is_true() const
Definition: ov-lazy-idx.h:139
octave_base_value * clone() const
Definition: ov-lazy-idx.h:53
octave_base_value * empty_clone() const
Definition: ov-lazy-idx.h:55
bool is_defined() const
Definition: ov-lazy-idx.h:135
octave_lazy_index(const octave::idx_vector &idx)
Definition: ov-lazy-idx.h:45
mxArray * as_mxArray(bool interleaved) const
Definition: ov-lazy-idx.h:246
bool isreal() const
Definition: ov-lazy-idx.h:77
std::size_t byte_size() const
Definition: ov-lazy-idx.h:63
builtin_type_t builtin_type() const
Definition: ov-lazy-idx.h:73
bool print_as_scalar() const
Definition: ov-lazy-idx.h:142
void print(std::ostream &os, bool pr_as_read_syntax=false)
Definition: ov-lazy-idx.h:145
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-lazy-idx.h:200
bool isfloat() const
Definition: ov-lazy-idx.h:81
octave_value all(int dim=0) const
Definition: ov-lazy-idx.h:113
octave_idx_type numel() const
Definition: ov-lazy-idx.h:102
dim_vector dims() const
Definition: ov-lazy-idx.h:100
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
Definition: ov-lazy-idx.h:92
bool is_matrix_type() const
Definition: ov-lazy-idx.h:131
bool is_constant() const
Definition: ov-lazy-idx.h:137
octave_lazy_index(const octave_lazy_index &i)
Definition: ov-lazy-idx.h:48
~octave_lazy_index()=default
bool is_double_type() const
Definition: ov-lazy-idx.h:79
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-lazy-idx.h:88
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
Definition: ov-lazy-idx.h:218
octave_value map(unary_mapper_t umap) const
Definition: ov-lazy-idx.h:251
bool is_real_matrix() const
Definition: ov-lazy-idx.h:75
MatrixType matrix_type(const MatrixType &_typ) const
Definition: ov-lazy-idx.h:117
MatrixType matrix_type() const
Definition: ov-lazy-idx.h:116
octave_value full_value() const
Definition: ov-lazy-idx.h:68
octave_idx_type nnz() const
Definition: ov-lazy-idx.h:104
octave_value any(int dim=0) const
Definition: ov-lazy-idx.h:114
void print_info(std::ostream &os, const std::string &prefix) const
Definition: ov-lazy-idx.h:148
octave::idx_vector index_vector(bool=false) const
Definition: ov-lazy-idx.h:70
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-lazy-idx.h:232
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov.h:1312
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov.h:476
octave_value index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov.h:504
octave_value any(int dim=0) const
Definition: ov.h:687
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1513
octave_value all(int dim=0) const
Definition: ov.h:684
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov.h:580
octave_value diag(octave_idx_type k=0) const
Definition: ov.h:1410
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
float_format
Definition: mach-info.h:38
octave_idx_type n
Definition: mx-inlines.cc:761
std::complex< double > Complex
Definition: oct-cmplx.h:33
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
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
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:181
builtin_type_t
Definition: ov-base.h:83
@ btyp_double
Definition: ov-base.h:84
#define FORWARD_VALUE_QUERY(TYPE, NAME)
Definition: ov-lazy-idx.h:151
#define FORWARD_VALUE_QUERY1(TYPE, NAME)
Definition: ov-lazy-idx.h:166