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