changeset 10703:5eb420d92307

fix sort and nth_element when trailing singleton dim is specified
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 14 Jun 2010 14:53:11 +0200
parents c49911ab7ac7
children 0bcd17cad9d1
files liboctave/Array.cc liboctave/ChangeLog liboctave/Sparse.cc src/ChangeLog src/data.cc
diffstat 5 files changed, 24 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/Array.cc	Mon Jun 14 10:41:47 2010 +0200
+++ b/liboctave/Array.cc	Mon Jun 14 14:53:11 2010 +0200
@@ -1682,7 +1682,7 @@
 Array<T>
 Array<T>::sort (int dim, sortmode mode) const
 {
-  if (dim < 0 || dim >= ndims ())
+  if (dim < 0)
     {
       (*current_liboctave_error_handler)
         ("sort: invalid dimension");
@@ -1696,6 +1696,9 @@
   if (m.length () < 1)
     return m;
 
+  if (dim >= dv.length ())
+    dv.resize (dim+1, 1);
+
   octave_idx_type ns = dv(dim);
   octave_idx_type iter = dv.numel () / ns;
   octave_idx_type stride = 1;
@@ -2228,7 +2231,7 @@
 Array<T>
 Array<T>::nth_element (const idx_vector& n, int dim) const
 {
-  if (dim < 0 || dim >= ndims ())
+  if (dim < 0)
     {
       (*current_liboctave_error_handler)
         ("nth_element: invalid dimension");
@@ -2236,6 +2239,9 @@
     }
 
   dim_vector dv = dims ();
+  if (dim >= dv.length ())
+    dv.resize (dim+1, 1);
+
   octave_idx_type ns = dv(dim);
 
   octave_idx_type nn = n.length (ns);
--- a/liboctave/ChangeLog	Mon Jun 14 10:41:47 2010 +0200
+++ b/liboctave/ChangeLog	Mon Jun 14 14:53:11 2010 +0200
@@ -1,3 +1,9 @@
+2010-06-14  Jaroslav Hajek  <highegg@gmail.com>
+
+	* Array.cc (Array::sort, Array::nth_element): Fix behavior when
+	trailing singleton dim is specified.
+	* Sparse.cc (Sparse::sort): Ditto.
+
 2010-06-14  Jaroslav Hajek  <highegg@gmail.com>
 
 	* idx-vector.cc (idx_vector::copy_data): Handle class_mask case.
--- a/liboctave/Sparse.cc	Mon Jun 14 10:41:47 2010 +0200
+++ b/liboctave/Sparse.cc	Mon Jun 14 14:53:11 2010 +0200
@@ -2050,7 +2050,7 @@
   octave_idx_type nr = m.rows ();
   octave_idx_type nc = m.columns ();
 
-  if (m.length () < 1)
+  if (m.length () < 1 || dim > 1)
     return m;
 
   if (dim > 0)
@@ -2116,9 +2116,9 @@
   octave_idx_type nr = m.rows ();
   octave_idx_type nc = m.columns ();
 
-  if (m.length () < 1)
+  if (m.length () < 1 || dim > 1)
     {
-      sidx = Array<octave_idx_type> (dim_vector (nr, nc));
+      sidx = Array<octave_idx_type> (dim_vector (nr, nc), 1);
       return m;
     }
 
--- a/src/ChangeLog	Mon Jun 14 10:41:47 2010 +0200
+++ b/src/ChangeLog	Mon Jun 14 14:53:11 2010 +0200
@@ -1,3 +1,8 @@
+2010-06-14  Jaroslav Hajek  <highegg@gmail.com>
+
+	* data.cc (Fsort, Fnth_element): Fix behavior when trailing singleton
+	dim is specified.
+
 2010-06-12  Ben Abbott <bpabbott@mac.com>
 
 	* DLD-FUNCTIONS/gammainc.cc: Fix doc-string typo.
--- a/src/data.cc	Mon Jun 14 10:41:47 2010 +0200
+++ b/src/data.cc	Mon Jun 14 14:53:11 2010 +0200
@@ -5328,7 +5328,7 @@
     }
   else
     {
-      if (dim < 0 || dim > dv.length () - 1)
+      if (dim < 0)
         {
           error ("sort: dim must be a valid dimension");
           return retval;
@@ -5740,7 +5740,7 @@
       if (nargin == 3)
         {
           dim = args(2).int_value (true) - 1;
-          if (dim < 0 || dim >= argx.ndims ())
+          if (dim < 0)
             error ("nth_element: dim must be a valid dimension");
         }
       if (dim < 0)