Mercurial > hg > octave-nkf
annotate libinterp/octave-value/ov-bool.cc @ 19669:c2031ad6dbe7
Fix octave header includes in audiodevinfo
* audiodevinfo.cc: change includes to use local octave headers
author | Vytautas Jančauskas <unaudio@gmail.com> |
---|---|
date | Wed, 11 Sep 2013 21:32:14 +0300 |
parents | 2fc554ffbc28 |
children | d63878346099 |
rev | line source |
---|---|
2871 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
3 Copyright (C) 1996-2012 John W. Eaton |
2871 | 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
2871 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
2871 | 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" | |
15149
62a35ae7d6a2
use forward decls for mxArray in ov.h and ov-base.h
John W. Eaton <jwe@octave.org>
parents:
15057
diff
changeset
|
32 #include "mxarray.h" |
2871 | 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 |
5759 | 53 static octave_base_value * |
54 default_numeric_conversion_function (const octave_base_value& a) | |
2871 | 55 { |
56 CAST_CONV_ARG (const octave_bool&); | |
57 | |
58 return new octave_scalar (v.bool_value ()); | |
59 } | |
60 | |
8345
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
61 octave_base_value::type_conv_info |
2871 | 62 octave_bool::numeric_conversion_function (void) const |
63 { | |
8345
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
64 return octave_base_value::type_conv_info (default_numeric_conversion_function, |
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
65 octave_scalar::static_type_id ()); |
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
66 |
2871 | 67 } |
68 | |
69 octave_value | |
5885 | 70 octave_bool::do_index_op (const octave_value_list& idx, bool resize_ok) |
2871 | 71 { |
8437
f00578b495e9
remove valid_as_scalar_index
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
72 // FIXME -- this doesn't solve the problem of |
f00578b495e9
remove valid_as_scalar_index
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
73 // |
f00578b495e9
remove valid_as_scalar_index
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
74 // a = 1; a([1,1], [1,1], [1,1]) |
f00578b495e9
remove valid_as_scalar_index
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
75 // |
f00578b495e9
remove valid_as_scalar_index
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
76 // and similar constructions. Hmm... |
2871 | 77 |
8437
f00578b495e9
remove valid_as_scalar_index
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
78 // FIXME -- using this constructor avoids narrowing the |
f00578b495e9
remove valid_as_scalar_index
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
79 // 1x1 matrix back to a scalar value. Need a better solution |
f00578b495e9
remove valid_as_scalar_index
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
80 // to this problem. |
2871 | 81 |
8437
f00578b495e9
remove valid_as_scalar_index
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
82 octave_value tmp (new octave_bool_matrix (bool_matrix_value ())); |
2871 | 83 |
8437
f00578b495e9
remove valid_as_scalar_index
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
84 return tmp.do_index_op (idx, resize_ok); |
2871 | 85 } |
86 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
87 octave_value |
5731 | 88 octave_bool::resize (const dim_vector& dv, bool fill) const |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
89 { |
5731 | 90 if (fill) |
91 { | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
92 boolNDArray retval (dv, false); |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
93 if (dv.numel ()) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
94 retval(0) = scalar; |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
95 return retval; |
5731 | 96 } |
97 else | |
98 { | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
99 boolNDArray retval (dv); |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
100 if (dv.numel ()) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
101 retval(0) = scalar; |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
102 return retval; |
5731 | 103 } |
104 } | |
105 | |
2871 | 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 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
116 bool |
6974 | 117 octave_bool::save_ascii (std::ostream& os) |
4687 | 118 { |
119 double d = double_value (); | |
120 | |
121 octave_write_double (os, d); | |
122 os << "\n"; | |
123 | |
124 return true; | |
125 } | |
126 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
127 bool |
4687 | 128 octave_bool::load_ascii (std::istream& is) |
129 { | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
130 scalar = (octave_read_value<double> (is) != 0.); |
4687 | 131 |
132 if (!is) | |
133 { | |
134 error ("load: failed to load scalar constant"); | |
135 return false; | |
136 } | |
137 | |
138 return true; | |
139 } | |
140 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
141 bool |
4687 | 142 octave_bool::save_binary (std::ostream& os, bool& /* save_as_floats */) |
143 { | |
144 char tmp = (scalar ? 1 : 0); | |
5760 | 145 os.write (reinterpret_cast<char *> (&tmp), 1); |
4687 | 146 |
147 return true; | |
148 } | |
149 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
150 bool |
4687 | 151 octave_bool::load_binary (std::istream& is, bool /* swap */, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
152 oct_mach_info::float_format /* fmt */) |
4687 | 153 { |
154 char tmp; | |
5760 | 155 if (! is.read (reinterpret_cast<char *> (&tmp), 1)) |
4687 | 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, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
165 bool /* save_as_floats */) |
4687 | 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; |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
173 #if HAVE_HDF5_18 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
174 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
175 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
176 #else |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
177 data_hid = H5Dcreate (loc_id, name, H5T_NATIVE_DOUBLE, space_hid, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
178 H5P_DEFAULT); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
179 #endif |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
180 if (data_hid < 0) |
4687 | 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, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
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 | |
9881
b3089dba88bf
Remove HDF5 cruft for older versions of HDF5
Kacper Kowalik
parents:
9469
diff
changeset
|
197 octave_bool::load_hdf5 (hid_t loc_id, const char *name) |
4687 | 198 { |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
199 #if HAVE_HDF5_18 |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
200 hid_t data_hid = H5Dopen (loc_id, name, H5P_DEFAULT); |
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
201 #else |
4687 | 202 hid_t data_hid = H5Dopen (loc_id, name); |
9892
ac69e6f4b33d
Add HDF5-1.8 compatibility while maintaining compatibility with HDF5-1.6 versions
Kacper Kowalik <xarthisius.kk@gmail.com>
parents:
9881
diff
changeset
|
203 #endif |
4687 | 204 hid_t space_id = H5Dget_space (data_hid); |
205 | |
206 hsize_t rank = H5Sget_simple_extent_ndims (space_id); | |
207 | |
208 if (rank != 0) | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
209 { |
4687 | 210 H5Dclose (data_hid); |
211 return false; | |
212 } | |
213 | |
214 double dtmp; | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
215 if (H5Dread (data_hid, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
216 H5P_DEFAULT, &dtmp) < 0) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
217 { |
4687 | 218 H5Dclose (data_hid); |
219 return false; | |
220 } | |
221 | |
222 scalar = (dtmp != 0.); | |
4837 | 223 |
4687 | 224 H5Dclose (data_hid); |
4837 | 225 |
4687 | 226 return true; |
227 } | |
4944 | 228 |
4687 | 229 #endif |
230 | |
5900 | 231 mxArray * |
232 octave_bool::as_mxArray (void) const | |
233 { | |
234 mxArray *retval = new mxArray (mxLOGICAL_CLASS, 1, 1, mxREAL); | |
235 | |
236 bool *pr = static_cast<bool *> (retval->get_data ()); | |
237 | |
238 pr[0] = scalar; | |
239 | |
240 return retval; | |
241 } |