GNU Octave  4.4.1
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-2018 VZLU Prague
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "ov-lazy-idx.h"
28 #include "ops.h"
29 #include "ov-scalar.h"
30 #include "ls-oct-text.h"
31 #include "ls-oct-binary.h"
32 
34 
35 static octave_base_value *
37 {
38  const octave_lazy_index& v = dynamic_cast<const octave_lazy_index&> (a);
39 
40  return v.full_value ().clone ();
41 }
42 
45 {
48 }
49 
52 {
53  octave_base_value *retval = nullptr;
54 
55  switch (index.length (0))
56  {
57  case 1:
58  retval = new octave_scalar (static_cast<double> (index(0) + 1));
59  break;
60 
61  case 0:
63  break;
64 
65  default:
66  break;
67  }
68 
69  return retval;
70 }
71 
74 {
75  return double (index.checkelem (n) + 1);
76 }
77 
79 octave_lazy_index::reshape (const dim_vector& new_dims) const
80 {
81  return idx_vector (index.as_array ().reshape (new_dims),
82  index.extent (0));
83 }
84 
86 octave_lazy_index::permute (const Array<int>& vec, bool inv) const
87 {
88  // If the conversion has already been made, forward the operation.
89  if (value.is_defined ())
90  return value.permute (vec, inv);
91  else
92  return idx_vector (index.as_array ().permute (vec, inv),
93  index.extent (0));
94 }
95 
98 {
99  return idx_vector (index.as_array ().squeeze (),
100  index.extent (0));
101 }
102 
105 {
106  const dim_vector odims = index.orig_dimensions ();
107  // index_vector can employ a more efficient sorting algorithm.
108  if (mode == ASCENDING && odims.ndims () == 2
109  && (dim >= 0 && dim <= 1) && odims(1-dim) == 1)
110  return index_vector ().sorted ();
111  else
112  return idx_vector (index.as_array ().sort (dim, mode),
113  index.extent (0));
114 }
115 
118  sortmode mode) const
119 {
120  const dim_vector odims = index.orig_dimensions ();
121  // index_vector can employ a more efficient sorting algorithm.
122  if (mode == ASCENDING && odims.ndims () == 2
123  && (dim >= 0 && dim <= 1) && odims(1-dim) == 1)
124  return index_vector ().sorted (sidx);
125  else
126  return idx_vector (index.as_array ().sort (sidx, dim, mode),
127  index.extent (0));
128 }
129 
130 sortmode
132 {
133  if (index.is_range ())
134  {
135  // Avoid the array conversion.
137  if (inc == 0)
138  return (mode == UNSORTED ? ASCENDING : mode);
139  else if (inc > 0)
140  return (mode == DESCENDING ? UNSORTED : ASCENDING);
141  else
142  return (mode == ASCENDING ? UNSORTED : DESCENDING);
143  }
144  else
145  return index.as_array ().issorted (mode);
146 }
147 
150 {
151  return index.as_array ().sort_rows_idx (mode);
152 }
153 
154 sortmode
156 {
157  return index.as_array ().is_sorted_rows (mode);
158 }
159 
162 {
163  return array_value ();
164 }
165 
168 {
169  return float_array_value ();
170 }
171 
174 {
175  return int8_array_value ();
176 }
177 
180 {
181  return int16_array_value ();
182 }
183 
186 {
187  return int32_array_value ();
188 }
189 
192 {
193  return int64_array_value ();
194 }
195 
198 {
199  return uint8_array_value ();
200 }
201 
204 {
205  return uint16_array_value ();
206 }
207 
210 {
211  return uint32_array_value ();
212 }
213 
216 {
217  return uint64_array_value ();
218 }
219 
220 static const std::string value_save_tag ("index_value");
221 
222 bool octave_lazy_index::save_ascii (std::ostream& os)
223 {
224  return save_text_data (os, make_value (), value_save_tag, false, 0);
225 }
226 
227 bool octave_lazy_index::load_ascii (std::istream& is)
228 {
229  bool dummy;
230 
231  std::string nm = read_text_data (is, "", dummy, value, 0);
232  if (nm != value_save_tag)
233  error ("lazy_index: corrupted data on load");
234 
235  index = value.index_vector ();
236 
237  return true;
238 }
239 
241 {
243  "", false, save_as_floats);
244 }
245 
246 bool octave_lazy_index::load_binary (std::istream& is, bool swap,
248 {
249  bool dummy;
250  std::string doc;
251 
252  std::string nm = read_binary_data (is, swap, fmt, "", dummy, value, doc);
253  if (nm != value_save_tag)
254  error ("lazy_index: corrupted data on load");
255 
256  index = value.index_vector ();
257 
258  return true;
259 }
260 
261 /*
262 %!shared x, y
263 %! x = find ([-1, 0, -2, 1, 3, -4] < 0);
264 %! y = [1, 3, 6];
265 %!assert (typeinfo (x), "lazy_index")
266 %!assert (double (x), y)
267 %!assert (single (x), single (y))
268 %!assert (int8 (x), int8 (y))
269 %!assert (int16 (x), int16 (y))
270 %!assert (int32 (x), int32 (y))
271 %!assert (int64 (x), int64 (y))
272 %!assert (uint8 (x), uint8 (y))
273 %!assert (uint16 (x), uint16 (y))
274 %!assert (uint32 (x), uint32 (y))
275 %!assert (uint64 (x), uint64 (y))
276 */
octave_value as_int64(void) const
Definition: ov-lazy-idx.cc:191
octave_value value
Definition: ov-lazy-idx.h:260
type_conv_info numeric_conversion_function(void) const
Definition: ov-lazy-idx.cc:44
octave_idx_type checkelem(octave_idx_type n) const
Definition: idx-vector.h:566
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-lazy-idx.cc:155
octave_value squeeze(void) const
Definition: ov-lazy-idx.cc:97
dim_vector orig_dimensions(void) const
Definition: idx-vector.h:593
octave_base_value * clone(void) const
octave_value as_int8(void) const
Definition: ov-lazy-idx.cc:173
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-lazy-idx.cc:86
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov-lazy-idx.cc:131
NDArray array_value(bool flag=false) const
Definition: ov-lazy-idx.h:175
octave_idx_type extent(octave_idx_type n) const
Definition: idx-vector.h:560
sortmode
Definition: oct-sort.h:105
idx_vector index_vector(bool require_integers=false) const
Definition: ov.h:462
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Definition: ov-lazy-idx.cc:149
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6348
const octave_value & make_value(void) const
Definition: ov-lazy-idx.h:243
Array< T > squeeze(void) const
Chop off leading singleton dimensions.
Definition: Array.cc:116
octave_value as_double(void) const
Definition: ov-lazy-idx.cc:161
int64NDArray int64_array_value(void) const
Definition: ov-lazy-idx.h:150
Array< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition: Array.cc:430
octave_value full_value(void) const
Definition: ov-lazy-idx.h:64
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-lazy-idx.cc:104
void error(const char *fmt,...)
Definition: error.cc:578
octave_value as_uint32(void) const
Definition: ov-lazy-idx.cc:209
bool save_ascii(std::ostream &os)
Definition: ov-lazy-idx.cc:222
uint8NDArray uint8_array_value(void) const
Definition: ov-lazy-idx.h:151
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
Definition: Array.cc:2068
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:180
bool is_defined(void) const
Definition: ov.h:523
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Ordering is auto-detected or can be specified.
Definition: Array.cc:2086
octave_value fast_elem_extract(octave_idx_type n) const
Definition: ov-lazy-idx.cc:73
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-lazy-idx.cc:246
static const std::string value_save_tag("index_value")
Array< T > reshape(octave_idx_type nr, octave_idx_type nc) const
Definition: Array.h:549
octave_value as_int16(void) const
Definition: ov-lazy-idx.cc:179
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:400
bool swap
Definition: load-save.cc:738
Array< T > sort(int dim=0, sortmode mode=ASCENDING) const
Definition: Array.cc:1756
int32NDArray int32_array_value(void) const
Definition: ov-lazy-idx.h:149
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:975
sortmode issorted(sortmode mode=UNSORTED) const
Ordering is auto-detected or can be specified.
Definition: Array.cc:2033
bool is_range(void) const
Definition: idx-vector.h:581
uint64NDArray uint64_array_value(void) const
Definition: ov-lazy-idx.h:154
static int static_type_id(void)
Definition: ov-re-mat.h:248
bool save_as_floats
Definition: load-save.cc:1617
octave_value as_uint64(void) const
Definition: ov-lazy-idx.cc:215
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov.h:505
octave_value as_int32(void) const
Definition: ov-lazy-idx.cc:185
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)
octave_idx_type length(octave_idx_type n=0) const
Definition: idx-vector.h:557
octave_value retval
Definition: data.cc:6246
idx_vector index
Definition: ov-lazy-idx.h:259
octave_idx_type increment(void) const
Definition: idx-vector.cc:1007
idx_vector sorted(bool uniq=false) const
Definition: idx-vector.h:587
uint32NDArray uint32_array_value(void) const
Definition: ov-lazy-idx.h:153
int16NDArray int16_array_value(void) const
Definition: ov-lazy-idx.h:148
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-lazy-idx.cc:240
bool load_ascii(std::istream &is)
Definition: ov-lazy-idx.cc:227
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
Definition: ov-lazy-idx.cc:36
uint16NDArray uint16_array_value(void) const
Definition: ov-lazy-idx.h:152
idx_vector index_vector(bool=false) const
Definition: ov-lazy-idx.h:66
std::string read_text_data(std::istream &is, const std::string &filename, bool &global, octave_value &tc, octave_idx_type count)
Definition: ls-oct-text.cc:238
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:307
octave_value as_single(void) const
Definition: ov-lazy-idx.cc:167
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:295
FloatNDArray float_array_value(bool flag=false) const
Definition: ov-lazy-idx.h:176
Array< octave_idx_type > as_array(void) const
Definition: idx-vector.cc:1271
write the output to stdout if nargout is
Definition: load-save.cc:1612
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
octave_value as_uint16(void) const
Definition: ov-lazy-idx.cc:203
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
octave::stream os
Definition: file-io.cc:627
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)
int8NDArray int8_array_value(void) const
Definition: ov-lazy-idx.h:147
octave_value as_uint8(void) const
Definition: ov-lazy-idx.cc:197
octave_base_value * try_narrowing_conversion(void)
Definition: ov-lazy-idx.cc:51
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-lazy-idx.cc:79