changeset 10285:22a7913bbeb5

optimize return values of find and sort
author Jaroslav Hajek <highegg@gmail.com>
date Tue, 09 Feb 2010 12:56:47 +0100
parents c3df189b1b15
children 8cf666139297
files liboctave/ChangeLog liboctave/idx-vector.cc liboctave/idx-vector.h src/ChangeLog src/DLD-FUNCTIONS/find.cc src/data.cc
diffstat 6 files changed, 48 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/ChangeLog
+++ b/liboctave/ChangeLog
@@ -1,3 +1,11 @@
+2010-02-09  Jaroslav Hajek  <highegg@gmail.com>
+
+	* idx-vector.cc (idx_vector::idx_vector_rep::idx_vector_rep (const
+	Array<octave_idx_type>&, octave_idx_type, direct)): New constructor.
+	* idx-vector.h: Declare it.
+	(idx_vector::idx_vector (const Array<octave_idx_type>&,
+	octave_idx_type)): New constructor.
+
 2010-02-08  John W. Eaton  <jwe@octave.org>
 
 	* oct-time.cc: Include "strftime.h", not <strftime.h>.
--- a/liboctave/idx-vector.cc
+++ b/liboctave/idx-vector.cc
@@ -330,6 +330,23 @@
     }
 }
 
+idx_vector::idx_vector_rep::idx_vector_rep (const Array<octave_idx_type>& inda,
+                                            octave_idx_type _ext, direct)
+  : data (inda.data ()), len (inda.numel ()), ext (_ext), 
+  aowner (new Array<octave_idx_type> (inda)), orig_dims (inda.dims ())
+{
+  // No checking.
+  if (ext < 0)
+    {
+      octave_idx_type max = -1;
+      for (octave_idx_type i = 0; i < len; i++)
+        if (data[i] > max)
+          max = data[i];
+
+      ext = max + 1;
+    }
+}
+
 idx_vector::idx_vector_rep::idx_vector_rep (bool b)
   : data (0), len (b ? 1 : 0), ext (0), aowner (0), orig_dims (len, len)
 {
--- a/liboctave/idx-vector.h
+++ b/liboctave/idx-vector.h
@@ -287,6 +287,9 @@
     // Zero-based constructor.
     idx_vector_rep (const Array<octave_idx_type>& inda);
 
+    idx_vector_rep (const Array<octave_idx_type>& inda,
+                    octave_idx_type _ext, direct);
+
     template <class T>
     idx_vector_rep (const Array<T>&);
 
@@ -473,6 +476,11 @@
     : rep (new idx_vector_rep (inda))
     { chkerr (); }
 
+  // Directly pass extent, no checking.
+  idx_vector (const Array<octave_idx_type>& inda, octave_idx_type ext) 
+    : rep (new idx_vector_rep (inda, ext, DIRECT))
+    { }
+
   // Colon is best constructed by simply copying (or referencing) this member.
   static const idx_vector colon;
 
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,9 @@
+2010-02-09  Jaroslav Hajek  <highegg@gmail.com>
+
+	* DLD-FUNCTIONS/find.cc (find_nonzero_elem_idx (const Array<T>&, ...)): 
+	Optimize creation of result indices.
+	* data.cc (Fsort): Ditto.
+
 2010-02-09  Jaroslav Hajek  <highegg@gmail.com>
 
 	* DLD-FUNCTIONS/colamd.cc (Fcolamd, Fsymamd, Fetree): Fix improper arg
--- a/src/DLD-FUNCTIONS/find.cc
+++ b/src/DLD-FUNCTIONS/find.cc
@@ -49,6 +49,9 @@
   else
     idx = nda.find ();
 
+  // The maximum element is always at the end.
+  octave_idx_type iext = idx.is_empty () ? 0 : idx.xelem (idx.numel () - 1) + 1;
+
   switch (nargout)
     {
     default:
@@ -65,13 +68,14 @@
             jdx.xelem (i) = idx.xelem (i) / nr;
             idx.xelem (i) %= nr;
           }
-        retval(1) = octave_value (jdx, true, true);
+        iext = -1;
+        retval(1) = idx_vector (jdx, -1);
       }
       // Fall through!
 
     case 1:
     case 0:
-      retval(0) = octave_value (idx, true, true);
+      retval(0) = idx_vector (idx, iext);
       break;
     }
 
--- a/src/data.cc
+++ b/src/data.cc
@@ -5810,21 +5810,11 @@
 	}
     }
 
-  dim_vector dv = arg.dims ();
-  if (error_state)
-    {
-      gripe_wrong_type_arg ("sort", arg);
-      return retval;
-    }
+  const dim_vector dv = arg.dims ();
   if (nargin == 1 || args(1).is_string ())
     {
       // Find first non singleton dimension
-      for (int i = 0; i < dv.length (); i++)
-	if (dv(i) > 1)
-	  {
-	    dim = i;
-	    break;
-	  }
+      dim = dv.first_non_singleton ();
     }
   else
     {
@@ -5840,7 +5830,7 @@
       Array<octave_idx_type> sidx;
 
       retval (0) = arg.sort (sidx, dim, smode);
-      retval (1) = octave_value (sidx, true, true);
+      retval (1) = idx_vector (sidx, dv(dim)); // No checking, the extent is known.
     }
   else
     retval(0) = arg.sort (dim, smode);