GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-lazy-idx.cc
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 (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "ov-lazy-idx.h"
31 #include "ops.h"
32 #include "ov-scalar.h"
33 #include "ls-oct-text.h"
34 #include "ls-oct-binary.h"
35 
37 
38 static octave_base_value *
39 default_numeric_conversion_function (const octave_base_value& a)
40 {
41  const octave_lazy_index& v = dynamic_cast<const octave_lazy_index&> (a);
42 
43  return v.full_value ().clone ();
44 }
45 
48 {
49  return octave_base_value::type_conv_info (default_numeric_conversion_function,
51 }
52 
55 {
56  octave_base_value *retval = nullptr;
57 
58  switch (m_index.length (0))
59  {
60  case 1:
61  retval = new octave_scalar (static_cast<double> (m_index(0) + 1));
62  break;
63 
64  case 0:
65  retval = new octave_matrix (NDArray (m_index.orig_dimensions ()));
66  break;
67 
68  default:
69  break;
70  }
71 
72  return retval;
73 }
74 
77 {
78  return double (m_index.checkelem (n) + 1);
79 }
80 
82 octave_lazy_index::reshape (const dim_vector& new_dims) const
83 {
84  return octave::idx_vector (m_index.as_array ().reshape (new_dims),
85  m_index.extent (0));
86 }
87 
89 octave_lazy_index::permute (const Array<int>& vec, bool inv) const
90 {
91  // If the conversion has already been made, forward the operation.
92  if (m_value.is_defined ())
93  return m_value.permute (vec, inv);
94  else
95  return octave::idx_vector (m_index.as_array ().permute (vec, inv),
96  m_index.extent (0));
97 }
98 
101 {
102  return octave::idx_vector (m_index.as_array ().squeeze (),
103  m_index.extent (0));
104 }
105 
108 {
109  const dim_vector odims = m_index.orig_dimensions ();
110  // index_vector can employ a more efficient sorting algorithm.
111  if (mode == ASCENDING && odims.ndims () == 2
112  && (dim >= 0 && dim <= 1) && odims(1-dim) == 1)
113  return index_vector ().sorted ();
114  else
115  return octave::idx_vector (m_index.as_array ().sort (dim, mode),
116  m_index.extent (0));
117 }
118 
121  sortmode mode) const
122 {
123  const dim_vector odims = m_index.orig_dimensions ();
124  // index_vector can employ a more efficient sorting algorithm.
125  if (mode == ASCENDING && odims.ndims () == 2
126  && (dim >= 0 && dim <= 1) && odims(1-dim) == 1)
127  return index_vector ().sorted (sidx);
128  else
129  return octave::idx_vector (m_index.as_array ().sort (sidx, dim, mode),
130  m_index.extent (0));
131 }
132 
133 sortmode
135 {
136  if (m_index.is_range ())
137  {
138  // Avoid the array conversion.
139  octave_idx_type inc = m_index.increment ();
140  if (inc == 0)
141  return (mode == UNSORTED ? ASCENDING : mode);
142  else if (inc > 0)
143  return (mode == DESCENDING ? UNSORTED : ASCENDING);
144  else
145  return (mode == ASCENDING ? UNSORTED : DESCENDING);
146  }
147  else
148  return m_index.as_array ().issorted (mode);
149 }
150 
153 {
154  return m_index.as_array ().sort_rows_idx (mode);
155 }
156 
157 sortmode
159 {
160  return m_index.as_array ().is_sorted_rows (mode);
161 }
162 
165 {
166  return array_value ();
167 }
168 
171 {
172  return float_array_value ();
173 }
174 
177 {
178  return int8_array_value ();
179 }
180 
183 {
184  return int16_array_value ();
185 }
186 
189 {
190  return int32_array_value ();
191 }
192 
195 {
196  return int64_array_value ();
197 }
198 
201 {
202  return uint8_array_value ();
203 }
204 
207 {
208  return uint16_array_value ();
209 }
210 
213 {
214  return uint32_array_value ();
215 }
216 
219 {
220  return uint64_array_value ();
221 }
222 
223 static const std::string value_save_tag ("index_value");
224 
225 bool
227 {
228  return save_text_data (os, make_value (), value_save_tag, false, 0);
229 }
230 
231 bool
233 {
234  bool dummy;
235 
236  std::string nm = read_text_data (is, "", dummy, m_value, 0);
237  if (nm != value_save_tag)
238  error ("lazy_index: corrupted data on load");
239 
240  m_index = m_value.index_vector ();
241 
242  return true;
243 }
244 
245 bool
246 octave_lazy_index::save_binary (std::ostream& os, bool save_as_floats)
247 {
248  return save_binary_data (os, make_value (), value_save_tag,
249  "", false, save_as_floats);
250 }
251 
252 bool
253 octave_lazy_index::load_binary (std::istream& is, bool swap,
255 {
256  bool dummy;
257  std::string doc;
258 
259  std::string nm = read_binary_data (is, swap, fmt, "", dummy, m_value, doc);
260  if (nm != value_save_tag)
261  error ("lazy_index: corrupted data on load");
262 
263  m_index = m_value.index_vector ();
264 
265  return true;
266 }
267 
268 /*
269 %!shared x, y
270 %! x = find ([-1, 0, -2, 1, 3, -4] < 0);
271 %! y = [1, 3, 6];
272 %!assert (typeinfo (x), "lazy_index")
273 %!assert (double (x), y)
274 %!assert (single (x), single (y))
275 %!assert (int8 (x), int8 (y))
276 %!assert (int16 (x), int16 (y))
277 %!assert (int32 (x), int32 (y))
278 %!assert (int64 (x), int64 (y))
279 %!assert (uint8 (x), uint8 (y))
280 %!assert (uint16 (x), uint16 (y))
281 %!assert (uint32 (x), uint32 (y))
282 %!assert (uint64 (x), uint64 (y))
283 */
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_idx_type ndims() const
Number of dimensions.
Definition: dim-vector.h:257
octave_value as_int64() const
Definition: ov-lazy-idx.cc:194
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov-lazy-idx.cc:134
bool save_ascii(std::ostream &os)
Definition: ov-lazy-idx.cc:226
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-lazy-idx.cc:82
octave_value as_int32() const
Definition: ov-lazy-idx.cc:188
octave_value as_int16() const
Definition: ov-lazy-idx.cc:182
octave_value as_uint64() const
Definition: ov-lazy-idx.cc:218
uint8NDArray uint8_array_value() const
Definition: ov-lazy-idx.h:161
int8NDArray int8_array_value() const
Definition: ov-lazy-idx.h:157
uint32NDArray uint32_array_value() const
Definition: ov-lazy-idx.h:163
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-lazy-idx.cc:253
int16NDArray int16_array_value() const
Definition: ov-lazy-idx.h:158
int32NDArray int32_array_value() const
Definition: ov-lazy-idx.h:159
octave_value fast_elem_extract(octave_idx_type n) const
Definition: ov-lazy-idx.cc:76
bool load_ascii(std::istream &is)
Definition: ov-lazy-idx.cc:232
uint64NDArray uint64_array_value() const
Definition: ov-lazy-idx.h:164
octave_base_value * try_narrowing_conversion()
Definition: ov-lazy-idx.cc:54
uint16NDArray uint16_array_value() const
Definition: ov-lazy-idx.h:162
FloatNDArray float_array_value(bool flag=false) const
Definition: ov-lazy-idx.h:186
octave_value as_uint32() const
Definition: ov-lazy-idx.cc:212
octave_value as_double() const
Definition: ov-lazy-idx.cc:164
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-lazy-idx.cc:107
bool save_binary(std::ostream &os, bool save_as_floats)
Definition: ov-lazy-idx.cc:246
octave_value squeeze() const
Definition: ov-lazy-idx.cc:100
NDArray array_value(bool flag=false) const
Definition: ov-lazy-idx.h:185
type_conv_info numeric_conversion_function() const
Definition: ov-lazy-idx.cc:47
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-lazy-idx.cc:89
int64NDArray int64_array_value() const
Definition: ov-lazy-idx.h:160
octave_value as_uint16() const
Definition: ov-lazy-idx.cc:206
octave_value as_single() const
Definition: ov-lazy-idx.cc:170
octave_value as_uint8() const
Definition: ov-lazy-idx.cc:200
octave_value as_int8() const
Definition: ov-lazy-idx.cc:176
octave_value full_value() const
Definition: ov-lazy-idx.h:68
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-lazy-idx.cc:158
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Definition: ov-lazy-idx.cc:152
octave::idx_vector index_vector(bool=false) const
Definition: ov-lazy-idx.h:70
static int static_type_id()
Definition: ov-re-mat.h:249
octave_base_value * clone() const
octave::idx_vector index_vector(bool require_integers=false) const
Definition: ov.h:534
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov.h:574
bool is_defined() const
Definition: ov.h:592
void() error(const char *fmt,...)
Definition: error.cc:988
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
std::string read_binary_data(std::istream &is, bool swap, octave::mach_info::float_format fmt, const std::string &filename, bool &global, octave_value &tc, std::string &doc)
bool save_binary_data(std::ostream &os, const octave_value &tc, const std::string &name, const std::string &doc, bool mark_global, bool save_as_floats)
std::string read_text_data(std::istream &is, const std::string &filename, bool &global, octave_value &tc, octave_idx_type count, const bool do_name_validation)
Definition: ls-oct-text.cc:286
bool save_text_data(std::ostream &os, const octave_value &val_arg, const std::string &name, bool mark_global, int precision)
Definition: ls-oct-text.cc:361
float_format
Definition: mach-info.h:38
octave_idx_type n
Definition: mx-inlines.cc:761
sortmode
Definition: oct-sort.h:97
@ UNSORTED
Definition: oct-sort.h:97
@ ASCENDING
Definition: oct-sort.h:97
@ DESCENDING
Definition: oct-sort.h:97
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:235