diff src/ov-range.cc @ 9986:672e1b49e01e

optimize indexing of ranges by single subscripts
author Jaroslav Hajek <highegg@gmail.com>
date Tue, 15 Dec 2009 10:04:34 +0100
parents ac69e6f4b33d
children cd96d29c5efa
line wrap: on
line diff
--- a/src/ov-range.cc	Tue Dec 15 10:01:35 2009 +0100
+++ b/src/ov-range.cc	Tue Dec 15 10:04:34 2009 +0100
@@ -118,19 +118,28 @@
 octave_value
 octave_range::do_index_op (const octave_value_list& idx, bool resize_ok)
 {
-  // FIXME -- this doesn't solve the problem of
-  //
-  //   a = 1:5; a(1, 1, 1)
-  //
-  // and similar constructions.  Hmm...
+  if (idx.length () == 1 && ! resize_ok)
+    {
+      octave_value retval;
 
-  // FIXME -- using this constructor avoids possibly narrowing
-  // the range to a scalar value.  Need a better solution to this
-  // problem.
+      // The range can handle a single subscript.
+      idx_vector i = idx(0).index_vector ();
+      if (! error_state)
+        {
+          if (i.is_scalar () && i(0) < range.nelem ())
+            retval = range.elem (i(0));
+          else
+            retval = range.index (i);
+        }
 
-  octave_value tmp (new octave_matrix (range.matrix_value ()));
+      return retval;
+    }
+  else
+    {
+      octave_value tmp (new octave_matrix (range.matrix_value ()));
 
-  return tmp.do_index_op (idx, resize_ok);
+      return tmp.do_index_op (idx, resize_ok);
+    }
 }
 
 double