Mercurial > octave-nkf
annotate src/ov-base-int.cc @ 7961:a5d1e27ee1f4 ss-3-1-51
3.1.51 snapshot
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 22 Jul 2008 11:40:48 -0400 |
parents | ef755c763b62 |
children | 25bc2d31e1bf |
rev | line source |
---|---|
4903 | 1 /* |
2 | |
7017 | 3 Copyright (C) 2004, 2005, 2006, 2007 John W. Eaton |
4903 | 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. | |
4903 | 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/>. | |
4903 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
27 #include <climits> | |
28 | |
29 #include <iostream> | |
30 #include <vector> | |
31 | |
32 #include "lo-ieee.h" | |
33 #include "lo-utils.h" | |
34 #include "mx-base.h" | |
35 #include "quit.h" | |
36 | |
37 #include "defun.h" | |
38 #include "gripes.h" | |
39 #include "oct-obj.h" | |
40 #include "oct-lvalue.h" | |
4944 | 41 #include "oct-stream.h" |
4903 | 42 #include "ops.h" |
43 #include "ov-base.h" | |
44 #include "ov-base-mat.h" | |
45 #include "ov-base-mat.cc" | |
46 #include "ov-base-scalar.h" | |
47 #include "ov-base-scalar.cc" | |
48 #include "ov-base-int.h" | |
49 #include "ov-int-traits.h" | |
50 #include "pr-output.h" | |
51 #include "variables.h" | |
52 | |
53 #include "byte-swap.h" | |
54 #include "ls-oct-ascii.h" | |
55 #include "ls-utils.h" | |
56 #include "ls-hdf5.h" | |
57 | |
7534
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
58 // We have all the machinery below (octave_base_int_helper and |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
59 // octave_base_int_helper_traits) to avoid a few warnings from GCC |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
60 // about comparisons always false due to limited range of data types. |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
61 // Ugh. The cure may be worse than the disease. |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
62 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
63 template <class T, bool is_signed = true, bool can_be_too_big = true> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
64 struct octave_base_int_helper |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
65 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
66 static bool |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
67 char_value_out_of_range (T val) { return val < 0 || val > UCHAR_MAX; } |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
68 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
69 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
70 template <class T> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
71 struct octave_base_int_helper<T, false, false> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
72 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
73 static bool char_value_out_of_range (T) { return true; } |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
74 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
75 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
76 template <class T> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
77 struct octave_base_int_helper<T, false, true> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
78 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
79 static bool char_value_out_of_range (T val) { return val > UCHAR_MAX; } |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
80 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
81 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
82 template <class T> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
83 struct octave_base_int_helper<T, true, false> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
84 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
85 static bool char_value_out_of_range (T val) { return val < 0; } |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
86 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
87 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
88 // For all types other than char, signed char, and unsigned char, we |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
89 // assume that the upper limit for the range of allowable values is |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
90 // larger than the range for unsigned char. If that's not true, we |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
91 // are still OK, but will see the warnings again for any other types |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
92 // that do not meet this assumption. |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
93 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
94 template <class T> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
95 struct octave_base_int_helper_traits |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
96 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
97 static const bool can_be_larger_than_uchar_max = true; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
98 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
99 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
100 template <> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
101 struct octave_base_int_helper_traits<char> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
102 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
103 static const bool can_be_larger_than_uchar_max = false; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
104 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
105 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
106 template <> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
107 struct octave_base_int_helper_traits<signed char> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
108 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
109 static const bool can_be_larger_than_uchar_max = false; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
110 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
111 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
112 template <> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
113 struct octave_base_int_helper_traits<unsigned char> |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
114 { |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
115 static const bool can_be_larger_than_uchar_max = false; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
116 }; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
117 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
118 |
4903 | 119 template <class T> |
5759 | 120 octave_base_value * |
4903 | 121 octave_base_int_matrix<T>::try_narrowing_conversion (void) |
122 { | |
5759 | 123 octave_base_value *retval = 0; |
4903 | 124 |
4932 | 125 if (this->matrix.nelem () == 1) |
126 retval = new typename octave_value_int_traits<T>::scalar_type (this->matrix (0)); | |
4903 | 127 |
128 return retval; | |
129 } | |
130 | |
131 template <class T> | |
5992 | 132 octave_value |
133 octave_base_int_matrix<T>::convert_to_str_internal (bool, bool, char type) const | |
134 { | |
135 octave_value retval; | |
136 dim_vector dv = this->dims (); | |
137 octave_idx_type nel = dv.numel (); | |
138 | |
139 charNDArray chm (dv); | |
140 | |
141 bool warned = false; | |
142 | |
143 for (octave_idx_type i = 0; i < nel; i++) | |
144 { | |
145 OCTAVE_QUIT; | |
146 | |
147 typename T::elt_type tmp = this->matrix(i); | |
148 | |
7534
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
149 typedef typename T::elt_type::val_type val_type; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
150 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
151 val_type ival = tmp.value (); |
5992 | 152 |
7534
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
153 static const bool is_signed = std::numeric_limits<val_type>::is_signed; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
154 static const bool can_be_larger_than_uchar_max |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
155 = octave_base_int_helper_traits<val_type>::can_be_larger_than_uchar_max; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
156 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
157 if (octave_base_int_helper<val_type, is_signed, |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
158 can_be_larger_than_uchar_max>::char_value_out_of_range (ival)) |
5992 | 159 { |
160 // FIXME -- is there something better we could do? | |
161 | |
162 ival = 0; | |
163 | |
164 if (! warned) | |
165 { | |
166 ::warning ("range error for conversion to character value"); | |
167 warned = true; | |
168 } | |
169 } | |
170 else | |
171 chm (i) = static_cast<char> (ival); | |
172 } | |
173 | |
174 retval = octave_value (chm, true, type); | |
175 | |
176 return retval; | |
177 } | |
178 | |
179 template <class T> | |
4903 | 180 bool |
6974 | 181 octave_base_int_matrix<T>::save_ascii (std::ostream& os) |
4903 | 182 { |
4932 | 183 dim_vector d = this->dims (); |
4903 | 184 |
185 os << "# ndims: " << d.length () << "\n"; | |
186 | |
187 for (int i = 0; i < d.length (); i++) | |
188 os << " " << d (i); | |
189 | |
4932 | 190 os << "\n" << this->matrix; |
4903 | 191 |
192 return true; | |
193 } | |
194 | |
195 template <class T> | |
196 bool | |
197 octave_base_int_matrix<T>::load_ascii (std::istream& is) | |
198 { | |
199 int mdims = 0; | |
200 bool success = true; | |
201 | |
202 if (extract_keyword (is, "ndims", mdims, true)) | |
203 { | |
204 if (mdims >= 0) | |
205 { | |
206 dim_vector dv; | |
207 dv.resize (mdims); | |
208 | |
209 for (int i = 0; i < mdims; i++) | |
210 is >> dv(i); | |
211 | |
212 T tmp(dv); | |
213 | |
214 is >> tmp; | |
215 | |
216 if (!is) | |
217 { | |
218 error ("load: failed to load matrix constant"); | |
219 success = false; | |
220 } | |
221 | |
4932 | 222 this->matrix = tmp; |
4903 | 223 } |
224 else | |
225 { | |
226 error ("load: failed to extract number of rows and columns"); | |
227 success = false; | |
228 } | |
229 } | |
230 else | |
231 error ("load: failed to extract number of dimensions"); | |
232 | |
233 return success; | |
234 } | |
235 | |
236 template <class T> | |
237 bool | |
4917 | 238 octave_base_int_matrix<T>::save_binary (std::ostream& os, bool&) |
4903 | 239 { |
4932 | 240 dim_vector d = this->dims (); |
4903 | 241 if (d.length() < 1) |
242 return false; | |
243 | |
244 // Use negative value for ndims to differentiate with old format!! | |
5828 | 245 int32_t tmp = - d.length(); |
5760 | 246 os.write (reinterpret_cast<char *> (&tmp), 4); |
4903 | 247 for (int i=0; i < d.length (); i++) |
248 { | |
249 tmp = d(i); | |
5760 | 250 os.write (reinterpret_cast<char *> (&tmp), 4); |
4903 | 251 } |
252 | |
5760 | 253 os.write (reinterpret_cast<const char *> (this->matrix.data()), this->byte_size()); |
4903 | 254 |
255 return true; | |
256 } | |
257 | |
258 template <class T> | |
259 bool | |
260 octave_base_int_matrix<T>::load_binary (std::istream& is, bool swap, | |
4917 | 261 oct_mach_info::float_format ) |
4903 | 262 { |
5828 | 263 int32_t mdims; |
5760 | 264 if (! is.read (reinterpret_cast<char *> (&mdims), 4)) |
4903 | 265 return false; |
266 if (swap) | |
4944 | 267 swap_bytes<4> (&mdims); |
4917 | 268 if (mdims >= 0) |
269 return false; | |
4903 | 270 |
4917 | 271 mdims = - mdims; |
5828 | 272 int32_t di; |
4917 | 273 dim_vector dv; |
274 dv.resize (mdims); | |
4903 | 275 |
4917 | 276 for (int i = 0; i < mdims; i++) |
4903 | 277 { |
5760 | 278 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4903 | 279 return false; |
280 if (swap) | |
4944 | 281 swap_bytes<4> (&di); |
4917 | 282 dv(i) = di; |
4903 | 283 } |
284 | |
5157 | 285 // Convert an array with a single dimension to be a row vector. |
286 // Octave should never write files like this, other software | |
287 // might. | |
288 | |
289 if (mdims == 1) | |
290 { | |
291 mdims = 2; | |
292 dv.resize (mdims); | |
293 dv(1) = dv(0); | |
294 dv(0) = 1; | |
295 } | |
296 | |
4917 | 297 T m (dv); |
298 | |
5760 | 299 if (! is.read (reinterpret_cast<char *> (m.fortran_vec ()), m.byte_size ())) |
4917 | 300 return false; |
4903 | 301 |
4917 | 302 if (swap) |
303 { | |
304 int nel = dv.numel (); | |
305 int bytes = nel / m.byte_size(); | |
306 for (int i = 0; i < nel; i++) | |
307 switch (bytes) | |
308 { | |
309 case 8: | |
4944 | 310 swap_bytes<8> (&m(i)); |
4917 | 311 break; |
312 case 4: | |
4944 | 313 swap_bytes<4> (&m(i)); |
4917 | 314 break; |
315 case 2: | |
4944 | 316 swap_bytes<2> (&m(i)); |
4917 | 317 break; |
318 case 1: | |
319 default: | |
320 break; | |
321 } | |
322 } | |
323 | |
4932 | 324 this->matrix = m; |
4903 | 325 return true; |
326 } | |
327 | |
328 #if defined (HAVE_HDF5) | |
329 | |
330 template <class T> | |
331 bool | |
4917 | 332 octave_base_int_matrix<T>::save_hdf5 (hid_t loc_id, const char *name, bool) |
4903 | 333 { |
4917 | 334 hid_t save_type_hid = HDF5_SAVE_TYPE; |
4903 | 335 bool retval = true; |
4932 | 336 dim_vector dv = this->dims (); |
4903 | 337 int empty = save_hdf5_empty (loc_id, name, dv); |
338 if (empty) | |
339 return (empty > 0); | |
340 | |
341 int rank = dv.length (); | |
342 hid_t space_hid = -1, data_hid = -1; | |
343 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
344 | |
345 // Octave uses column-major, while HDF5 uses row-major ordering | |
346 for (int i = 0; i < rank; i++) | |
347 hdims[i] = dv (rank-i-1); | |
348 | |
349 space_hid = H5Screate_simple (rank, hdims, 0); | |
350 | |
351 if (space_hid < 0) return false; | |
352 | |
353 data_hid = H5Dcreate (loc_id, name, save_type_hid, space_hid, | |
354 H5P_DEFAULT); | |
355 if (data_hid < 0) | |
356 { | |
357 H5Sclose (space_hid); | |
358 return false; | |
359 } | |
360 | |
4917 | 361 retval = H5Dwrite (data_hid, save_type_hid, H5S_ALL, H5S_ALL, |
4932 | 362 H5P_DEFAULT, this->matrix.data()) >= 0; |
4903 | 363 |
364 H5Dclose (data_hid); | |
365 H5Sclose (space_hid); | |
366 | |
367 return retval; | |
368 } | |
369 | |
370 template <class T> | |
371 bool | |
372 octave_base_int_matrix<T>::load_hdf5 (hid_t loc_id, const char *name, | |
373 bool /* have_h5giterate_bug */) | |
374 { | |
4917 | 375 hid_t save_type_hid = HDF5_SAVE_TYPE; |
4903 | 376 bool retval = false; |
377 dim_vector dv; | |
378 int empty = load_hdf5_empty (loc_id, name, dv); | |
379 if (empty > 0) | |
4932 | 380 this->matrix.resize(dv); |
4903 | 381 if (empty) |
382 return (empty > 0); | |
383 | |
384 hid_t data_hid = H5Dopen (loc_id, name); | |
385 hid_t space_id = H5Dget_space (data_hid); | |
386 | |
387 hsize_t rank = H5Sget_simple_extent_ndims (space_id); | |
388 | |
389 if (rank < 1) | |
390 { | |
391 H5Sclose (space_id); | |
392 H5Dclose (data_hid); | |
393 return false; | |
394 } | |
395 | |
396 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
397 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
398 | |
399 H5Sget_simple_extent_dims (space_id, hdims, maxdims); | |
400 | |
401 // Octave uses column-major, while HDF5 uses row-major ordering | |
402 if (rank == 1) | |
403 { | |
404 dv.resize (2); | |
405 dv(0) = 1; | |
406 dv(1) = hdims[0]; | |
407 } | |
408 else | |
409 { | |
410 dv.resize (rank); | |
411 for (hsize_t i = 0, j = rank - 1; i < rank; i++, j--) | |
412 dv(j) = hdims[i]; | |
413 } | |
414 | |
4917 | 415 T m (dv); |
416 if (H5Dread (data_hid, save_type_hid, H5S_ALL, H5S_ALL, | |
417 H5P_DEFAULT, m.fortran_vec()) >= 0) | |
4903 | 418 { |
419 retval = true; | |
4932 | 420 this->matrix = m; |
4903 | 421 } |
422 | |
423 H5Sclose (space_id); | |
424 H5Dclose (data_hid); | |
425 | |
426 return retval; | |
427 } | |
428 | |
429 #endif | |
430 | |
431 template <class T> | |
432 void | |
433 octave_base_int_matrix<T>::print_raw (std::ostream& os, | |
434 bool pr_as_read_syntax) const | |
435 { | |
4932 | 436 octave_print_internal (os, this->matrix, pr_as_read_syntax, |
437 this->current_print_indent_level ()); | |
4903 | 438 } |
439 | |
440 template <class T> | |
5992 | 441 octave_value |
442 octave_base_int_scalar<T>::convert_to_str_internal (bool, bool, char type) const | |
443 { | |
444 octave_value retval; | |
445 | |
446 T tmp = this->scalar; | |
447 | |
7534
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
448 typedef typename T::val_type val_type; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
449 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
450 val_type ival = tmp.value (); |
5992 | 451 |
7534
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
452 static const bool is_signed = std::numeric_limits<val_type>::is_signed; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
453 static const bool can_be_larger_than_uchar_max |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
454 = octave_base_int_helper_traits<val_type>::can_be_larger_than_uchar_max; |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
455 |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
456 if (octave_base_int_helper<val_type, is_signed, |
ef755c763b62
avoid more "comparison is always false due to limited range of data type" warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
457 can_be_larger_than_uchar_max>::char_value_out_of_range (ival)) |
5992 | 458 { |
459 // FIXME -- is there something better we could do? | |
460 | |
461 ival = 0; | |
462 | |
463 ::warning ("range error for conversion to character value"); | |
464 } | |
465 else | |
466 retval = octave_value (std::string (1, static_cast<char> (ival)), type); | |
467 | |
468 return retval; | |
469 } | |
470 | |
471 template <class T> | |
4903 | 472 bool |
6974 | 473 octave_base_int_scalar<T>::save_ascii (std::ostream& os) |
4903 | 474 { |
4932 | 475 os << this->scalar << "\n"; |
4903 | 476 return true; |
477 } | |
478 | |
479 template <class T> | |
480 bool | |
481 octave_base_int_scalar<T>::load_ascii (std::istream& is) | |
482 { | |
4932 | 483 is >> this->scalar; |
4903 | 484 if (!is) |
485 { | |
486 error ("load: failed to load scalar constant"); | |
487 return false; | |
488 } | |
489 return true; | |
490 } | |
491 | |
492 template <class T> | |
493 bool | |
4917 | 494 octave_base_int_scalar<T>::save_binary (std::ostream& os, bool&) |
4903 | 495 { |
5760 | 496 os.write (reinterpret_cast<char *> (&(this->scalar)), this->byte_size()); |
4903 | 497 return true; |
498 } | |
499 | |
500 template <class T> | |
501 bool | |
502 octave_base_int_scalar<T>::load_binary (std::istream& is, bool swap, | |
4917 | 503 oct_mach_info::float_format) |
4903 | 504 { |
4917 | 505 T tmp; |
5760 | 506 if (! is.read (reinterpret_cast<char *> (&tmp), this->byte_size())) |
4903 | 507 return false; |
508 | |
4917 | 509 if (swap) |
4932 | 510 switch (this->byte_size()) |
4917 | 511 { |
512 case 8: | |
4944 | 513 swap_bytes<8> (&tmp); |
4917 | 514 break; |
515 case 4: | |
4944 | 516 swap_bytes<4> (&tmp); |
4917 | 517 break; |
518 case 2: | |
4944 | 519 swap_bytes<2> (&tmp); |
4917 | 520 break; |
521 case 1: | |
522 default: | |
523 break; | |
524 } | |
4932 | 525 this->scalar = tmp; |
4903 | 526 return true; |
527 } | |
528 | |
529 #if defined (HAVE_HDF5) | |
4944 | 530 |
4903 | 531 template <class T> |
532 bool | |
4917 | 533 octave_base_int_scalar<T>::save_hdf5 (hid_t loc_id, const char *name, bool) |
4903 | 534 { |
4917 | 535 hid_t save_type_hid = HDF5_SAVE_TYPE; |
4903 | 536 bool retval = true; |
537 hsize_t dimens[3]; | |
538 hid_t space_hid = -1, data_hid = -1; | |
539 | |
540 space_hid = H5Screate_simple (0, dimens, 0); | |
541 if (space_hid < 0) return false; | |
542 | |
4917 | 543 data_hid = H5Dcreate (loc_id, name, save_type_hid, space_hid, |
4903 | 544 H5P_DEFAULT); |
545 if (data_hid < 0) | |
546 { | |
547 H5Sclose (space_hid); | |
548 return false; | |
549 } | |
550 | |
4917 | 551 retval = H5Dwrite (data_hid, save_type_hid, H5S_ALL, H5S_ALL, |
4932 | 552 H5P_DEFAULT, &(this->scalar)) >= 0; |
4903 | 553 |
554 H5Dclose (data_hid); | |
555 H5Sclose (space_hid); | |
556 | |
557 return retval; | |
558 } | |
559 | |
560 template <class T> | |
561 bool | |
562 octave_base_int_scalar<T>::load_hdf5 (hid_t loc_id, const char *name, | |
563 bool /* have_h5giterate_bug */) | |
564 { | |
4917 | 565 hid_t save_type_hid = HDF5_SAVE_TYPE; |
4903 | 566 hid_t data_hid = H5Dopen (loc_id, name); |
567 hid_t space_id = H5Dget_space (data_hid); | |
568 | |
569 hsize_t rank = H5Sget_simple_extent_ndims (space_id); | |
570 | |
571 if (rank != 0) | |
572 { | |
573 H5Dclose (data_hid); | |
574 return false; | |
575 } | |
576 | |
4917 | 577 T tmp; |
578 if (H5Dread (data_hid, save_type_hid, H5S_ALL, H5S_ALL, | |
579 H5P_DEFAULT, &tmp) < 0) | |
4903 | 580 { |
581 H5Dclose (data_hid); | |
582 return false; | |
583 } | |
584 | |
4932 | 585 this->scalar = tmp; |
4903 | 586 |
587 H5Dclose (data_hid); | |
588 | |
589 return true; | |
590 } | |
4944 | 591 |
4903 | 592 #endif |
593 | |
594 /* | |
595 ;;; Local Variables: *** | |
596 ;;; mode: C++ *** | |
597 ;;; End: *** | |
598 */ |