Mercurial > hg > octave-nkf
annotate src/ov-bool-sparse.cc @ 9469:c6edba80dfae
sanity checks for loading sparse matrices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 29 Jul 2009 12:15:27 -0400 |
parents | eb63fbe60fab |
children | b3089dba88bf |
rev | line source |
---|---|
5164 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2004, 2005, 2006, 2007, 2008 David Bateman |
7016 | 4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Andy Adler |
5 | |
6 This file is part of Octave. | |
5164 | 7 |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
5164 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
5164 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include <climits> | |
29 | |
30 #include <iostream> | |
31 #include <vector> | |
32 | |
33 #include "ov-base.h" | |
34 #include "ov-scalar.h" | |
35 #include "ov-bool.h" | |
36 #include "ov-bool-mat.h" | |
37 #include "gripes.h" | |
38 #include "ops.h" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8345
diff
changeset
|
39 #include "oct-locbuf.h" |
5164 | 40 |
41 #include "ov-re-sparse.h" | |
42 #include "ov-cx-sparse.h" | |
43 #include "ov-bool-sparse.h" | |
44 | |
45 #include "ov-base-sparse.h" | |
46 #include "ov-base-sparse.cc" | |
47 | |
6109 | 48 template class OCTINTERP_API octave_base_sparse<SparseBoolMatrix>; |
5164 | 49 |
50 DEFINE_OCTAVE_ALLOCATOR (octave_sparse_bool_matrix); | |
51 | |
6823 | 52 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_sparse_bool_matrix, "sparse bool matrix", "logical"); |
5164 | 53 |
5759 | 54 static octave_base_value * |
55 default_numeric_conversion_function (const octave_base_value& a) | |
5164 | 56 { |
57 CAST_CONV_ARG (const octave_sparse_bool_matrix&); | |
58 | |
59 return new octave_sparse_matrix (SparseMatrix (v.sparse_bool_matrix_value ())); | |
60 } | |
61 | |
8345
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7503
diff
changeset
|
62 octave_base_value::type_conv_info |
5164 | 63 octave_sparse_bool_matrix::numeric_conversion_function (void) const |
64 { | |
8345
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7503
diff
changeset
|
65 return octave_base_value::type_conv_info (default_numeric_conversion_function, |
c777f3ce02d8
smarter conversion lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7503
diff
changeset
|
66 octave_sparse_matrix::static_type_id ()); |
5164 | 67 } |
68 | |
5759 | 69 octave_base_value * |
5164 | 70 octave_sparse_bool_matrix::try_narrowing_conversion (void) |
71 { | |
5759 | 72 octave_base_value *retval = 0; |
5164 | 73 |
7193 | 74 if (Vsparse_auto_mutate) |
5164 | 75 { |
7193 | 76 // Don't use numel, since it can overflow for very large matrices |
77 // Note that for the second test, this means it becomes approximative | |
78 // since it involves a cast to double to avoid issues of overflow | |
79 if (matrix.rows () == 1 && matrix.cols () == 1) | |
80 { | |
81 // Const copy of the matrix, so the right version of () operator used | |
82 const SparseBoolMatrix tmp (matrix); | |
5164 | 83 |
7193 | 84 retval = new octave_bool (tmp (0)); |
85 } | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
86 else if (matrix.cols () > 0 && matrix.rows () > 0 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
87 && (double (matrix.byte_size ()) > double (matrix.rows ()) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
88 * double (matrix.cols ()) * sizeof (bool))) |
7193 | 89 retval = new octave_bool_matrix (matrix.matrix_value ()); |
5164 | 90 } |
91 | |
92 return retval; | |
93 } | |
94 | |
95 double | |
96 octave_sparse_bool_matrix::double_value (bool) const | |
97 { | |
98 double retval = lo_ieee_nan_value (); | |
99 | |
100 if (numel () > 0) | |
101 { | |
6221 | 102 if (numel () > 1) |
103 gripe_implicit_conversion ("Octave:array-as-scalar", | |
104 "bool sparse matrix", "real scalar"); | |
5164 | 105 |
106 retval = matrix (0, 0); | |
107 } | |
108 else | |
109 gripe_invalid_conversion ("bool sparse matrix", "real scalar"); | |
110 | |
111 return retval; | |
112 } | |
113 | |
114 Complex | |
115 octave_sparse_bool_matrix::complex_value (bool) const | |
116 { | |
117 double tmp = lo_ieee_nan_value (); | |
118 | |
119 Complex retval (tmp, tmp); | |
120 | |
121 if (rows () > 0 && columns () > 0) | |
122 { | |
6221 | 123 if (numel () > 1) |
124 gripe_implicit_conversion ("Octave:array-as-scalar", | |
125 "bool sparse matrix", "complex scalar"); | |
5164 | 126 |
127 retval = matrix (0, 0); | |
128 } | |
129 else | |
130 gripe_invalid_conversion ("bool sparse matrix", "complex scalar"); | |
131 | |
132 return retval; | |
133 } | |
134 | |
135 octave_value | |
5279 | 136 octave_sparse_bool_matrix::convert_to_str_internal (bool pad, bool force, |
137 char type) const | |
5164 | 138 { |
139 octave_value tmp = octave_value (array_value ()); | |
5279 | 140 return tmp.convert_to_str (pad, force, type); |
5164 | 141 } |
142 | |
5775 | 143 // FIXME These are inefficient ways of creating full matrices |
5164 | 144 |
145 Matrix | |
146 octave_sparse_bool_matrix::matrix_value (bool) const | |
147 { | |
148 return Matrix (matrix.matrix_value ()); | |
149 } | |
150 | |
151 ComplexMatrix | |
152 octave_sparse_bool_matrix::complex_matrix_value (bool) const | |
153 { | |
154 return ComplexMatrix (matrix.matrix_value ()); | |
155 } | |
156 | |
157 ComplexNDArray | |
158 octave_sparse_bool_matrix::complex_array_value (bool) const | |
159 { | |
160 return ComplexNDArray (ComplexMatrix (matrix.matrix_value ())); | |
161 } | |
162 | |
163 NDArray | |
164 octave_sparse_bool_matrix::array_value (bool) const | |
165 { | |
166 return NDArray (Matrix(matrix.matrix_value ())); | |
167 } | |
168 | |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
169 charNDArray |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
170 octave_sparse_bool_matrix::char_array_value (bool) const |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
171 { |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
172 charNDArray retval (dims (), 0); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
173 octave_idx_type nc = matrix.cols (); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
174 octave_idx_type nr = matrix.rows (); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
175 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
176 for (octave_idx_type j = 0; j < nc; j++) |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
177 for (octave_idx_type i = matrix.cidx(j); i < matrix.cidx(j+1); i++) |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
178 retval(matrix.ridx(i) + nr * j) = static_cast<char>(matrix.data (i)); |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
179 |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
180 return retval; |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
181 } |
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7193
diff
changeset
|
182 |
5164 | 183 boolMatrix |
5944 | 184 octave_sparse_bool_matrix::bool_matrix_value (bool) const |
5164 | 185 { |
186 return matrix.matrix_value (); | |
187 } | |
188 | |
189 boolNDArray | |
5944 | 190 octave_sparse_bool_matrix::bool_array_value (bool) const |
5164 | 191 { |
192 return boolNDArray (matrix.matrix_value ()); | |
193 } | |
194 | |
195 | |
196 SparseMatrix | |
5188 | 197 octave_sparse_bool_matrix::sparse_matrix_value (bool) const |
5164 | 198 { |
199 return SparseMatrix (this->matrix); | |
200 } | |
201 | |
202 SparseComplexMatrix | |
5188 | 203 octave_sparse_bool_matrix::sparse_complex_matrix_value (bool) const |
5164 | 204 { |
205 return SparseComplexMatrix (this->matrix); | |
206 } | |
207 | |
208 bool | |
209 octave_sparse_bool_matrix::save_binary (std::ostream& os, bool&) | |
210 { | |
211 dim_vector d = this->dims (); | |
212 if (d.length() < 1) | |
213 return false; | |
214 | |
215 // Ensure that additional memory is deallocated | |
216 matrix.maybe_compress (); | |
217 | |
218 int nr = d(0); | |
219 int nc = d(1); | |
5604 | 220 int nz = nzmax (); |
5164 | 221 |
5828 | 222 int32_t itmp; |
5164 | 223 // Use negative value for ndims to be consistent with other formats |
224 itmp= -2; | |
5760 | 225 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164 | 226 |
227 itmp= nr; | |
5760 | 228 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164 | 229 |
230 itmp= nc; | |
5760 | 231 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164 | 232 |
233 itmp= nz; | |
5760 | 234 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164 | 235 |
236 // add one to the printed indices to go from | |
237 // zero-based to one-based arrays | |
238 for (int i = 0; i < nc+1; i++) | |
239 { | |
240 OCTAVE_QUIT; | |
241 itmp = matrix.cidx(i); | |
5760 | 242 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164 | 243 } |
244 | |
245 for (int i = 0; i < nz; i++) | |
246 { | |
247 OCTAVE_QUIT; | |
248 itmp = matrix.ridx(i); | |
5760 | 249 os.write (reinterpret_cast<char *> (&itmp), 4); |
5164 | 250 } |
251 | |
252 OCTAVE_LOCAL_BUFFER (char, htmp, nz); | |
253 | |
254 for (int i = 0; i < nz; i++) | |
255 htmp[i] = (matrix.data (i) ? 1 : 0); | |
256 | |
257 os.write (htmp, nz); | |
258 | |
259 return true; | |
260 } | |
261 | |
262 bool | |
263 octave_sparse_bool_matrix::load_binary (std::istream& is, bool swap, | |
5322 | 264 oct_mach_info::float_format /* fmt */) |
5164 | 265 { |
5828 | 266 int32_t nz, nc, nr, tmp; |
5760 | 267 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
5164 | 268 return false; |
269 | |
270 if (swap) | |
271 swap_bytes<4> (&tmp); | |
272 | |
273 if (tmp != -2) { | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
274 error ("load: only 2D sparse matrices are supported"); |
5164 | 275 return false; |
276 } | |
277 | |
5760 | 278 if (! is.read (reinterpret_cast<char *> (&nr), 4)) |
5164 | 279 return false; |
5760 | 280 if (! is.read (reinterpret_cast<char *> (&nc), 4)) |
5164 | 281 return false; |
5760 | 282 if (! is.read (reinterpret_cast<char *> (&nz), 4)) |
5164 | 283 return false; |
284 | |
285 if (swap) | |
286 { | |
287 swap_bytes<4> (&nr); | |
288 swap_bytes<4> (&nc); | |
289 swap_bytes<4> (&nz); | |
290 } | |
291 | |
5275 | 292 SparseBoolMatrix m (static_cast<octave_idx_type> (nr), |
293 static_cast<octave_idx_type> (nc), | |
294 static_cast<octave_idx_type> (nz)); | |
5164 | 295 |
296 for (int i = 0; i < nc+1; i++) | |
297 { | |
298 OCTAVE_QUIT; | |
5760 | 299 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
5164 | 300 return false; |
301 if (swap) | |
302 swap_bytes<4> (&tmp); | |
303 m.cidx(i) = tmp; | |
304 } | |
305 | |
306 for (int i = 0; i < nz; i++) | |
307 { | |
308 OCTAVE_QUIT; | |
5760 | 309 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
5164 | 310 return false; |
311 if (swap) | |
312 swap_bytes<4> (&tmp); | |
313 m.ridx(i) = tmp; | |
314 } | |
315 | |
316 if (error_state || ! is) | |
317 return false; | |
318 | |
319 OCTAVE_LOCAL_BUFFER (char, htmp, nz); | |
320 | |
321 if (! is.read (htmp, nz)) | |
322 return false; | |
323 | |
324 for (int i = 0; i < nz; i++) | |
325 m.data(i) = (htmp[i] ? 1 : 0); | |
326 | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
327 if (! m.indices_ok ()) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
328 return false; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
329 |
5164 | 330 matrix = m; |
331 | |
332 return true; | |
333 } | |
334 | |
335 #if defined (HAVE_HDF5) | |
5900 | 336 |
5164 | 337 bool |
338 octave_sparse_bool_matrix::save_hdf5 (hid_t loc_id, const char *name, bool) | |
339 { | |
340 dim_vector dv = dims (); | |
341 int empty = save_hdf5_empty (loc_id, name, dv); | |
342 if (empty) | |
343 return (empty > 0); | |
344 | |
345 // Ensure that additional memory is deallocated | |
346 matrix.maybe_compress (); | |
347 | |
348 hid_t group_hid = H5Gcreate (loc_id, name, 0); | |
349 if (group_hid < 0) | |
350 return false; | |
351 | |
352 hid_t space_hid = -1, data_hid = -1; | |
353 bool retval = true; | |
354 SparseBoolMatrix m = sparse_bool_matrix_value (); | |
5351 | 355 octave_idx_type tmp; |
5164 | 356 hsize_t hdims[2]; |
357 | |
5760 | 358 space_hid = H5Screate_simple (0, hdims, 0); |
5164 | 359 if (space_hid < 0) |
360 { | |
361 H5Gclose (group_hid); | |
362 return false; | |
363 } | |
364 | |
5760 | 365 data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid, |
5164 | 366 H5P_DEFAULT); |
367 if (data_hid < 0) | |
368 { | |
369 H5Sclose (space_hid); | |
370 H5Gclose (group_hid); | |
371 return false; | |
372 } | |
373 | |
374 tmp = m.rows (); | |
5760 | 375 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &tmp) >= 0; |
5164 | 376 H5Dclose (data_hid); |
377 if (!retval) | |
378 { | |
379 H5Sclose (space_hid); | |
380 H5Gclose (group_hid); | |
381 return false; | |
382 } | |
383 | |
5760 | 384 data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid, |
5164 | 385 H5P_DEFAULT); |
386 if (data_hid < 0) | |
387 { | |
388 H5Sclose (space_hid); | |
389 H5Gclose (group_hid); | |
390 return false; | |
391 } | |
392 | |
393 tmp = m.cols (); | |
5351 | 394 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
5760 | 395 H5P_DEFAULT, &tmp) >= 0; |
5164 | 396 H5Dclose (data_hid); |
397 if (!retval) | |
398 { | |
399 H5Sclose (space_hid); | |
400 H5Gclose (group_hid); | |
401 return false; | |
402 } | |
403 | |
5351 | 404 data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid, |
5164 | 405 H5P_DEFAULT); |
406 if (data_hid < 0) | |
407 { | |
408 H5Sclose (space_hid); | |
409 H5Gclose (group_hid); | |
410 return false; | |
411 } | |
412 | |
5604 | 413 tmp = m.nzmax (); |
5351 | 414 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
5760 | 415 H5P_DEFAULT, &tmp) >= 0; |
5164 | 416 H5Dclose (data_hid); |
417 if (!retval) | |
418 { | |
419 H5Sclose (space_hid); | |
420 H5Gclose (group_hid); | |
421 return false; | |
422 } | |
423 | |
424 H5Sclose (space_hid); | |
425 | |
426 hdims[0] = m.cols() + 1; | |
427 hdims[1] = 1; | |
428 | |
429 space_hid = H5Screate_simple (2, hdims, 0); | |
430 | |
431 if (space_hid < 0) | |
432 { | |
433 H5Gclose (group_hid); | |
434 return false; | |
435 } | |
436 | |
5351 | 437 data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid, |
5164 | 438 H5P_DEFAULT); |
439 if (data_hid < 0) | |
440 { | |
441 H5Sclose (space_hid); | |
442 H5Gclose (group_hid); | |
443 return false; | |
444 } | |
445 | |
5351 | 446 octave_idx_type * itmp = m.xcidx (); |
447 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, | |
5760 | 448 H5P_DEFAULT, itmp) >= 0; |
5164 | 449 H5Dclose (data_hid); |
450 if (!retval) | |
451 { | |
452 H5Sclose (space_hid); | |
453 H5Gclose (group_hid); | |
454 return false; | |
455 } | |
456 | |
457 H5Sclose (space_hid); | |
458 | |
5604 | 459 hdims[0] = m.nzmax (); |
5164 | 460 hdims[1] = 1; |
461 | |
462 space_hid = H5Screate_simple (2, hdims, 0); | |
463 | |
464 if (space_hid < 0) | |
465 { | |
466 H5Gclose (group_hid); | |
467 return false; | |
468 } | |
469 | |
5351 | 470 data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid, |
5164 | 471 H5P_DEFAULT); |
472 if (data_hid < 0) | |
473 { | |
474 H5Sclose (space_hid); | |
475 H5Gclose (group_hid); | |
476 return false; | |
477 } | |
478 | |
479 itmp = m.xridx (); | |
5351 | 480 retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
5760 | 481 H5P_DEFAULT, itmp) >= 0; |
5164 | 482 H5Dclose (data_hid); |
483 if (!retval) | |
484 { | |
485 H5Sclose (space_hid); | |
486 H5Gclose (group_hid); | |
487 return false; | |
488 } | |
489 | |
490 data_hid = H5Dcreate (group_hid, "data", H5T_NATIVE_HBOOL, space_hid, | |
491 H5P_DEFAULT); | |
492 if (data_hid < 0) | |
493 { | |
494 H5Sclose (space_hid); | |
495 H5Gclose (group_hid); | |
496 return false; | |
497 } | |
6276 | 498 |
499 OCTAVE_LOCAL_BUFFER (hbool_t, htmp, m.nzmax ()); | |
5604 | 500 for (int i = 0; i < m.nzmax (); i++) |
5164 | 501 htmp[i] = m.xdata(i); |
502 | |
503 retval = H5Dwrite (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, | |
6718 | 504 H5P_DEFAULT, htmp) >= 0; |
5164 | 505 H5Dclose (data_hid); |
506 H5Sclose (space_hid); | |
507 H5Gclose (group_hid); | |
508 | |
509 return retval; | |
510 } | |
511 | |
512 bool | |
513 octave_sparse_bool_matrix::load_hdf5 (hid_t loc_id, const char *name, | |
514 bool /* have_h5giterate_bug */) | |
515 { | |
5351 | 516 octave_idx_type nr, nc, nz; |
5164 | 517 hid_t group_hid, data_hid, space_hid; |
518 hsize_t rank; | |
519 | |
520 dim_vector dv; | |
521 int empty = load_hdf5_empty (loc_id, name, dv); | |
522 if (empty > 0) | |
523 matrix.resize(dv); | |
524 if (empty) | |
525 return (empty > 0); | |
526 | |
527 group_hid = H5Gopen (loc_id, name); | |
528 if (group_hid < 0 ) return false; | |
529 | |
530 data_hid = H5Dopen (group_hid, "nr"); | |
531 space_hid = H5Dget_space (data_hid); | |
532 rank = H5Sget_simple_extent_ndims (space_hid); | |
533 | |
534 if (rank != 0) | |
535 { | |
536 H5Dclose (data_hid); | |
537 H5Gclose (group_hid); | |
538 return false; | |
539 } | |
540 | |
5760 | 541 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nr) < 0) |
5164 | 542 { |
543 H5Dclose (data_hid); | |
544 H5Gclose (group_hid); | |
545 return false; | |
546 } | |
547 | |
548 H5Dclose (data_hid); | |
549 | |
550 data_hid = H5Dopen (group_hid, "nc"); | |
551 space_hid = H5Dget_space (data_hid); | |
552 rank = H5Sget_simple_extent_ndims (space_hid); | |
553 | |
554 if (rank != 0) | |
555 { | |
556 H5Dclose (data_hid); | |
557 H5Gclose (group_hid); | |
558 return false; | |
559 } | |
560 | |
5760 | 561 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nc) < 0) |
5164 | 562 { |
563 H5Dclose (data_hid); | |
564 H5Gclose (group_hid); | |
565 return false; | |
566 } | |
567 | |
568 H5Dclose (data_hid); | |
569 | |
570 data_hid = H5Dopen (group_hid, "nz"); | |
571 space_hid = H5Dget_space (data_hid); | |
572 rank = H5Sget_simple_extent_ndims (space_hid); | |
573 | |
574 if (rank != 0) | |
575 { | |
576 H5Dclose (data_hid); | |
577 H5Gclose (group_hid); | |
578 return false; | |
579 } | |
580 | |
5760 | 581 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nz) < 0) |
5164 | 582 { |
583 H5Dclose (data_hid); | |
584 H5Gclose (group_hid); | |
585 return false; | |
586 } | |
587 | |
588 H5Dclose (data_hid); | |
589 | |
5275 | 590 SparseBoolMatrix m (static_cast<octave_idx_type> (nr), |
591 static_cast<octave_idx_type> (nc), | |
592 static_cast<octave_idx_type> (nz)); | |
5164 | 593 |
594 data_hid = H5Dopen (group_hid, "cidx"); | |
595 space_hid = H5Dget_space (data_hid); | |
596 rank = H5Sget_simple_extent_ndims (space_hid); | |
597 | |
598 if (rank != 2) | |
599 { | |
600 H5Sclose (space_hid); | |
601 H5Dclose (data_hid); | |
602 H5Gclose (group_hid); | |
603 return false; | |
604 } | |
605 | |
606 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | |
607 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
608 | |
609 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | |
610 | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
611 if (static_cast<int> (hdims[0]) != nc + 1 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
612 || static_cast<int> (hdims[1]) != 1) |
5164 | 613 { |
614 H5Sclose (space_hid); | |
615 H5Dclose (data_hid); | |
616 H5Gclose (group_hid); | |
617 return false; | |
618 } | |
619 | |
5351 | 620 octave_idx_type *itmp = m.xcidx (); |
5760 | 621 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, itmp) < 0) |
5164 | 622 { |
623 H5Sclose (space_hid); | |
624 H5Dclose (data_hid); | |
625 H5Gclose (group_hid); | |
626 return false; | |
627 } | |
628 | |
629 H5Sclose (space_hid); | |
630 H5Dclose (data_hid); | |
631 | |
632 data_hid = H5Dopen (group_hid, "ridx"); | |
633 space_hid = H5Dget_space (data_hid); | |
634 rank = H5Sget_simple_extent_ndims (space_hid); | |
635 | |
636 if (rank != 2) | |
637 { | |
638 H5Sclose (space_hid); | |
639 H5Dclose (data_hid); | |
640 H5Gclose (group_hid); | |
641 return false; | |
642 } | |
643 | |
644 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | |
645 | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
646 if (static_cast<int> (hdims[0]) != nz |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
647 || static_cast<int> (hdims[1]) != 1) |
5164 | 648 { |
649 H5Sclose (space_hid); | |
650 H5Dclose (data_hid); | |
651 H5Gclose (group_hid); | |
652 return false; | |
653 } | |
654 | |
655 itmp = m.xridx (); | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
656 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
657 H5P_DEFAULT, itmp) < 0) |
5164 | 658 { |
659 H5Sclose (space_hid); | |
660 H5Dclose (data_hid); | |
661 H5Gclose (group_hid); | |
662 return false; | |
663 } | |
664 | |
665 H5Sclose (space_hid); | |
666 H5Dclose (data_hid); | |
667 | |
668 data_hid = H5Dopen (group_hid, "data"); | |
669 space_hid = H5Dget_space (data_hid); | |
670 rank = H5Sget_simple_extent_ndims (space_hid); | |
671 | |
672 if (rank != 2) | |
673 { | |
674 H5Sclose (space_hid); | |
675 H5Dclose (data_hid); | |
676 H5Gclose (group_hid); | |
677 return false; | |
678 } | |
679 | |
680 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | |
681 | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
682 if (static_cast<int> (hdims[0]) != nz |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
683 || static_cast<int> (hdims[1]) != 1) |
5164 | 684 { |
685 H5Sclose (space_hid); | |
686 H5Dclose (data_hid); | |
687 H5Gclose (group_hid); | |
688 return false; | |
689 } | |
690 | |
6775 | 691 OCTAVE_LOCAL_BUFFER (hbool_t, htmp, nz); |
5164 | 692 bool retval = false; |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
693 if (H5Dread (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL, |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
694 H5P_DEFAULT, htmp) >= 0 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
695 && m.indices_ok ()) |
5164 | 696 { |
697 retval = true; | |
698 | |
699 for (int i = 0; i < nz; i++) | |
700 m.xdata(i) = htmp[i]; | |
701 | |
702 matrix = m; | |
703 } | |
704 | |
705 H5Sclose (space_hid); | |
706 H5Dclose (data_hid); | |
707 H5Gclose (group_hid); | |
708 | |
709 return retval; | |
710 } | |
5900 | 711 |
5164 | 712 #endif |
713 | |
5900 | 714 mxArray * |
715 octave_sparse_bool_matrix::as_mxArray (void) const | |
716 { | |
6686 | 717 mwSize nz = nzmax (); |
5903 | 718 mxArray *retval = new mxArray (mxLOGICAL_CLASS, rows (), columns (), |
719 nz, mxREAL); | |
720 bool *pr = static_cast<bool *> (retval->get_data ()); | |
6686 | 721 mwIndex *ir = retval->get_ir (); |
722 mwIndex *jc = retval->get_jc (); | |
5903 | 723 |
6686 | 724 for (mwIndex i = 0; i < nz; i++) |
5903 | 725 { |
726 pr[i] = matrix.data(i); | |
727 ir[i] = matrix.ridx(i); | |
728 } | |
729 | |
6686 | 730 for (mwIndex i = 0; i < columns () + 1; i++) |
5903 | 731 jc[i] = matrix.cidx(i); |
732 | |
733 return retval; | |
5900 | 734 } |
735 | |
5164 | 736 /* |
737 ;;; Local Variables: *** | |
738 ;;; mode: C++ *** | |
739 ;;; End: *** | |
740 */ |