GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-lazy-idx.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2010-2017 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://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 {
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:
62  retval = new octave_matrix (NDArray (index.orig_dimensions ()));
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 ().is_sorted (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 
160 static const std::string value_save_tag ("index_value");
161 
162 bool octave_lazy_index::save_ascii (std::ostream& os)
163 {
164  return save_text_data (os, make_value (), value_save_tag, false, 0);
165 }
166 
167 bool octave_lazy_index::load_ascii (std::istream& is)
168 {
169  bool dummy;
170 
171  std::string nm = read_text_data (is, "", dummy, value, 0);
172  if (nm != value_save_tag)
173  error ("lazy_index: corrupted data on load");
174 
175  index = value.index_vector ();
176 
177  return true;
178 }
179 
180 bool octave_lazy_index::save_binary (std::ostream& os, bool& save_as_floats)
181 {
183  "", false, save_as_floats);
184 }
185 
186 bool octave_lazy_index::load_binary (std::istream& is, bool swap,
188 {
189  bool dummy;
190  std::string doc;
191 
192  std::string nm = read_binary_data (is, swap, fmt, "", dummy, value, doc);
193  if (nm != value_save_tag)
194  error ("lazy_index: corrupted data on load");
195 
196  index = value.index_vector ();
197 
198  return true;
199 }
octave_value value
Definition: ov-lazy-idx.h:247
octave_idx_type length(octave_idx_type n=0) const
Definition: idx-vector.h:541
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov.h:518
Array< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition: Array.cc:451
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
Definition: Array.cc:2087
sortmode
Definition: oct-sort.h:105
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-lazy-idx.cc:86
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:6386
bool is_defined(void) const
Definition: ov.h:536
void error(const char *fmt,...)
Definition: error.cc:570
bool save_ascii(std::ostream &os)
Definition: ov-lazy-idx.cc:162
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:169
bool load_binary(std::istream &is, bool swap, octave::mach_info::float_format fmt)
Definition: ov-lazy-idx.cc:186
octave_idx_type checkelem(octave_idx_type n) const
Definition: idx-vector.h:550
type_conv_info numeric_conversion_function(void) const
Definition: ov-lazy-idx.cc:44
static const std::string value_save_tag("index_value")
octave_base_value * clone(void) const
Definition: ov.cc:1149
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:398
bool swap
Definition: load-save.cc:725
idx_vector index_vector(bool require_integers=false) const
Definition: ov.h:479
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-lazy-idx.cc:104
dim_vector orig_dimensions(void) const
Definition: idx-vector.h:583
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:941
bool save_text_data(std::ostream &os, const octave_value &val_arg, const std::string &name, bool mark_as_global, int precision)
Definition: ls-oct-text.cc:300
Array< T > sort(int dim=0, sortmode mode=ASCENDING) const
Definition: Array.cc:1775
idx_vector index_vector(bool=false) const
Definition: ov-lazy-idx.h:66
static int static_type_id(void)
Definition: ov-re-mat.h:250
const octave_value & make_value(void) const
Definition: ov-lazy-idx.h:230
bool save_as_floats
Definition: load-save.cc:1581
Array< T > squeeze(void) const
Chop off leading singleton dimensions.
Definition: Array.cc:125
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_value retval
Definition: data.cc:6294
idx_vector index
Definition: ov-lazy-idx.h:246
octave_value full_value(void) const
Definition: ov-lazy-idx.h:64
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-lazy-idx.cc:180
bool load_ascii(std::istream &is)
Definition: ov-lazy-idx.cc:167
octave_value squeeze(void) const
Definition: ov-lazy-idx.cc:97
Array< octave_idx_type > as_array(void) const
Definition: idx-vector.cc:1267
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
Definition: ov-lazy-idx.cc:36
sortmode is_sorted(sortmode mode=UNSORTED) const
Ordering is auto-detected or can be specified.
Definition: Array.cc:2052
octave_idx_type extent(octave_idx_type n) const
Definition: idx-vector.h:544
bool is_range(void) const
Definition: idx-vector.h:571
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-lazy-idx.cc:155
bool save_binary_data(std::ostream &os, const octave_value &tc, const std::string &name, const std::string &doc, bool mark_as_global, bool save_as_floats)
sortmode is_sorted(sortmode mode=UNSORTED) const
Definition: ov-lazy-idx.cc:131
issues an error eealso double
Definition: ov-bool-mat.cc:594
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:236
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:301
Array< T > reshape(octave_idx_type nr, octave_idx_type nc) const
Definition: Array.h:563
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Definition: ov-lazy-idx.cc:149
write the output to stdout if nargout is
Definition: load-save.cc:1576
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
octave_value fast_elem_extract(octave_idx_type n) const
Definition: ov-lazy-idx.cc:73
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:854
octave_idx_type increment(void) const
Definition: idx-vector.cc:1002
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Ordering is auto-detected or can be specified.
Definition: Array.cc:2105
idx_vector sorted(bool uniq=false) const
Definition: idx-vector.h:577
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-lazy-idx.cc:79
octave_base_value * try_narrowing_conversion(void)
Definition: ov-lazy-idx.cc:51