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