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 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3503
|
28 #include <iostream> |
2901
|
29 |
2376
|
30 #include "lo-ieee.h" |
|
31 #include "lo-utils.h" |
|
32 |
|
33 #include "gripes.h" |
|
34 #include "ops.h" |
3933
|
35 #include "oct-obj.h" |
2376
|
36 #include "ov-range.h" |
|
37 #include "ov-re-mat.h" |
2410
|
38 #include "ov-scalar.h" |
2376
|
39 #include "pr-output.h" |
|
40 |
4687
|
41 #include "byte-swap.h" |
|
42 #include "ls-hdf5.h" |
|
43 #include "ls-utils.h" |
|
44 |
3219
|
45 DEFINE_OCTAVE_ALLOCATOR (octave_range); |
2376
|
46 |
4612
|
47 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_range, "range", "double"); |
2376
|
48 |
5759
|
49 static octave_base_value * |
|
50 default_numeric_conversion_function (const octave_base_value& a) |
2376
|
51 { |
|
52 CAST_CONV_ARG (const octave_range&); |
|
53 |
|
54 return new octave_matrix (v.matrix_value ()); |
|
55 } |
|
56 |
5759
|
57 octave_base_value::type_conv_fcn |
2376
|
58 octave_range::numeric_conversion_function (void) const |
|
59 { |
|
60 return default_numeric_conversion_function; |
|
61 } |
|
62 |
5759
|
63 octave_base_value * |
2410
|
64 octave_range::try_narrowing_conversion (void) |
|
65 { |
5759
|
66 octave_base_value *retval = 0; |
2410
|
67 |
|
68 switch (range.nelem ()) |
|
69 { |
|
70 case 1: |
|
71 retval = new octave_scalar (range.base ()); |
|
72 break; |
|
73 |
|
74 case 0: |
4417
|
75 retval = new octave_matrix (Matrix (1, 0)); |
2410
|
76 break; |
|
77 |
|
78 default: |
|
79 break; |
|
80 } |
|
81 |
|
82 return retval; |
|
83 } |
|
84 |
2436
|
85 octave_value |
4247
|
86 octave_range::subsref (const std::string& type, |
4219
|
87 const std::list<octave_value_list>& idx) |
3933
|
88 { |
|
89 octave_value retval; |
|
90 |
|
91 switch (type[0]) |
|
92 { |
|
93 case '(': |
|
94 retval = do_index_op (idx.front ()); |
|
95 break; |
|
96 |
|
97 case '{': |
|
98 case '.': |
|
99 { |
|
100 std::string nm = type_name (); |
|
101 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
102 } |
|
103 break; |
|
104 |
|
105 default: |
|
106 panic_impossible (); |
|
107 } |
|
108 |
|
109 return retval.next_subsref (type, idx); |
|
110 } |
|
111 |
|
112 octave_value |
|
113 octave_range::do_index_op (const octave_value_list& idx, int resize_ok) |
2436
|
114 { |
5775
|
115 // FIXME -- this doesn't solve the problem of |
2436
|
116 // |
|
117 // a = 1:5; a(1, 1, 1) |
|
118 // |
|
119 // and similar constructions. Hmm... |
|
120 |
5775
|
121 // FIXME -- using this constructor avoids possibly narrowing |
2436
|
122 // the range to a scalar value. Need a better solution to this |
|
123 // problem. |
|
124 |
|
125 octave_value tmp (new octave_matrix (range.matrix_value ())); |
|
126 |
3933
|
127 return tmp.do_index_op (idx, resize_ok); |
2436
|
128 } |
|
129 |
2376
|
130 double |
|
131 octave_range::double_value (bool) const |
|
132 { |
4102
|
133 double retval = lo_ieee_nan_value (); |
2376
|
134 |
5275
|
135 octave_idx_type nel = range.nelem (); |
2376
|
136 |
4455
|
137 if (nel > 0) |
|
138 { |
5775
|
139 // FIXME -- is warn_fortran_indexing the right variable here? |
4455
|
140 if (Vwarn_fortran_indexing) |
|
141 gripe_implicit_conversion ("range", "real scalar"); |
|
142 |
|
143 retval = range.base (); |
|
144 } |
2376
|
145 else |
|
146 gripe_invalid_conversion ("range", "real scalar"); |
|
147 |
|
148 return retval; |
|
149 } |
|
150 |
|
151 octave_value |
4017
|
152 octave_range::all (int dim) const |
2376
|
153 { |
5775
|
154 // FIXME -- this is a potential waste of memory. |
2436
|
155 |
|
156 Matrix m = range.matrix_value (); |
|
157 |
4017
|
158 return m.all (dim); |
2376
|
159 } |
|
160 |
|
161 octave_value |
4017
|
162 octave_range::any (int dim) const |
2376
|
163 { |
5775
|
164 // FIXME -- this is a potential waste of memory. |
4017
|
165 |
|
166 Matrix m = range.matrix_value (); |
|
167 |
|
168 return m.any (dim); |
2376
|
169 } |
|
170 |
|
171 bool |
|
172 octave_range::is_true (void) const |
|
173 { |
|
174 bool retval = false; |
2436
|
175 |
4478
|
176 if (range.nelem () != 0) |
2436
|
177 { |
5775
|
178 // FIXME -- this is a potential waste of memory. |
2436
|
179 |
|
180 Matrix m ((range.matrix_value () . all ()) . all ()); |
|
181 |
|
182 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0); |
|
183 } |
|
184 |
2376
|
185 return retval; |
|
186 } |
|
187 |
|
188 Complex |
|
189 octave_range::complex_value (bool) const |
|
190 { |
4102
|
191 double tmp = lo_ieee_nan_value (); |
|
192 |
|
193 Complex retval (tmp, tmp); |
2376
|
194 |
5275
|
195 octave_idx_type nel = range.nelem (); |
2376
|
196 |
4455
|
197 if (nel > 0) |
|
198 { |
5775
|
199 // FIXME -- is warn_fortran_indexing the right variable here? |
4455
|
200 if (Vwarn_fortran_indexing) |
|
201 gripe_implicit_conversion ("range", "complex scalar"); |
|
202 |
|
203 retval = range.base (); |
|
204 } |
2376
|
205 else |
|
206 gripe_invalid_conversion ("range", "complex scalar"); |
|
207 |
|
208 return retval; |
|
209 } |
|
210 |
5731
|
211 octave_value |
|
212 octave_range::resize (const dim_vector& dv, bool fill) const |
|
213 { |
|
214 NDArray retval = array_value (); |
|
215 if (fill) |
|
216 retval.resize (dv, NDArray::resize_fill_value()); |
|
217 else |
|
218 retval.resize (dv); |
|
219 return retval; |
|
220 } |
|
221 |
2376
|
222 octave_value |
5279
|
223 octave_range::convert_to_str_internal (bool pad, bool force, char type) const |
2449
|
224 { |
|
225 octave_value tmp (range.matrix_value ()); |
5279
|
226 return tmp.convert_to_str (pad, force, type); |
2449
|
227 } |
|
228 |
2376
|
229 void |
3523
|
230 octave_range::print (std::ostream& os, bool pr_as_read_syntax) const |
2901
|
231 { |
|
232 print_raw (os, pr_as_read_syntax); |
|
233 newline (os); |
|
234 } |
|
235 |
|
236 void |
3523
|
237 octave_range::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
2901
|
238 { |
|
239 octave_print_internal (os, range, pr_as_read_syntax, |
|
240 current_print_indent_level ()); |
|
241 } |
|
242 |
|
243 bool |
3523
|
244 octave_range::print_name_tag (std::ostream& os, const std::string& name) const |
2376
|
245 { |
2901
|
246 bool retval = false; |
|
247 |
5275
|
248 octave_idx_type n = range.nelem (); |
2901
|
249 |
|
250 indent (os); |
|
251 |
|
252 if (n == 0 || n == 1) |
|
253 os << name << " = "; |
|
254 else |
|
255 { |
|
256 os << name << " ="; |
|
257 newline (os); |
|
258 newline (os); |
|
259 retval = true; |
|
260 } |
|
261 |
|
262 return retval; |
2376
|
263 } |
|
264 |
4687
|
265 // Skip white space and comments on stream IS. |
|
266 |
|
267 static void |
|
268 skip_comments (std::istream& is) |
|
269 { |
|
270 char c = '\0'; |
|
271 while (is.get (c)) |
|
272 { |
|
273 if (c == ' ' || c == '\t' || c == '\n') |
|
274 ; // Skip whitespace on way to beginning of next line. |
|
275 else |
|
276 break; |
|
277 } |
|
278 |
|
279 for (;;) |
|
280 { |
|
281 if (is && (c == '%' || c == '#')) |
|
282 while (is.get (c) && c != '\n') |
|
283 ; // Skip to beginning of next line, ignoring everything. |
|
284 else |
|
285 break; |
|
286 } |
|
287 } |
|
288 |
|
289 bool |
|
290 octave_range::save_ascii (std::ostream& os, bool& /* infnan_warned */, |
|
291 bool /* strip_nan_and_inf */) |
|
292 { |
|
293 Range r = range_value (); |
|
294 double base = r.base (); |
|
295 double limit = r.limit (); |
|
296 double inc = r.inc (); |
|
297 |
|
298 os << "# base, limit, increment\n"; |
|
299 octave_write_double (os, base); |
|
300 os << " "; |
|
301 octave_write_double (os, limit); |
|
302 os << " "; |
|
303 octave_write_double (os, inc); |
|
304 os << "\n"; |
|
305 |
|
306 return true; |
|
307 } |
|
308 |
|
309 bool |
|
310 octave_range::load_ascii (std::istream& is) |
|
311 { |
|
312 // # base, limit, range comment added by save (). |
|
313 skip_comments (is); |
|
314 |
|
315 is >> range; |
|
316 |
|
317 if (!is) |
|
318 { |
|
319 error ("load: failed to load range constant"); |
|
320 return false; |
|
321 } |
|
322 |
|
323 return true; |
|
324 } |
|
325 |
|
326 bool |
|
327 octave_range::save_binary (std::ostream& os, bool& /* save_as_floats */) |
|
328 { |
5760
|
329 char tmp = LS_DOUBLE; |
|
330 os.write (reinterpret_cast<char *> (&tmp), 1); |
4687
|
331 Range r = range_value (); |
|
332 double bas = r.base (); |
|
333 double lim = r.limit (); |
|
334 double inc = r.inc (); |
5760
|
335 os.write (reinterpret_cast<char *> (&bas), 8); |
|
336 os.write (reinterpret_cast<char *> (&lim), 8); |
|
337 os.write (reinterpret_cast<char *> (&inc), 8); |
4687
|
338 |
|
339 return true; |
|
340 } |
|
341 |
|
342 bool |
|
343 octave_range::load_binary (std::istream& is, bool swap, |
|
344 oct_mach_info::float_format /* fmt */) |
|
345 { |
|
346 char tmp; |
5760
|
347 if (! is.read (reinterpret_cast<char *> (&tmp), 1)) |
4687
|
348 return false; |
|
349 double bas, lim, inc; |
5760
|
350 if (! is.read (reinterpret_cast<char *> (&bas), 8)) |
4687
|
351 return false; |
|
352 if (swap) |
4944
|
353 swap_bytes<8> (&bas); |
5760
|
354 if (! is.read (reinterpret_cast<char *> (&lim), 8)) |
4687
|
355 return false; |
|
356 if (swap) |
4944
|
357 swap_bytes<8> (&lim); |
5760
|
358 if (! is.read (reinterpret_cast<char *> (&inc), 8)) |
4687
|
359 return false; |
|
360 if (swap) |
4944
|
361 swap_bytes<8> (&inc); |
4687
|
362 Range r (bas, lim, inc); |
|
363 range = r; |
|
364 return true; |
|
365 } |
|
366 |
|
367 #if defined (HAVE_HDF5) |
4944
|
368 |
4687
|
369 // The following subroutines creates an HDF5 representation of the way |
|
370 // we will store Octave range types (triplets of floating-point numbers). |
|
371 // NUM_TYPE is the HDF5 numeric type to use for storage (e.g. |
|
372 // H5T_NATIVE_DOUBLE to save as 'double'). Note that any necessary |
|
373 // conversions are handled automatically by HDF5. |
|
374 |
|
375 static hid_t |
|
376 hdf5_make_range_type (hid_t num_type) |
|
377 { |
|
378 hid_t type_id = H5Tcreate (H5T_COMPOUND, sizeof (double) * 3); |
|
379 |
|
380 H5Tinsert (type_id, "base", 0 * sizeof (double), num_type); |
|
381 H5Tinsert (type_id, "limit", 1 * sizeof (double), num_type); |
|
382 H5Tinsert (type_id, "increment", 2 * sizeof (double), num_type); |
|
383 |
|
384 return type_id; |
|
385 } |
|
386 |
|
387 bool |
|
388 octave_range::save_hdf5 (hid_t loc_id, const char *name, |
|
389 bool /* save_as_floats */) |
|
390 { |
4792
|
391 hsize_t dimens[3]; |
4687
|
392 hid_t space_hid = -1, type_hid = -1, data_hid = -1; |
|
393 bool retval = true; |
|
394 |
4815
|
395 space_hid = H5Screate_simple (0, dimens, 0); |
4687
|
396 if (space_hid < 0) return false; |
|
397 |
|
398 type_hid = hdf5_make_range_type (H5T_NATIVE_DOUBLE); |
|
399 if (type_hid < 0) |
|
400 { |
|
401 H5Sclose (space_hid); |
|
402 return false; |
|
403 } |
|
404 |
|
405 data_hid = H5Dcreate (loc_id, name, type_hid, space_hid, H5P_DEFAULT); |
|
406 if (data_hid < 0) |
|
407 { |
|
408 H5Sclose (space_hid); |
|
409 H5Tclose (type_hid); |
|
410 return false; |
|
411 } |
|
412 |
|
413 Range r = range_value (); |
|
414 double range_vals[3]; |
|
415 range_vals[0] = r.base (); |
|
416 range_vals[1] = r.limit (); |
|
417 range_vals[2] = r.inc (); |
|
418 |
|
419 retval = H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
4815
|
420 range_vals) >= 0; |
4687
|
421 |
|
422 H5Dclose (data_hid); |
|
423 H5Tclose (type_hid); |
|
424 H5Sclose (space_hid); |
4837
|
425 |
4687
|
426 return retval; |
|
427 } |
|
428 |
|
429 bool |
|
430 octave_range::load_hdf5 (hid_t loc_id, const char *name, |
|
431 bool /* have_h5giterate_bug */) |
|
432 { |
|
433 bool retval = false; |
4837
|
434 |
4687
|
435 hid_t data_hid = H5Dopen (loc_id, name); |
|
436 hid_t type_hid = H5Dget_type (data_hid); |
|
437 |
|
438 hid_t range_type = hdf5_make_range_type (H5T_NATIVE_DOUBLE); |
|
439 |
|
440 if (! hdf5_types_compatible (type_hid, range_type)) |
|
441 { |
4837
|
442 H5Tclose (range_type); |
4687
|
443 H5Dclose (data_hid); |
|
444 return false; |
|
445 } |
|
446 |
|
447 hid_t space_hid = H5Dget_space (data_hid); |
|
448 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); |
|
449 |
|
450 if (rank != 0) |
|
451 { |
4837
|
452 H5Tclose (range_type); |
4687
|
453 H5Sclose (space_hid); |
|
454 H5Dclose (data_hid); |
|
455 return false; |
|
456 } |
|
457 |
|
458 double rangevals[3]; |
|
459 if (H5Dread (data_hid, range_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, |
4815
|
460 rangevals) >= 0) |
4687
|
461 { |
|
462 retval = true; |
|
463 Range r (rangevals[0], rangevals[1], rangevals[2]); |
|
464 range = r; |
|
465 } |
|
466 |
4837
|
467 H5Tclose (range_type); |
4687
|
468 H5Sclose (space_hid); |
|
469 H5Dclose (data_hid); |
4837
|
470 |
4687
|
471 return retval; |
|
472 } |
4944
|
473 |
4687
|
474 #endif |
|
475 |
2376
|
476 /* |
|
477 ;;; Local Variables: *** |
|
478 ;;; mode: C++ *** |
|
479 ;;; End: *** |
|
480 */ |