# HG changeset patch # User Jaroslav Hajek # Date 1262239526 -3600 # Node ID 2d7a8c227713df8c90b3a5c72be71b5c4ef648ea # Parent 7fbdeaa9b0e0f15aeec937fbba2d48b4bf35e013 rewrite uniformoutput optimization in cellfun diff -r 7fbdeaa9b0e0 -r 2d7a8c227713 src/ChangeLog --- a/src/ChangeLog Wed Dec 30 06:01:51 2009 +0100 +++ b/src/ChangeLog Thu Dec 31 07:05:26 2009 +0100 @@ -1,3 +1,12 @@ +2009-12-30 Jaroslav Hajek + + * DLD-FUNCTIONS/cellfun.cc (scalar_query_helper): Remove. + (can_extract): New overloaded helper function. + (scalar_col_helper_nda): Rewrite using can_extract and + octave_value_extract. Instantiate for integer types. + (make_col_helper): Support integer types. + + 2009-12-30 Jaroslav Hajek * DLD-FUNCTIONS/cellfun.cc (Fcellfun): Fix tests. diff -r 7fbdeaa9b0e0 -r 2d7a8c227713 src/DLD-FUNCTIONS/cellfun.cc --- a/src/DLD-FUNCTIONS/cellfun.cc Wed Dec 30 06:01:51 2009 +0100 +++ b/src/DLD-FUNCTIONS/cellfun.cc Thu Dec 31 07:05:26 2009 +0100 @@ -43,6 +43,20 @@ #include "gripes.h" #include "utils.h" +#include "ov-scalar.h" +#include "ov-float.h" +#include "ov-complex.h" +#include "ov-flt-complex.h" +#include "ov-bool.h" +#include "ov-int8.h" +#include "ov-int16.h" +#include "ov-int32.h" +#include "ov-int64.h" +#include "ov-uint8.h" +#include "ov-uint16.h" +#include "ov-uint32.h" +#include "ov-uint64.h" + // Rationale: // The octave_base_value::subsasgn method carries too much overhead for // per-element assignment strategy. @@ -95,26 +109,41 @@ }; template -struct scalar_query_helper { }; +static bool can_extract (const octave_value& val) +{ return false; } -#define DEF_QUERY_HELPER(T, TEST, QUERY) \ +#define DEF_CAN_EXTRACT(T, CLASS) \ template <> \ -struct scalar_query_helper \ -{ \ - static bool has_value (const octave_value& val) \ - { return TEST; } \ - static T get_value (const octave_value& val) \ - { return QUERY; } \ +bool can_extract (const octave_value& val) \ +{ return val.type_id () == octave_ ## CLASS::static_type_id (); } + +DEF_CAN_EXTRACT (double, scalar); +DEF_CAN_EXTRACT (float, float_scalar); +DEF_CAN_EXTRACT (bool, bool); +DEF_CAN_EXTRACT (octave_int8, int8_scalar); +DEF_CAN_EXTRACT (octave_int16, int16_scalar); +DEF_CAN_EXTRACT (octave_int32, int32_scalar); +DEF_CAN_EXTRACT (octave_int64, int64_scalar); +DEF_CAN_EXTRACT (octave_uint8, uint8_scalar); +DEF_CAN_EXTRACT (octave_uint16, uint16_scalar); +DEF_CAN_EXTRACT (octave_uint32, uint32_scalar); +DEF_CAN_EXTRACT (octave_uint64, uint64_scalar); + +template <> +bool can_extract (const octave_value& val) +{ + int t = val.type_id (); + return (t == octave_complex::static_type_id () + || t == octave_scalar::static_type_id ()); } -DEF_QUERY_HELPER (double, val.is_real_scalar (), val.scalar_value ()); -DEF_QUERY_HELPER (Complex, val.is_complex_scalar (), val.complex_value ()); -DEF_QUERY_HELPER (float, val.is_single_type () && val.is_real_scalar (), - val.float_scalar_value ()); -DEF_QUERY_HELPER (FloatComplex, val.is_single_type () && val.is_complex_scalar (), - val.float_complex_value ()); -DEF_QUERY_HELPER (bool, val.is_bool_scalar (), val.bool_value ()); -// FIXME: More? +template <> +bool can_extract (const octave_value& val) +{ + int t = val.type_id (); + return (t == octave_float_complex::static_type_id () + || t == octave_float_scalar::static_type_id ()); +} // This specializes for collecting elements of a single type, by accessing // an array directly. If the scalar is not valid, it returns false. @@ -128,15 +157,15 @@ scalar_col_helper_nda (const octave_value& val, const dim_vector& dims) : arrayval (dims) { - arrayval(0) = scalar_query_helper::get_value (val); + arrayval(0) = octave_value_extract (val); } ~scalar_col_helper_nda (void) { } bool collect (octave_idx_type i, const octave_value& val) { - bool retval = scalar_query_helper::has_value (val); + bool retval = can_extract (val); if (retval) - arrayval(i) = scalar_query_helper::get_value (val); + arrayval(i) = octave_value_extract (val); return retval; } octave_value result (void) @@ -150,6 +179,14 @@ template class scalar_col_helper_nda; template class scalar_col_helper_nda; template class scalar_col_helper_nda; +template class scalar_col_helper_nda; +template class scalar_col_helper_nda; +template class scalar_col_helper_nda; +template class scalar_col_helper_nda; +template class scalar_col_helper_nda; +template class scalar_col_helper_nda; +template class scalar_col_helper_nda; +template class scalar_col_helper_nda; // the virtual constructor. scalar_col_helper * @@ -157,24 +194,31 @@ { scalar_col_helper *retval; - if (val.is_bool_scalar ()) - retval = new scalar_col_helper_nda (val, dims); - else if (val.is_complex_scalar ()) + // No need to check numel() here. + switch (val.builtin_type ()) { - if (val.is_single_type ()) - retval = new scalar_col_helper_nda (val, dims); - else - retval = new scalar_col_helper_nda (val, dims); +#define ARRAYCASE(BTYP, ARRAY) \ + case BTYP: \ + retval = new scalar_col_helper_nda (val, dims); \ + break + + ARRAYCASE (btyp_double, NDArray); + ARRAYCASE (btyp_float, FloatNDArray); + ARRAYCASE (btyp_complex, ComplexNDArray); + ARRAYCASE (btyp_float_complex, FloatComplexNDArray); + ARRAYCASE (btyp_bool, boolNDArray); + ARRAYCASE (btyp_int8, int8NDArray); + ARRAYCASE (btyp_int16, int16NDArray); + ARRAYCASE (btyp_int32, int32NDArray); + ARRAYCASE (btyp_int64, int64NDArray); + ARRAYCASE (btyp_uint8, uint8NDArray); + ARRAYCASE (btyp_uint16, uint16NDArray); + ARRAYCASE (btyp_uint32, uint32NDArray); + ARRAYCASE (btyp_uint64, uint64NDArray); + default: + retval = new scalar_col_helper_def (val, dims); + break; } - else if (val.is_real_scalar ()) - { - if (val.is_single_type ()) - retval = new scalar_col_helper_nda (val, dims); - else - retval = new scalar_col_helper_nda (val, dims); - } - else - retval = new scalar_col_helper_def (val, dims); return retval; }