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