GNU Octave 7.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-2022 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
38static 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 (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
83{
84 return octave::idx_vector (m_index.as_array ().reshape (new_dims),
85 m_index.extent (0));
86}
87
89octave_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{
103}
104
107{
108 const dim_vector odims = m_index.orig_dimensions ();
109 // index_vector can employ a more efficient sorting algorithm.
110 if (mode == ASCENDING && odims.ndims () == 2
111 && (dim >= 0 && dim <= 1) && odims(1-dim) == 1)
112 return index_vector ().sorted ();
113 else
114 return octave::idx_vector (m_index.as_array ().sort (dim, mode),
115 m_index.extent (0));
116}
117
120 sortmode mode) const
121{
122 const dim_vector odims = m_index.orig_dimensions ();
123 // index_vector can employ a more efficient sorting algorithm.
124 if (mode == ASCENDING && odims.ndims () == 2
125 && (dim >= 0 && dim <= 1) && odims(1-dim) == 1)
126 return index_vector ().sorted (sidx);
127 else
128 return octave::idx_vector (m_index.as_array ().sort (sidx, dim, mode),
129 m_index.extent (0));
130}
131
134{
135 if (m_index.is_range ())
136 {
137 // Avoid the array conversion.
139 if (inc == 0)
140 return (mode == UNSORTED ? ASCENDING : mode);
141 else if (inc > 0)
142 return (mode == DESCENDING ? UNSORTED : ASCENDING);
143 else
144 return (mode == ASCENDING ? UNSORTED : DESCENDING);
145 }
146 else
147 return m_index.as_array ().issorted (mode);
148}
149
152{
153 return m_index.as_array ().sort_rows_idx (mode);
154}
155
158{
159 return m_index.as_array ().is_sorted_rows (mode);
160}
161
164{
165 return array_value ();
166}
167
170{
171 return float_array_value ();
172}
173
176{
177 return int8_array_value ();
178}
179
182{
183 return int16_array_value ();
184}
185
188{
189 return int32_array_value ();
190}
191
194{
195 return int64_array_value ();
196}
197
200{
201 return uint8_array_value ();
202}
203
206{
207 return uint16_array_value ();
208}
209
212{
213 return uint32_array_value ();
214}
215
218{
219 return uint64_array_value ();
220}
221
222static const std::string value_save_tag ("index_value");
223
224bool octave_lazy_index::save_ascii (std::ostream& os)
225{
226 return save_text_data (os, make_value (), value_save_tag, false, 0);
227}
228
229bool octave_lazy_index::load_ascii (std::istream& is)
230{
231 bool dummy;
232
233 std::string nm = read_text_data (is, "", dummy, m_value, 0);
234 if (nm != value_save_tag)
235 error ("lazy_index: corrupted data on load");
236
238
239 return true;
240}
241
242bool octave_lazy_index::save_binary (std::ostream& os, bool save_as_floats)
243{
245 "", false, save_as_floats);
246}
247
248bool octave_lazy_index::load_binary (std::istream& is, bool swap,
250{
251 bool dummy;
252 std::string doc;
253
254 std::string nm = read_binary_data (is, swap, fmt, "", dummy, m_value, doc);
255 if (nm != value_save_tag)
256 error ("lazy_index: corrupted data on load");
257
259
260 return true;
261}
262
263/*
264%!shared x, y
265%! x = find ([-1, 0, -2, 1, 3, -4] < 0);
266%! y = [1, 3, 6];
267%!assert (typeinfo (x), "lazy_index")
268%!assert (double (x), y)
269%!assert (single (x), single (y))
270%!assert (int8 (x), int8 (y))
271%!assert (int16 (x), int16 (y))
272%!assert (int32 (x), int32 (y))
273%!assert (int64 (x), int64 (y))
274%!assert (uint8 (x), uint8 (y))
275%!assert (uint16 (x), uint16 (y))
276%!assert (uint32 (x), uint32 (y))
277%!assert (uint64 (x), uint64 (y))
278*/
OCTARRAY_API Array< T, Alloc > squeeze(void) const
Chop off leading singleton dimensions.
Definition: Array.cc:117
OCTARRAY_API Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
Definition: Array.cc:2059
OCTARRAY_API Array< T, Alloc > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Size of the specified dimension.
Definition: Array.cc:431
OCTARRAY_API Array< T, Alloc > sort(int dim=0, sortmode mode=ASCENDING) const
Size of the specified dimension.
Definition: Array.cc:1761
Array< T, Alloc > reshape(octave_idx_type nr, octave_idx_type nc) const
Size of the specified dimension.
Definition: Array.h:595
OCTARRAY_API sortmode issorted(sortmode mode=UNSORTED) const
Ordering is auto-detected or can be specified.
Definition: Array.cc:2027
OCTARRAY_API sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Ordering is auto-detected or can be specified.
Definition: Array.cc:2077
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:257
OCTAVE_API Array< octave_idx_type > as_array(void) const
Definition: idx-vector.cc:1225
idx_vector sorted(bool uniq=false) const
Definition: idx-vector.h:568
octave_idx_type extent(octave_idx_type n) const
Definition: idx-vector.h:540
octave_idx_type checkelem(octave_idx_type n) const
Definition: idx-vector.h:546
dim_vector orig_dimensions(void) const
Definition: idx-vector.h:574
OCTAVE_API octave_idx_type increment(void) const
Definition: idx-vector.cc:968
bool is_range(void) const
Definition: idx-vector.h:562
octave_idx_type length(octave_idx_type n=0) const
Definition: idx-vector.h:537
octave_value as_single(void) const
Definition: ov-lazy-idx.cc:169
octave_value full_value(void) const
Definition: ov-lazy-idx.h:67
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov-lazy-idx.cc:133
bool save_ascii(std::ostream &os)
Definition: ov-lazy-idx.cc:224
int64NDArray int64_array_value(void) const
Definition: ov-lazy-idx.h:159
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
octave_value as_int8(void) const
Definition: ov-lazy-idx.cc:175
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-lazy-idx.cc:248
octave_value as_int16(void) const
Definition: ov-lazy-idx.cc:181
octave_value as_int64(void) const
Definition: ov-lazy-idx.cc:193
uint64NDArray uint64_array_value(void) const
Definition: ov-lazy-idx.h:163
octave_value as_uint64(void) const
Definition: ov-lazy-idx.cc:217
octave_value as_double(void) const
Definition: ov-lazy-idx.cc:163
uint8NDArray uint8_array_value(void) const
Definition: ov-lazy-idx.h:160
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:229
octave_value as_uint32(void) const
Definition: ov-lazy-idx.cc:211
FloatNDArray float_array_value(bool flag=false) const
Definition: ov-lazy-idx.h:185
octave_value as_int32(void) const
Definition: ov-lazy-idx.cc:187
uint32NDArray uint32_array_value(void) const
Definition: ov-lazy-idx.h:162
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-lazy-idx.cc:106
octave_value m_value
Definition: ov-lazy-idx.h:274
bool save_binary(std::ostream &os, bool save_as_floats)
Definition: ov-lazy-idx.cc:242
NDArray array_value(bool flag=false) const
Definition: ov-lazy-idx.h:184
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:161
octave::idx_vector m_index
Definition: ov-lazy-idx.h:273
int8NDArray int8_array_value(void) const
Definition: ov-lazy-idx.h:156
int32NDArray int32_array_value(void) const
Definition: ov-lazy-idx.h:158
octave_value as_uint16(void) const
Definition: ov-lazy-idx.cc:205
octave_value as_uint8(void) const
Definition: ov-lazy-idx.cc:199
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-lazy-idx.cc:157
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Definition: ov-lazy-idx.cc:151
octave::idx_vector index_vector(bool=false) const
Definition: ov-lazy-idx.h:69
int16NDArray int16_array_value(void) const
Definition: ov-lazy-idx.h:157
const octave_value & make_value(void) const
Definition: ov-lazy-idx.h:257
static int static_type_id(void)
Definition: ov-re-mat.h:246
octave::idx_vector index_vector(bool require_integers=false) const
Definition: ov.h:579
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov.h:619
bool is_defined(void) const
Definition: ov.h:637
OCTINTERP_API octave_base_value * clone(void) const
void error(const char *fmt,...)
Definition: error.cc:980
octave::idx_vector idx_vector
Definition: idx-vector.h:1037
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
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:222
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")