
Valarray helpers
A numeric array class used to represent a BLAS-like slice from a valarray.
| Member Functions | |
| operator%=() operator&=() operator>>=() operator<<=() operator*=() operator+=() |
operator-=() operator/=() operator=() operator^=() |
#include <valarray> template <class T> class gslice_array ;
gslice_array<T> creates a gslice view into a valarray. gslice_arrays are only produced by applying the gslice subscript operator to a valarray. The elements in a gslice_array are references to selected elements in the valarray (so changing an element in the gslice_array really changes the corresponding element in the valarray). A gslice_array does not itself hold any distinct elements. The template cannot be instantiated directly since all its constructors are private. However, you can copy a gslice_array to a valarray using either the valarray copy constructor or the assignment operator. Reference semantics are lost at that point.
template <class T> class gslice_array {
public:
// types
typedef T value_type;
// destructor
~gslice_array();
// public assignment
void operator= (const valarray<T>& array) const;
// computed assignment
void operator*= (const valarray<T>& array) const;
void operator/= (const valarray<T>& array) const;
void operator%= (const valarray<T>& array) const;
void operator+= (const valarray<T>& array) const;
void operator-= (const valarray<T>& array) const;
void operator^= (const valarray<T>& array) const;
void operator&= (const valarray<T>& array) const;
void operator|= (const valarray<T>& array) const;
void operator<<= (const valarray<T>& array) const;
void operator>>= (const valarray<T>& array) const;
// fill function
void operator=(const T&);
private:
// constructors
gslice_array();
gslice_array(const gslice_array<T>&);
// operator =
gslice_array<T>& operator= (const gslice_array<T>& array);
};
gslice_array(); gslice_array(const gslice_array&);
All gslice_array constructors are private and cannot be called directly. This prevents copy construction of gslice_arrays.
void operator=(const valarray<T>& x) const;
Assigns values from x to the selected elements of the valarray that self refers to. Remember that a gslice_array never holds any elements itself; it simply refers to selected elements in the valarray used to generate it.
gslice_array<T>& operator=(const gslice-_array<T>& x);
Private assignment operator. Cannot be called directly, thus preventing assignment between gslice_arrays.
void operator*=(const valarray<T>& val) const; void operator/=(const valarray<T>& val) const; void operator%=(const valarray<T>& val) const; void operator+=(const valarray<T>& val) const; void operator-=(const valarray<T>& val) const; void operator^=(const valarray<T>& val) const; void operator&=(const valarray<T>& val) const; void operator|=(const valarray<T>& val) const; void operator<<=(const valarray<T>& val) const; void operator>>=(const valarray<T>& val) const;
Applies the indicated operation using elements from val to the selected elements of the valarray that self refers to. Remember that a gslice_array never holds any elements itself; it simply refers to selected elements in the valarray used to generate it.
void operator=(const T& x) const;
Assigns x to the selected elements of the valarray that self refers to.
//
// gslice_array.cpp
//
#include "valarray.h" // Contains a valarray stream inserter
using namespace std;
int main(void)
{
int ibuf[27] =
{0,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,10};
int buf13[9] = {13,13,13,13,13,13,13,13,13};
size_t len_buf[3] = {3,3,3};
size_t stride_buf[3] = {9,3,1};
// create a valarray of ints
valarray<int> vi(ibuf,27);
// print out the valarray
cout << vi << endl;
// Get a two dimensional diagonal slice out of the middle
valarray<size_t> len2(2);
len2[0] = 3;
len2[1] = 3;
valarray<size_t> stride2(2);
stride2[0] = 3;
stride2[1] = 10;
gslice_array<int> gsl = vi[gslice(0,len2,stride2)];
// print out the slice
cout << gsl << endl;
// Assign 13's to everything in the slice
gsl = valarray<int>(buf13,9);
// print out the slice and our original valarray
cout << gsl << endl << vi << endl;
return 0;
}
Program Output
[0,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,10] [0,2,4,3,5,7,6,8,10] [13,13,13,13,13,13,13,13,13] [13,1,2,13,4,5,13,7,8,1,13,3,4,13,6,7,13,9,2,3,13,5,6,13,8,9,13]
If your compiler does not support namespaces, then you do not need the using declaration for std.
valarray, slice_array, slice, gslice, mask_array, indirect_array