changeset 10421:99e9bae2d81e

improve sparse indexing interface
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 18 Mar 2010 14:55:52 +0100
parents 3373fdc0b14a
children 9c4f403bec2b
files liboctave/CSparse.cc liboctave/CSparse.h liboctave/ChangeLog liboctave/MSparse.h liboctave/Sparse.cc liboctave/Sparse.h liboctave/boolSparse.cc liboctave/boolSparse.h liboctave/dSparse.cc liboctave/dSparse.h src/ov-base-sparse.cc
diffstat 11 files changed, 101 insertions(+), 148 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/CSparse.cc	Wed Mar 17 09:34:43 2010 -0400
+++ b/liboctave/CSparse.cc	Thu Mar 18 14:55:52 2010 +0100
@@ -7198,23 +7198,18 @@
 }
 
 SparseComplexMatrix
-SparseComplexMatrix::index (idx_vector& i, int resize_ok) const 
+SparseComplexMatrix::index (const idx_vector& i, bool resize_ok) const 
 { 
   return MSparse<Complex>::index (i, resize_ok); 
 }
 
 SparseComplexMatrix
-SparseComplexMatrix::index (idx_vector& i, idx_vector& j, int resize_ok) const 
+SparseComplexMatrix::index (const idx_vector& i, const idx_vector& j, bool resize_ok) const 
 { 
   return MSparse<Complex>::index (i, j, resize_ok); 
 }
   
 SparseComplexMatrix
-SparseComplexMatrix::index (Array<idx_vector>& ra_idx, int resize_ok) const 
-{ 
-  return MSparse<Complex>::index (ra_idx, resize_ok); 
-}
-SparseComplexMatrix
 SparseComplexMatrix::reshape (const dim_vector& new_dims) const
 {
   return MSparse<Complex>::reshape (new_dims);
--- a/liboctave/CSparse.h	Wed Mar 17 09:34:43 2010 -0400
+++ b/liboctave/CSparse.h	Thu Mar 18 14:55:52 2010 +0100
@@ -400,11 +400,9 @@
 
   SparseComplexMatrix squeeze (void) const;
 
-  SparseComplexMatrix index (idx_vector& i, int resize_ok) const;
+  SparseComplexMatrix index (const idx_vector& i, bool resize_ok) const;
 
-  SparseComplexMatrix index (idx_vector& i, idx_vector& j, int resize_ok) const;
-  
-  SparseComplexMatrix index (Array<idx_vector>& ra_idx, int resize_ok) const;
+  SparseComplexMatrix index (const idx_vector& i, const idx_vector& j, bool resize_ok) const;
 
   SparseComplexMatrix reshape (const dim_vector& new_dims) const;
 
--- a/liboctave/ChangeLog	Wed Mar 17 09:34:43 2010 -0400
+++ b/liboctave/ChangeLog	Thu Mar 18 14:55:52 2010 +0100
@@ -1,3 +1,14 @@
+2010-03-18  Jaroslav Hajek  <highegg@gmail.com>
+
+	* Sparse.h (Sparse<T>::index): Use const references. Use bool for
+	resize_ok. Remove Array<idx_vector> overload.
+	* MSparse.h (MSparse<T>::index): Ditto.
+	* dSparse.h, dSparse.cc (SparseMatrix::index): Ditto.
+	* CSparse.h, CSparse.cc (SparseComplexMatrix::index): Ditto.
+	* boolSparse.h, boolSparse.cc (SparseBoolMatrix::index): Ditto.
+	* Sparse.cc (Sparse<T>::index): Ditto. Redim index to 2 dimensions.
+	Don't use non-const methods. Use gripe_index_out_of_range.
+
 2010-03-17  John W. Eaton  <jwe@octave.org>
 
 	* dim-vector.h (dim_vector): Also use DIM_VECTOR macro for 2 and
--- a/liboctave/MSparse.h	Wed Mar 17 09:34:43 2010 -0400
+++ b/liboctave/MSparse.h	Thu Mar 18 14:55:52 2010 +0100
@@ -93,15 +93,12 @@
 
   MSparse<T> squeeze (void) const { return Sparse<T>::squeeze (); }
 
-  MSparse<T> index (idx_vector& i, int resize_ok) const 
+  MSparse<T> index (const idx_vector& i, bool resize_ok) const 
     { return Sparse<T>::index (i, resize_ok); }
 
-  MSparse<T> index (idx_vector& i, idx_vector& j, int resize_ok) const 
+  MSparse<T> index (const idx_vector& i, const idx_vector& j, bool resize_ok) const 
     { return Sparse<T>::index (i, j, resize_ok); }
   
-  MSparse<T> index (Array<idx_vector>& ra_idx, int resize_ok) const 
-    { return Sparse<T>::index (ra_idx, resize_ok); }
-
   MSparse<T> reshape (const dim_vector& new_dims) const
     { return Sparse<T>::reshape (new_dims); }
      
--- a/liboctave/Sparse.cc	Wed Mar 17 09:34:43 2010 -0400
+++ b/liboctave/Sparse.cc	Thu Mar 18 14:55:52 2010 +0100
@@ -1611,7 +1611,7 @@
 
 template <class T>
 Sparse<T>
-Sparse<T>::index (idx_vector& idx_arg, int resize_ok) const
+Sparse<T>::index (const idx_vector& idx_arg, bool resize_ok) const
 {
   Sparse<T> retval;
 
@@ -1621,6 +1621,7 @@
   octave_idx_type nc = dim2 ();
   octave_idx_type nz = nnz ();
 
+  // Use safe_numel so that we get an error if the matrix is too big to be indexed.
   octave_idx_type orig_len = nr * nc;
 
   dim_vector idx_orig_dims = idx_arg.orig_dimensions ();
@@ -1630,7 +1631,7 @@
 
   if (idx_orig_dims.length () > 2)
     (*current_liboctave_error_handler)
-      ("Sparse<T>::index: Can not index Sparse<T> with an N-D Array");
+      ("cannot index sparse matrix with an N-D Array");
   else if (idx_arg.is_colon ())
     {
       // Fast magic colon processing.
@@ -1646,6 +1647,10 @@
       retval.xcidx(0) = 0;
       retval.xcidx(1) = nz;
     }
+  else if (! resize_ok && idx_arg.extent (length ()) > length ())
+    {
+      gripe_index_out_of_range (1, 1, idx_arg.extent (orig_len), orig_len);
+    }
   else if (nr == 1 && nc == 1)
     {
       // You have to be pretty sick to get to this bit of code,
@@ -1653,7 +1658,7 @@
       // then want to make a dense matrix with sparse 
       // representation. Ok, we'll do it, but you deserve what 
       // you get!!
-      octave_idx_type n = idx_arg.freeze (length (), "sparse vector", resize_ok);
+      octave_idx_type n = idx_arg.length (length ());
       if (n == 0)
 
           retval = Sparse<T> (idx_orig_dims);
@@ -1709,7 +1714,7 @@
       // shape as the index.  Otherwise, it has same orientation as
       // indexed object.
       octave_idx_type len = length ();
-      octave_idx_type n = idx_arg.freeze (len, "sparse vector", resize_ok);
+      octave_idx_type n = idx_arg.length (len);
       octave_idx_type l, u;
       if (n == 0)
         if (nr == 1)
@@ -1917,74 +1922,68 @@
       // This code is only for indexing matrices.  The vector
       // cases are handled above.
 
-      idx_arg.freeze (nr * nc, "matrix", resize_ok);
-
-      if (idx_arg)
+      octave_idx_type result_nr = idx_orig_rows;
+      octave_idx_type result_nc = idx_orig_columns;
+
+      if (nz < 1)
+        retval = Sparse<T> (result_nr, result_nc);
+      else
         {
-          octave_idx_type result_nr = idx_orig_rows;
-          octave_idx_type result_nc = idx_orig_columns;
-
-          if (nz < 1)
-            retval = Sparse<T> (result_nr, result_nc);
-          else
+          // Count number of non-zero elements
+          octave_idx_type new_nzmx = 0;
+          octave_idx_type kk = 0;
+          for (octave_idx_type j = 0; j < result_nc; j++)
             {
-              // Count number of non-zero elements
-              octave_idx_type new_nzmx = 0;
-              octave_idx_type kk = 0;
-              for (octave_idx_type j = 0; j < result_nc; j++)
+              for (octave_idx_type i = 0; i < result_nr; i++)
                 {
-                  for (octave_idx_type i = 0; i < result_nr; i++)
+                  octave_quit ();
+
+                  octave_idx_type ii = idx_arg.elem (kk++);
+                  if (ii < orig_len)
                     {
-                      octave_quit ();
-                      
-                      octave_idx_type ii = idx_arg.elem (kk++);
-                      if (ii < orig_len)
+                      octave_idx_type fr = ii % nr;
+                      octave_idx_type fc = (ii - fr) / nr;
+                      for (octave_idx_type k = cidx(fc); k < cidx(fc+1); k++)
                         {
-                          octave_idx_type fr = ii % nr;
-                          octave_idx_type fc = (ii - fr) / nr;
-                          for (octave_idx_type k = cidx(fc); k < cidx(fc+1); k++)
-                            {
-                              if (ridx(k) == fr)
-                                new_nzmx++;
-                              if (ridx(k) >= fr)
-                                break;
-                            }
+                          if (ridx(k) == fr)
+                            new_nzmx++;
+                          if (ridx(k) >= fr)
+                            break;
                         }
                     }
                 }
-              
-              retval = Sparse<T> (result_nr, result_nc, new_nzmx);
-
-              kk = 0;
-              octave_idx_type jj = 0;
-              retval.xcidx(0) = 0;
-              for (octave_idx_type j = 0; j < result_nc; j++)
+            }
+
+          retval = Sparse<T> (result_nr, result_nc, new_nzmx);
+
+          kk = 0;
+          octave_idx_type jj = 0;
+          retval.xcidx(0) = 0;
+          for (octave_idx_type j = 0; j < result_nc; j++)
+            {
+              for (octave_idx_type i = 0; i < result_nr; i++)
                 {
-                  for (octave_idx_type i = 0; i < result_nr; i++)
+                  octave_quit ();
+
+                  octave_idx_type ii = idx_arg.elem (kk++);
+                  if (ii < orig_len)
                     {
-                      octave_quit ();
-
-                      octave_idx_type ii = idx_arg.elem (kk++);
-                      if (ii < orig_len)
+                      octave_idx_type fr = ii % nr;
+                      octave_idx_type fc = (ii - fr) / nr;
+                      for (octave_idx_type k = cidx(fc); k < cidx(fc+1); k++)
                         {
-                          octave_idx_type fr = ii % nr;
-                          octave_idx_type fc = (ii - fr) / nr;
-                          for (octave_idx_type k = cidx(fc); k < cidx(fc+1); k++)
+                          if (ridx(k) == fr)
                             {
-                              if (ridx(k) == fr)
-                                {
-                                  retval.xdata(jj) = data(k);
-                                  retval.xridx(jj++) = i;
-                                }
-                              if (ridx(k) >= fr)
-                                break;
+                              retval.xdata(jj) = data(k);
+                              retval.xridx(jj++) = i;
                             }
+                          if (ridx(k) >= fr)
+                            break;
                         }
                     }
-                  retval.xcidx(j+1) = jj;
                 }
+              retval.xcidx(j+1) = jj;
             }
-          // idx_vector::freeze() printed an error message for us.
         }
     }
 
@@ -2000,7 +1999,7 @@
 
 template <class T>
 Sparse<T>
-Sparse<T>::index (idx_vector& idx_i, idx_vector& idx_j, int resize_ok) const
+Sparse<T>::index (const idx_vector& idx_i, const idx_vector& idx_j, bool resize_ok) const
 {
   Sparse<T> retval;
 
@@ -2009,19 +2008,23 @@
   octave_idx_type nr = dim1 ();
   octave_idx_type nc = dim2 ();
 
-  octave_idx_type n = idx_i.freeze (nr, "row", resize_ok);
-  octave_idx_type m = idx_j.freeze (nc, "column", resize_ok);
-
-  if (idx_i && idx_j)
+  octave_idx_type n = idx_i.length (nr);
+  octave_idx_type m = idx_j.length (nc);
+
+  if (! resize_ok && idx_i.extent (nr) > nr)
+    gripe_index_out_of_range (2, 1, idx_i.extent (nr), nr);
+  else if (! resize_ok && idx_j.extent (nc) > nc)
+    gripe_index_out_of_range (2, 2, idx_j.extent (nc), nc);
+  else
     {
-      if (idx_i.orig_empty () || idx_j.orig_empty () || n == 0 || m == 0)
+      if (n == 0 || m == 0)
         {
           retval.resize_no_fill (n, m);
         }
       else 
         {
-          int idx_i_colon = idx_i.is_colon_equiv (nr);
-          int idx_j_colon = idx_j.is_colon_equiv (nc);
+          bool idx_i_colon = idx_i.is_colon_equiv (nr);
+          bool idx_j_colon = idx_j.is_colon_equiv (nc);
 
           if (idx_i_colon && idx_j_colon)
             {
@@ -2213,25 +2216,10 @@
             }
         }
     }
-  // idx_vector::freeze() printed an error message for us.
 
   return retval;
 }
 
-template <class T>
-Sparse<T>
-Sparse<T>::index (Array<idx_vector>& ra_idx, int resize_ok) const
-{
-
-  if (ra_idx.length () != 2)
-    {
-      (*current_liboctave_error_handler) ("range error for index");
-      return *this;
-    }
-
-  return index (ra_idx (0), ra_idx (1), resize_ok);
-}
-
 // Can't use versions of these in Array.cc due to duplication of the 
 // instantiations for Array<double and Sparse<double>, etc
 template <class T>
--- a/liboctave/Sparse.h	Wed Mar 17 09:34:43 2010 -0400
+++ b/liboctave/Sparse.h	Thu Mar 18 14:55:52 2010 +0100
@@ -495,11 +495,9 @@
 
   Sparse<T> value (void);
 
-  Sparse<T> index (idx_vector& i, int resize_ok = 0) const;
+  Sparse<T> index (const idx_vector& i, bool resize_ok = false) const;
 
-  Sparse<T> index (idx_vector& i, idx_vector& j, int resize_ok = 0) const;
-
-  Sparse<T> index (Array<idx_vector>& ra_idx, int resize_ok = 0) const;
+  Sparse<T> index (const idx_vector& i, const idx_vector& j, bool resize_ok = false) const;
 
   void print_info (std::ostream& os, const std::string& prefix) const;
 
--- a/liboctave/boolSparse.cc	Wed Mar 17 09:34:43 2010 -0400
+++ b/liboctave/boolSparse.cc	Thu Mar 18 14:55:52 2010 +0100
@@ -192,24 +192,18 @@
 }
 
 SparseBoolMatrix
-SparseBoolMatrix::index (idx_vector& i, int resize_ok) const 
+SparseBoolMatrix::index (const idx_vector& i, bool resize_ok) const 
 { 
   return Sparse<bool>::index (i, resize_ok); 
 }
 
 SparseBoolMatrix
-SparseBoolMatrix::index (idx_vector& i, idx_vector& j, int resize_ok) const 
+SparseBoolMatrix::index (const idx_vector& i, const idx_vector& j, bool resize_ok) const 
 { 
   return Sparse<bool>::index (i, j, resize_ok); 
 }
   
 SparseBoolMatrix
-SparseBoolMatrix::index (Array<idx_vector>& ra_idx, int resize_ok) const 
-{ 
-  return Sparse<bool>::index (ra_idx, resize_ok); 
-}
-
-SparseBoolMatrix
 SparseBoolMatrix::reshape (const dim_vector& new_dims) const
 {
   return Sparse<bool>::reshape (new_dims);
--- a/liboctave/boolSparse.h	Wed Mar 17 09:34:43 2010 -0400
+++ b/liboctave/boolSparse.h	Thu Mar 18 14:55:52 2010 +0100
@@ -94,11 +94,9 @@
 
   SparseBoolMatrix squeeze (void) const;
 
-  SparseBoolMatrix index (idx_vector& i, int resize_ok) const;
+  SparseBoolMatrix index (const idx_vector& i, bool resize_ok) const;
 
-  SparseBoolMatrix index (idx_vector& i, idx_vector& j, int resize_ok) const;
-  
-  SparseBoolMatrix index (Array<idx_vector>& ra_idx, int resize_ok) const;
+  SparseBoolMatrix index (const idx_vector& i, const idx_vector& j, bool resize_ok) const;
 
   SparseBoolMatrix reshape (const dim_vector& new_dims) const;
 
--- a/liboctave/dSparse.cc	Wed Mar 17 09:34:43 2010 -0400
+++ b/liboctave/dSparse.cc	Thu Mar 18 14:55:52 2010 +0100
@@ -7599,24 +7599,18 @@
 }
 
 SparseMatrix
-SparseMatrix::index (idx_vector& i, int resize_ok) const 
+SparseMatrix::index (const idx_vector& i, bool resize_ok) const 
 { 
   return MSparse<double>::index (i, resize_ok); 
 }
 
 SparseMatrix
-SparseMatrix::index (idx_vector& i, idx_vector& j, int resize_ok) const 
+SparseMatrix::index (const idx_vector& i, const idx_vector& j, bool resize_ok) const 
 { 
   return MSparse<double>::index (i, j, resize_ok); 
 }
   
 SparseMatrix
-SparseMatrix::index (Array<idx_vector>& ra_idx, int resize_ok) const 
-{ 
-  return MSparse<double>::index (ra_idx, resize_ok); 
-}
-
-SparseMatrix
 SparseMatrix::reshape (const dim_vector& new_dims) const
 {
   return MSparse<double>::reshape (new_dims);
--- a/liboctave/dSparse.h	Wed Mar 17 09:34:43 2010 -0400
+++ b/liboctave/dSparse.h	Thu Mar 18 14:55:52 2010 +0100
@@ -410,11 +410,9 @@
 
   SparseMatrix squeeze (void) const;
 
-  SparseMatrix index (idx_vector& i, int resize_ok) const;
+  SparseMatrix index (const idx_vector& i, bool resize_ok) const;
 
-  SparseMatrix index (idx_vector& i, idx_vector& j, int resize_ok) const;
-  
-  SparseMatrix index (Array<idx_vector>& ra_idx, int resize_ok) const;
+  SparseMatrix index (const idx_vector& i, const idx_vector& j, bool resize_ok) const;
 
   SparseMatrix reshape (const dim_vector& new_dims) const;
 
--- a/src/ov-base-sparse.cc	Wed Mar 17 09:34:43 2010 -0400
+++ b/src/ov-base-sparse.cc	Thu Mar 18 14:55:52 2010 +0100
@@ -51,8 +51,6 @@
 
   octave_idx_type n_idx = idx.length ();
 
-  int nd = matrix.ndims ();
-
   switch (n_idx)
     {
     case 0:
@@ -68,37 +66,21 @@
       }
       break;
 
-    default:
+    case 2:
       {
-        if (n_idx == 2 && nd == 2)
+        idx_vector i = idx (0).index_vector ();
+
+        if (! error_state)
           {
-            idx_vector i = idx (0).index_vector ();
+            idx_vector j = idx (1).index_vector ();
 
             if (! error_state)
-              {
-                idx_vector j = idx (1).index_vector ();
-
-                if (! error_state)
-                  retval = octave_value (matrix.index (i, j, resize_ok));
-              }
-          }
-        else
-          {
-            Array<idx_vector> idx_vec (n_idx, 1);
-
-            for (octave_idx_type i = 0; i < n_idx; i++)
-              {
-                idx_vec(i) = idx(i).index_vector ();
-
-                if (error_state)
-                  break;
-              }
-
-            if (! error_state)
-              retval = octave_value (matrix.index (idx_vec, resize_ok));
+              retval = octave_value (matrix.index (i, j, resize_ok));
           }
       }
       break;
+    default:
+      error ("sparse indexing needs 1 or 2 indices");
     }
 
   return retval;