gslice_array
class template
<valarray>
template <class T> gslice_array;
Valarray gslice selection
This class is used as an intermediate type returned by the non-constant version of valarray's subscript operator (valarray operator[] ) when used with gslices.
It contains references to the elements in the valarray object that are selected in a gslice, and overloads the assignment and compound assignment operators, allowing direct access to the elements in the selection when used as l-value (left-value of an assignment operation).
When used as r-value (right-value of an assignment operation), it can initialize a valarray object, since valarray has a constructor taking a gslice_array object as argument.
All the constructors of this class are private, preventing direct instantiations by a program - its only purpose is to be an efficient way to access the elements selected by a gslice with valarray's operator[] .
It is declared as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
template <class T> class gslice_array {
public:
typedef T value_type;
void operator= (const valarray<T>&) const;
void operator*= (const valarray<T>&) const;
void operator/= (const valarray<T>&) const;
void operator%= (const valarray<T>&) const;
void operator+= (const valarray<T>&) const;
void operator-= (const valarray<T>&) const;
void operator^= (const valarray<T>&) const;
void operator&= (const valarray<T>&) const;
void operator|= (const valarray<T>&) const;
void operator<<= (const valarray<T>&) const;
void operator>>= (const valarray<T>&) const;
void operator=(const T&);
~gslice_array();
private:
gslice_array();
gslice_array(const gslice_array&);
gslice_array& operator= (const gslice_array&);
};
|
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
// gslice_array example
#include <iostream>
#include <valarray>
using namespace std;
int main ()
{
valarray<int> foo (14);
for (int i=0; i<14; ++i) foo[i]=i;
size_t start=1;
size_t lengths[]= {2,3};
size_t strides[]= {7,2};
gslice mygslice (start,valarray<size_t>(lengths,2),valarray<size_t>(strides,2));
foo[mygslice] = 0;
cout << "foo:\n";
for (size_t n=0; n<foo.size(); n++)
cout << foo[n] << ' ';
cout << endl;
return 0;
}
|
Output:
0 0 2 0 4 0 6 7 0 9 0 11 0 13
|
See also
mask_array | Valarray mask selection (class template) |
|