GNU Octave  3.8.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-2013 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 
73 octave_lazy_index::reshape (const dim_vector& new_dims) const
74 {
75  return idx_vector (index.as_array ().reshape (new_dims),
76  index.extent (0));
77 }
78 
80 octave_lazy_index::permute (const Array<int>& vec, bool inv) const
81 {
82  // If the conversion has already been made, forward the operation.
83  if (value.is_defined ())
84  return value.permute (vec, inv);
85  else
86  return idx_vector (index.as_array ().permute (vec, inv),
87  index.extent (0));
88 }
89 
92 {
93  return idx_vector (index.as_array ().squeeze (),
94  index.extent (0));
95 }
96 
99 {
100  const dim_vector odims = index.orig_dimensions ();
101  // index_vector can employ a more efficient sorting algorithm.
102  if (mode == ASCENDING && odims.length () == 2
103  && (dim >= 0 && dim <= 1) && odims (1-dim) == 1)
104  return index_vector ().sorted ();
105  else
106  return idx_vector (index.as_array ().sort (dim, mode),
107  index.extent (0));
108 }
109 
112  sortmode mode) const
113 {
114  const dim_vector odims = index.orig_dimensions ();
115  // index_vector can employ a more efficient sorting algorithm.
116  if (mode == ASCENDING && odims.length () == 2
117  && (dim >= 0 && dim <= 1) && odims (1-dim) == 1)
118  return index_vector ().sorted (sidx);
119  else
120  return idx_vector (index.as_array ().sort (sidx, dim, mode),
121  index.extent (0));
122 }
123 
124 sortmode
126 {
127  if (index.is_range ())
128  {
129  // Avoid the array conversion.
131  if (inc == 0)
132  return (mode == UNSORTED ? ASCENDING : mode);
133  else if (inc > 0)
134  return (mode == DESCENDING ? UNSORTED : ASCENDING);
135  else
136  return (mode == ASCENDING ? UNSORTED : DESCENDING);
137  }
138  else
139  return index.as_array ().is_sorted (mode);
140 }
141 
144 {
145  return index.as_array ().sort_rows_idx (mode);
146 }
147 
148 sortmode
150 {
151  return index.as_array ().is_sorted_rows (mode);
152 }
153 
154 static const std::string value_save_tag ("index_value");
155 
156 bool octave_lazy_index::save_ascii (std::ostream& os)
157 {
158  return save_ascii_data (os, make_value (), value_save_tag, false, 0);
159 }
160 
161 bool octave_lazy_index::load_ascii (std::istream& is)
162 {
163  bool dummy;
164 
165  std::string nm = read_ascii_data (is, std::string (), dummy, value, 0);
166 
167  if (nm != value_save_tag)
168  error ("lazy_index: corrupted data on load");
169  else
170  index = value.index_vector ();
171 
172  return ! error_state;
173 }
174 
175 
176 bool octave_lazy_index::save_binary (std::ostream& os, bool& save_as_floats)
177 {
179  std::string (), false, save_as_floats);
180 }
181 
182 bool octave_lazy_index::load_binary (std::istream& is, bool swap,
184 {
185  bool dummy;
186  std::string doc;
187 
188  std::string nm = read_binary_data (is, swap, fmt, std::string (),
189  dummy, value, doc);
190 
191  if (nm != value_save_tag)
192  error ("lazy_index: corrupted data on load");
193  else
194  index = value.index_vector ();
195 
196  return ! error_state;
197 }