3 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2006,
6 This file is part of Octave.
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
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
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
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
37 #include "ov-re-mat.h"
38 #include "ov-scalar.h"
39 #include "pr-output.h"
41 #include "byte-swap.h"
44 #include "ls-ascii-helper.h"
46 DEFINE_OCTAVE_ALLOCATOR (octave_range);
48 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_range, "range", "double");
50 static octave_base_value *
51 default_numeric_conversion_function (const octave_base_value& a)
53 CAST_CONV_ARG (const octave_range&);
55 return new octave_matrix (v.matrix_value ());
58 octave_base_value::type_conv_fcn
59 octave_range::numeric_conversion_function (void) const
61 return default_numeric_conversion_function;
65 octave_range::try_narrowing_conversion (void)
67 octave_base_value *retval = 0;
69 switch (range.nelem ())
72 retval = new octave_scalar (range.base ());
76 retval = new octave_matrix (Matrix (1, 0));
87 octave_range::subsref (const std::string& type,
88 const std::list<octave_value_list>& idx)
95 retval = do_index_op (idx.front ());
101 std::string nm = type_name ();
102 error ("%s cannot be indexed with %c", nm.c_str (), type[0]);
110 return retval.next_subsref (type, idx);
114 octave_range::do_index_op (const octave_value_list& idx, bool resize_ok)
116 // FIXME -- this doesn't solve the problem of
118 // a = 1:5; a(1, 1, 1)
120 // and similar constructions. Hmm...
122 // FIXME -- using this constructor avoids possibly narrowing
123 // the range to a scalar value. Need a better solution to this
126 octave_value tmp (new octave_matrix (range.matrix_value ()));
128 return tmp.do_index_op (idx, resize_ok);
132 octave_range::double_value (bool) const
134 double retval = lo_ieee_nan_value ();
136 octave_idx_type nel = range.nelem ();
140 gripe_implicit_conversion ("Octave:array-as-scalar",
141 "range", "real scalar");
143 retval = range.base ();
146 gripe_invalid_conversion ("range", "real scalar");
152 octave_range::all (int dim) const
154 // FIXME -- this is a potential waste of memory.
156 Matrix m = range.matrix_value ();
162 octave_range::any (int dim) const
164 // FIXME -- this is a potential waste of memory.
166 Matrix m = range.matrix_value ();
172 octave_range::is_true (void) const
176 if (range.nelem () != 0)
178 // FIXME -- this is a potential waste of memory.
180 Matrix m ((range.matrix_value () . all ()) . all ());
182 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0);
189 octave_range::complex_value (bool) const
191 double tmp = lo_ieee_nan_value ();
193 Complex retval (tmp, tmp);
195 octave_idx_type nel = range.nelem ();
199 gripe_implicit_conversion ("Octave:array-as-scalar",
200 "range", "complex scalar");
202 retval = range.base ();
205 gripe_invalid_conversion ("range", "complex scalar");
211 octave_range::resize (const dim_vector& dv, bool fill) const
213 NDArray retval = array_value ();
215 retval.resize (dv, NDArray::resize_fill_value());
222 octave_range::convert_to_str_internal (bool pad, bool force, char type) const
224 octave_value tmp (range.matrix_value ());
225 return tmp.convert_to_str (pad, force, type);
229 octave_range::print (std::ostream& os, bool pr_as_read_syntax) const
231 print_raw (os, pr_as_read_syntax);
236 octave_range::print_raw (std::ostream& os, bool pr_as_read_syntax) const
238 octave_print_internal (os, range, pr_as_read_syntax,
239 current_print_indent_level ());
243 octave_range::print_name_tag (std::ostream& os, const std::string& name) const
247 octave_idx_type n = range.nelem ();
251 if (n == 0 || n == 1)
264 // Skip white space and comments on stream IS.
267 skip_comments (std::istream& is)
272 if (c == ' ' || c == '\t' || c == '\n')
273 ; // Skip whitespace on way to beginning of next line.
278 // Skip to beginning of next line, ignoring everything.
279 skip_until_newline (is, false);
284 octave_range::save_ascii (std::ostream& os)
286 Range r = range_value ();
287 double base = r.base ();
288 double limit = r.limit ();
289 double inc = r.inc ();
291 os << "# base, limit, increment\n";
292 octave_write_double (os, base);
294 octave_write_double (os, limit);
296 octave_write_double (os, inc);
303 octave_range::load_ascii (std::istream& is)
305 // # base, limit, range comment added by save ().
312 error ("load: failed to load range constant");
320 octave_range::save_binary (std::ostream& os, bool& /* save_as_floats */)
322 char tmp = LS_DOUBLE;
323 os.write (reinterpret_cast<char *> (&tmp), 1);
324 Range r = range_value ();
325 double bas = r.base ();
326 double lim = r.limit ();
327 double inc = r.inc ();
328 os.write (reinterpret_cast<char *> (&bas), 8);
329 os.write (reinterpret_cast<char *> (&lim), 8);
330 os.write (reinterpret_cast<char *> (&inc), 8);
336 octave_range::load_binary (std::istream& is, bool swap,
337 oct_mach_info::float_format /* fmt */)
340 if (! is.read (reinterpret_cast<char *> (&tmp), 1))
342 double bas, lim, inc;
343 if (! is.read (reinterpret_cast<char *> (&bas), 8))
346 swap_bytes<8> (&bas);
347 if (! is.read (reinterpret_cast<char *> (&lim), 8))
350 swap_bytes<8> (&lim);
351 if (! is.read (reinterpret_cast<char *> (&inc), 8))
354 swap_bytes<8> (&inc);
355 Range r (bas, lim, inc);
360 #if defined (HAVE_HDF5)
362 // The following subroutines creates an HDF5 representation of the way
363 // we will store Octave range types (triplets of floating-point numbers).
364 // NUM_TYPE is the HDF5 numeric type to use for storage (e.g.
365 // H5T_NATIVE_DOUBLE to save as 'double'). Note that any necessary
366 // conversions are handled automatically by HDF5.
369 hdf5_make_range_type (hid_t num_type)
371 hid_t type_id = H5Tcreate (H5T_COMPOUND, sizeof (double) * 3);
373 H5Tinsert (type_id, "base", 0 * sizeof (double), num_type);
374 H5Tinsert (type_id, "limit", 1 * sizeof (double), num_type);
375 H5Tinsert (type_id, "increment", 2 * sizeof (double), num_type);
381 octave_range::save_hdf5 (hid_t loc_id, const char *name,
382 bool /* save_as_floats */)
385 hid_t space_hid = -1, type_hid = -1, data_hid = -1;
388 space_hid = H5Screate_simple (0, dimens, 0);
389 if (space_hid < 0) return false;
391 type_hid = hdf5_make_range_type (H5T_NATIVE_DOUBLE);
394 H5Sclose (space_hid);
398 data_hid = H5Dcreate (loc_id, name, type_hid, space_hid, H5P_DEFAULT);
401 H5Sclose (space_hid);
406 Range r = range_value ();
407 double range_vals[3];
408 range_vals[0] = r.base ();
409 range_vals[1] = r.limit ();
410 range_vals[2] = r.inc ();
412 retval = H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, H5P_DEFAULT,
417 H5Sclose (space_hid);
423 octave_range::load_hdf5 (hid_t loc_id, const char *name,
424 bool /* have_h5giterate_bug */)
428 hid_t data_hid = H5Dopen (loc_id, name);
429 hid_t type_hid = H5Dget_type (data_hid);
431 hid_t range_type = hdf5_make_range_type (H5T_NATIVE_DOUBLE);
433 if (! hdf5_types_compatible (type_hid, range_type))
435 H5Tclose (range_type);
440 hid_t space_hid = H5Dget_space (data_hid);
441 hsize_t rank = H5Sget_simple_extent_ndims (space_hid);
445 H5Tclose (range_type);
446 H5Sclose (space_hid);
452 if (H5Dread (data_hid, range_type, H5S_ALL, H5S_ALL, H5P_DEFAULT,
456 Range r (rangevals[0], rangevals[1], rangevals[2]);
460 H5Tclose (range_type);
461 H5Sclose (space_hid);
470 octave_range::as_mxArray (void) const
472 mxArray *retval = new mxArray (mxDOUBLE_CLASS, dims (), mxREAL);
474 double *pr = static_cast<double *> (retval->get_data ());
476 mwSize nel = numel ();
478 Matrix m = matrix_value ();
480 const double *p = m.data ();
482 for (mwSize i = 0; i < nel; i++)
489 ;;; Local Variables: ***