GNU Octave  4.0.0
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-2015 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 #ifdef 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-ascii.h"
31 #include "ls-oct-binary.h"
32 
34 
35 static octave_base_value *
37 {
39 
40  return v.full_value ().clone ();
41 }
42 
45 {
48 }
49 
52 {
53  octave_base_value *retval = 0;
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.length () == 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.length () == 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_ascii_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_ascii_data (is, std::string (), dummy, value, 0);
172 
173  if (nm != value_save_tag)
174  error ("lazy_index: corrupted data on load");
175  else
176  index = value.index_vector ();
177 
178  return ! error_state;
179 }
180 
181 
182 bool octave_lazy_index::save_binary (std::ostream& os, bool& save_as_floats)
183 {
185  std::string (), false, save_as_floats);
186 }
187 
188 bool octave_lazy_index::load_binary (std::istream& is, bool swap,
190 {
191  bool dummy;
192  std::string doc;
193 
194  std::string nm = read_binary_data (is, swap, fmt, std::string (),
195  dummy, value, doc);
196 
197  if (nm != value_save_tag)
198  error ("lazy_index: corrupted data on load");
199  else
200  index = value.index_vector ();
201 
202  return ! error_state;
203 }
octave_value value
Definition: ov-lazy-idx.h:243
octave_idx_type length(octave_idx_type n=0) const
Definition: idx-vector.h:551
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov.h:501
Array< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition: Array.cc:441
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Sort by rows returns only indices.
Definition: Array.cc:2086
sortmode
Definition: oct-sort.h:103
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-lazy-idx.cc:86
bool is_defined(void) const
Definition: ov.h:520
void error(const char *fmt,...)
Definition: error.cc:476
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:164
octave_idx_type checkelem(octave_idx_type n) const
Definition: idx-vector.h:560
type_conv_info numeric_conversion_function(void) const
Definition: ov-lazy-idx.cc:44
static const std::string value_save_tag("index_value")
idx_vector index_vector(bool require_integers=false) const
Definition: ov.h:463
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:593
#define CAST_CONV_ARG(t)
Definition: ops.h:83
Array< T > sort(int dim=0, sortmode mode=ASCENDING) const
Definition: Array.cc:1766
idx_vector index_vector(bool=false) const
Definition: ov-lazy-idx.h:64
static int static_type_id(void)
Definition: ov-re-mat.h:235
bool save_ascii_data(std::ostream &os, const octave_value &val_arg, const std::string &name, bool mark_as_global, int precision)
const octave_value & make_value(void) const
Definition: ov-lazy-idx.h:226
int error_state
Definition: error.cc:101
Array< T > squeeze(void) const
Chop off leading singleton dimensions.
Definition: Array.cc:114
idx_vector index
Definition: ov-lazy-idx.h:242
std::string read_binary_data(std::istream &is, bool swap, oct_mach_info::float_format fmt, const std::string &filename, bool &global, octave_value &tc, std::string &doc)
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-lazy-idx.cc:182
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:1277
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:2051
octave_idx_type extent(octave_idx_type n) const
Definition: idx-vector.h:554
bool is_range(void) const
Definition: idx-vector.h:581
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
Array< T > reshape(octave_idx_type nr, octave_idx_type nc) const
Definition: Array.h:460
Array< octave_idx_type > sort_rows_idx(sortmode mode=ASCENDING) const
Definition: ov-lazy-idx.cc:149
bool load_binary(std::istream &is, bool swap, oct_mach_info::float_format fmt)
Definition: ov-lazy-idx.cc:188
octave_value fast_elem_extract(octave_idx_type n) const
Definition: ov-lazy-idx.cc:73
int length(void) const
Definition: dim-vector.h:281
octave_idx_type increment(void) const
Definition: idx-vector.cc:1012
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:587
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
std::string read_ascii_data(std::istream &is, const std::string &filename, bool &global, octave_value &tc, octave_idx_type count)