2871
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 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 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. |
2871
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3503
|
28 #include <iostream> |
2901
|
29 |
2871
|
30 #include "mx-base.h" |
|
31 |
|
32 #include "gripes.h" |
|
33 #include "oct-obj.h" |
|
34 #include "ops.h" |
|
35 #include "ov-bool.h" |
4964
|
36 #include "ov-bool-mat.h" |
3223
|
37 #include "ov-base.h" |
|
38 #include "ov-base-scalar.h" |
|
39 #include "ov-base-scalar.cc" |
2871
|
40 #include "ov-re-mat.h" |
|
41 #include "ov-scalar.h" |
|
42 #include "pr-output.h" |
|
43 |
4687
|
44 #include "ls-oct-ascii.h" |
|
45 #include "ls-hdf5.h" |
|
46 |
3223
|
47 template class octave_base_scalar<bool>; |
|
48 |
3219
|
49 DEFINE_OCTAVE_ALLOCATOR (octave_bool); |
2871
|
50 |
4612
|
51 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_bool, "bool", "logical"); |
2871
|
52 |
|
53 static octave_value * |
|
54 default_numeric_conversion_function (const octave_value& a) |
|
55 { |
|
56 CAST_CONV_ARG (const octave_bool&); |
|
57 |
|
58 return new octave_scalar (v.bool_value ()); |
|
59 } |
|
60 |
|
61 type_conv_fcn |
|
62 octave_bool::numeric_conversion_function (void) const |
|
63 { |
|
64 return default_numeric_conversion_function; |
|
65 } |
|
66 |
|
67 static inline bool |
|
68 valid_scalar_indices (const octave_value_list& args) |
|
69 { |
|
70 int nargin = args.length (); |
|
71 |
|
72 for (int i = 0; i < nargin; i++) |
|
73 if (! args(i).valid_as_scalar_index ()) |
|
74 return false; |
|
75 |
|
76 return true; |
|
77 } |
|
78 |
|
79 octave_value |
3933
|
80 octave_bool::do_index_op (const octave_value_list& idx, int resize_ok) |
2871
|
81 { |
|
82 octave_value retval; |
|
83 |
|
84 if (valid_scalar_indices (idx)) |
|
85 retval = scalar; |
|
86 else |
|
87 { |
|
88 // XXX FIXME XXX -- this doesn't solve the problem of |
|
89 // |
|
90 // a = 1; a([1,1], [1,1], [1,1]) |
|
91 // |
|
92 // and similar constructions. Hmm... |
|
93 |
|
94 // XXX FIXME XXX -- using this constructor avoids narrowing the |
|
95 // 1x1 matrix back to a scalar value. Need a better solution |
|
96 // to this problem. |
|
97 |
4964
|
98 octave_value tmp (new octave_bool_matrix (bool_matrix_value ())); |
2871
|
99 |
3933
|
100 retval = tmp.do_index_op (idx, resize_ok); |
2871
|
101 } |
|
102 |
|
103 return retval; |
|
104 } |
|
105 |
5731
|
106 octave_value |
|
107 octave_bool::resize (const dim_vector& dv, bool fill) const |
|
108 { |
|
109 if (fill) |
|
110 { |
|
111 boolNDArray retval (dv, false); |
|
112 if (dv.numel()) |
|
113 retval(0) = scalar; |
|
114 return retval; |
|
115 } |
|
116 else |
|
117 { |
|
118 boolNDArray retval (dv); |
|
119 if (dv.numel()) |
|
120 retval(0) = scalar; |
|
121 return retval; |
|
122 } |
|
123 } |
|
124 |
2871
|
125 octave_value |
5279
|
126 octave_bool::convert_to_str_internal (bool, bool, char type) const |
2871
|
127 { |
|
128 char s[2]; |
4358
|
129 s[0] = static_cast<char> (scalar); |
2871
|
130 s[1] = '\0'; |
|
131 |
5279
|
132 return octave_value (s, type); |
2871
|
133 } |
|
134 |
4687
|
135 bool |
|
136 octave_bool::save_ascii (std::ostream& os, bool& /* infnan_warned */, |
|
137 bool /* strip_nan_and_inf */) |
|
138 { |
|
139 double d = double_value (); |
|
140 |
|
141 octave_write_double (os, d); |
|
142 os << "\n"; |
|
143 |
|
144 return true; |
|
145 } |
|
146 |
|
147 bool |
|
148 octave_bool::load_ascii (std::istream& is) |
|
149 { |
|
150 scalar = (octave_read_double (is) != 0.); |
|
151 |
|
152 if (!is) |
|
153 { |
|
154 error ("load: failed to load scalar constant"); |
|
155 return false; |
|
156 } |
|
157 |
|
158 return true; |
|
159 } |
|
160 |
|
161 bool |
|
162 octave_bool::save_binary (std::ostream& os, bool& /* save_as_floats */) |
|
163 { |
|
164 char tmp = (scalar ? 1 : 0); |
|
165 os.write (X_CAST (char *, &tmp), 1); |
|
166 |
|
167 return true; |
|
168 } |
|
169 |
|
170 bool |
|
171 octave_bool::load_binary (std::istream& is, bool /* swap */, |
|
172 oct_mach_info::float_format /* fmt */) |
|
173 { |
|
174 char tmp; |
|
175 if (! is.read (X_CAST (char *, &tmp), 1)) |
|
176 return false; |
|
177 scalar = (tmp ? 1 : 0); |
|
178 return true; |
|
179 } |
|
180 |
|
181 #if defined (HAVE_HDF5) |
4944
|
182 |
4687
|
183 bool |
|
184 octave_bool::save_hdf5 (hid_t loc_id, const char *name, |
|
185 bool /* save_as_floats */) |
|
186 { |
|
187 hsize_t dimens[3]; |
|
188 hid_t space_hid = -1, data_hid = -1; |
|
189 bool retval = true; |
|
190 |
4815
|
191 space_hid = H5Screate_simple (0, dimens, 0); |
4687
|
192 if (space_hid < 0) return false; |
|
193 |
|
194 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid, |
|
195 H5P_DEFAULT); |
|
196 if (data_hid < 0) |
|
197 { |
|
198 H5Sclose (space_hid); |
|
199 return false; |
|
200 } |
|
201 |
|
202 double tmp = double_value (); |
|
203 retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
4815
|
204 H5P_DEFAULT, &tmp) >= 0; |
4687
|
205 |
|
206 H5Dclose (data_hid); |
|
207 H5Sclose (space_hid); |
4837
|
208 |
4687
|
209 return retval; |
|
210 } |
|
211 |
|
212 bool |
|
213 octave_bool::load_hdf5 (hid_t loc_id, const char *name, |
|
214 bool /* have_h5giterate_bug */) |
|
215 { |
|
216 hid_t data_hid = H5Dopen (loc_id, name); |
|
217 hid_t space_id = H5Dget_space (data_hid); |
|
218 |
|
219 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
220 |
|
221 if (rank != 0) |
|
222 { |
|
223 H5Dclose (data_hid); |
|
224 return false; |
|
225 } |
|
226 |
|
227 double dtmp; |
|
228 if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
4815
|
229 H5P_DEFAULT, &dtmp) < 0) |
4687
|
230 { |
|
231 H5Dclose (data_hid); |
|
232 return false; |
|
233 } |
|
234 |
|
235 scalar = (dtmp != 0.); |
4837
|
236 |
4687
|
237 H5Dclose (data_hid); |
4837
|
238 |
4687
|
239 return true; |
|
240 } |
4944
|
241 |
4687
|
242 #endif |
|
243 |
2871
|
244 /* |
|
245 ;;; Local Variables: *** |
|
246 ;;; mode: C++ *** |
|
247 ;;; End: *** |
|
248 */ |