changeset 7751:7c020c067a60

F__end__: correctly handle fewer indices than dimensions
author John W. Eaton <jwe@octave.org>
date Sat, 03 May 2008 22:48:24 -0400
parents 5c6c6f4803c8
children 40c428ea3408
files src/ChangeLog src/pt-arg-list.cc
diffstat 2 files changed, 29 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Sat May 03 22:07:26 2008 -0400
+++ b/src/ChangeLog	Sat May 03 22:48:24 2008 -0400
@@ -1,5 +1,10 @@
 2008-05-03  John W. Eaton  <jwe@octave.org>
 
+	* pt-arg-list.cc (F__end__): If there are more dimensions than
+	indices, smash extra dimensions first.
+	(num_indices): New static variable.
+	(tree_argument_list::convert_to_const_vector): Save and set it.
+
 	* parse.y (parse_fcn_file): Also temporarily set parser_end_of_input
 	and get_input_from_eval_string to false while reading script files.
 
--- a/src/pt-arg-list.cc	Sat May 03 22:07:26 2008 -0400
+++ b/src/pt-arg-list.cc	Sat May 03 22:48:24 2008 -0400
@@ -94,6 +94,7 @@
 
 static const octave_value *indexed_object = 0;
 static int index_position = 0;
+static int num_indices = 0;
 
 DEFCONSTFUN (__end__, , ,
   "internal function")
@@ -103,34 +104,30 @@
   if (indexed_object)
     {
       dim_vector dv = indexed_object->dims ();
+      int ndims = dv.length ();
 
-      switch (index_position)
+      if (num_indices < ndims)
 	{
-	case -1:
-	  {
-	    octave_idx_type numel = dv.numel ();
+	  for (int i = num_indices; i < ndims; i++)
+	    dv(num_indices-1) *= dv(i);
 
-	    if (numel < 0)
-	      {
-		std::string dv_str = dv.str ();
-		::error ("invalid use of end: (index 1, dims %s)",
-			 dv_str.c_str ());
-	      }
-	    else
-	      retval = numel;
-	  }
-	  break;
+	  if (num_indices == 1)
+	    {
+	      ndims = 2;
+	      dv.resize (ndims);
+	      dv(1) = 1;
+	    }
+	  else
+	    {
+	      ndims = num_indices;
+	      dv.resize (ndims);
+	    }
+	}
 
-	default:
-	  {
-
-	    if (index_position < dv.length ())
-	      retval = dv(index_position);
-	    else
-	      retval = 1;
-	  }
-	  break;
-	}
+      if (index_position < ndims)
+	retval = dv(index_position);
+      else
+	retval = 1;
     }
   else
     ::error ("invalid use of end");
@@ -171,8 +168,10 @@
       if (stash_object)
 	{
 	  unwind_protect_int (index_position);
+	  unwind_protect_int (num_indices);
 
-	  index_position = (len == 1) ? -1 : k;
+	  index_position = k;
+	  num_indices = len;
 	}
 
       tree_expression *elt = *p++;