2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
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 2, or (at your option) any |
|
10 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, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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" |
|
43 #include "ov-typeinfo.h" |
|
44 |
|
45 class Octave_map; |
|
46 class octave_value_list; |
|
47 |
|
48 class tree_walker; |
|
49 |
|
50 // Range values. |
|
51 |
|
52 class |
|
53 octave_range : public octave_base_value |
|
54 { |
|
55 public: |
|
56 |
|
57 octave_range (void) |
|
58 : octave_base_value () { } |
|
59 |
|
60 octave_range (double base, double limit, double inc) |
|
61 : octave_base_value (), range (base, limit, inc) |
|
62 { |
|
63 if (range.nelem () < 0) |
|
64 ::error ("invalid range"); |
|
65 } |
|
66 |
|
67 octave_range (const Range& r) |
|
68 : octave_base_value (), range (r) |
|
69 { |
|
70 if (range.nelem () < 0) |
|
71 ::error ("invalid range"); |
|
72 } |
|
73 |
|
74 octave_range (const octave_range& r) |
|
75 : octave_base_value (), range (r.range) { } |
|
76 |
|
77 ~octave_range (void) { } |
|
78 |
5759
|
79 octave_base_value *clone (void) const { return new octave_range (*this); } |
|
80 octave_base_value *empty_clone (void) const { return new octave_range (); } |
2376
|
81 |
2410
|
82 type_conv_fcn numeric_conversion_function (void) const; |
|
83 |
5759
|
84 octave_base_value *try_narrowing_conversion (void); |
2376
|
85 |
4247
|
86 octave_value subsref (const std::string& type, |
4219
|
87 const std::list<octave_value_list>& idx); |
3933
|
88 |
4661
|
89 octave_value_list subsref (const std::string&, |
|
90 const std::list<octave_value_list>&, int) |
4271
|
91 { |
|
92 panic_impossible (); |
|
93 return octave_value_list (); |
|
94 } |
|
95 |
5885
|
96 octave_value do_index_op (const octave_value_list& idx, |
|
97 bool resize_ok = false); |
2436
|
98 |
2376
|
99 idx_vector index_vector (void) const { return idx_vector (range); } |
|
100 |
4563
|
101 dim_vector dims (void) const |
5275
|
102 { |
|
103 octave_idx_type n = range.nelem (); |
4563
|
104 return dim_vector (n > 0, n); |
|
105 } |
3195
|
106 |
5731
|
107 octave_value resize (const dim_vector& dv, bool fill = false) const; |
|
108 |
4915
|
109 |
4792
|
110 size_t byte_size (void) const { return 3 * sizeof (double); } |
4791
|
111 |
4627
|
112 octave_value reshape (const dim_vector& new_dims) const |
4901
|
113 { return NDArray (array_value().reshape (new_dims)); } |
4627
|
114 |
5434
|
115 octave_value permute (const Array<int>& vec, bool inv = false) const |
|
116 { return NDArray (array_value().permute (vec, inv)); } |
|
117 |
2376
|
118 bool is_defined (void) const { return true; } |
|
119 |
2974
|
120 bool is_constant (void) const { return true; } |
|
121 |
2376
|
122 bool is_range (void) const { return true; } |
|
123 |
4017
|
124 octave_value all (int dim = 0) const; |
|
125 |
|
126 octave_value any (int dim = 0) const; |
2376
|
127 |
|
128 bool is_real_type (void) const { return true; } |
|
129 |
5895
|
130 bool is_double_type (void) const { return true; } |
|
131 |
2376
|
132 bool valid_as_scalar_index (void) const |
|
133 { |
5042
|
134 double b = range.base (); |
2376
|
135 return (range.nelem () == 1 |
5275
|
136 && ! xisnan (b) && D_NINT (b) == b && NINTbig (b) == 1); |
2376
|
137 } |
|
138 |
|
139 bool valid_as_zero_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) == 0); |
2376
|
144 } |
|
145 |
3678
|
146 bool is_numeric_type (void) const { return true; } |
|
147 |
2376
|
148 bool is_true (void) const; |
|
149 |
3145
|
150 double double_value (bool = false) const; |
2376
|
151 |
3145
|
152 double scalar_value (bool frc_str_conv = false) const |
|
153 { return double_value (frc_str_conv); } |
2916
|
154 |
3145
|
155 Matrix matrix_value (bool = false) const |
2376
|
156 { return range.matrix_value (); } |
|
157 |
4668
|
158 NDArray array_value (bool = false) const |
|
159 { return range.matrix_value (); } |
|
160 |
3145
|
161 Complex complex_value (bool = false) const; |
2376
|
162 |
5943
|
163 boolNDArray bool_array_value (bool warn = false) const |
|
164 { |
|
165 Matrix m = range.matrix_value (); |
|
166 |
|
167 if (warn && m.any_element_not_one_or_zero ()) |
|
168 gripe_logical_conversion (); |
|
169 |
|
170 return boolNDArray (m); |
|
171 } |
5898
|
172 |
3145
|
173 ComplexMatrix complex_matrix_value (bool = false) const |
3587
|
174 { return ComplexMatrix (range.matrix_value ()); } |
2376
|
175 |
4668
|
176 ComplexNDArray complex_array_value (bool = false) const |
|
177 { return ComplexMatrix (range.matrix_value ()); } |
|
178 |
2376
|
179 Range range_value (void) const { return range; } |
|
180 |
5279
|
181 octave_value convert_to_str_internal (bool pad, bool force, char type) const; |
2449
|
182 |
3523
|
183 void print (std::ostream& os, bool pr_as_read_syntax = false) const; |
2901
|
184 |
3523
|
185 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; |
2901
|
186 |
3523
|
187 bool print_name_tag (std::ostream& os, const std::string& name) const; |
2376
|
188 |
4687
|
189 bool save_ascii (std::ostream& os, bool& infnan_warned, |
|
190 bool strip_nan_and_inf); |
|
191 |
|
192 bool load_ascii (std::istream& is); |
|
193 |
|
194 bool save_binary (std::ostream& os, bool& save_as_floats); |
|
195 |
|
196 bool load_binary (std::istream& is, bool swap, |
|
197 oct_mach_info::float_format fmt); |
|
198 |
|
199 #if defined (HAVE_HDF5) |
|
200 bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats); |
|
201 |
|
202 bool load_hdf5 (hid_t loc_id, const char *name, bool have_h5giterate_bug); |
|
203 #endif |
|
204 |
5051
|
205 int write (octave_stream& os, int block_size, |
|
206 oct_data_conv::data_type output_type, int skip, |
|
207 oct_mach_info::float_format flt_fmt) const |
|
208 { |
5775
|
209 // FIXME -- could be more memory efficient by having a |
5051
|
210 // special case of the octave_stream::write method for ranges. |
|
211 |
|
212 return os.write (matrix_value (), block_size, output_type, skip, |
|
213 flt_fmt); |
|
214 } |
|
215 |
5900
|
216 mxArray *as_mxArray (void) const; |
|
217 |
2376
|
218 private: |
|
219 |
|
220 Range range; |
|
221 |
4612
|
222 DECLARE_OCTAVE_ALLOCATOR |
2477
|
223 |
4612
|
224 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
2376
|
225 }; |
|
226 |
|
227 #endif |
|
228 |
|
229 /* |
|
230 ;;; Local Variables: *** |
|
231 ;;; mode: C++ *** |
|
232 ;;; End: *** |
|
233 */ |