gslice_array

template<class T>
    class gslice_array {
public:
    typedef T value_type;
    void operator=(const valarray<T> x) const;
    void operator=(const T& x);
    void operator*=(const valarray<T> x) const;
    void operator/=(const valarray<T> x) const;
    void operator%=(const valarray<T> x) const;
    void operator+=(const valarray<T> x) const;
    void operator-=(const valarray<T> x) const;
    void operator^=(const valarray<T> x) const;
    void operator&=(const valarray<T> x) const;
    void operator|=(const valarray<T> x) const;
    void operator<<=(const valarray<T> x) const;
    void operator>>=(const valarray<T> x) const;
    void fill();
    };

The class describes an object that stores a reference to an object x of class valarray<T>, along with an object gs of class gslice, which describes the sequence of elements to select from the valarray<T> object.

You construct a gslice_array<T> object only by writing an expression of the form x[gs]. The member functions of class gslice_array then behave like the corresponding function signatures defined for valarray<T>, except that only the sequence of selected elements is affected.

The sequence is determined as follows. For a length vector gs.size() of length N, construct the index vector valarray<size_t> idx(0, N). This designates the initial element of the sequence, whose index k within x is given by the mapping:

k = start;
for (size_t i = 0; i < gs.size()[i]; ++i)
    k += idx[i] * gs.stride()[i];

The successor to an index vector value is given by:

for (size_t i = N; 0 < i--; )
    if (++idx[i] < gs.size()[i])
        break;
    else
        idx[i] = 0;

For example:

const size_t lv[] = {2, 3};
const size_t dv[] = {7, 2};
const valarray<size_t> len(lv, 2), str(dv, 2);
// x[gslice(3, len, str)] selects elements with indices
//   3, 5, 7, 10, 12, 14