# HG changeset patch # User Jaroslav Hajek # Date 1277462934 -7200 # Node ID d808eb829d4896fe5a93630af7565949dee8de35 # Parent 6ba7937a6fa4231fdd03f5eb7236673175b81a90 optimize num2cell on structs diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2010-06-25 Jaroslav Hajek + + * DLD-FUNCTIONS/cellfun.cc (Fnum2cell, do_num2cell): Optimize cells + and structs. + 2010-06-25 Jaroslav Hajek * ov-struct.h (octave_scalar_struct::resize, octave_struct::resize): diff --git a/src/DLD-FUNCTIONS/cellfun.cc b/src/DLD-FUNCTIONS/cellfun.cc --- a/src/DLD-FUNCTIONS/cellfun.cc +++ b/src/DLD-FUNCTIONS/cellfun.cc @@ -906,6 +906,16 @@ } template +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 static Cell do_num2cell (const NDA& array, const Array& 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 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); } diff --git a/src/oct-map.h b/src/oct-map.h --- 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) { }