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 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
3503
|
27 #include <iostream> |
2901
|
28 |
2871
|
29 #include "mx-base.h" |
|
30 |
|
31 #include "gripes.h" |
|
32 #include "oct-obj.h" |
|
33 #include "ops.h" |
|
34 #include "ov-bool.h" |
4964
|
35 #include "ov-bool-mat.h" |
3223
|
36 #include "ov-base.h" |
|
37 #include "ov-base-scalar.h" |
|
38 #include "ov-base-scalar.cc" |
2871
|
39 #include "ov-re-mat.h" |
|
40 #include "ov-scalar.h" |
|
41 #include "pr-output.h" |
|
42 |
4687
|
43 #include "ls-oct-ascii.h" |
|
44 #include "ls-hdf5.h" |
|
45 |
3223
|
46 template class octave_base_scalar<bool>; |
|
47 |
3219
|
48 DEFINE_OCTAVE_ALLOCATOR (octave_bool); |
2871
|
49 |
4612
|
50 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_bool, "bool", "logical"); |
2871
|
51 |
|
52 static octave_value * |
|
53 default_numeric_conversion_function (const octave_value& a) |
|
54 { |
|
55 CAST_CONV_ARG (const octave_bool&); |
|
56 |
|
57 return new octave_scalar (v.bool_value ()); |
|
58 } |
|
59 |
|
60 type_conv_fcn |
|
61 octave_bool::numeric_conversion_function (void) const |
|
62 { |
|
63 return default_numeric_conversion_function; |
|
64 } |
|
65 |
|
66 static inline bool |
|
67 valid_scalar_indices (const octave_value_list& args) |
|
68 { |
|
69 int nargin = args.length (); |
|
70 |
|
71 for (int i = 0; i < nargin; i++) |
|
72 if (! args(i).valid_as_scalar_index ()) |
|
73 return false; |
|
74 |
|
75 return true; |
|
76 } |
|
77 |
|
78 octave_value |
3933
|
79 octave_bool::do_index_op (const octave_value_list& idx, int resize_ok) |
2871
|
80 { |
|
81 octave_value retval; |
|
82 |
|
83 if (valid_scalar_indices (idx)) |
|
84 retval = scalar; |
|
85 else |
|
86 { |
|
87 // XXX FIXME XXX -- this doesn't solve the problem of |
|
88 // |
|
89 // a = 1; a([1,1], [1,1], [1,1]) |
|
90 // |
|
91 // and similar constructions. Hmm... |
|
92 |
|
93 // XXX FIXME XXX -- using this constructor avoids narrowing the |
|
94 // 1x1 matrix back to a scalar value. Need a better solution |
|
95 // to this problem. |
|
96 |
4964
|
97 octave_value tmp (new octave_bool_matrix (bool_matrix_value ())); |
2871
|
98 |
3933
|
99 retval = tmp.do_index_op (idx, resize_ok); |
2871
|
100 } |
|
101 |
|
102 return retval; |
|
103 } |
|
104 |
|
105 octave_value |
5279
|
106 octave_bool::convert_to_str_internal (bool, bool, char type) const |
2871
|
107 { |
|
108 char s[2]; |
4358
|
109 s[0] = static_cast<char> (scalar); |
2871
|
110 s[1] = '\0'; |
|
111 |
5279
|
112 return octave_value (s, type); |
2871
|
113 } |
|
114 |
4687
|
115 bool |
|
116 octave_bool::save_ascii (std::ostream& os, bool& /* infnan_warned */, |
|
117 bool /* strip_nan_and_inf */) |
|
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_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 (X_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 (X_CAST (char *, &tmp), 1)) |
|
156 return false; |
|
157 scalar = (tmp ? 1 : 0); |
|
158 return true; |
|
159 } |
|
160 |
|
161 #if defined (HAVE_HDF5) |
4944
|
162 |
4687
|
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 |
4815
|
171 space_hid = H5Screate_simple (0, dimens, 0); |
4687
|
172 if (space_hid < 0) return false; |
|
173 |
|
174 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid, |
|
175 H5P_DEFAULT); |
|
176 if (data_hid < 0) |
|
177 { |
|
178 H5Sclose (space_hid); |
|
179 return false; |
|
180 } |
|
181 |
|
182 double tmp = double_value (); |
|
183 retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
4815
|
184 H5P_DEFAULT, &tmp) >= 0; |
4687
|
185 |
|
186 H5Dclose (data_hid); |
|
187 H5Sclose (space_hid); |
4837
|
188 |
4687
|
189 return retval; |
|
190 } |
|
191 |
|
192 bool |
|
193 octave_bool::load_hdf5 (hid_t loc_id, const char *name, |
|
194 bool /* have_h5giterate_bug */) |
|
195 { |
|
196 hid_t data_hid = H5Dopen (loc_id, name); |
|
197 hid_t space_id = H5Dget_space (data_hid); |
|
198 |
|
199 hsize_t rank = H5Sget_simple_extent_ndims (space_id); |
|
200 |
|
201 if (rank != 0) |
|
202 { |
|
203 H5Dclose (data_hid); |
|
204 return false; |
|
205 } |
|
206 |
|
207 double dtmp; |
|
208 if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
4815
|
209 H5P_DEFAULT, &dtmp) < 0) |
4687
|
210 { |
|
211 H5Dclose (data_hid); |
|
212 return false; |
|
213 } |
|
214 |
|
215 scalar = (dtmp != 0.); |
4837
|
216 |
4687
|
217 H5Dclose (data_hid); |
4837
|
218 |
4687
|
219 return true; |
|
220 } |
4944
|
221 |
4687
|
222 #endif |
|
223 |
2871
|
224 /* |
|
225 ;;; Local Variables: *** |
|
226 ;;; mode: C++ *** |
|
227 ;;; End: *** |
|
228 */ |