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