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