ov-bool-sparse.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2004-2012 David Bateman
00004 Copyright (C) 1998-2004 Andy Adler
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #include <climits>
00029 
00030 #include <iostream>
00031 #include <vector>
00032 
00033 #include "ov-base.h"
00034 #include "ov-scalar.h"
00035 #include "ov-bool.h"
00036 #include "ov-bool-mat.h"
00037 #include "gripes.h"
00038 #include "ops.h"
00039 #include "oct-locbuf.h"
00040 
00041 #include "ov-re-sparse.h"
00042 #include "ov-cx-sparse.h"
00043 #include "ov-bool-sparse.h"
00044 
00045 #include "ov-base-sparse.h"
00046 #include "ov-base-sparse.cc"
00047 
00048 template class OCTINTERP_API octave_base_sparse<SparseBoolMatrix>;
00049 
00050 DEFINE_OCTAVE_ALLOCATOR (octave_sparse_bool_matrix);
00051 
00052 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_sparse_bool_matrix, "sparse bool matrix", "logical");
00053 
00054 static octave_base_value *
00055 default_numeric_conversion_function (const octave_base_value& a)
00056 {
00057   CAST_CONV_ARG (const octave_sparse_bool_matrix&);
00058 
00059   return new octave_sparse_matrix (SparseMatrix (v.sparse_bool_matrix_value ()));
00060 }
00061 
00062 octave_base_value::type_conv_info
00063 octave_sparse_bool_matrix::numeric_conversion_function (void) const
00064 {
00065   return octave_base_value::type_conv_info (default_numeric_conversion_function,
00066                                             octave_sparse_matrix::static_type_id ());
00067 }
00068 
00069 octave_base_value *
00070 octave_sparse_bool_matrix::try_narrowing_conversion (void)
00071 {
00072   octave_base_value *retval = 0;
00073 
00074   if (Vsparse_auto_mutate)
00075     {
00076       // Don't use numel, since it can overflow for very large matrices
00077       // Note that for the second test, this means it becomes approximative
00078       // since it involves a cast to double to avoid issues of overflow
00079       if (matrix.rows () == 1 && matrix.cols () == 1)
00080         {
00081           // Const copy of the matrix, so the right version of () operator used
00082           const SparseBoolMatrix tmp (matrix);
00083 
00084           retval = new octave_bool (tmp (0));
00085         }
00086       else if (matrix.cols () > 0 && matrix.rows () > 0
00087                && (double (matrix.byte_size ()) > double (matrix.rows ())
00088                    * double (matrix.cols ()) * sizeof (bool)))
00089         retval = new octave_bool_matrix (matrix.matrix_value ());
00090     }
00091 
00092   return retval;
00093 }
00094 
00095 double
00096 octave_sparse_bool_matrix::double_value (bool) const
00097 {
00098   double retval = lo_ieee_nan_value ();
00099 
00100   if (numel () > 0)
00101     {
00102       if (numel () > 1)
00103         gripe_implicit_conversion ("Octave:array-as-scalar",
00104                                    "bool sparse matrix", "real scalar");
00105 
00106       retval = matrix (0, 0);
00107     }
00108   else
00109     gripe_invalid_conversion ("bool sparse matrix", "real scalar");
00110 
00111   return retval;
00112 }
00113 
00114 Complex
00115 octave_sparse_bool_matrix::complex_value (bool) const
00116 {
00117   double tmp = lo_ieee_nan_value ();
00118 
00119   Complex retval (tmp, tmp);
00120 
00121   if (rows () > 0 && columns () > 0)
00122     {
00123       if (numel () > 1)
00124         gripe_implicit_conversion ("Octave:array-as-scalar",
00125                                    "bool sparse matrix", "complex scalar");
00126 
00127       retval = matrix (0, 0);
00128     }
00129   else
00130     gripe_invalid_conversion ("bool sparse matrix", "complex scalar");
00131 
00132   return retval;
00133 }
00134 
00135 octave_value
00136 octave_sparse_bool_matrix::convert_to_str_internal (bool pad, bool force,
00137                                                     char type) const
00138 {
00139   octave_value tmp = octave_value (array_value ());
00140   return tmp.convert_to_str (pad, force, type);
00141 }
00142 
00143 // FIXME These are inefficient ways of creating full matrices
00144 
00145 Matrix
00146 octave_sparse_bool_matrix::matrix_value (bool) const
00147 {
00148   return Matrix (matrix.matrix_value ());
00149 }
00150 
00151 ComplexMatrix
00152 octave_sparse_bool_matrix::complex_matrix_value (bool) const
00153 {
00154   return ComplexMatrix (matrix.matrix_value ());
00155 }
00156 
00157 ComplexNDArray
00158 octave_sparse_bool_matrix::complex_array_value (bool) const
00159 {
00160   return ComplexNDArray (ComplexMatrix (matrix.matrix_value ()));
00161 }
00162 
00163 NDArray
00164 octave_sparse_bool_matrix::array_value (bool) const
00165 {
00166   return NDArray (Matrix(matrix.matrix_value ()));
00167 }
00168 
00169 charNDArray
00170 octave_sparse_bool_matrix::char_array_value (bool) const
00171 {
00172   charNDArray retval (dims (), 0);
00173   octave_idx_type nc = matrix.cols ();
00174   octave_idx_type nr = matrix.rows ();
00175 
00176   for (octave_idx_type j = 0; j < nc; j++)
00177     for (octave_idx_type i = matrix.cidx(j); i < matrix.cidx(j+1); i++)
00178       retval(matrix.ridx(i) + nr * j) = static_cast<char>(matrix.data (i));
00179 
00180   return retval;
00181 }
00182 
00183 boolMatrix
00184 octave_sparse_bool_matrix::bool_matrix_value (bool) const
00185 {
00186   return matrix.matrix_value ();
00187 }
00188 
00189 boolNDArray
00190 octave_sparse_bool_matrix::bool_array_value (bool) const
00191 {
00192   return boolNDArray (matrix.matrix_value ());
00193 }
00194 
00195 
00196 SparseMatrix
00197 octave_sparse_bool_matrix::sparse_matrix_value (bool) const
00198 {
00199   return SparseMatrix (this->matrix);
00200 }
00201 
00202 SparseComplexMatrix
00203 octave_sparse_bool_matrix::sparse_complex_matrix_value (bool) const
00204 {
00205   return SparseComplexMatrix (this->matrix);
00206 }
00207 
00208 bool
00209 octave_sparse_bool_matrix::save_binary (std::ostream& os, bool&)
00210 {
00211   dim_vector d = this->dims ();
00212   if (d.length() < 1)
00213     return false;
00214 
00215   // Ensure that additional memory is deallocated
00216   matrix.maybe_compress ();
00217 
00218   int nr = d(0);
00219   int nc = d(1);
00220   int nz = nnz ();
00221 
00222   int32_t itmp;
00223   // Use negative value for ndims to be consistent with other formats
00224   itmp= -2;
00225   os.write (reinterpret_cast<char *> (&itmp), 4);
00226 
00227   itmp= nr;
00228   os.write (reinterpret_cast<char *> (&itmp), 4);
00229 
00230   itmp= nc;
00231   os.write (reinterpret_cast<char *> (&itmp), 4);
00232 
00233   itmp= nz;
00234   os.write (reinterpret_cast<char *> (&itmp), 4);
00235 
00236   // add one to the printed indices to go from
00237   // zero-based to one-based arrays
00238   for (int i = 0; i < nc+1; i++)
00239     {
00240       octave_quit ();
00241       itmp = matrix.cidx(i);
00242       os.write (reinterpret_cast<char *> (&itmp), 4);
00243     }
00244 
00245   for (int i = 0; i < nz; i++)
00246     {
00247       octave_quit ();
00248       itmp = matrix.ridx(i);
00249       os.write (reinterpret_cast<char *> (&itmp), 4);
00250     }
00251 
00252   OCTAVE_LOCAL_BUFFER (char, htmp, nz);
00253 
00254   for (int i = 0; i < nz; i++)
00255     htmp[i] = (matrix.data (i) ? 1 : 0);
00256 
00257   os.write (htmp, nz);
00258 
00259   return true;
00260 }
00261 
00262 bool
00263 octave_sparse_bool_matrix::load_binary (std::istream& is, bool swap,
00264                                         oct_mach_info::float_format /* fmt */)
00265 {
00266   int32_t nz, nc, nr, tmp;
00267   if (! is.read (reinterpret_cast<char *> (&tmp), 4))
00268     return false;
00269 
00270   if (swap)
00271     swap_bytes<4> (&tmp);
00272 
00273   if (tmp != -2) {
00274     error ("load: only 2D sparse matrices are supported");
00275     return false;
00276   }
00277 
00278   if (! is.read (reinterpret_cast<char *> (&nr), 4))
00279     return false;
00280   if (! is.read (reinterpret_cast<char *> (&nc), 4))
00281     return false;
00282   if (! is.read (reinterpret_cast<char *> (&nz), 4))
00283     return false;
00284 
00285   if (swap)
00286     {
00287       swap_bytes<4> (&nr);
00288       swap_bytes<4> (&nc);
00289       swap_bytes<4> (&nz);
00290     }
00291 
00292   SparseBoolMatrix m (static_cast<octave_idx_type> (nr),
00293                       static_cast<octave_idx_type> (nc),
00294                       static_cast<octave_idx_type> (nz));
00295 
00296   for (int i = 0; i < nc+1; i++)
00297     {
00298       octave_quit ();
00299       if (! is.read (reinterpret_cast<char *> (&tmp), 4))
00300         return false;
00301       if (swap)
00302         swap_bytes<4> (&tmp);
00303       m.cidx(i) = tmp;
00304     }
00305 
00306   for (int i = 0; i < nz; i++)
00307     {
00308       octave_quit ();
00309       if (! is.read (reinterpret_cast<char *> (&tmp), 4))
00310         return false;
00311       if (swap)
00312         swap_bytes<4> (&tmp);
00313       m.ridx(i) = tmp;
00314     }
00315 
00316   if (error_state || ! is)
00317     return false;
00318 
00319   OCTAVE_LOCAL_BUFFER (char, htmp, nz);
00320 
00321   if (! is.read (htmp, nz))
00322     return false;
00323 
00324   for (int i = 0; i < nz; i++)
00325     m.data(i) = (htmp[i] ? 1 : 0);
00326 
00327   if (! m.indices_ok ())
00328     return false;
00329 
00330   matrix = m;
00331 
00332   return true;
00333 }
00334 
00335 #if defined (HAVE_HDF5)
00336 
00337 bool
00338 octave_sparse_bool_matrix::save_hdf5 (hid_t loc_id, const char *name, bool)
00339 {
00340   dim_vector dv = dims ();
00341   int empty = save_hdf5_empty (loc_id, name, dv);
00342   if (empty)
00343     return (empty > 0);
00344 
00345   // Ensure that additional memory is deallocated
00346   matrix.maybe_compress ();
00347 #if HAVE_HDF5_18
00348   hid_t group_hid = H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
00349 #else
00350   hid_t group_hid = H5Gcreate (loc_id, name, 0);
00351 #endif
00352   if (group_hid < 0)
00353     return false;
00354 
00355   hid_t space_hid = -1, data_hid = -1;
00356   bool retval = true;
00357   SparseBoolMatrix m = sparse_bool_matrix_value ();
00358   octave_idx_type tmp;
00359   hsize_t hdims[2];
00360 
00361   space_hid = H5Screate_simple (0, hdims, 0);
00362   if (space_hid < 0)
00363     {
00364       H5Gclose (group_hid);
00365       return false;
00366     }
00367 #if HAVE_HDF5_18
00368   data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid,
00369                         H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
00370 #else
00371   data_hid = H5Dcreate (group_hid, "nr", H5T_NATIVE_IDX, space_hid,
00372                         H5P_DEFAULT);
00373 #endif
00374   if (data_hid < 0)
00375     {
00376       H5Sclose (space_hid);
00377       H5Gclose (group_hid);
00378       return false;
00379     }
00380 
00381   tmp = m.rows ();
00382   retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &tmp) >= 0;
00383   H5Dclose (data_hid);
00384   if (!retval)
00385     {
00386       H5Sclose (space_hid);
00387       H5Gclose (group_hid);
00388       return false;
00389     }
00390 
00391 #if HAVE_HDF5_18
00392   data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid,
00393                         H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
00394 #else
00395   data_hid = H5Dcreate (group_hid, "nc", H5T_NATIVE_IDX, space_hid,
00396                         H5P_DEFAULT);
00397 #endif
00398   if (data_hid < 0)
00399     {
00400       H5Sclose (space_hid);
00401       H5Gclose (group_hid);
00402       return false;
00403     }
00404 
00405   tmp = m.cols ();
00406   retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL,
00407                      H5P_DEFAULT, &tmp) >= 0;
00408   H5Dclose (data_hid);
00409   if (!retval)
00410     {
00411       H5Sclose (space_hid);
00412       H5Gclose (group_hid);
00413       return false;
00414     }
00415 
00416 #if HAVE_HDF5_18
00417   data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid,
00418                         H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
00419 #else
00420   data_hid = H5Dcreate (group_hid, "nz", H5T_NATIVE_IDX, space_hid,
00421                         H5P_DEFAULT);
00422 #endif
00423   if (data_hid < 0)
00424     {
00425       H5Sclose (space_hid);
00426       H5Gclose (group_hid);
00427       return false;
00428     }
00429 
00430   tmp = m.nnz ();
00431   retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL,
00432                      H5P_DEFAULT, &tmp) >= 0;
00433   H5Dclose (data_hid);
00434   if (!retval)
00435     {
00436       H5Sclose (space_hid);
00437       H5Gclose (group_hid);
00438       return false;
00439     }
00440 
00441   H5Sclose (space_hid);
00442 
00443   hdims[0] = m.cols() + 1;
00444   hdims[1] = 1;
00445 
00446   space_hid = H5Screate_simple (2, hdims, 0);
00447 
00448   if (space_hid < 0)
00449     {
00450       H5Gclose (group_hid);
00451       return false;
00452     }
00453 
00454 #if HAVE_HDF5_18
00455   data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid,
00456                         H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
00457 #else
00458   data_hid = H5Dcreate (group_hid, "cidx", H5T_NATIVE_IDX, space_hid,
00459                         H5P_DEFAULT);
00460 #endif
00461   if (data_hid < 0)
00462     {
00463       H5Sclose (space_hid);
00464       H5Gclose (group_hid);
00465       return false;
00466     }
00467 
00468   octave_idx_type * itmp = m.xcidx ();
00469   retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL,
00470                      H5P_DEFAULT, itmp) >= 0;
00471   H5Dclose (data_hid);
00472   if (!retval)
00473     {
00474       H5Sclose (space_hid);
00475       H5Gclose (group_hid);
00476       return false;
00477     }
00478 
00479   H5Sclose (space_hid);
00480 
00481   hdims[0] = m.nnz ();
00482   hdims[1] = 1;
00483 
00484   space_hid = H5Screate_simple (2, hdims, 0);
00485 
00486   if (space_hid < 0)
00487     {
00488       H5Gclose (group_hid);
00489       return false;
00490     }
00491 
00492 #if HAVE_HDF5_18
00493   data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid,
00494                         H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
00495 #else
00496   data_hid = H5Dcreate (group_hid, "ridx", H5T_NATIVE_IDX, space_hid,
00497                         H5P_DEFAULT);
00498 #endif
00499   if (data_hid < 0)
00500     {
00501       H5Sclose (space_hid);
00502       H5Gclose (group_hid);
00503       return false;
00504     }
00505 
00506   itmp = m.xridx ();
00507   retval = H5Dwrite (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL,
00508                      H5P_DEFAULT, itmp) >= 0;
00509   H5Dclose (data_hid);
00510   if (!retval)
00511     {
00512       H5Sclose (space_hid);
00513       H5Gclose (group_hid);
00514       return false;
00515     }
00516 
00517 #if HAVE_HDF5_18
00518   data_hid = H5Dcreate (group_hid, "data", H5T_NATIVE_HBOOL, space_hid,
00519                         H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
00520 #else
00521   data_hid = H5Dcreate (group_hid, "data", H5T_NATIVE_HBOOL, space_hid,
00522                         H5P_DEFAULT);
00523 #endif
00524   if (data_hid < 0)
00525     {
00526       H5Sclose (space_hid);
00527       H5Gclose (group_hid);
00528       return false;
00529     }
00530 
00531   OCTAVE_LOCAL_BUFFER (hbool_t, htmp, m.nnz ());
00532   for (int i = 0; i < m.nnz (); i++)
00533     htmp[i] = m.xdata(i);
00534 
00535   retval = H5Dwrite (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL,
00536                      H5P_DEFAULT, htmp) >= 0;
00537   H5Dclose (data_hid);
00538   H5Sclose (space_hid);
00539   H5Gclose (group_hid);
00540 
00541   return retval;
00542 }
00543 
00544 bool
00545 octave_sparse_bool_matrix::load_hdf5 (hid_t loc_id, const char *name)
00546 {
00547   octave_idx_type nr, nc, nz;
00548   hid_t group_hid, data_hid, space_hid;
00549   hsize_t rank;
00550 
00551   dim_vector dv;
00552   int empty = load_hdf5_empty (loc_id, name, dv);
00553   if (empty > 0)
00554     matrix.resize(dv);
00555   if (empty)
00556     return (empty > 0);
00557 
00558 #if HAVE_HDF5_18
00559   group_hid = H5Gopen (loc_id, name, H5P_DEFAULT);
00560 #else
00561   group_hid = H5Gopen (loc_id, name);
00562 #endif
00563   if (group_hid < 0 ) return false;
00564 
00565 #if HAVE_HDF5_18
00566   data_hid = H5Dopen (group_hid, "nr", H5P_DEFAULT);
00567 #else
00568   data_hid = H5Dopen (group_hid, "nr");
00569 #endif
00570   space_hid = H5Dget_space (data_hid);
00571   rank = H5Sget_simple_extent_ndims (space_hid);
00572 
00573   if (rank != 0)
00574     {
00575       H5Dclose (data_hid);
00576       H5Gclose (group_hid);
00577       return false;
00578     }
00579 
00580   if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nr) < 0)
00581     {
00582       H5Dclose (data_hid);
00583       H5Gclose (group_hid);
00584       return false;
00585     }
00586 
00587   H5Dclose (data_hid);
00588 
00589 #if HAVE_HDF5_18
00590   data_hid = H5Dopen (group_hid, "nc", H5P_DEFAULT);
00591 #else
00592   data_hid = H5Dopen (group_hid, "nc");
00593 #endif
00594   space_hid = H5Dget_space (data_hid);
00595   rank = H5Sget_simple_extent_ndims (space_hid);
00596 
00597   if (rank != 0)
00598     {
00599       H5Dclose (data_hid);
00600       H5Gclose (group_hid);
00601       return false;
00602     }
00603 
00604   if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nc) < 0)
00605     {
00606       H5Dclose (data_hid);
00607       H5Gclose (group_hid);
00608       return false;
00609     }
00610 
00611   H5Dclose (data_hid);
00612 
00613 #if HAVE_HDF5_18
00614   data_hid = H5Dopen (group_hid, "nz", H5P_DEFAULT);
00615 #else
00616   data_hid = H5Dopen (group_hid, "nz");
00617 #endif
00618   space_hid = H5Dget_space (data_hid);
00619   rank = H5Sget_simple_extent_ndims (space_hid);
00620 
00621   if (rank != 0)
00622     {
00623       H5Dclose (data_hid);
00624       H5Gclose (group_hid);
00625       return false;
00626     }
00627 
00628   if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, &nz) < 0)
00629     {
00630       H5Dclose (data_hid);
00631       H5Gclose (group_hid);
00632       return false;
00633     }
00634 
00635   H5Dclose (data_hid);
00636 
00637   SparseBoolMatrix m (static_cast<octave_idx_type> (nr),
00638                       static_cast<octave_idx_type> (nc),
00639                       static_cast<octave_idx_type> (nz));
00640 
00641 #if HAVE_HDF5_18
00642   data_hid = H5Dopen (group_hid, "cidx", H5P_DEFAULT);
00643 #else
00644   data_hid = H5Dopen (group_hid, "cidx");
00645 #endif
00646   space_hid = H5Dget_space (data_hid);
00647   rank = H5Sget_simple_extent_ndims (space_hid);
00648 
00649   if (rank != 2)
00650     {
00651       H5Sclose (space_hid);
00652       H5Dclose (data_hid);
00653       H5Gclose (group_hid);
00654       return false;
00655     }
00656 
00657   OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank);
00658   OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank);
00659 
00660   H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
00661 
00662   if (static_cast<int> (hdims[0]) != nc + 1
00663       || static_cast<int> (hdims[1]) != 1)
00664     {
00665       H5Sclose (space_hid);
00666       H5Dclose (data_hid);
00667       H5Gclose (group_hid);
00668       return false;
00669     }
00670 
00671   octave_idx_type *itmp = m.xcidx ();
00672   if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, H5P_DEFAULT, itmp) < 0)
00673     {
00674       H5Sclose (space_hid);
00675       H5Dclose (data_hid);
00676       H5Gclose (group_hid);
00677       return false;
00678     }
00679 
00680   H5Sclose (space_hid);
00681   H5Dclose (data_hid);
00682 
00683 #if HAVE_HDF5_18
00684   data_hid = H5Dopen (group_hid, "ridx", H5P_DEFAULT);
00685 #else
00686   data_hid = H5Dopen (group_hid, "ridx");
00687 #endif
00688   space_hid = H5Dget_space (data_hid);
00689   rank = H5Sget_simple_extent_ndims (space_hid);
00690 
00691   if (rank != 2)
00692     {
00693       H5Sclose (space_hid);
00694       H5Dclose (data_hid);
00695       H5Gclose (group_hid);
00696       return false;
00697     }
00698 
00699   H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
00700 
00701   if (static_cast<int> (hdims[0]) != nz
00702       || static_cast<int> (hdims[1]) != 1)
00703     {
00704       H5Sclose (space_hid);
00705       H5Dclose (data_hid);
00706       H5Gclose (group_hid);
00707       return false;
00708     }
00709 
00710   itmp = m.xridx ();
00711   if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL,
00712                H5P_DEFAULT, itmp) < 0)
00713     {
00714       H5Sclose (space_hid);
00715       H5Dclose (data_hid);
00716       H5Gclose (group_hid);
00717       return false;
00718     }
00719 
00720   H5Sclose (space_hid);
00721   H5Dclose (data_hid);
00722 
00723 #if HAVE_HDF5_18
00724   data_hid = H5Dopen (group_hid, "data", H5P_DEFAULT);
00725 #else
00726   data_hid = H5Dopen (group_hid, "data");
00727 #endif
00728   space_hid = H5Dget_space (data_hid);
00729   rank = H5Sget_simple_extent_ndims (space_hid);
00730 
00731   if (rank != 2)
00732     {
00733       H5Sclose (space_hid);
00734       H5Dclose (data_hid);
00735       H5Gclose (group_hid);
00736       return false;
00737     }
00738 
00739   H5Sget_simple_extent_dims (space_hid, hdims, maxdims);
00740 
00741   if (static_cast<int> (hdims[0]) != nz
00742       || static_cast<int> (hdims[1]) != 1)
00743     {
00744       H5Sclose (space_hid);
00745       H5Dclose (data_hid);
00746       H5Gclose (group_hid);
00747       return false;
00748     }
00749 
00750   OCTAVE_LOCAL_BUFFER (hbool_t, htmp, nz);
00751   bool retval = false;
00752   if (H5Dread (data_hid, H5T_NATIVE_HBOOL, H5S_ALL, H5S_ALL,
00753                H5P_DEFAULT, htmp) >= 0
00754       && m.indices_ok ())
00755     {
00756       retval = true;
00757 
00758       for (int i = 0; i < nz; i++)
00759         m.xdata(i) = htmp[i];
00760 
00761       matrix = m;
00762     }
00763 
00764   H5Sclose (space_hid);
00765   H5Dclose (data_hid);
00766   H5Gclose (group_hid);
00767 
00768   return retval;
00769 }
00770 
00771 #endif
00772 
00773 mxArray *
00774 octave_sparse_bool_matrix::as_mxArray (void) const
00775 {
00776   mwSize nz = nzmax ();
00777   mxArray *retval = new mxArray (mxLOGICAL_CLASS, rows (), columns (),
00778                                  nz, mxREAL);
00779   bool *pr = static_cast<bool *> (retval->get_data ());
00780   mwIndex *ir = retval->get_ir ();
00781   mwIndex *jc = retval->get_jc ();
00782 
00783   for (mwIndex i = 0; i < nz; i++)
00784     {
00785       pr[i] = matrix.data(i);
00786       ir[i] = matrix.ridx(i);
00787     }
00788 
00789   for (mwIndex i = 0; i < columns () + 1; i++)
00790     jc[i] = matrix.cidx(i);
00791 
00792   return retval;
00793 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines