comparison libinterp/octave-value/ov-range.h @ 15195:2fc554ffbc28

split libinterp from src * libinterp: New directory. Move all files from src directory here except Makefile.am, main.cc, main-cli.cc, mkoctfile.in.cc, mkoctfilr.in.sh, octave-config.in.cc, octave-config.in.sh. * libinterp/Makefile.am: New file, extracted from src/Makefile.am. * src/Makefile.am: Delete everything except targets and definitions needed to build and link main and utility programs. * Makefile.am (SUBDIRS): Include libinterp in the list. * autogen.sh: Run config-module.sh in libinterp/dldfcn directory, not src/dldfcn directory. * configure.ac (AC_CONFIG_SRCDIR): Use libinterp/octave.cc, not src/octave.cc. (DL_LDFLAGS, LIBOCTINTERP): Use libinterp, not src. (AC_CONFIG_FILES): Include libinterp/Makefile in the list. * find-docstring-files.sh: Look in libinterp, not src. * gui/src/Makefile.am (liboctgui_la_CPPFLAGS): Find header files in libinterp, not src.
author John W. Eaton <jwe@octave.org>
date Sat, 18 Aug 2012 16:23:39 -0400
parents src/octave-value/ov-range.h@46b19589b593
children 0f143f68078d
comparison
equal deleted inserted replaced
15194:0f0b795044c3 15195:2fc554ffbc28
1 /*
2
3 Copyright (C) 1996-2012 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_range_h)
24 #define octave_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
52 octave_range : public octave_base_value
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
73 octave_range (const octave_range& r)
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
147 octave_value sort (Array<octave_idx_type>& sidx, octave_idx_type dim = 0,
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
200 int8NDArray
201 int8_array_value (void) const { return int8NDArray (array_value ()); }
202
203 int16NDArray
204 int16_array_value (void) const { return int16NDArray (array_value ()); }
205
206 int32NDArray
207 int32_array_value (void) const { return int32NDArray (array_value ()); }
208
209 int64NDArray
210 int64_array_value (void) const { return int64NDArray (array_value ()); }
211
212 uint8NDArray
213 uint8_array_value (void) const { return uint8NDArray (array_value ()); }
214
215 uint16NDArray
216 uint16_array_value (void) const { return uint16NDArray (array_value ()); }
217
218 uint32NDArray
219 uint32_array_value (void) const { return uint32NDArray (array_value ()); }
220
221 uint64NDArray
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 bool save_ascii (std::ostream& os);
259
260 bool load_ascii (std::istream& is);
261
262 bool save_binary (std::ostream& os, bool& save_as_floats);
263
264 bool load_binary (std::istream& is, bool swap,
265 oct_mach_info::float_format fmt);
266
267 #if defined (HAVE_HDF5)
268 bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
269
270 bool load_hdf5 (hid_t loc_id, const char *name);
271 #endif
272
273 int write (octave_stream& os, int block_size,
274 oct_data_conv::data_type output_type, int skip,
275 oct_mach_info::float_format flt_fmt) const
276 {
277 // FIXME -- could be more memory efficient by having a
278 // special case of the octave_stream::write method for ranges.
279
280 return os.write (matrix_value (), block_size, output_type, skip,
281 flt_fmt);
282 }
283
284 mxArray *as_mxArray (void) const;
285
286 octave_value map (unary_mapper_t umap) const
287 {
288 octave_matrix m (matrix_value ());
289 return m.map (umap);
290 }
291
292 private:
293
294 Range range;
295
296 idx_vector set_idx_cache (const idx_vector& idx) const
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
308 mutable idx_vector *idx_cache;
309
310 // No assignment.
311
312 octave_range& operator = (const octave_range&);
313
314 DECLARE_OCTAVE_ALLOCATOR
315
316 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
317 };
318
319 // If TRUE, allow ranges with non-integer elements as array indices.
320 extern bool Vallow_noninteger_range_as_index;
321
322 #endif