# HG changeset patch # User Jaroslav Hajek # Date 1259062226 -3600 # Node ID 47c5af1868dfc0c7ead15f5a8f4e0f168f44f5d4 # Parent 43a7adf62534bad7f3f84fdf671871f17230e747 move idx_add methods to MArrayN diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,9 @@ +2009-11-24 Jaroslav Hajek + + * MArrayN.cc (MArrayN::idx_add): New methods. + * MArrayN.h: Declare them. + * MArray.cc, MArray.h: Remove from here. + 2009-11-19 Jaroslav Hajek * dim-vector.h (dim_vector::safe_numel): New method. diff --git a/liboctave/MArray.cc b/liboctave/MArray.cc --- a/liboctave/MArray.cc +++ b/liboctave/MArray.cc @@ -54,62 +54,6 @@ return 0; } -template -struct _idxadds_helper -{ - T *array; - T val; - _idxadds_helper (T *a, T v) : array (a), val (v) { } - void operator () (octave_idx_type i) - { array[i] += val; } -}; - -template -struct _idxadda_helper -{ - T *array; - const T *vals; - _idxadda_helper (T *a, const T *v) : array (a), vals (v) { } - void operator () (octave_idx_type i) - { array[i] += *vals++; } -}; - -template -void -MArray::idx_add (const idx_vector& idx, T val) -{ - octave_idx_type n = this->length (); - octave_idx_type ext = idx.extent (n); - if (ext > n) - { - this->resize (ext); - n = ext; - } - - OCTAVE_QUIT; - - octave_idx_type len = idx.length (n); - idx.loop (len, _idxadds_helper (this->fortran_vec (), val)); -} - -template -void -MArray::idx_add (const idx_vector& idx, const MArray& vals) -{ - octave_idx_type n = this->length (); - octave_idx_type ext = idx.extent (n); - if (ext > n) - { - this->resize (ext); - n = ext; - } - - OCTAVE_QUIT; - - octave_idx_type len = std::min (idx.length (n), vals.length ()); - idx.loop (len, _idxadda_helper (this->fortran_vec (), vals.data ())); -} - // Element by element MArray by scalar ops. template diff --git a/liboctave/MArray.h b/liboctave/MArray.h --- a/liboctave/MArray.h +++ b/liboctave/MArray.h @@ -104,12 +104,6 @@ map (U (&fcn) (const T&)) const { return Array::template map (fcn); } - // Performs indexed accumulative addition. - - void idx_add (const idx_vector& idx, T val); - - void idx_add (const idx_vector& idx, const MArray& vals); - // Currently, the OPS functions don't need to be friends, but that // may change. diff --git a/liboctave/MArrayN.cc b/liboctave/MArrayN.cc --- a/liboctave/MArrayN.cc +++ b/liboctave/MArrayN.cc @@ -31,6 +31,62 @@ #include "MArray-defs.h" #include "mx-inlines.cc" +template +struct _idxadds_helper +{ + T *array; + T val; + _idxadds_helper (T *a, T v) : array (a), val (v) { } + void operator () (octave_idx_type i) + { array[i] += val; } +}; + +template +struct _idxadda_helper +{ + T *array; + const T *vals; + _idxadda_helper (T *a, const T *v) : array (a), vals (v) { } + void operator () (octave_idx_type i) + { array[i] += *vals++; } +}; + +template +void +MArrayN::idx_add (const idx_vector& idx, T val) +{ + octave_idx_type n = this->length (); + octave_idx_type ext = idx.extent (n); + if (ext > n) + { + this->resize (ext); + n = ext; + } + + OCTAVE_QUIT; + + octave_idx_type len = idx.length (n); + idx.loop (len, _idxadds_helper (this->fortran_vec (), val)); +} + +template +void +MArrayN::idx_add (const idx_vector& idx, const MArrayN& vals) +{ + octave_idx_type n = this->length (); + octave_idx_type ext = idx.extent (n); + if (ext > n) + { + this->resize (ext); + n = ext; + } + + OCTAVE_QUIT; + + octave_idx_type len = std::min (idx.length (n), vals.length ()); + idx.loop (len, _idxadda_helper (this->fortran_vec (), vals.data ())); +} + // N-dimensional array with math ops. template void diff --git a/liboctave/MArrayN.h b/liboctave/MArrayN.h --- a/liboctave/MArrayN.h +++ b/liboctave/MArrayN.h @@ -104,6 +104,12 @@ return Array::diag (k); } + // Performs indexed accumulative addition. + + void idx_add (const idx_vector& idx, T val); + + void idx_add (const idx_vector& idx, const MArrayN& vals); + void changesign (void); }; diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2009-11-24 Jaroslav Hajek + + * data.cc (do_accumarray_sum): Simplify. + 2009-11-24 Jaroslav Hajek * ov-struct.cc (octave_struct::save_binary): Save dimensions for diff --git a/src/data.cc b/src/data.cc --- a/src/data.cc +++ b/src/data.cc @@ -6330,24 +6330,16 @@ else if (idx.extent (n) > n) error ("accumarray: index out of range"); - // FIXME: the class tree in liboctave is overly complicated, hence the - // following type gymnastics. - MArray array; + NDT retval (dim_vector (n, 1), T()); if (vals.numel () == 1) - { - array = MArray (n, T ()); - array.idx_add (idx, vals (0)); - } - else if (vals.length () == idx.length (n)) - { - array = MArray (n, T ()); - array.idx_add (idx, MArray (vals)); - } + retval.idx_add (idx, vals (0)); + else if (vals.numel () == idx.length (n)) + retval.idx_add (idx, vals); else error ("accumarray: dimensions mismatch"); - return NDT (MArrayN (Array (array))); + return retval; } DEFUN (__accumarray_sum__, args, ,