# HG changeset patch # User Jaroslav Hajek # Date 1255942320 -7200 # Node ID 13b57eec944075b67a6b201df1599f36eb349069 # Parent a141154ee825343b4a0fc3dd7a56e97c9be0a8c3 a few handy methods for dim_vector diff --git a/liboctave/ChangeLog b/liboctave/ChangeLog --- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,9 @@ +2009-10-19 Jaroslav Hajek + + * dim-vector.h (dim_vector::compute_index, + dim_vector::increment_index, dim_vector::cumulative, + dim_vector::cum_compute_index): New methods. + 2009-10-18 Carsten Clark * Makefile.in: Remove ArrayN.cc. diff --git a/liboctave/dim-vector.h b/liboctave/dim-vector.h --- a/liboctave/dim-vector.h +++ b/liboctave/dim-vector.h @@ -516,6 +516,58 @@ return def; } + // Computes a linear index from an index tuple. + octave_idx_type compute_index (const octave_idx_type *idx) + { + octave_idx_type k = 0; + for (int i = length () - 1; i >= 0; i++) + k = k * rep[i] + idx[i]; + + return k; + } + + // Increments a multi-dimensional index tuple, optionally starting + // from an offset position. Returns the index of the last index + // position that was changed, or length () if just cycled over. + int increment_index (octave_idx_type *idx, int start = 0) + { + int i; + for (i = start; i < length (); i++) + { + if (++(*idx) == rep[i]) + *idx = 0; + else + break; + } + return i; + } + + // Returns cumulative dimensions. + dim_vector cumulative (void) const + { + int nd = length (); + dim_vector retval = alloc (nd); + + octave_idx_type k = 1; + for (int i = 0; i < nd; i++) + retval.rep[i] = k *= rep[i]; + + return retval; + } + + // Computes a linear index from an index tuple. Assumes that the dimensions + // are cumulative. + octave_idx_type cum_compute_index (const octave_idx_type *idx) + { + octave_idx_type k = idx[0]; + + for (int i = 1; i < length (); i++) + k += rep[i-1] * idx[i]; + + return k; + } + + friend bool operator == (const dim_vector& a, const dim_vector& b); };