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-range.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2017 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 "octave-config.h"
27 
28 #include <cstdlib>
29 
30 #include <iosfwd>
31 #include <string>
32 
33 #include "Range.h"
34 
35 #include "lo-mappers.h"
36 #include "lo-utils.h"
37 #include "mx-base.h"
38 #include "str-vec.h"
39 
40 #include "error.h"
41 #include "oct-stream.h"
42 #include "ov-base.h"
43 #include "ov-re-mat.h"
44 #include "ov-typeinfo.h"
45 
46 class octave_value_list;
47 
48 class tree_walker;
49 
50 // Range values.
51 
52 class
54 {
55 public:
56 
57  octave_range (void)
58  : octave_base_value (), range (), idx_cache () { }
59 
60  octave_range (double base, double limit, double inc)
61  : octave_base_value (), range (base, limit, inc), idx_cache ()
62  {
63  if (range.numel () < 0)
64  error ("invalid range");
65  }
66 
67  octave_range (const Range& r)
68  : octave_base_value (), range (r), idx_cache ()
69  {
70  if (range.numel () < 0 && range.numel () != -2)
71  error ("invalid range");
72  }
73 
75  : octave_base_value (), range (r.range),
76  idx_cache (r.idx_cache ? new idx_vector (*r.idx_cache) : 0)
77  { }
78 
79  octave_range (const Range& r, const idx_vector& cache)
80  : octave_base_value (), range (r), idx_cache ()
81  {
82  set_idx_cache (cache);
83  }
84 
85  ~octave_range (void) { clear_cached_info (); }
86 
87  octave_base_value *clone (void) const { return new octave_range (*this); }
88 
89  // A range is really just a special kind of real matrix object. In
90  // the places where we need to call empty_clone, it makes more sense
91  // to create an empty matrix (0x0) instead of an empty range (1x0).
92  octave_base_value *empty_clone (void) const { return new octave_matrix (); }
93 
94  type_conv_info numeric_conversion_function (void) const;
95 
96  octave_base_value *try_narrowing_conversion (void);
97 
99  const std::list<octave_value_list>& idx);
100 
102  const std::list<octave_value_list>& idx, int)
103  { return subsref (type, idx); }
104 
105  octave_value do_index_op (const octave_value_list& idx,
106  bool resize_ok = false);
107 
108  idx_vector index_vector (bool require_integers = false) const;
109 
110  dim_vector dims (void) const
111  {
112  octave_idx_type n = range.numel ();
113  return dim_vector (n > 0, n);
114  }
115 
116  octave_value resize (const dim_vector& dv, bool fill = false) const;
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 
143 
145  { return range.sort (dim, mode); }
146 
148  sortmode mode = ASCENDING) const
149  { return range.sort (sidx, dim, mode); }
150 
152  { return range.is_sorted (mode); }
153 
155  { return Array<octave_idx_type> (dim_vector (1, 0)); }
156 
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 
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 
240  { return FloatComplexMatrix (range.matrix_value ()); }
241 
242  ComplexNDArray complex_array_value (bool = false) const
243  { return ComplexMatrix (range.matrix_value ()); }
244 
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  octave_value as_double (void) const;
253  octave_value as_single (void) const;
254 
255  octave_value as_int8 (void) const;
256  octave_value as_int16 (void) const;
257  octave_value as_int32 (void) const;
258  octave_value as_int64 (void) const;
259 
260  octave_value as_uint8 (void) const;
261  octave_value as_uint16 (void) const;
262  octave_value as_uint32 (void) const;
263  octave_value as_uint64 (void) const;
264 
265  void print (std::ostream& os, bool pr_as_read_syntax = false);
266 
267  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
268 
269  bool print_name_tag (std::ostream& os, const std::string& name) const;
270 
271  void short_disp (std::ostream& os) const;
272 
273  bool save_ascii (std::ostream& os);
274 
275  bool load_ascii (std::istream& is);
276 
277  bool save_binary (std::ostream& os, bool& save_as_floats);
278 
279  bool load_binary (std::istream& is, bool swap,
281 
282  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
283 
284  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
285 
286  int write (octave_stream& os, int block_size,
287  oct_data_conv::data_type output_type, int skip,
289  {
290  // FIXME: could be more memory efficient by having a
291  // special case of the octave_stream::write method for ranges.
292 
293  return os.write (matrix_value (), block_size, output_type, skip, flt_fmt);
294  }
295 
296  mxArray *as_mxArray (void) const;
297 
299  {
300  octave_matrix m (matrix_value ());
301  return m.map (umap);
302  }
303 
304  octave_value fast_elem_extract (octave_idx_type n) const;
305 
306 private:
307 
309 
311  {
312  delete idx_cache;
313  idx_cache = idx ? new idx_vector (idx) : 0;
314  return idx;
315  }
316 
317  void clear_cached_info (void) const
318  {
319  delete idx_cache; idx_cache = 0;
320  }
321 
323 
324  // No assignment.
325 
327 
329 };
330 
331 #endif
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-range.h:185
bool is_true(const std::string &s)
Definition: mkoctfile.cc:398
octave_range(void)
Definition: ov-range.h:57
intNDArray< octave_int64 > int64NDArray
Definition: int64NDArray.h:33
float float_scalar_value(bool frc_str_conv=false) const
Definition: ov-range.h:179
dim_vector dims(void) const
Definition: ov-range.h:110
int8NDArray int8_array_value(void) const
Definition: ov-range.h:201
Range range_value(void) const
Definition: ov-range.h:248
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-range.h:123
uint8NDArray uint8_array_value(void) const
Definition: ov-range.h:213
octave_idx_type write(const octave_value &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, octave::mach_info::float_format flt_fmt)
Definition: oct-stream.cc:6647
sortmode
Definition: oct-sort.h:105
uint64NDArray uint64_array_value(void) const
Definition: ov-range.h:222
octave_value full_value(void) const
Definition: ov-range.h:128
NDArray array_value(bool=false) const
Definition: ov-range.h:188
intNDArray< octave_uint32 > uint32NDArray
Definition: uint32NDArray.h:33
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
idx subsref(val, idx) esult
Definition: ov.cc:3080
intNDArray< octave_uint8 > uint8NDArray
Definition: uint8NDArray.h:33
intNDArray< octave_uint16 > uint16NDArray
Definition: uint16NDArray.h:33
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-range.h:144
for large enough k
Definition: lu.cc:606
Range range
Definition: ov-range.h:308
Definition: Range.h:33
Array< octave_idx_type > sort_rows_idx(sortmode) const
Definition: ov-range.h:154
sortmode is_sorted(sortmode mode=UNSORTED) const
Definition: ov-range.h:151
void error(const char *fmt,...)
Definition: error.cc:570
int64NDArray int64_array_value(void) const
Definition: ov-range.h:210
FloatComplexMatrix float_complex_matrix_value(bool=false) const
Definition: ov-range.h:239
builtin_type_t builtin_type(void) const
Definition: ov-range.h:160
octave_base_value * clone(void) const
Definition: ov-range.h:87
octave::mach_info::float_format flt_fmt
Definition: load-save.cc:723
octave_range(const Range &r, const idx_vector &cache)
Definition: ov-range.h:79
bool is_float_type(void) const
Definition: ov-range.h:166
bool is_range(void) const
Definition: ov-range.h:134
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-range.h:224
uint32NDArray uint32_array_value(void) const
Definition: ov-range.h:219
bool is_numeric_type(void) const
Definition: ov-range.h:168
intNDArray< octave_int16 > int16NDArray
Definition: int16NDArray.h:33
octave_range(const Range &r)
Definition: ov-range.h:67
octave_value squeeze(void) const
Definition: ov-range.h:126
double scalar_value(bool frc_str_conv=false) const
Definition: ov-range.h:176
builtin_type_t
Definition: ov-base.h:61
uint16NDArray uint16_array_value(void) const
Definition: ov-range.h:216
void clear_cached_info(void) const
Definition: ov-range.h:317
bool is_constant(void) const
Definition: ov-range.h:132
idx_vector * idx_cache
Definition: ov-range.h:322
int32NDArray int32_array_value(void) const
Definition: ov-range.h:207
octave_range(const octave_range &r)
Definition: ov-range.h:74
bool swap
Definition: load-save.cc:725
octave_value map(unary_mapper_t umap) const
Definition: ov-re-mat.cc:899
bool is_defined(void) const
Definition: ov-range.h:130
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:148
OCTAVE_EXPORT octave_value_list any number nd example oindent prints the prompt xample Pick a any number!nd example oindent and waits for the user to enter a value The string entered by the user is evaluated as an so it may be a literal a variable name
Definition: input.cc:871
intNDArray< octave_int8 > int8NDArray
Definition: int8NDArray.h:33
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
FloatNDArray float_array_value(bool=false) const
Definition: ov-range.h:191
bool save_as_floats
Definition: load-save.cc:1581
Matrix matrix_value(bool=false) const
Definition: ov-range.h:182
int64_t octave_hdf5_id
size_t byte_size(void) const
Definition: ov-range.h:118
idx type
Definition: ov.cc:3129
Definition: dMatrix.h:37
bool is_real_type(void) const
Definition: ov-range.h:162
int16NDArray int16_array_value(void) const
Definition: ov-range.h:204
octave_range(double base, double limit, double inc)
Definition: ov-range.h:60
octave_base_value * empty_clone(void) const
Definition: ov-range.h:92
FloatComplexNDArray float_complex_array_value(bool=false) const
Definition: ov-range.h:245
intNDArray< octave_int32 > int32NDArray
Definition: int32NDArray.h:33
~octave_range(void)
Definition: ov-range.h:85
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-range.h:120
octave_value map(unary_mapper_t umap) const
Definition: ov-range.h:298
issues an error eealso double
Definition: ov-bool-mat.cc:594
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-range.h:157
ComplexNDArray complex_array_value(bool=false) const
Definition: ov-range.h:242
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
bool is_double_type(void) const
Definition: ov-range.h:164
std::complex< double > Complex
Definition: oct-cmplx.h:31
write the output to stdout if nargout is
Definition: load-save.cc:1576
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-range.h:227
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
Definition: ov-range.h:101
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
the second is matched to the second specifier and placed in the second column and so forth If there are more words than specifiers then the process is repeated until all words have been processed or the limit imposed by any(non-whitespace) text in the format that is not one of these specifiers is considered a literal.If there is a literal between two format specifiers then that same literal must appear in the input stream between the matching words.The following specifiers are valid
Definition: file-io.cc:1491
ComplexMatrix complex_matrix_value(bool=false) const
Definition: ov-range.h:236
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
int write(octave_stream &os, int block_size, oct_data_conv::data_type output_type, int skip, octave::mach_info::float_format flt_fmt) const
Definition: ov-range.h:286
dim_vector dv
Definition: sub2ind.cc:263
OCTAVE_EXPORT octave_value_list or cell arrays Arguments are concatenated vertically The returned values are padded with blanks as needed to make each row of the string array have the same length Empty input strings are significant and will concatenated in the output For numerical each element is converted to the corresponding ASCII character A range error results if an input is outside the ASCII range(0-255).For cell arrays
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:147
intNDArray< octave_uint64 > uint64NDArray
Definition: uint64NDArray.h:33
idx_vector set_idx_cache(const idx_vector &idx) const
Definition: ov-range.h:310