GNU Octave  4.0.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-2015 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-hdf5.h"
34 #include "oct-obj.h"
35 #include "ops.h"
36 #include "ov-bool.h"
37 #include "ov-bool-mat.h"
38 #include "ov-base.h"
39 #include "ov-base-scalar.h"
40 #include "ov-base-scalar.cc"
41 #include "ov-re-mat.h"
42 #include "ov-scalar.h"
43 #include "pr-output.h"
44 
45 #include "ls-oct-ascii.h"
46 #include "ls-hdf5.h"
47 
48 template class octave_base_scalar<bool>;
49 
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 bool
162 octave_bool::save_hdf5 (octave_hdf5_id loc_id, const char *name,
163  bool /* save_as_floats */)
164 {
165  bool retval = false;
166 
167 #if defined (HAVE_HDF5)
168 
169  hsize_t dimens[3];
170  hid_t space_hid, data_hid;
171  space_hid = data_hid = -1;
172 
173  space_hid = H5Screate_simple (0, dimens, 0);
174  if (space_hid < 0) return false;
175 #if HAVE_HDF5_18
176  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
177  H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
178 #else
179  data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid,
180  H5P_DEFAULT);
181 #endif
182  if (data_hid < 0)
183  {
184  H5Sclose (space_hid);
185  return false;
186  }
187 
188  double tmp = double_value ();
189  retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
190  H5P_DEFAULT, &tmp) >= 0;
191 
192  H5Dclose (data_hid);
193  H5Sclose (space_hid);
194 
195 #else
196  gripe_save ("hdf5");
197 #endif
198 
199  return retval;
200 }
201 
202 bool
203 octave_bool::load_hdf5 (octave_hdf5_id loc_id, const char *name)
204 {
205 #if defined (HAVE_HDF5)
206 
207 #if HAVE_HDF5_18
208  hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT);
209 #else
210  hid_t data_hid = H5Dopen (loc_id, name);
211 #endif
212  hid_t space_id = H5Dget_space (data_hid);
213 
214  hsize_t rank = H5Sget_simple_extent_ndims (space_id);
215 
216  if (rank != 0)
217  {
218  H5Dclose (data_hid);
219  return false;
220  }
221 
222  double dtmp;
223  if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
224  H5P_DEFAULT, &dtmp) < 0)
225  {
226  H5Dclose (data_hid);
227  return false;
228  }
229 
230  scalar = (dtmp != 0.);
231 
232  H5Dclose (data_hid);
233 
234 #else
235  gripe_load ("hdf5");
236 #endif
237 
238  return true;
239 }
240 
241 mxArray *
243 {
244  mxArray *retval = new mxArray (mxLOGICAL_CLASS, 1, 1, mxREAL);
245 
246  bool *pr = static_cast<bool *> (retval->get_data ());
247 
248  pr[0] = scalar;
249 
250  return retval;
251 }
void octave_write_double(std::ostream &os, double d)
Definition: lo-utils.cc:386
type_conv_info numeric_conversion_function(void) const
Definition: ov-bool.cc:62
bool load_ascii(std::istream &is)
Definition: ov-bool.cc:128
void gripe_load(const char *type) const
Definition: ov-base.cc:1258
void error(const char *fmt,...)
Definition: error.cc:476
double double_value(bool=false) const
Definition: ov-bool.h:146
void * get_data(void) const
Definition: mxarray.h:433
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:164
void gripe_save(const char *type) const
Definition: ov-base.cc:1267
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov-bool.cc:70
bool load_binary(std::istream &is, bool swap, oct_mach_info::float_format fmt)
Definition: ov-bool.cc:151
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:361
F77_RET_T const double const double double * d
bool load_hdf5(octave_hdf5_id loc_id, const char *name)
Definition: ov-bool.cc:203
bool save_binary(std::ostream &os, bool &save_as_floats)
Definition: ov-bool.cc:142
#define CAST_CONV_ARG(t)
Definition: ops.h:83
mxArray * as_mxArray(void) const
Definition: ov-bool.cc:242
octave_value convert_to_str_internal(bool pad, bool force, char type) const
Definition: ov-bool.cc:107
static octave_base_value * default_numeric_conversion_function(const octave_base_value &a)
Definition: ov-bool.cc:54
void mxArray
Definition: mex.h:53
friend class octave_value
Definition: ov-base.h:206
boolMatrix bool_matrix_value(bool=false) const
Definition: ov-bool.h:202
bool save_hdf5(octave_hdf5_id loc_id, const char *name, bool save_as_floats)
Definition: ov-bool.cc:162
bool save_ascii(std::ostream &os)
Definition: ov-bool.cc:117
Definition: mxarray.h:52
static int static_type_id(void)
Definition: ov-scalar.h:254
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov-bool.cc:88
octave_value do_index_op(const octave_value_list &idx, bool resize_ok=false)
Definition: ov.h:438