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-range.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 John W. Eaton
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 (octave_ov_range_h)
24 #define octave_ov_range_h 1
25 
26 #include <cstdlib>
27 
28 #include <iosfwd>
29 #include <string>
30 
31 #include "Range.h"
32 
33 #include "lo-mappers.h"
34 #include "lo-utils.h"
35 #include "mx-base.h"
36 #include "oct-alloc.h"
37 #include "str-vec.h"
38 
39 #include "error.h"
40 #include "oct-stream.h"
41 #include "ov-base.h"
42 #include "ov-re-mat.h"
43 #include "ov-typeinfo.h"
44 
45 class octave_value_list;
46 
47 class tree_walker;
48 
49 // Range values.
50 
51 class
53 {
54 public:
55 
56  octave_range (void)
57  : octave_base_value (), range (), idx_cache () { }
58 
59  octave_range (double base, double limit, double inc)
60  : octave_base_value (), range (base, limit, inc), idx_cache ()
61  {
62  if (range.nelem () < 0)
63  ::error ("invalid range");
64  }
65 
66  octave_range (const Range& r)
67  : octave_base_value (), range (r), idx_cache ()
68  {
69  if (range.nelem () < 0 && range.nelem () != -2)
70  ::error ("invalid range");
71  }
72 
74  : octave_base_value (), range (r.range),
75  idx_cache (r.idx_cache ? new idx_vector (*r.idx_cache) : 0)
76  { }
77 
78  octave_range (const Range& r, const idx_vector& cache)
79  : octave_base_value (), range (r), idx_cache ()
80  {
81  set_idx_cache (cache);
82  }
83 
84  ~octave_range (void) { clear_cached_info (); }
85 
86  octave_base_value *clone (void) const { return new octave_range (*this); }
87 
88  // A range is really just a special kind of real matrix object. In
89  // the places where we need to call empty_clone, it makes more sense
90  // to create an empty matrix (0x0) instead of an empty range (1x0).
91  octave_base_value *empty_clone (void) const { return new octave_matrix (); }
92 
93  type_conv_info numeric_conversion_function (void) const;
94 
95  octave_base_value *try_narrowing_conversion (void);
96 
97  octave_value subsref (const std::string& type,
98  const std::list<octave_value_list>& idx);
99 
100  octave_value_list subsref (const std::string& type,
101  const std::list<octave_value_list>& idx, int)
102  { return subsref (type, idx); }
103 
104  octave_value do_index_op (const octave_value_list& idx,
105  bool resize_ok = false);
106 
107  idx_vector index_vector (void) const;
108 
109  dim_vector dims (void) const
110  {
111  octave_idx_type n = range.nelem ();
112  return dim_vector (n > 0, n);
113  }
114 
115  octave_value resize (const dim_vector& dv, bool fill = false) const;
116 
117 
118  size_t byte_size (void) const { return 3 * sizeof (double); }
119 
120  octave_value reshape (const dim_vector& new_dims) const
121  { return NDArray (array_value ().reshape (new_dims)); }
122 
123  octave_value permute (const Array<int>& vec, bool inv = false) const
124  { return NDArray (array_value ().permute (vec, inv)); }
125 
126  octave_value squeeze (void) const { return range; }
127 
128  octave_value full_value (void) const { return range.matrix_value (); }
129 
130  bool is_defined (void) const { return true; }
131 
132  bool is_constant (void) const { return true; }
133 
134  bool is_range (void) const { return true; }
135 
136  octave_value all (int dim = 0) const;
137 
138  octave_value any (int dim = 0) const;
139 
140  octave_value diag (octave_idx_type k = 0) const;
141 
142  octave_value diag (octave_idx_type m, octave_idx_type n) const;
143 
144  octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const
145  { return range.sort (dim, mode); }
146 
148  sortmode mode = ASCENDING) const
149  { return range.sort (sidx, dim, mode); }
150 
151  sortmode is_sorted (sortmode mode = UNSORTED) const
152  { return range.is_sorted (mode); }
153 
154  Array<octave_idx_type> sort_rows_idx (sortmode) const
155  { return Array<octave_idx_type> (dim_vector (1, 0)); }
156 
157  sortmode is_sorted_rows (sortmode mode = UNSORTED) const
158  { return mode ? mode : ASCENDING; }
159 
160  builtin_type_t builtin_type (void) const { return btyp_double; }
161 
162  bool is_real_type (void) const { return true; }
163 
164  bool is_double_type (void) const { return true; }
165 
166  bool is_float_type (void) const { return true; }
167 
168  bool is_numeric_type (void) const { return true; }
169 
170  bool is_true (void) const;
171 
172  double double_value (bool = false) const;
173 
174  float float_value (bool = false) const;
175 
176  double scalar_value (bool frc_str_conv = false) const
177  { return double_value (frc_str_conv); }
178 
179  float float_scalar_value (bool frc_str_conv = false) const
180  { return float_value (frc_str_conv); }
181 
182  Matrix matrix_value (bool = false) const
183  { return range.matrix_value (); }
184 
185  FloatMatrix float_matrix_value (bool = false) const
186  { return range.matrix_value (); }
187 
188  NDArray array_value (bool = false) const
189  { return range.matrix_value (); }
190 
191  FloatNDArray float_array_value (bool = false) const
192  { return FloatMatrix (range.matrix_value ()); }
193 
194  charNDArray char_array_value (bool = false) const;
195 
196  // FIXME: it would be better to have Range::intXNDArray_value
197  // functions to avoid the intermediate conversion to a matrix
198  // object.
199 
201  int8_array_value (void) const { return int8NDArray (array_value ()); }
202 
204  int16_array_value (void) const { return int16NDArray (array_value ()); }
205 
207  int32_array_value (void) const { return int32NDArray (array_value ()); }
208 
210  int64_array_value (void) const { return int64NDArray (array_value ()); }
211 
213  uint8_array_value (void) const { return uint8NDArray (array_value ()); }
214 
216  uint16_array_value (void) const { return uint16NDArray (array_value ()); }
217 
219  uint32_array_value (void) const { return uint32NDArray (array_value ()); }
220 
222  uint64_array_value (void) const { return uint64NDArray (array_value ()); }
223 
224  SparseMatrix sparse_matrix_value (bool = false) const
225  { return SparseMatrix (range.matrix_value ()); }
226 
227  SparseComplexMatrix sparse_complex_matrix_value (bool = false) const
228  { return SparseComplexMatrix (sparse_matrix_value ()); }
229 
230  Complex complex_value (bool = false) const;
231 
232  FloatComplex float_complex_value (bool = false) const;
233 
234  boolNDArray bool_array_value (bool warn = false) const;
235 
236  ComplexMatrix complex_matrix_value (bool = false) const
237  { return ComplexMatrix (range.matrix_value ()); }
238 
239  FloatComplexMatrix float_complex_matrix_value (bool = false) const
240  { return FloatComplexMatrix (range.matrix_value ()); }
241 
242  ComplexNDArray complex_array_value (bool = false) const
243  { return ComplexMatrix (range.matrix_value ()); }
244 
245  FloatComplexNDArray float_complex_array_value (bool = false) const
246  { return FloatComplexMatrix (range.matrix_value ()); }
247 
248  Range range_value (void) const { return range; }
249 
250  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
251 
252  void print (std::ostream& os, bool pr_as_read_syntax = false) const;
253 
254  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
255 
256  bool print_name_tag (std::ostream& os, const std::string& name) const;
257 
258  void short_disp (std::ostream& os) const;
259 
260  bool save_ascii (std::ostream& os);
261 
262  bool load_ascii (std::istream& is);
263 
264  bool save_binary (std::ostream& os, bool& save_as_floats);
265 
266  bool load_binary (std::istream& is, bool swap,
268 
269 #if defined (HAVE_HDF5)
270  bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
271 
272  bool load_hdf5 (hid_t loc_id, const char *name);
273 #endif
274 
275  int write (octave_stream& os, int block_size,
276  oct_data_conv::data_type output_type, int skip,
277  oct_mach_info::float_format flt_fmt) const
278  {
279  // FIXME: could be more memory efficient by having a
280  // special case of the octave_stream::write method for ranges.
281 
282  return os.write (matrix_value (), block_size, output_type, skip, flt_fmt);
283  }
284 
285  mxArray *as_mxArray (void) const;
286 
287  octave_value map (unary_mapper_t umap) const
288  {
289  octave_matrix m (matrix_value ());
290  return m.map (umap);
291  }
292 
293 private:
294 
296 
297  idx_vector set_idx_cache (const idx_vector& idx) const
298  {
299  delete idx_cache;
300  idx_cache = idx ? new idx_vector (idx) : 0;
301  return idx;
302  }
303 
304  void clear_cached_info (void) const
305  {
306  delete idx_cache; idx_cache = 0;
307  }
308 
310 
311  // No assignment.
312 
314 
316 
318 };
319 
320 // If TRUE, allow ranges with non-integer elements as array indices.
322 
323 #endif