4904
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 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 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. |
4904
|
21 |
|
22 */ |
|
23 |
|
24 #include <cstdlib> |
|
25 |
|
26 #include <iostream> |
|
27 #include <string> |
|
28 |
|
29 #include "mx-base.h" |
|
30 #include "oct-alloc.h" |
|
31 #include "so-array.h" |
|
32 #include "str-vec.h" |
|
33 |
|
34 #include "error.h" |
4944
|
35 #include "oct-stream.h" |
4904
|
36 #include "ov-base.h" |
|
37 #include "ov-base-int.h" |
|
38 #include "ov-typeinfo.h" |
4982
|
39 #include "gripes.h" |
4904
|
40 |
|
41 class |
|
42 OCTAVE_VALUE_INT_MATRIX_T |
|
43 : public octave_base_int_matrix<OCTAVE_INT_NDARRAY_T> |
|
44 { |
|
45 public: |
|
46 |
|
47 OCTAVE_VALUE_INT_MATRIX_T (void) |
|
48 : octave_base_int_matrix<OCTAVE_INT_NDARRAY_T> () { } |
|
49 |
|
50 OCTAVE_VALUE_INT_MATRIX_T (const OCTAVE_INT_NDARRAY_T& nda) |
|
51 : octave_base_int_matrix<OCTAVE_INT_NDARRAY_T> (nda) { } |
|
52 |
|
53 ~OCTAVE_VALUE_INT_MATRIX_T (void) { } |
|
54 |
5759
|
55 octave_base_value *clone (void) const |
4904
|
56 { return new OCTAVE_VALUE_INT_MATRIX_T (*this); } |
|
57 |
5759
|
58 octave_base_value *empty_clone (void) const |
4904
|
59 { return new OCTAVE_VALUE_INT_MATRIX_T (); } |
|
60 |
5533
|
61 int8NDArray |
|
62 int8_array_value (void) const { return int8NDArray (matrix); } |
|
63 |
|
64 int16NDArray |
|
65 int16_array_value (void) const { return int16NDArray (matrix); } |
|
66 |
|
67 int32NDArray |
|
68 int32_array_value (void) const { return int32NDArray (matrix); } |
|
69 |
|
70 int64NDArray |
|
71 int64_array_value (void) const { return int64NDArray (matrix); } |
|
72 |
|
73 uint8NDArray |
|
74 uint8_array_value (void) const { return uint8NDArray (matrix); } |
|
75 |
|
76 uint16NDArray |
|
77 uint16_array_value (void) const { return uint16NDArray (matrix); } |
|
78 |
|
79 uint32NDArray |
|
80 uint32_array_value (void) const { return uint32NDArray (matrix); } |
|
81 |
|
82 uint64NDArray |
|
83 uint64_array_value (void) const { return uint64NDArray (matrix); } |
4904
|
84 |
4982
|
85 double |
|
86 double_value (bool = false) const |
|
87 { |
|
88 double retval = lo_ieee_nan_value (); |
|
89 |
|
90 if (numel () > 0) |
|
91 { |
5775
|
92 // FIXME -- is warn_fortran_indexing the right variable here? |
4982
|
93 if (Vwarn_fortran_indexing) |
|
94 gripe_implicit_conversion (type_name (), "real scalar"); |
|
95 |
5030
|
96 retval = double (matrix(0)); |
4982
|
97 } |
|
98 else |
|
99 gripe_invalid_conversion (type_name (), "real scalar"); |
|
100 |
|
101 return retval; |
|
102 |
|
103 } |
|
104 |
4983
|
105 double scalar_value (bool = false) const { return double_value (); } |
4982
|
106 |
4915
|
107 NDArray |
|
108 array_value (bool = false) const |
|
109 { |
|
110 NDArray retval (matrix.dims ()); |
|
111 int nel = matrix.numel (); |
|
112 for (int i = 0; i < nel; i++) |
4963
|
113 retval(i) = double (matrix(i)); |
|
114 return retval; |
|
115 } |
|
116 |
|
117 ComplexNDArray |
|
118 complex_array_value (bool = false) const |
|
119 { |
|
120 ComplexNDArray retval (matrix.dims ()); |
|
121 int nel = matrix.numel (); |
|
122 for (int i = 0; i < nel; i++) |
5030
|
123 retval(i) = Complex (double (matrix(i))); |
4915
|
124 return retval; |
|
125 } |
|
126 |
5533
|
127 charNDArray |
|
128 char_array_value (bool = false) const |
|
129 { |
|
130 charNDArray retval (dims ()); |
|
131 |
|
132 octave_idx_type nel = numel (); |
|
133 |
|
134 for (octave_idx_type i = 0; i < nel; i++) |
|
135 retval(i) = static_cast<char>(matrix(i)); |
|
136 |
|
137 return retval; |
|
138 } |
|
139 |
4938
|
140 idx_vector index_vector (void) const { return idx_vector (matrix); } |
|
141 |
4944
|
142 int write (octave_stream& os, int block_size, |
|
143 oct_data_conv::data_type output_type, int skip, |
|
144 oct_mach_info::float_format flt_fmt) const |
|
145 { return os.write (matrix, block_size, output_type, skip, flt_fmt); } |
|
146 |
4904
|
147 private: |
|
148 |
|
149 DECLARE_OCTAVE_ALLOCATOR |
|
150 |
|
151 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
|
152 }; |
|
153 |
|
154 class |
|
155 OCTAVE_VALUE_INT_SCALAR_T |
|
156 : public octave_base_int_scalar<OCTAVE_INT_T> |
|
157 { |
|
158 public: |
|
159 |
|
160 OCTAVE_VALUE_INT_SCALAR_T (void) |
|
161 : octave_base_int_scalar<OCTAVE_INT_T> () { } |
|
162 |
|
163 OCTAVE_VALUE_INT_SCALAR_T (const OCTAVE_INT_T& nda) |
|
164 : octave_base_int_scalar<OCTAVE_INT_T> (nda) { } |
|
165 |
|
166 ~OCTAVE_VALUE_INT_SCALAR_T (void) { } |
|
167 |
5759
|
168 octave_base_value *clone (void) const |
4904
|
169 { return new OCTAVE_VALUE_INT_SCALAR_T (*this); } |
|
170 |
5759
|
171 octave_base_value *empty_clone (void) const |
4904
|
172 { return new OCTAVE_VALUE_INT_SCALAR_T (); } |
|
173 |
4964
|
174 octave_value do_index_op (const octave_value_list& idx, int resize_ok) |
4983
|
175 { |
|
176 octave_value retval; |
|
177 |
|
178 if (idx.valid_scalar_indices ()) |
|
179 retval = scalar; |
|
180 else |
|
181 { |
5775
|
182 // FIXME -- this doesn't solve the problem of |
4983
|
183 // |
|
184 // a = 1; a([1,1], [1,1], [1,1]) |
|
185 // |
|
186 // and similar constructions. Hmm... |
4964
|
187 |
5775
|
188 // FIXME -- using this constructor avoids narrowing the |
4983
|
189 // 1x1 matrix back to a scalar value. Need a better solution |
|
190 // to this problem. |
4964
|
191 |
4983
|
192 octave_value tmp |
|
193 (new OCTAVE_VALUE_INT_MATRIX_T |
|
194 (OCTAVE_VALUE_INT_NDARRAY_EXTRACTOR_FUNCTION ())); |
4964
|
195 |
4983
|
196 retval = tmp.do_index_op (idx, resize_ok); |
|
197 } |
4964
|
198 |
4983
|
199 return retval; |
|
200 } |
4964
|
201 |
5533
|
202 octave_int8 |
|
203 int8_scalar_value (void) const { return octave_int8 (scalar); } |
|
204 |
|
205 octave_int16 |
|
206 int16_scalar_value (void) const { return octave_int16 (scalar); } |
|
207 |
|
208 octave_int32 |
|
209 int32_scalar_value (void) const { return octave_int32 (scalar); } |
|
210 |
|
211 octave_int64 |
|
212 int64_scalar_value (void) const { return octave_int64 (scalar); } |
|
213 |
|
214 octave_uint8 |
|
215 uint8_scalar_value (void) const { return octave_uint8 (scalar); } |
|
216 |
|
217 octave_uint16 |
|
218 uint16_scalar_value (void) const { return octave_uint16 (scalar); } |
|
219 |
|
220 octave_uint32 |
|
221 uint32_scalar_value (void) const { return octave_uint32 (scalar); } |
|
222 |
|
223 octave_uint64 |
|
224 uint64_scalar_value (void) const { return octave_uint64 (scalar); } |
|
225 |
|
226 int8NDArray |
|
227 int8_array_value (void) const |
|
228 { return int8NDArray (dim_vector (1, 1), scalar); } |
4904
|
229 |
5533
|
230 int16NDArray |
|
231 int16_array_value (void) const |
|
232 { return int16NDArray (dim_vector (1, 1), scalar); } |
|
233 |
|
234 int32NDArray |
|
235 int32_array_value (void) const |
|
236 { return int32NDArray (dim_vector (1, 1), scalar); } |
|
237 |
|
238 int64NDArray |
|
239 int64_array_value (void) const |
|
240 { return int64NDArray (dim_vector (1, 1), scalar); } |
|
241 |
|
242 uint8NDArray |
|
243 uint8_array_value (void) const |
|
244 { return uint8NDArray (dim_vector (1, 1), scalar); } |
|
245 |
|
246 uint16NDArray |
|
247 uint16_array_value (void) const |
|
248 { return uint16NDArray (dim_vector (1, 1), scalar); } |
|
249 |
|
250 uint32NDArray |
|
251 uint32_array_value (void) const |
|
252 { return uint32NDArray (dim_vector (1, 1), scalar); } |
|
253 |
|
254 uint64NDArray |
|
255 uint64_array_value (void) const |
|
256 { return uint64NDArray (dim_vector (1, 1), scalar); } |
4904
|
257 |
5731
|
258 octave_value resize (const dim_vector& dv, bool fill = false) const |
4982
|
259 { |
5731
|
260 if (fill) |
|
261 { |
|
262 OCTAVE_INT_NDARRAY_T retval (dv, 0); |
|
263 if (dv.numel()) |
|
264 retval(0) = scalar; |
|
265 return retval; |
|
266 } |
|
267 else |
|
268 { |
|
269 OCTAVE_INT_NDARRAY_T retval (dv); |
|
270 if (dv.numel()) |
|
271 retval(0) = scalar; |
|
272 return retval; |
|
273 } |
4982
|
274 } |
|
275 |
4983
|
276 double double_value (bool = false) const { return double (scalar); } |
|
277 |
|
278 double scalar_value (bool = false) const { return double (scalar); } |
4982
|
279 |
4915
|
280 NDArray |
|
281 array_value (bool = false) const |
|
282 { |
5533
|
283 NDArray retval (dim_vector (1, 1)); |
4963
|
284 retval(0) = double (scalar); |
|
285 return retval; |
|
286 } |
|
287 |
|
288 ComplexNDArray |
|
289 complex_array_value (bool = false) const |
|
290 { |
5533
|
291 ComplexNDArray retval (dim_vector (1, 1)); |
5030
|
292 retval(0) = Complex (double (scalar)); |
4915
|
293 return retval; |
|
294 } |
|
295 |
5533
|
296 charNDArray |
|
297 char_array_value (bool = false) const |
|
298 { |
|
299 charNDArray retval (dim_vector (1, 1)); |
|
300 retval(0) = static_cast<char>(scalar); |
|
301 return retval; |
|
302 } |
|
303 |
4938
|
304 idx_vector index_vector (void) const { return idx_vector (scalar); } |
|
305 |
4944
|
306 int write (octave_stream& os, int block_size, |
5275
|
307 oct_data_conv::data_type output_type, octave_idx_type skip, |
4944
|
308 oct_mach_info::float_format flt_fmt) const |
|
309 { |
|
310 return os.write (OCTAVE_VALUE_INT_NDARRAY_EXTRACTOR_FUNCTION (), |
|
311 block_size, output_type, skip, flt_fmt); |
|
312 } |
|
313 |
4904
|
314 private: |
|
315 |
|
316 DECLARE_OCTAVE_ALLOCATOR |
|
317 |
|
318 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
|
319 }; |
|
320 |
|
321 /* |
|
322 ;;; Local Variables: *** |
|
323 ;;; mode: C++ *** |
|
324 ;;; End: *** |
|
325 */ |