changeset 10756:d808eb829d48

optimize num2cell on structs
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 25 Jun 2010 12:48:54 +0200
parents 6ba7937a6fa4
children 1cc44f3ec814
files src/ChangeLog src/DLD-FUNCTIONS/cellfun.cc src/oct-map.h
diffstat 3 files changed, 22 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2010-06-25  Jaroslav Hajek  <highegg@gmail.com>
+
+	* DLD-FUNCTIONS/cellfun.cc (Fnum2cell, do_num2cell): Optimize cells
+	and structs.
+
 2010-06-25  Jaroslav Hajek  <highegg@gmail.com>
 
 	* ov-struct.h (octave_scalar_struct::resize, octave_struct::resize):
--- a/src/DLD-FUNCTIONS/cellfun.cc
+++ b/src/DLD-FUNCTIONS/cellfun.cc
@@ -906,6 +906,16 @@
 }
 
 template<class NDA>
+static inline typename NDA::element_type
+do_num2cell_elem (const NDA& array, octave_idx_type i)
+{ return array(i); }
+
+static inline Cell
+do_num2cell_elem (const Cell& array, octave_idx_type i)
+{ return Cell (array(i)); }
+
+
+template<class NDA>
 static Cell
 do_num2cell (const NDA& array, const Array<int>& dimv)
 {
@@ -914,7 +924,7 @@
       Cell retval (array.dims ());
       octave_idx_type nel = array.numel ();
       for (octave_idx_type i = 0; i < nel; i++)
-        retval.xelem (i) = array(i);
+        retval.xelem (i) = do_num2cell_elem (array, i);
 
       return retval;
     }
@@ -1030,34 +1040,10 @@
                 retval = do_num2cell (array.array_value (), dimv);
             }
         }
-      else if (array.is_cell () || array.is_map ())
-        {
-          dim_vector celldv, arraydv;
-          Array<int> perm;
-          do_num2cell_helper (array.dims (), dimv, celldv, arraydv, perm);
-
-          if (! error_state)
-            {
-              // FIXME: this operation may be rather inefficient.
-              octave_value parray = array.permute (perm);
-
-              octave_idx_type nela = arraydv.numel (), nelc = celldv.numel ();
-              parray = parray.reshape (dim_vector (nela, nelc));
-
-              Cell retcell (celldv);
-              octave_value_list idx (2);
-              idx(0) = octave_value::magic_colon_t;
-
-              for (octave_idx_type i = 0; i < nelc; i++)
-                {
-                  idx(1) = i + 1;
-                  octave_value tmp = parray.do_index_op (idx);
-                  retcell(i) = tmp.reshape (arraydv);
-                }
-
-              retval = retcell;
-            }
-        }
+      else if (array.is_map ())
+        retval = do_num2cell (array.map_value (), dimv);
+      else if (array.is_cell ())
+        retval = do_num2cell (array.cell_value (), dimv);
       else
         gripe_wrong_type_arg ("num2cell", array);
     }
--- a/src/oct-map.h
+++ b/src/oct-map.h
@@ -252,6 +252,8 @@
 
 public:
 
+  typedef octave_scalar_map element_type;
+
   octave_map (void) : xkeys (), xvals (), dimensions () { }
 
   octave_map (const dim_vector& dv) : xkeys (), xvals (), dimensions (dv) { }