GNU Octave  6.2.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-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 (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 *
40 {
41  const octave_lazy_index& v = dynamic_cast<const octave_lazy_index&> (a);
42 
43  return v.full_value ().clone ();
44 }
45 
48 {
51 }
52 
55 {
56  octave_base_value *retval = nullptr;
57 
58  switch (index.length (0))
59  {
60  case 1:
61  retval = new octave_scalar (static_cast<double> (index(0) + 1));
62  break;
63 
64  case 0:
66  break;
67 
68  default:
69  break;
70  }
71 
72  return retval;
73 }
74 
77 {
78  return double (index.checkelem (n) + 1);
79 }
80 
82 octave_lazy_index::reshape (const dim_vector& new_dims) const
83 {
84  return idx_vector (index.as_array ().reshape (new_dims),
85  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 (value.is_defined ())
93  return value.permute (vec, inv);
94  else
95  return idx_vector (index.as_array ().permute (vec, inv),
96  index.extent (0));
97 }
98 
101 {
102  return idx_vector (index.as_array ().squeeze (),
103  index.extent (0));
104 }
105 
108 {
109  const dim_vector odims = 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 idx_vector (index.as_array ().sort (dim, mode),
116  index.extent (0));
117 }
118 
121  sortmode mode) const
122 {
123  const dim_vector odims = 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 idx_vector (index.as_array ().sort (sidx, dim, mode),
130  index.extent (0));
131 }
132 
133 sortmode
135 {
136  if (index.is_range ())
137  {
138  // Avoid the array conversion.
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 index.as_array ().issorted (mode);
149 }
150 
153 {
154  return index.as_array ().sort_rows_idx (mode);
155 }
156 
157 sortmode
159 {
160  return 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 octave_lazy_index::save_ascii (std::ostream& os)
226 {
227  return save_text_data (os, make_value (), value_save_tag, false, 0);
228 }
229 
230 bool octave_lazy_index::load_ascii (std::istream& is)
231 {
232  bool dummy;
233 
234  std::string nm = read_text_data (is, "", dummy, value, 0);
235  if (nm != value_save_tag)
236  error ("lazy_index: corrupted data on load");
237 
238  index = value.index_vector ();
239 
240  return true;
241 }
242 
243 bool octave_lazy_index::save_binary (std::ostream& os, bool save_as_floats)
244 {
246  "", false, save_as_floats);
247 }
248 
249 bool octave_lazy_index::load_binary (std::istream& is, bool swap,
251 {
252  bool dummy;
253  std::string doc;
254 
255  std::string nm = read_binary_data (is, swap, fmt, "", dummy, value, doc);
256  if (nm != value_save_tag)
257  error ("lazy_index: corrupted data on load");
258 
259  index = value.index_vector ();
260 
261  return true;
262 }
263 
264 /*
265 %!shared x, y
266 %! x = find ([-1, 0, -2, 1, 3, -4] < 0);
267 %! y = [1, 3, 6];
268 %!assert (typeinfo (x), "lazy_index")
269 %!assert (double (x), y)
270 %!assert (single (x), single (y))
271 %!assert (int8 (x), int8 (y))
272 %!assert (int16 (x), int16 (y))
273 %!assert (int32 (x), int32 (y))
274 %!assert (int64 (x), int64 (y))
275 %!assert (uint8 (x), uint8 (y))
276 %!assert (uint16 (x), uint16 (y))
277 %!assert (uint32 (x), uint32 (y))
278 %!assert (uint64 (x), uint64 (y))
279 */
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
Definition: Array.cc:2066
Array< T > sort(int dim=0, sortmode mode=ASCENDING) const
Size of the specified dimension.
Definition: Array.cc:1757
sortmode issorted(sortmode mode=UNSORTED) const
Ordering is auto-detected or can be specified.
Definition: Array.cc:2034
Array< T > squeeze(void) const
Chop off leading singleton dimensions.
Definition: Array.cc:117
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Ordering is auto-detected or can be specified.
Definition: Array.cc:2084
Array< T > reshape(octave_idx_type nr, octave_idx_type nc) const
Size of the specified dimension.
Definition: Array.h:560
Array< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Size of the specified dimension.
Definition: Array.cc:431
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:334
idx_vector sorted(bool uniq=false) const
Definition: idx-vector.h:588
octave_idx_type length(octave_idx_type n=0) const
Definition: idx-vector.h:558
octave_idx_type checkelem(octave_idx_type n) const
Definition: idx-vector.h:567
dim_vector orig_dimensions(void) const
Definition: idx-vector.h:594
bool is_range(void) const
Definition: idx-vector.h:582
Array< octave_idx_type > as_array(void) const
Definition: idx-vector.cc:1273
octave_idx_type increment(void) const
Definition: idx-vector.cc:1009
octave_idx_type extent(octave_idx_type n) const
Definition: idx-vector.h:561
octave_value as_single(void) const
Definition: ov-lazy-idx.cc:170
octave_value full_value(void) const
Definition: ov-lazy-idx.h:67
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov-lazy-idx.cc:134
bool save_ascii(std::ostream &os)
Definition: ov-lazy-idx.cc:225
int64NDArray int64_array_value(void) const
Definition: ov-lazy-idx.h:158
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-lazy-idx.cc:82
octave_value squeeze(void) const
Definition: ov-lazy-idx.cc:100
octave_base_value * try_narrowing_conversion(void)
Definition: ov-lazy-idx.cc:54
const octave_value & make_value(void) const
Definition: ov-lazy-idx.h:256
octave_value as_int8(void) const
Definition: ov-lazy-idx.cc:176
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-lazy-idx.cc:249
octave_value as_int16(void) const
Definition: ov-lazy-idx.cc:182
octave_value as_int64(void) const
Definition: ov-lazy-idx.cc:194
uint64NDArray uint64_array_value(void) const
Definition: ov-lazy-idx.h:162
octave_value as_uint64(void) const
Definition: ov-lazy-idx.cc:218
octave_value as_double(void) const
Definition: ov-lazy-idx.cc:164
uint8NDArray uint8_array_value(void) 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:230
octave_value as_uint32(void) const
Definition: ov-lazy-idx.cc:212
FloatNDArray float_array_value(bool flag=false) const
Definition: ov-lazy-idx.h:184
octave_value as_int32(void) const
Definition: ov-lazy-idx.cc:188
uint32NDArray uint32_array_value(void) const
Definition: ov-lazy-idx.h:161
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:243
idx_vector index_vector(bool=false) const
Definition: ov-lazy-idx.h:69
NDArray array_value(bool flag=false) const
Definition: ov-lazy-idx.h:183
type_conv_info numeric_conversion_function(void) const
Definition: ov-lazy-idx.cc:47
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-lazy-idx.cc:89
uint16NDArray uint16_array_value(void) const
Definition: ov-lazy-idx.h:160
int8NDArray int8_array_value(void) const
Definition: ov-lazy-idx.h:155
octave_value value
Definition: ov-lazy-idx.h:273
idx_vector index
Definition: ov-lazy-idx.h:272
int32NDArray int32_array_value(void) const
Definition: ov-lazy-idx.h:157
octave_value as_uint16(void) const
Definition: ov-lazy-idx.cc:206
octave_value as_uint8(void) const
Definition: ov-lazy-idx.cc:200
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
int16NDArray int16_array_value(void) const
Definition: ov-lazy-idx.h:156
static int static_type_id(void)
Definition: ov-re-mat.h:250
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov.h:533
bool is_defined(void) const
Definition: ov.h:551
idx_vector index_vector(bool require_integers=false) const
Definition: ov.h:493
octave_base_value * clone(void) const
void error(const char *fmt,...)
Definition: error.cc:968
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:287
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:363
octave_idx_type n
Definition: mx-inlines.cc:753
sortmode
Definition: oct-sort.h:95
@ UNSORTED
Definition: oct-sort.h:95
@ ASCENDING
Definition: oct-sort.h:95
@ DESCENDING
Definition: oct-sort.h:95
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:180
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
Definition: ov-lazy-idx.cc:39
static const std::string value_save_tag("index_value")
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811