2376
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006, |
|
4 2007 John W. Eaton |
2376
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2376
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
2376
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_range_h) |
|
25 #define octave_range_h 1 |
|
26 |
|
27 #include <cstdlib> |
|
28 |
3503
|
29 #include <iostream> |
2376
|
30 #include <string> |
|
31 |
|
32 #include "Range.h" |
|
33 |
2889
|
34 #include "lo-mappers.h" |
2376
|
35 #include "lo-utils.h" |
|
36 #include "mx-base.h" |
2477
|
37 #include "oct-alloc.h" |
2376
|
38 #include "str-vec.h" |
|
39 |
|
40 #include "error.h" |
5051
|
41 #include "oct-stream.h" |
2376
|
42 #include "ov-base.h" |
6127
|
43 #include "ov-re-mat.h" |
2376
|
44 #include "ov-typeinfo.h" |
|
45 |
|
46 class Octave_map; |
|
47 class octave_value_list; |
|
48 |
|
49 class tree_walker; |
|
50 |
|
51 // Range values. |
|
52 |
|
53 class |
|
54 octave_range : public octave_base_value |
|
55 { |
|
56 public: |
|
57 |
|
58 octave_range (void) |
|
59 : octave_base_value () { } |
|
60 |
|
61 octave_range (double base, double limit, double inc) |
|
62 : octave_base_value (), range (base, limit, inc) |
|
63 { |
|
64 if (range.nelem () < 0) |
|
65 ::error ("invalid range"); |
|
66 } |
|
67 |
|
68 octave_range (const Range& r) |
|
69 : octave_base_value (), range (r) |
|
70 { |
|
71 if (range.nelem () < 0) |
|
72 ::error ("invalid range"); |
|
73 } |
|
74 |
|
75 octave_range (const octave_range& r) |
|
76 : octave_base_value (), range (r.range) { } |
|
77 |
|
78 ~octave_range (void) { } |
|
79 |
5759
|
80 octave_base_value *clone (void) const { return new octave_range (*this); } |
6127
|
81 |
|
82 // A range is really just a special kind of real matrix object. In |
|
83 // the places where we need to call empty_clone, it makes more sense |
|
84 // to create an empty matrix (0x0) instead of an empty range (1x0). |
|
85 octave_base_value *empty_clone (void) const { return new octave_matrix (); } |
2376
|
86 |
2410
|
87 type_conv_fcn numeric_conversion_function (void) const; |
|
88 |
5759
|
89 octave_base_value *try_narrowing_conversion (void); |
2376
|
90 |
4247
|
91 octave_value subsref (const std::string& type, |
4219
|
92 const std::list<octave_value_list>& idx); |
3933
|
93 |
4661
|
94 octave_value_list subsref (const std::string&, |
|
95 const std::list<octave_value_list>&, int) |
4271
|
96 { |
|
97 panic_impossible (); |
|
98 return octave_value_list (); |
|
99 } |
|
100 |
5885
|
101 octave_value do_index_op (const octave_value_list& idx, |
|
102 bool resize_ok = false); |
2436
|
103 |
2376
|
104 idx_vector index_vector (void) const { return idx_vector (range); } |
|
105 |
4563
|
106 dim_vector dims (void) const |
5275
|
107 { |
|
108 octave_idx_type n = range.nelem (); |
4563
|
109 return dim_vector (n > 0, n); |
|
110 } |
3195
|
111 |
5731
|
112 octave_value resize (const dim_vector& dv, bool fill = false) const; |
|
113 |
4915
|
114 |
4792
|
115 size_t byte_size (void) const { return 3 * sizeof (double); } |
4791
|
116 |
4627
|
117 octave_value reshape (const dim_vector& new_dims) const |
4901
|
118 { return NDArray (array_value().reshape (new_dims)); } |
4627
|
119 |
5434
|
120 octave_value permute (const Array<int>& vec, bool inv = false) const |
|
121 { return NDArray (array_value().permute (vec, inv)); } |
|
122 |
6999
|
123 octave_value squeeze (void) const { return range; } |
|
124 |
2376
|
125 bool is_defined (void) const { return true; } |
|
126 |
2974
|
127 bool is_constant (void) const { return true; } |
|
128 |
2376
|
129 bool is_range (void) const { return true; } |
|
130 |
4017
|
131 octave_value all (int dim = 0) const; |
|
132 |
|
133 octave_value any (int dim = 0) const; |
2376
|
134 |
|
135 bool is_real_type (void) const { return true; } |
|
136 |
5895
|
137 bool is_double_type (void) const { return true; } |
|
138 |
2376
|
139 bool valid_as_scalar_index (void) const |
|
140 { |
5042
|
141 double b = range.base (); |
2376
|
142 return (range.nelem () == 1 |
5275
|
143 && ! xisnan (b) && D_NINT (b) == b && NINTbig (b) == 1); |
2376
|
144 } |
|
145 |
|
146 bool valid_as_zero_index (void) const |
|
147 { |
5042
|
148 double b = range.base (); |
2376
|
149 return (range.nelem () == 1 |
5275
|
150 && ! xisnan (b) && D_NINT (b) == b && NINTbig (b) == 0); |
2376
|
151 } |
|
152 |
3678
|
153 bool is_numeric_type (void) const { return true; } |
|
154 |
2376
|
155 bool is_true (void) const; |
|
156 |
3145
|
157 double double_value (bool = false) const; |
2376
|
158 |
3145
|
159 double scalar_value (bool frc_str_conv = false) const |
|
160 { return double_value (frc_str_conv); } |
2916
|
161 |
3145
|
162 Matrix matrix_value (bool = false) const |
2376
|
163 { return range.matrix_value (); } |
|
164 |
4668
|
165 NDArray array_value (bool = false) const |
|
166 { return range.matrix_value (); } |
|
167 |
6934
|
168 // FIXME -- it would be better to have Range::intXNDArray_value |
|
169 // functions to avoid the intermediate conversion to a matrix |
|
170 // object. |
|
171 |
6932
|
172 int8NDArray |
|
173 int8_array_value (void) const { return int8NDArray (array_value ()); } |
|
174 |
|
175 int16NDArray |
|
176 int16_array_value (void) const { return int16NDArray (array_value ()); } |
|
177 |
|
178 int32NDArray |
|
179 int32_array_value (void) const { return int32NDArray (array_value ()); } |
|
180 |
|
181 int64NDArray |
|
182 int64_array_value (void) const { return int64NDArray (array_value ()); } |
|
183 |
|
184 uint8NDArray |
|
185 uint8_array_value (void) const { return uint8NDArray (array_value ()); } |
|
186 |
|
187 uint16NDArray |
|
188 uint16_array_value (void) const { return uint16NDArray (array_value ()); } |
|
189 |
|
190 uint32NDArray |
|
191 uint32_array_value (void) const { return uint32NDArray (array_value ()); } |
|
192 |
|
193 uint64NDArray |
|
194 uint64_array_value (void) const { return uint64NDArray (array_value ()); } |
|
195 |
6928
|
196 SparseMatrix sparse_matrix_value (bool = false) const |
|
197 { return SparseMatrix (range.matrix_value ()); } |
|
198 |
|
199 SparseComplexMatrix sparse_complex_matrix_value (bool = false) const |
|
200 { return SparseComplexMatrix (sparse_matrix_value ()); } |
|
201 |
3145
|
202 Complex complex_value (bool = false) const; |
2376
|
203 |
5943
|
204 boolNDArray bool_array_value (bool warn = false) const |
|
205 { |
|
206 Matrix m = range.matrix_value (); |
|
207 |
|
208 if (warn && m.any_element_not_one_or_zero ()) |
|
209 gripe_logical_conversion (); |
|
210 |
|
211 return boolNDArray (m); |
|
212 } |
5898
|
213 |
3145
|
214 ComplexMatrix complex_matrix_value (bool = false) const |
3587
|
215 { return ComplexMatrix (range.matrix_value ()); } |
2376
|
216 |
4668
|
217 ComplexNDArray complex_array_value (bool = false) const |
|
218 { return ComplexMatrix (range.matrix_value ()); } |
|
219 |
2376
|
220 Range range_value (void) const { return range; } |
|
221 |
5279
|
222 octave_value convert_to_str_internal (bool pad, bool force, char type) const; |
2449
|
223 |
3523
|
224 void print (std::ostream& os, bool pr_as_read_syntax = false) const; |
2901
|
225 |
3523
|
226 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; |
2901
|
227 |
3523
|
228 bool print_name_tag (std::ostream& os, const std::string& name) const; |
2376
|
229 |
6974
|
230 bool save_ascii (std::ostream& os); |
4687
|
231 |
|
232 bool load_ascii (std::istream& is); |
|
233 |
|
234 bool save_binary (std::ostream& os, bool& save_as_floats); |
|
235 |
|
236 bool load_binary (std::istream& is, bool swap, |
|
237 oct_mach_info::float_format fmt); |
|
238 |
|
239 #if defined (HAVE_HDF5) |
|
240 bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats); |
|
241 |
|
242 bool load_hdf5 (hid_t loc_id, const char *name, bool have_h5giterate_bug); |
|
243 #endif |
|
244 |
5051
|
245 int write (octave_stream& os, int block_size, |
|
246 oct_data_conv::data_type output_type, int skip, |
|
247 oct_mach_info::float_format flt_fmt) const |
|
248 { |
5775
|
249 // FIXME -- could be more memory efficient by having a |
5051
|
250 // special case of the octave_stream::write method for ranges. |
|
251 |
|
252 return os.write (matrix_value (), block_size, output_type, skip, |
|
253 flt_fmt); |
|
254 } |
|
255 |
5900
|
256 mxArray *as_mxArray (void) const; |
|
257 |
2376
|
258 private: |
|
259 |
|
260 Range range; |
|
261 |
4612
|
262 DECLARE_OCTAVE_ALLOCATOR |
2477
|
263 |
4612
|
264 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
2376
|
265 }; |
|
266 |
|
267 #endif |
|
268 |
|
269 /* |
|
270 ;;; Local Variables: *** |
|
271 ;;; mode: C++ *** |
|
272 ;;; End: *** |
|
273 */ |