GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-lazy-idx.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2010-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 (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
36class OCTINTERP_API octave_lazy_index : public octave_base_value
37{
38public:
39
41 : octave_base_value (), m_index (), m_value () { }
42
43 octave_lazy_index (const octave::idx_vector& idx)
44 : octave_base_value (), m_index (idx), m_value () { }
45
47 : octave_base_value (), m_index (i.m_index), m_value (i.m_value) { }
48
49 ~octave_lazy_index () = default;
50
52 { return new octave_lazy_index (*this); }
53 octave_base_value * empty_clone () const { return new octave_matrix (); }
54
55 type_conv_info numeric_conversion_function () const;
56
58
60
61 std::size_t byte_size () const
62 { return numel () * sizeof (octave_idx_type); }
63
64 octave_value squeeze () const;
65
66 octave_value full_value () const { return make_value (); }
67
68 octave::idx_vector index_vector (bool /* require_integers */ = false) const
69 { return m_index; }
70
72
73 bool is_real_matrix () const { return true; }
74
75 bool isreal () const { return true; }
76
77 bool is_double_type () const { return true; }
78
79 bool isfloat () 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 ().index_op (idx, resize_ok); }
97
98 dim_vector dims () const { return m_index.orig_dimensions (); }
99
100 octave_idx_type numel () const { return m_index.length (0); }
101
102 octave_idx_type nnz () 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 () const { return make_value ().matrix_type (); }
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
126
128
129 bool is_matrix_type () const { return true; }
130
131 bool isnumeric () const { return true; }
132
133 bool is_defined () const { return true; }
134
135 bool is_constant () const { return true; }
136
137 bool is_true () const
138 { return make_value ().is_true (); }
139
140 bool print_as_scalar () 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 std::string edit_display (const float_display_format& fmt,
148 { return make_value ().edit_display (fmt, i, j); }
149
150 void print_info (std::ostream& os, const std::string& prefix) const
151 { make_value ().print_info (os, prefix); }
152
153#define FORWARD_VALUE_QUERY(TYPE, NAME) \
154 TYPE NAME () const \
155 { \
156 return make_value ().NAME (); \
157 }
158
164 FORWARD_VALUE_QUERY (uint16NDArray, uint16_array_value)
165 FORWARD_VALUE_QUERY (uint32NDArray, uint32_array_value)
166 FORWARD_VALUE_QUERY (uint64NDArray, uint64_array_value)
167
168#define FORWARD_VALUE_QUERY1(TYPE, NAME) \
169 TYPE NAME (bool flag = false) const \
170 { \
171 return make_value ().NAME (flag); \
172 }
173
174 FORWARD_VALUE_QUERY1 (double, double_value)
175 FORWARD_VALUE_QUERY1 (float, float_value)
176 FORWARD_VALUE_QUERY1 (double, scalar_value)
178 FORWARD_VALUE_QUERY1 (FloatMatrix, float_matrix_value)
180 FORWARD_VALUE_QUERY1 (FloatComplex, float_complex_value)
181 FORWARD_VALUE_QUERY1 (ComplexMatrix, complex_matrix_value)
182 FORWARD_VALUE_QUERY1 (FloatComplexMatrix, float_complex_matrix_value)
184 FORWARD_VALUE_QUERY1 (FloatComplexNDArray, float_complex_array_value)
189 FORWARD_VALUE_QUERY1 (SparseMatrix, sparse_matrix_value)
190 FORWARD_VALUE_QUERY1 (SparseComplexMatrix, sparse_complex_matrix_value)
191
192 // We don't need to override both forms of the diag method. The using
193 // declaration will avoid warnings about partially-overloaded virtual
194 // functions.
195 using octave_base_value::diag;
196
197 octave_value diag (octave_idx_type k = 0) const
198 {
199 return make_value ().diag (k);
200 }
201
202 octave_value convert_to_str_internal (bool pad, bool force, char type) const
203 {
204 return make_value ().convert_to_str_internal (pad, force, type);
205 }
206
207 octave_value as_double () const;
208 octave_value as_single () const;
209
210 octave_value as_int8 () const;
211 octave_value as_int16 () const;
212 octave_value as_int32 () const;
213 octave_value as_int64 () const;
214
215 octave_value as_uint8 () const;
216 octave_value as_uint16 () const;
217 octave_value as_uint32 () const;
218 octave_value as_uint64 () const;
219
220 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const
221 {
222 return make_value ().print_raw (os, pr_as_read_syntax);
223 }
224
225 bool save_ascii (std::ostream& os);
226
227 bool load_ascii (std::istream& is);
228
229 bool save_binary (std::ostream& os, bool save_as_floats);
230
231 bool load_binary (std::istream& is, bool swap,
232 octave::mach_info::float_format fmt);
233
234 int write (octave::stream& os, int block_size,
235 oct_data_conv::data_type output_type, int skip,
236 octave::mach_info::float_format flt_fmt) const
237 {
238 return make_value ().write (os, block_size, output_type, skip, flt_fmt);
239 }
240
241 // This function exists to support the MEX interface.
242 // You should not use it anywhere else.
243 const void * mex_get_data () const
244 {
245 return make_value ().mex_get_data ();
246 }
247
248 mxArray * as_mxArray (bool interleaved) const
249 {
250 return make_value ().as_mxArray (interleaved);
251 }
252
254 {
255 return make_value ().map (umap);
256 }
257
258private:
259
260 const octave_value& make_value () const
261 {
262 if (m_value.is_undefined ())
263 m_value = octave_value (m_index, false);
264
265 return m_value;
266 }
267
268 octave_value& make_value ()
269 {
270 if (m_value.is_undefined ())
271 m_value = octave_value (m_index, false);
272
273 return m_value;
274 }
275
276 octave::idx_vector m_index;
277 mutable octave_value m_value;
278
279 static octave_base_value *
281
283};
284
285#endif
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition ov-base.cc:245
virtual bool save_ascii(std::ostream &os)
Definition ov-base.cc:986
virtual octave_value as_double() const
Definition ov-base.cc:153
virtual octave_idx_type numel() const
Definition ov-base.h:401
virtual octave_value fast_elem_extract(octave_idx_type n) const
Definition ov-base.cc:1394
virtual octave_value as_uint8() const
Definition ov-base.cc:196
virtual octave_value permute(const Array< int > &vec, bool=false) const
Definition ov-base.cc:395
virtual sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition ov-base.cc:1073
virtual octave_value as_uint16() const
Definition ov-base.cc:202
virtual octave_base_value * try_narrowing_conversion()
Definition ov-base.h:332
virtual octave_value as_int32() const
Definition ov-base.cc:184
virtual bool save_binary(std::ostream &os, bool save_as_floats)
Definition ov-base.cc:998
virtual type_conv_info numeric_conversion_function() const
Definition ov-base.h:303
virtual octave_value reshape(const dim_vector &) const
Definition ov-base.cc:389
virtual bool load_ascii(std::istream &is)
Definition ov-base.cc:992
virtual bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition ov-base.cc:1004
virtual Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Definition ov-base.cc:1067
virtual octave_value as_int64() const
Definition ov-base.cc:190
virtual octave_value squeeze() const
Definition ov-base.cc:140
virtual octave_value as_uint32() const
Definition ov-base.cc:208
virtual octave_value as_int16() const
Definition ov-base.cc:178
virtual octave_value as_int8() const
Definition ov-base.cc:172
virtual octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition ov-base.cc:1048
virtual sortmode issorted(sortmode mode=UNSORTED) const
Definition ov-base.cc:1061
virtual octave_value as_single() const
Definition ov-base.cc:166
friend class octave_value
Definition ov-base.h:278
virtual octave_value as_uint64() const
Definition ov-base.cc:214
mxArray * as_mxArray(bool interleaved) const
bool isnumeric() const
octave_value resize(const dim_vector &dv, bool fill=false) const
octave_base_value * clone() const
Definition ov-lazy-idx.h:51
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition ov-lazy-idx.h:94
bool is_true() const
bool is_defined() const
octave_lazy_index(const octave::idx_vector &idx)
Definition ov-lazy-idx.h:43
bool isreal() const
Definition ov-lazy-idx.h:75
std::size_t byte_size() const
Definition ov-lazy-idx.h:61
std::string edit_display(const float_display_format &fmt, octave_idx_type i, octave_idx_type j) const
builtin_type_t builtin_type() const
Definition ov-lazy-idx.h:71
octave_base_value * empty_clone() const
Definition ov-lazy-idx.h:53
bool print_as_scalar() const
void print(std::ostream &os, bool pr_as_read_syntax=false)
octave_value convert_to_str_internal(bool pad, bool force, char type) const
bool isfloat() const
Definition ov-lazy-idx.h:79
octave_value all(int dim=0) const
octave_idx_type numel() const
dim_vector dims() const
Definition ov-lazy-idx.h:98
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
Definition ov-lazy-idx.h:90
bool is_matrix_type() const
bool is_constant() const
octave_lazy_index(const octave_lazy_index &i)
Definition ov-lazy-idx.h:46
~octave_lazy_index()=default
bool is_double_type() const
Definition ov-lazy-idx.h:77
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition ov-lazy-idx.h:86
const void * mex_get_data() const
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
octave_value map(unary_mapper_t umap) const
bool is_real_matrix() const
Definition ov-lazy-idx.h:73
MatrixType matrix_type(const MatrixType &_typ) const
MatrixType matrix_type() const
octave_value full_value() const
Definition ov-lazy-idx.h:66
octave_idx_type nnz() const
octave_value any(int dim=0) const
void print_info(std::ostream &os, const std::string &prefix) const
octave::idx_vector index_vector(bool=false) const
Definition ov-lazy-idx.h:68
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
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition ov.h:1327
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:1528
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:1425
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
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA_API(API)
Definition ov-base.h:185
builtin_type_t
Definition ov-base.h:83
@ btyp_double
Definition ov-base.h:84
#define FORWARD_VALUE_QUERY(TYPE, NAME)
#define FORWARD_VALUE_QUERY1(TYPE, NAME)