# HG changeset patch # User Jaroslav Hajek # Date 1277719608 -7200 # Node ID f3892d8eea9faa1c3f731d5d0476bbe44af52938 # Parent 1cc44f3ec8141ad5d0a662a537cdd83ab7647888 optimize horzcat/vertcat for scalars, cells and structs diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,8 @@ +2010-06-28 Jaroslav Hajek + + * dim-vector.h (dim_vector::scalar_1x1): New method. + * lo-traits.h (equal_types): Fix. + 2010-06-21 Jaroslav Hajek * Array.cc (Array::cat): Implement the loose horzcat/vertcat rules diff --git a/liboctave/lo-traits.h b/liboctave/lo-traits.h --- a/liboctave/lo-traits.h +++ b/liboctave/lo-traits.h @@ -62,7 +62,7 @@ { public: - static const bool value = false; + static const bool value = true; }; // Determine whether a type is an instance of a template. diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2010-06-28 Jaroslav Hajek + + * data.cc (single_type_concat): Optimize all scalars case where + applicable. + (single_type_concat_map, do_single_type_concat_map): New funcs. + * pt-mat.cc (get_concat_class): Handle cell and struct concats. + + 2010-06-25 Jaroslav Hajek * DLD-FUNCTIONS/cellfun.cc (Fnum2cell, do_num2cell): Optimize cells diff --git a/src/data.cc b/src/data.cc --- a/src/data.cc +++ b/src/data.cc @@ -1369,6 +1369,17 @@ */ +static bool +all_scalar_1x1 (const octave_value_list& args) +{ + int n_args = args.length (); + for (int i = 0; i < n_args; i++) + if (args(i).numel () != 1) + return false; + + return true; +} + template static void single_type_concat (Array& result, @@ -1376,17 +1387,41 @@ int dim) { int n_args = args.length (); - OCTAVE_LOCAL_BUFFER (Array, array_list, n_args); - - for (int j = 0; j < n_args && ! error_state; j++) + if (! (equal_types::value + || equal_types::value) + && all_scalar_1x1 (args)) { - octave_quit (); - - array_list[j] = octave_value_extract (args(j)); + // Optimize all scalars case. + dim_vector dv (1, 1); + if (dim == -1 || dim == -2) + dim = -dim - 1; + else if (dim >= 2) + dv.resize (dim+1, 1); + dv(dim) = n_args; + + result.clear (dv); + + for (int j = 0; j < n_args && ! error_state; j++) + { + octave_quit (); + + result(j) = octave_value_extract (args(j)); + } } - - if (! error_state) - result = Array::cat (dim, n_args, array_list); + else + { + OCTAVE_LOCAL_BUFFER (Array, array_list, n_args); + + for (int j = 0; j < n_args && ! error_state; j++) + { + octave_quit (); + + array_list[j] = octave_value_extract (args(j)); + } + + if (! error_state) + result = Array::cat (dim, n_args, array_list); + } } template @@ -1421,6 +1456,44 @@ return result; } +template +static void +single_type_concat_map (octave_map& result, + const octave_value_list& args, + int dim) +{ + int n_args = args.length (); + OCTAVE_LOCAL_BUFFER (MAP, map_list, n_args); + + for (int j = 0; j < n_args && ! error_state; j++) + { + octave_quit (); + + map_list[j] = octave_value_extract (args(j)); + } + + if (! error_state) + result = octave_map::cat (dim, n_args, map_list); +} + +static octave_map +do_single_type_concat_map (const octave_value_list& args, + int dim) +{ + octave_map result; + if (all_scalar_1x1 (args)) // optimize all scalars case. + { + if (dim < 0) + dim = -dim; + + single_type_concat_map (result, args, dim); + } + else + single_type_concat_map (result, args, dim); + + return result; +} + static octave_value do_cat (const octave_value_list& args, int dim, std::string fname) { @@ -1514,6 +1587,10 @@ retval = do_single_type_concat (args, dim); else if (result_type == "uint64") retval = do_single_type_concat (args, dim); + else if (result_type == "cell") + retval = do_single_type_concat (args, dim); + else if (result_type == "struct") + retval = do_single_type_concat_map (args, dim); else { dim_vector dv = args(0).dims (); diff --git a/src/pt-mat.cc b/src/pt-mat.cc --- a/src/pt-mat.cc +++ b/src/pt-mat.cc @@ -249,6 +249,10 @@ retval = c2; else if (c1_is_logical && c2_is_logical) retval = c1; + else if (c1 == "struct" && c2 == c1) + retval = c1; + else if (c1 == "cell" && c2 == c1) + retval = c1; } return retval;