Mercurial > hg > octave-nkf
comparison src/ov-cx-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 | f80c566bc751 |
comparison
equal
deleted
inserted
replaced
9468:5af462716bff | 9469:c6edba80dfae |
---|---|
79 retval = new octave_complex (c); | 79 retval = new octave_complex (c); |
80 } | 80 } |
81 else if (nr == 0 || nc == 0) | 81 else if (nr == 0 || nc == 0) |
82 retval = new octave_matrix (Matrix (nr, nc)); | 82 retval = new octave_matrix (Matrix (nr, nc)); |
83 else if (matrix.all_elements_are_real ()) | 83 else if (matrix.all_elements_are_real ()) |
84 if (matrix.cols () > 0 && matrix.rows () > 0 && | 84 if (matrix.cols () > 0 && matrix.rows () > 0 |
85 double (matrix.byte_size ()) > double (matrix.rows ()) * | 85 && (double (matrix.byte_size ()) > double (matrix.rows ()) |
86 double (matrix.cols ()) * sizeof (double)) | 86 * double (matrix.cols ()) * sizeof (double))) |
87 retval = new octave_matrix (::real (matrix.matrix_value ())); | 87 retval = new octave_matrix (::real (matrix.matrix_value ())); |
88 else | 88 else |
89 retval = new octave_sparse_matrix (::real (matrix)); | 89 retval = new octave_sparse_matrix (::real (matrix)); |
90 else if (matrix.cols () > 0 && matrix.rows () > 0 && | 90 else if (matrix.cols () > 0 && matrix.rows () > 0 |
91 double (matrix.byte_size ()) > double (matrix.rows ()) * | 91 && (double (matrix.byte_size ()) > double (matrix.rows ()) |
92 double (matrix.cols ()) * sizeof (Complex)) | 92 * double (matrix.cols ()) * sizeof (Complex))) |
93 retval = new octave_complex_matrix (matrix.matrix_value ()); | 93 retval = new octave_complex_matrix (matrix.matrix_value ()); |
94 } | 94 } |
95 else | 95 else |
96 { | 96 { |
97 if (matrix.all_elements_are_real ()) | 97 if (matrix.all_elements_are_real ()) |
310 | 310 |
311 if (swap) | 311 if (swap) |
312 swap_bytes<4> (&tmp); | 312 swap_bytes<4> (&tmp); |
313 | 313 |
314 if (tmp != -2) { | 314 if (tmp != -2) { |
315 error("load: only 2D sparse matrices are supported"); | 315 error ("load: only 2D sparse matrices are supported"); |
316 return false; | 316 return false; |
317 } | 317 } |
318 | 318 |
319 if (! is.read (reinterpret_cast<char *> (&nr), 4)) | 319 if (! is.read (reinterpret_cast<char *> (&nr), 4)) |
320 return false; | 320 return false; |
360 read_doubles (is, reinterpret_cast<double *> (m.data ()), | 360 read_doubles (is, reinterpret_cast<double *> (m.data ()), |
361 static_cast<save_type> (ctmp), 2 * nz, swap, fmt); | 361 static_cast<save_type> (ctmp), 2 * nz, swap, fmt); |
362 | 362 |
363 if (error_state || ! is) | 363 if (error_state || ! is) |
364 return false; | 364 return false; |
365 | |
366 if (! m.indices_ok ()) | |
367 return false; | |
368 | |
365 matrix = m; | 369 matrix = m; |
366 | 370 |
367 return true; | 371 return true; |
368 } | 372 } |
369 | 373 |
679 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); | 683 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
680 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | 684 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); |
681 | 685 |
682 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | 686 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
683 | 687 |
684 if (static_cast<int> (hdims[0]) != nc + 1 || | 688 if (static_cast<int> (hdims[0]) != nc + 1 |
685 static_cast<int> (hdims[1]) != 1) | 689 || static_cast<int> (hdims[1]) != 1) |
686 { | 690 { |
687 H5Sclose (space_hid); | 691 H5Sclose (space_hid); |
688 H5Dclose (data_hid); | 692 H5Dclose (data_hid); |
689 H5Gclose (group_hid); | 693 H5Gclose (group_hid); |
690 return false; | 694 return false; |
714 return false; | 718 return false; |
715 } | 719 } |
716 | 720 |
717 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | 721 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
718 | 722 |
719 if (static_cast<int> (hdims[0]) != nz || | 723 if (static_cast<int> (hdims[0]) != nz |
720 static_cast<int> (hdims[1]) != 1) | 724 || static_cast<int> (hdims[1]) != 1) |
721 { | 725 { |
722 H5Sclose (space_hid); | 726 H5Sclose (space_hid); |
723 H5Dclose (data_hid); | 727 H5Dclose (data_hid); |
724 H5Gclose (group_hid); | 728 H5Gclose (group_hid); |
725 return false; | 729 return false; |
761 return false; | 765 return false; |
762 } | 766 } |
763 | 767 |
764 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); | 768 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
765 | 769 |
766 if (static_cast<int> (hdims[0]) != nz || | 770 if (static_cast<int> (hdims[0]) != nz |
767 static_cast<int> (hdims[1]) != 1) | 771 || static_cast<int> (hdims[1]) != 1) |
768 { | 772 { |
769 H5Sclose (space_hid); | 773 H5Sclose (space_hid); |
770 H5Dclose (data_hid); | 774 H5Dclose (data_hid); |
771 H5Gclose (group_hid); | 775 H5Gclose (group_hid); |
772 return false; | 776 return false; |
773 } | 777 } |
774 | 778 |
775 Complex *ctmp = m.xdata (); | 779 Complex *ctmp = m.xdata (); |
776 bool retval = false; | 780 bool retval = false; |
777 if (H5Dread (data_hid, complex_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, ctmp) >= 0) | 781 if (H5Dread (data_hid, complex_type, H5S_ALL, H5S_ALL, |
782 H5P_DEFAULT, ctmp) >= 0 | |
783 && m.indices_ok ()) | |
778 { | 784 { |
779 retval = true; | 785 retval = true; |
780 matrix = m; | 786 matrix = m; |
781 } | 787 } |
782 | 788 |