GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-bool.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <iostream>
28 
29 #include "mx-base.h"
30 
31 #include "gripes.h"
32 #include "mxarray.h"
33 #include "oct-obj.h"
34 #include "ops.h"
35 #include "ov-bool.h"
36 #include "ov-bool-mat.h"
37 #include "ov-base.h"
38 #include "ov-base-scalar.h"
39 #include "ov-base-scalar.cc"
40 #include "ov-re-mat.h"
41 #include "ov-scalar.h"
42 #include "pr-output.h"
43 
44 #include "ls-oct-ascii.h"
45 #include "ls-hdf5.h"
46 
47 template class octave_base_scalar<bool>;
48 
50 
52 
53 static octave_base_value *
55 {
56  CAST_CONV_ARG (const octave_bool&);
57 
58  return new octave_scalar (v.bool_value ());
59 }
60 
63 {
66 
67 }
68 
70 octave_bool::do_index_op (const octave_value_list& idx, bool resize_ok)
71 {
72  // FIXME: this doesn't solve the problem of
73  //
74  // a = 1; a([1,1], [1,1], [1,1])
75  //
76  // and similar constructions. Hmm...
77 
78  // FIXME: using this constructor avoids narrowing the
79  // 1x1 matrix back to a scalar value. Need a better solution
80  // to this problem.
81 
83 
84  return tmp.do_index_op (idx, resize_ok);
85 }
86 
88 octave_bool::resize (const dim_vector& dv, bool fill) const
89 {
90  if (fill)
91  {
92  boolNDArray retval (dv, false);
93  if (dv.numel ())
94  retval(0) = scalar;
95  return retval;
96  }
97  else
98  {
99  boolNDArray retval (dv);
100  if (dv.numel ())
101  retval(0) = scalar;
102  return retval;
103  }
104 }
105 
108 {
109  char s[2];
110  s[0] = static_cast<char> (scalar);
111  s[1] = '\0';
112 
113  return octave_value (s, type);
114 }
115 
116 bool
117 octave_bool::save_ascii (std::ostream& os)
118 {
119  double d = double_value ();
120 
121  octave_write_double (os, d);
122  os << "\n";
123 
124  return true;
125 }
126 
127 bool
128 octave_bool::load_ascii (std::istream& is)
129 {
130  scalar = (octave_read_value<double> (is) != 0.);
131 
132  if (!is)
133  {
134  error ("load: failed to load scalar constant");
135  return false;
136  }
137 
138  return true;
139 }
140 
141 bool
142 octave_bool::save_binary (std::ostream& os, bool& /* save_as_floats */)
143 {
144  char tmp = (scalar ? 1 : 0);
145  os.write (reinterpret_cast<char *> (&tmp), 1);
146 
147  return true;
148 }
149 
150 bool
151 octave_bool::load_binary (std::istream& is, bool /* swap */,
152  oct_mach_info::float_format /* fmt */)
153 {
154  char tmp;
155  if (! is.read (reinterpret_cast<char *> (&tmp), 1))
156  return false;
157  scalar = (tmp ? 1 : 0);
158  return true;
159 }
160 
161 #if defined (HAVE_HDF5)
162 
163 bool
164 octave_bool::save_hdf5 (hid_t loc_id, const char *name,
165  bool /* save_as_floats */)
166 {
167  hsize_t dimens[3];
168  hid_t space_hid = -1, data_hid = -1;
169  bool retval = true;
170 
171  space_hid = H5Screate_simple (0, dimens, 0);
172  if (space_hid < 0) return false;
173 #if HAVE_HDF5_18
174  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
175  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
176 #else
177  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
178  H5P_DEFAULT);
179 #endif
180  if (data_hid < 0)
181  {
182  H5Sclose (space_hid);
183  return false;
184  }
185 
186  double tmp = double_value ();
187  retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
188  H5P_DEFAULT, &tmp) >= 0;
189 
190  H5Dclose (data_hid);
191  H5Sclose (space_hid);
192 
193  return retval;
194 }
195 
196 bool
197 octave_bool::load_hdf5 (hid_t loc_id, const char *name)
198 {
199 #if HAVE_HDF5_18
200  hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT);
201 #else
202  hid_t data_hid = H5Dopen (loc_id, name);
203 #endif
204  hid_t space_id = H5Dget_space (data_hid);
205 
206  hsize_t rank = H5Sget_simple_extent_ndims (space_id);
207 
208  if (rank != 0)
209  {
210  H5Dclose (data_hid);
211  return false;
212  }
213 
214  double dtmp;
215  if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
216  H5P_DEFAULT, &dtmp) < 0)
217  {
218  H5Dclose (data_hid);
219  return false;
220  }
221 
222  scalar = (dtmp != 0.);
223 
224  H5Dclose (data_hid);
225 
226  return true;
227 }
228 
229 #endif
230 
231 mxArray *
233 {
234  mxArray *retval = new mxArray (mxLOGICAL_CLASS, 1, 1, mxREAL);
235 
236  bool *pr = static_cast<bool *> (retval->get_data ());
237 
238  pr[0] = scalar;
239 
240  return retval;
241 }