GNU Octave  6.2.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-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_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 (), index (), value () { }
44 
46  : octave_base_value (), index (idx), value () { }
47 
49  : octave_base_value (), index (i.index), value (i.value) { }
50 
51  ~octave_lazy_index (void) = default;
52 
53  octave_base_value * clone (void) const
54  { return new octave_lazy_index (*this); }
55  octave_base_value * empty_clone (void) const { return new octave_matrix (); }
56 
57  type_conv_info numeric_conversion_function (void) const;
58 
59  octave_base_value * try_narrowing_conversion (void);
60 
61  octave_value fast_elem_extract (octave_idx_type n) const;
62 
63  size_t byte_size (void) const { return numel () * sizeof (octave_idx_type); }
64 
65  octave_value squeeze (void) const;
66 
67  octave_value full_value (void) const { return make_value (); }
68 
69  idx_vector index_vector (bool /* require_integers */ = false) const { return index; }
70 
71  builtin_type_t builtin_type (void) const { return btyp_double; }
72 
73  bool is_real_matrix (void) const { return true; }
74 
75  bool isreal (void) const { return true; }
76 
77  bool is_double_type (void) const { return true; }
78 
79  bool isfloat (void) const { return true; }
80 
81  // We don't need to override all three forms of subsref. The using
82  // declaration will avoid warnings about partially-overloaded virtual
83  // functions.
85 
86  octave_value subsref (const std::string& type,
87  const std::list<octave_value_list>& idx)
88  { return make_value ().subsref (type, idx); }
89 
90  octave_value_list subsref (const std::string& type,
91  const std::list<octave_value_list>& idx, int)
92  { return subsref (type, idx); }
93 
95  bool resize_ok = false)
96  { return make_value ().do_index_op (idx, resize_ok); }
97 
98  dim_vector dims (void) const { return index.orig_dimensions (); }
99 
100  octave_idx_type numel (void) const { return index.length (0); }
101 
102  octave_idx_type nnz (void) const { return numel (); }
103 
104  octave_value reshape (const dim_vector& new_dims) const;
105 
106  octave_value permute (const Array<int>& vec, bool inv = false) const;
107 
108  octave_value resize (const dim_vector& dv, bool fill = false) const
109  { return make_value ().resize (dv, fill); }
110 
111  octave_value all (int dim = 0) const { return make_value ().all (dim); }
112  octave_value any (int dim = 0) const { return make_value ().any (dim); }
113 
114  MatrixType matrix_type (void) const { return make_value ().matrix_type (); }
115  MatrixType matrix_type (const MatrixType& _typ) const
116  { return make_value ().matrix_type (_typ); }
117 
118  octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
119 
121  sortmode mode = ASCENDING) const;
122 
123  sortmode issorted (sortmode mode = UNSORTED) const;
124 
125  Array<octave_idx_type> sort_rows_idx (sortmode mode = ASCENDING) const;
126 
127  sortmode is_sorted_rows (sortmode mode = UNSORTED) const;
128 
129  bool is_matrix_type (void) const { return true; }
130 
131  bool isnumeric (void) const { return true; }
132 
133  bool is_defined (void) const { return true; }
134 
135  bool is_constant (void) const { return true; }
136 
137  bool is_true (void) const
138  { return make_value ().is_true (); }
139 
140  bool print_as_scalar (void) const
141  { return make_value ().print_as_scalar (); }
142 
143  void print (std::ostream& os, bool pr_as_read_syntax = false)
144  { make_value ().print (os, pr_as_read_syntax); }
145 
146  void print_info (std::ostream& os, const std::string& prefix) const
147  { make_value ().print_info (os, prefix); }
148 
149 #define FORWARD_VALUE_QUERY(TYPE, NAME) \
150  TYPE NAME (void) const \
151  { \
152  return make_value ().NAME (); \
153  }
154 
155  FORWARD_VALUE_QUERY (int8NDArray, int8_array_value)
156  FORWARD_VALUE_QUERY (int16NDArray, int16_array_value)
157  FORWARD_VALUE_QUERY (int32NDArray, int32_array_value)
158  FORWARD_VALUE_QUERY (int64NDArray, int64_array_value)
159  FORWARD_VALUE_QUERY (uint8NDArray, uint8_array_value)
160  FORWARD_VALUE_QUERY (uint16NDArray, uint16_array_value)
161  FORWARD_VALUE_QUERY (uint32NDArray, uint32_array_value)
162  FORWARD_VALUE_QUERY (uint64NDArray, uint64_array_value)
163 
164 #define FORWARD_VALUE_QUERY1(TYPE, NAME) \
165  TYPE NAME (bool flag = false) const \
166  { \
167  return make_value ().NAME (flag); \
168  }
169 
170  FORWARD_VALUE_QUERY1 (double, double_value)
171  FORWARD_VALUE_QUERY1 (float, float_value)
172  FORWARD_VALUE_QUERY1 (double, scalar_value)
173  FORWARD_VALUE_QUERY1 (Matrix, matrix_value)
174  FORWARD_VALUE_QUERY1 (FloatMatrix, float_matrix_value)
175  FORWARD_VALUE_QUERY1 (Complex, complex_value)
176  FORWARD_VALUE_QUERY1 (FloatComplex, float_complex_value)
177  FORWARD_VALUE_QUERY1 (ComplexMatrix, complex_matrix_value)
178  FORWARD_VALUE_QUERY1 (FloatComplexMatrix, float_complex_matrix_value)
179  FORWARD_VALUE_QUERY1 (ComplexNDArray, complex_array_value)
180  FORWARD_VALUE_QUERY1 (FloatComplexNDArray, float_complex_array_value)
181  FORWARD_VALUE_QUERY1 (boolNDArray, bool_array_value)
182  FORWARD_VALUE_QUERY1 (charNDArray, char_array_value)
184  FORWARD_VALUE_QUERY1 (FloatNDArray, float_array_value)
185  FORWARD_VALUE_QUERY1 (SparseMatrix, sparse_matrix_value)
186  FORWARD_VALUE_QUERY1 (SparseComplexMatrix, sparse_complex_matrix_value)
187 
188  // We don't need to override both forms of the diag method. The using
189  // declaration will avoid warnings about partially-overloaded virtual
190  // functions.
191  using octave_base_value::diag;
192 
193  octave_value diag (octave_idx_type k = 0) const
194  {
195  return make_value ().diag (k);
196  }
197 
198  octave_value convert_to_str_internal (bool pad, bool force, char type) const
199  {
200  return make_value ().convert_to_str_internal (pad, force, type);
201  }
202 
203  octave_value as_double (void) const;
204  octave_value as_single (void) const;
205 
206  octave_value as_int8 (void) const;
207  octave_value as_int16 (void) const;
208  octave_value as_int32 (void) const;
209  octave_value as_int64 (void) const;
210 
211  octave_value as_uint8 (void) const;
212  octave_value as_uint16 (void) const;
213  octave_value as_uint32 (void) const;
214  octave_value as_uint64 (void) const;
215 
216  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const
217  {
218  return make_value ().print_raw (os, pr_as_read_syntax);
219  }
220 
221  bool save_ascii (std::ostream& os);
222 
223  bool load_ascii (std::istream& is);
224 
225  bool save_binary (std::ostream& os, bool save_as_floats);
226 
227  bool load_binary (std::istream& is, bool swap,
229 
230  int write (octave::stream& os, int block_size,
231  oct_data_conv::data_type output_type, int skip,
232  octave::mach_info::float_format flt_fmt) const
233  {
234  return make_value ().write (os, block_size, output_type, skip, flt_fmt);
235  }
236 
237  // Unsafe. This function exists to support the MEX interface.
238  // You should not use it anywhere else.
239  void * mex_get_data (void) const
240  {
241  return make_value ().mex_get_data ();
242  }
243 
244  mxArray * as_mxArray (void) const
245  {
246  return make_value ().as_mxArray ();
247  }
248 
250  {
251  return make_value ().map (umap);
252  }
253 
254 private:
255 
256  const octave_value& make_value (void) const
257  {
258  if (value.is_undefined ())
259  value = octave_value (index, false);
260 
261  return value;
262  }
263 
265  {
266  if (value.is_undefined ())
267  value = octave_value (index, false);
268 
269  return value;
270  }
271 
274 
275  static octave_base_value *
277 
279 };
280 
281 #endif
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:201
bool print_as_scalar(void) const
Definition: ov-lazy-idx.h:140
octave_value full_value(void) const
Definition: ov-lazy-idx.h:67
void * mex_get_data(void) const
Definition: ov-lazy-idx.h:239
octave_idx_type nnz(void) const
Definition: ov-lazy-idx.h:102
bool isnumeric(void) const
Definition: ov-lazy-idx.h:131
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-lazy-idx.h:108
bool is_real_matrix(void) const
Definition: ov-lazy-idx.h:73
bool is_defined(void) const
Definition: ov-lazy-idx.h:133
static octave_base_value * numeric_conversion_function(const octave_base_value &)
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-lazy-idx.h:94
octave_lazy_index(void)
Definition: ov-lazy-idx.h:42
bool is_double_type(void) const
Definition: ov-lazy-idx.h:77
const octave_value & make_value(void) const
Definition: ov-lazy-idx.h:256
octave_base_value * empty_clone(void) const
Definition: ov-lazy-idx.h:55
dim_vector dims(void) const
Definition: ov-lazy-idx.h:98
bool isfloat(void) const
Definition: ov-lazy-idx.h:79
builtin_type_t builtin_type(void) const
Definition: ov-lazy-idx.h:71
octave_value & make_value(void)
Definition: ov-lazy-idx.h:264
octave_base_value * clone(void) const
Definition: ov-lazy-idx.h:53
void print(std::ostream &os, bool pr_as_read_syntax=false)
Definition: ov-lazy-idx.h:143
bool is_true(void) const
Definition: ov-lazy-idx.h:137
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-lazy-idx.h:198
bool is_matrix_type(void) const
Definition: ov-lazy-idx.h:129
idx_vector index_vector(bool=false) const
Definition: ov-lazy-idx.h:69
octave_value all(int dim=0) const
Definition: ov-lazy-idx.h:111
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
Definition: ov-lazy-idx.h:90
octave_lazy_index(const octave_lazy_index &i)
Definition: ov-lazy-idx.h:48
octave_idx_type numel(void) const
Definition: ov-lazy-idx.h:100
mxArray * as_mxArray(void) const
Definition: ov-lazy-idx.h:244
MatrixType matrix_type(void) const
Definition: ov-lazy-idx.h:114
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-lazy-idx.h:86
octave_value value
Definition: ov-lazy-idx.h:273
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
Definition: ov-lazy-idx.h:216
bool isreal(void) const
Definition: ov-lazy-idx.h:75
bool is_constant(void) const
Definition: ov-lazy-idx.h:135
octave_value map(unary_mapper_t umap) const
Definition: ov-lazy-idx.h:249
idx_vector index
Definition: ov-lazy-idx.h:272
MatrixType matrix_type(const MatrixType &_typ) const
Definition: ov-lazy-idx.h:115
octave_value any(int dim=0) const
Definition: ov-lazy-idx.h:112
octave_lazy_index(const idx_vector &idx)
Definition: ov-lazy-idx.h:45
size_t byte_size(void) const
Definition: ov-lazy-idx.h:63
void print_info(std::ostream &os, const std::string &prefix) const
Definition: ov-lazy-idx.h:146
~octave_lazy_index(void)=default
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:230
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov.h:1221
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov.h:449
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov.h:475
octave_value any(int dim=0) const
Definition: ov.h:640
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1436
octave_value all(int dim=0) const
Definition: ov.h:637
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov.h:539
octave_value diag(octave_idx_type k=0) const
Definition: ov.h:1333
octave_idx_type n
Definition: mx-inlines.cc:753
std::complex< double > Complex
Definition: oct-cmplx.h:33
std::complex< float > FloatComplex
Definition: oct-cmplx.h:34
sortmode
Definition: oct-sort.h:95
@ UNSORTED
Definition: oct-sort.h:95
@ ASCENDING
Definition: oct-sort.h:95
T::size_type numel(const T &str)
Definition: oct-string.cc:71
static double as_double(time_t sec, long usec)
Definition: oct-time.h:37
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:158
builtin_type_t
Definition: ov-base.h:72
@ btyp_double
Definition: ov-base.h:73
#define FORWARD_VALUE_QUERY(TYPE, NAME)
Definition: ov-lazy-idx.h:149
#define FORWARD_VALUE_QUERY1(TYPE, NAME)
Definition: ov-lazy-idx.h:164