
Valarray helpers
A numeric array class for representing 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 slice_array ;
slice_array<T> gives a slice view into a valarray. Slice_arrays are only produced by applying the slice subscript operator to a valarray. The elements in a slice_array are references to selected elements in the valarray (so changing an element in the slice_array really changes the corresponding element in the valarray). A slice_array does not itself hold any distinct elements. The template cannot be instantiated directly since all its constructors are private. However, you can easily copy a slice_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 slice_array {
public:
// types
typedef T value_type;
// destructor
~slice_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;
// other
void operator= (const T&);
private:
// constructors
slice_array();
slice_array(const slice_array<T>&);
// operator =
slice_array<T>& operator= (const slice_array<T>& array);
};
slice_array(); slice_array(const slice_array&);
All slice_array constructors are private and cannot be called directly. This prevents copy construction of slice_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 slice_array never holds any elements itself, it simply refers to selected elements in the valarray used to generate it.
slice_array<T>& operator=(const slice-_array<T>& x);
Private assignment operator. Cannot be called directly, thus preventing assignment between slice_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 slice_array never holds any elements itself; it simply refers to selected elements in the valarray used to generate it.
void operator= (const T& x);
Assigns x to the selected elements of the valarray that self refers to.
//
// slice_array.cpp
//
#include "valarray.h" // Contains a valarray
// stream inserter
using namespace std;
int main(void)
{
int ibuf[10] = {0,1,2,3,4,5,6,7,8,9};
int ibuf2[5] = {1,3,5,7,9};
// create a valarray of ints
valarray<int> vi(ibuf,10);
valarray<int> vi2(ibuf2,5);
// print it out
cout << vi << endl << vi2 << endl;
// Get a slice and assign that slice to another array
slice_array<int> sl = vi[slice(1,5,2)];
valarray<int> vi3 = sl;
// print out the slice
cout << vi3 << endl;
// Add slice from vi2 to slice of vi1
sl += vi2;
// print out vi1 again
cout << vi << endl;
return 0;
}
Program Output
[0,1,2,3,4,5,6,7,8,9] [1,3,5,7,9] [1,3,5,7,9] [0,2,2,6,4,10,6,14,8,18]
If your compiler does not support namespaces, then you do not need the using declaration for std.
valarray, slice, gslice, gslice_array, mask_array, indirect_array