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" |
3223
|
39 #include "ov-base.h" |
|
40 #include "ov-base-scalar.h" |
|
41 #include "ov-base-scalar.cc" |
2871
|
42 #include "ov-re-mat.h" |
|
43 #include "ov-scalar.h" |
|
44 #include "pr-output.h" |
|
45 |
4687
|
46 #include "ls-oct-ascii.h" |
|
47 #include "ls-hdf5.h" |
|
48 |
3223
|
49 template class octave_base_scalar<bool>; |
|
50 |
3219
|
51 DEFINE_OCTAVE_ALLOCATOR (octave_bool); |
2871
|
52 |
4612
|
53 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_bool, "bool", "logical"); |
2871
|
54 |
|
55 static octave_value * |
|
56 default_numeric_conversion_function (const octave_value& a) |
|
57 { |
|
58 CAST_CONV_ARG (const octave_bool&); |
|
59 |
|
60 return new octave_scalar (v.bool_value ()); |
|
61 } |
|
62 |
|
63 type_conv_fcn |
|
64 octave_bool::numeric_conversion_function (void) const |
|
65 { |
|
66 return default_numeric_conversion_function; |
|
67 } |
|
68 |
|
69 static inline bool |
|
70 valid_scalar_indices (const octave_value_list& args) |
|
71 { |
|
72 int nargin = args.length (); |
|
73 |
|
74 for (int i = 0; i < nargin; i++) |
|
75 if (! args(i).valid_as_scalar_index ()) |
|
76 return false; |
|
77 |
|
78 return true; |
|
79 } |
|
80 |
|
81 octave_value |
3933
|
82 octave_bool::do_index_op (const octave_value_list& idx, int resize_ok) |
2871
|
83 { |
|
84 octave_value retval; |
|
85 |
|
86 if (valid_scalar_indices (idx)) |
|
87 retval = scalar; |
|
88 else |
|
89 { |
|
90 // XXX FIXME XXX -- this doesn't solve the problem of |
|
91 // |
|
92 // a = 1; a([1,1], [1,1], [1,1]) |
|
93 // |
|
94 // and similar constructions. Hmm... |
|
95 |
|
96 // XXX FIXME XXX -- using this constructor avoids narrowing the |
|
97 // 1x1 matrix back to a scalar value. Need a better solution |
|
98 // to this problem. |
|
99 |
|
100 octave_value tmp (new octave_matrix (matrix_value ())); |
|
101 |
3933
|
102 retval = tmp.do_index_op (idx, resize_ok); |
2871
|
103 } |
|
104 |
|
105 return retval; |
|
106 } |
|
107 |
|
108 octave_value |
4457
|
109 octave_bool::convert_to_str_internal (bool, bool) const |
2871
|
110 { |
|
111 char s[2]; |
4358
|
112 s[0] = static_cast<char> (scalar); |
2871
|
113 s[1] = '\0'; |
|
114 |
|
115 return octave_value (s); |
|
116 } |
|
117 |
4687
|
118 bool |
|
119 octave_bool::save_ascii (std::ostream& os, bool& /* infnan_warned */, |
|
120 bool /* strip_nan_and_inf */) |
|
121 { |
|
122 double d = double_value (); |
|
123 |
|
124 octave_write_double (os, d); |
|
125 os << "\n"; |
|
126 |
|
127 return true; |
|
128 } |
|
129 |
|
130 bool |
|
131 octave_bool::load_ascii (std::istream& is) |
|
132 { |
|
133 scalar = (octave_read_double (is) != 0.); |
|
134 |
|
135 if (!is) |
|
136 { |
|
137 error ("load: failed to load scalar constant"); |
|
138 return false; |
|
139 } |
|
140 |
|
141 return true; |
|
142 } |
|
143 |
|
144 bool |
|
145 octave_bool::save_binary (std::ostream& os, bool& /* save_as_floats */) |
|
146 { |
|
147 char tmp = (scalar ? 1 : 0); |
|
148 os.write (X_CAST (char *, &tmp), 1); |
|
149 |
|
150 return true; |
|
151 } |
|
152 |
|
153 bool |
|
154 octave_bool::load_binary (std::istream& is, bool /* swap */, |
|
155 oct_mach_info::float_format /* fmt */) |
|
156 { |
|
157 char tmp; |
|
158 if (! is.read (X_CAST (char *, &tmp), 1)) |
|
159 return false; |
|
160 scalar = (tmp ? 1 : 0); |
|
161 return true; |
|
162 } |
|
163 |
|
164 #if defined (HAVE_HDF5) |
|
165 bool |
|
166 octave_bool::save_hdf5 (hid_t loc_id, const char *name, |
|
167 bool /* save_as_floats */) |
|
168 { |
|
169 hsize_t dimens[3]; |
|
170 hid_t space_hid = -1, data_hid = -1; |
|
171 bool retval = true; |
|
172 |
4815
|
173 space_hid = H5Screate_simple (0, dimens, 0); |
4687
|
174 if (space_hid < 0) return false; |
|
175 |
|
176 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid, |
|
177 H5P_DEFAULT); |
|
178 if (data_hid < 0) |
|
179 { |
|
180 H5Sclose (space_hid); |
|
181 return false; |
|
182 } |
|
183 |
|
184 double tmp = double_value (); |
|
185 retval = H5Dwrite (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
4815
|
186 H5P_DEFAULT, &tmp) >= 0; |
4687
|
187 |
|
188 H5Dclose (data_hid); |
|
189 H5Sclose (space_hid); |
|
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.); |
|
217 H5Dclose (data_hid); |
|
218 return true; |
|
219 } |
|
220 #endif |
|
221 |
2871
|
222 /* |
|
223 ;;; Local Variables: *** |
|
224 ;;; mode: C++ *** |
|
225 ;;; End: *** |
|
226 */ |