diff src/DLD-FUNCTIONS/cellfun.cc @ 10756:d808eb829d48

optimize num2cell on structs
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 25 Jun 2010 12:48:54 +0200
parents fbd7843974fa
children 146a97c3bc97
line wrap: on
line diff
--- a/src/DLD-FUNCTIONS/cellfun.cc	Fri Jun 25 10:47:14 2010 +0200
+++ b/src/DLD-FUNCTIONS/cellfun.cc	Fri Jun 25 12:48:54 2010 +0200
@@ -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);
     }