changeset 8662:af72c8137d64

improve looping over arrays
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 04 Feb 2009 13:53:57 +0100
parents 9c092b111b1d
children 4238f2600a17
files src/ChangeLog src/pt-eval.cc src/pt-loop.cc src/pt-loop.h
diffstat 4 files changed, 35 insertions(+), 160 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog	Wed Feb 04 10:18:25 2009 +0100
+++ b/src/ChangeLog	Wed Feb 04 13:53:57 2009 +0100
@@ -1,3 +1,12 @@
+2009-02-04  Jaroslav Hajek  <highegg@gmail.com>
+
+	* pt-loop.h (tree_simple_for_command::do_for_loop_once,
+	tree_complex_for_command::do_for_loop_once): Remove obsolete decls.
+	* pt-loop.cc (quit_loop_now): Remove obsolete method.
+	* pt-eval.cc (DO_ND_LOOP): Delete macro.
+	(tree_evaluator::visit_simple_for_command): Use the generic
+	do_index_op for iterating matrices.
+
 2009-02-04  John W. Eaton  <jwe@octave.org>
 
 	New evaluator and debugger derived from tree-walker class.
--- a/src/pt-eval.cc	Wed Feb 04 10:18:25 2009 +0100
+++ b/src/pt-eval.cc	Wed Feb 04 13:53:57 2009 +0100
@@ -258,83 +258,6 @@
     } \
   while (0)
 
-#define DO_ND_LOOP(MTYPE, TYPE, CONV, ARG) \
-  do \
-    { \
-      dim_vector dv = ARG.dims (); \
- \
-      bool quit = false; \
- \
-      TYPE *atmp = ARG.fortran_vec (); \
- \
-      octave_idx_type steps = dv(1); \
- \
-      octave_idx_type nrows = dv(0); \
- \
-      int ndims = dv.length (); \
-      if (ndims > 2) \
-        { \
-          for (int i = 2; i < ndims; i++) \
-            steps *= dv(i); \
-          dv(1) = steps; \
-          dv.resize (2); \
-        } \
- \
-      if (steps > 0) \
-	{ \
-          if (nrows == 0) \
-            { \
-	      MTYPE tarray (dim_vector (0, 1)); \
- \
-	      octave_value val (tarray); \
- \
-	      for (octave_idx_type i = 0; i < steps; i++) \
-		{ \
-	          DO_SIMPLE_FOR_LOOP_ONCE (val); \
- \
-	          if (quit) \
-	            break; \
-	       } \
-            } \
-          else if (nrows == 1) \
-            { \
-	      for (octave_idx_type i = 0; i < steps; i++) \
-		{ \
-		  octave_value val (CONV (*atmp++)); \
- \
-	          DO_SIMPLE_FOR_LOOP_ONCE (val); \
- \
-	          if (quit) \
-	            break; \
-	       } \
-            } \
-          else \
-            { \
-              if (ndims > 2) \
-                ARG = ARG.reshape (dv); \
- \
-              MTYPE tmp (dim_vector (nrows, 1)); \
- \
-              TYPE *ftmp = tmp.fortran_vec (); \
- \
-              for (octave_idx_type i = 0; i < steps; i++) \
-	        { \
- 	          for (int j = 0; j < nrows; j++) \
-	            ftmp[j] = *atmp++;  \
- \
-                  octave_value val (tmp); \
- \
-                  DO_SIMPLE_FOR_LOOP_ONCE (val); \
-                  quit = (i == steps - 1 ? true : quit); \
- \
-	          if (quit) \
-	            break; \
-	        } \
-	    } \
-        } \
-    } \
-  while (0)
-
 void
 tree_evaluator::visit_simple_for_command (tree_simple_for_command& cmd)
 {
@@ -398,61 +321,39 @@
 
 	DO_SIMPLE_FOR_LOOP_ONCE (rhs);
       }
-    else if (rhs.is_string ())
+    else if (rhs.is_matrix_type () 
+             || rhs.is_cell () || rhs.is_string ())
       {
-	charMatrix chm_tmp = rhs.char_matrix_value ();
-	octave_idx_type nr = chm_tmp.rows ();
-	octave_idx_type steps = chm_tmp.columns ();
-	bool quit = false;
-
-	if (error_state)
-	  goto cleanup;
+        // A matrix or cell is reshaped to 2 dimensions and iterated by
+        // columns.
 
-	if (nr == 1)
-	  {
-	    for (octave_idx_type i = 0; i < steps; i++)
-	      {
-		octave_value val (chm_tmp.xelem (0, i));
-
-		DO_SIMPLE_FOR_LOOP_ONCE (val);
+        bool quit = false;
 
-		if (quit)
-		  break;
-	      }
-	  }
-	else
-	  {
-	    for (octave_idx_type i = 0; i < steps; i++)
-	      {
-		octave_value val (chm_tmp.extract (0, i, nr-1, i), true);
+        dim_vector dv = rhs.dims ().redim (2);
+
+        octave_idx_type steps = dv(1);
 
-		DO_SIMPLE_FOR_LOOP_ONCE (val);
+        if (steps > 0)
+          {
+            octave_value arg = rhs;
+            if (rhs.ndims () > 2)
+              arg = arg.reshape (dv);
 
-		if (quit)
-		  break;
-	      }
-	  }
-      }
-    else if (rhs.is_matrix_type ())
-      {
-	if (rhs.is_real_type ())
-	  {
-	    NDArray m_tmp = rhs.array_value ();
+            //octave_value_list idx(2, octave_value ());
+            octave_value_list idx(2, octave_value ());
+            idx(0) = octave_value::magic_colon_t;
 
-	    if (error_state)
-	      goto cleanup;
+            for (octave_idx_type i = 1; i <= steps; i++)
+              {
+                // do_index_op expects one-based indices.
+                idx(1) = i;
+                octave_value val = arg.do_index_op (idx);
+                DO_SIMPLE_FOR_LOOP_ONCE (val);
 
-	    DO_ND_LOOP (NDArray, double, , m_tmp);
-	  }
-	else
-	  {
-	    ComplexNDArray cm_tmp = rhs.complex_array_value ();
-
-	    if (error_state)
-	      goto cleanup;
-
-	    DO_ND_LOOP (ComplexNDArray, Complex, , cm_tmp);
-	  }
+                if (quit)
+                  break;
+              }
+          }
       }
     else if (rhs.is_map ())
       {
@@ -475,12 +376,6 @@
 	      break;
 	  }
       }
-    else if (rhs.is_cell ())
-      {
-	Cell c_tmp = rhs.cell_value ();
-
-	DO_ND_LOOP (Cell, octave_value, Cell, c_tmp);
-      }
     else
       {
 	::error ("invalid type in for loop expression near line %d, column %d",
--- a/src/pt-loop.cc	Wed Feb 04 10:18:25 2009 +0100
+++ b/src/pt-loop.cc	Wed Feb 04 13:53:57 2009 +0100
@@ -45,28 +45,6 @@
 // TRUE means we are evaluating some kind of looping construct.
 bool evaluating_looping_command = false;
 
-// Decide if it's time to quit a for or while loop.
-static inline bool
-quit_loop_now (void)
-{
-  OCTAVE_QUIT;
-
-  // Maybe handle `continue N' someday...
-
-  if (tree_continue_command::continuing)
-    tree_continue_command::continuing--;
-
-  bool quit = (error_state
-	       || tree_return_command::returning
-	       || tree_break_command::breaking
-	       || tree_continue_command::continuing);
-
-  if (tree_break_command::breaking)
-    tree_break_command::breaking--;
-
-  return quit;
-}
-
 // While.
 
 tree_while_command::~tree_while_command (void)
--- a/src/pt-loop.h	Wed Feb 04 10:18:25 2009 +0100
+++ b/src/pt-loop.h	Wed Feb 04 13:53:57 2009 +0100
@@ -194,9 +194,6 @@
   // Comment preceding ENDFOR token.
   octave_comment_list *trail_comm;
 
-  void do_for_loop_once (octave_lvalue &ult, const octave_value& rhs,
-			 bool& quit);
-
   // No copying!
 
   tree_simple_for_command (const tree_simple_for_command&);
@@ -255,10 +252,6 @@
   // Comment preceding ENDFOR token.
   octave_comment_list *trail_comm;
 
-  void do_for_loop_once (octave_lvalue &val_ref, octave_lvalue &key_ref,
-			 const octave_value& val, const octave_value& key,
-			 bool& quit);
-
   // No copying!
 
   tree_complex_for_command (const tree_complex_for_command&);