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 |
|
106 octave_value |
5279
|
107 octave_bool::convert_to_str_internal (bool, bool, char type) const |
2871
|
108 { |
|
109 char s[2]; |
4358
|
110 s[0] = static_cast<char> (scalar); |
2871
|
111 s[1] = '\0'; |
|
112 |
5279
|
113 return octave_value (s, type); |
2871
|
114 } |
|
115 |
4687
|
116 bool |
|
117 octave_bool::save_ascii (std::ostream& os, bool& /* infnan_warned */, |
|
118 bool /* strip_nan_and_inf */) |
|
119 { |
|
120 double d = double_value (); |
|
121 |
|
122 octave_write_double (os, d); |
|
123 os << "\n"; |
|
124 |
|
125 return true; |
|
126 } |
|
127 |
|
128 bool |
|
129 octave_bool::load_ascii (std::istream& is) |
|
130 { |
|
131 scalar = (octave_read_double (is) != 0.); |
|
132 |
|
133 if (!is) |
|
134 { |
|
135 error ("load: failed to load scalar constant"); |
|
136 return false; |
|
137 } |
|
138 |
|
139 return true; |
|
140 } |
|
141 |
|
142 bool |
|
143 octave_bool::save_binary (std::ostream& os, bool& /* save_as_floats */) |
|
144 { |
|
145 char tmp = (scalar ? 1 : 0); |
|
146 os.write (X_CAST (char *, &tmp), 1); |
|
147 |
|
148 return true; |
|
149 } |
|
150 |
|
151 bool |
|
152 octave_bool::load_binary (std::istream& is, bool /* swap */, |
|
153 oct_mach_info::float_format /* fmt */) |
|
154 { |
|
155 char tmp; |
|
156 if (! is.read (X_CAST (char *, &tmp), 1)) |
|
157 return false; |
|
158 scalar = (tmp ? 1 : 0); |
|
159 return true; |
|
160 } |
|
161 |
|
162 #if defined (HAVE_HDF5) |
4944
|
163 |
4687
|
164 bool |
|
165 octave_bool::save_hdf5 (hid_t loc_id, const char *name, |
|
166 bool /* save_as_floats */) |
|
167 { |
|
168 hsize_t dimens[3]; |
|
169 hid_t space_hid = -1, data_hid = -1; |
|
170 bool retval = true; |
|
171 |
4815
|
172 space_hid = H5Screate_simple (0, dimens, 0); |
4687
|
173 if (space_hid < 0) return false; |
|
174 |
|
175 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid, |
|
176 H5P_DEFAULT); |
|
177 if (data_hid < 0) |
|
178 { |
|
179 H5Sclose (space_hid); |
|
180 return false; |
|
181 } |
|
182 |
|
183 double tmp = double_value (); |
|
184 retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
4815
|
185 H5P_DEFAULT, &tmp) >= 0; |
4687
|
186 |
|
187 H5Dclose (data_hid); |
|
188 H5Sclose (space_hid); |
4837
|
189 |
4687
|
190 return retval; |
|
191 } |
|
192 |
|
193 bool |
|
194 octave_bool::load_hdf5 (hid_t loc_id, const char *name, |
|
195 bool /* have_h5giterate_bug */) |
|
196 { |
|
197 hid_t data_hid = H5Dopen (loc_id, name); |
|
198 hid_t space_id = H5Dget_space (data_hid); |
|
199 |
|
200 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
201 |
|
202 if (rank != 0) |
|
203 { |
|
204 H5Dclose (data_hid); |
|
205 return false; |
|
206 } |
|
207 |
|
208 double dtmp; |
|
209 if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
4815
|
210 H5P_DEFAULT, &dtmp) < 0) |
4687
|
211 { |
|
212 H5Dclose (data_hid); |
|
213 return false; |
|
214 } |
|
215 |
|
216 scalar = (dtmp != 0.); |
4837
|
217 |
4687
|
218 H5Dclose (data_hid); |
4837
|
219 |
4687
|
220 return true; |
|
221 } |
4944
|
222 |
4687
|
223 #endif |
|
224 |
2871
|
225 /* |
|
226 ;;; Local Variables: *** |
|
227 ;;; mode: C++ *** |
|
228 ;;; End: *** |
|
229 */ |