Library: Numerics
Does not inherit
|
operator%=() operator&=() operator>>=() |
operator<<=() operator*=() operator+=() |
operator-=() operator/=() operator=() |
operator^=() operator|=() slice_array() |
A numeric array class for representing a BLAS-like slice from a valarray
#include <valarray>
namespace std {
template <class T>
class slice_array;
}
The valarray helper class slice_array gives a slice view into a valarray. A slice_array is 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.
namespace std {
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 T& x);
Assigns x to the selected elements of the valarray that self refers to.
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.
//
// slice_array.cpp
//
#include <valarray.h> // Includes valarray and provides stream inserter.
typedef std::valarray<int> valarray_t;
int main(void) {
valarray_t::value_type ibuf[9] = {0,1,2,3,4,5,6,7,8};
// Create a valarray initialized to the sequence of
// integers above.
valarray_t vi (ibuf,9);
std::cout << "original valarray vi\n\n" << vi << "\n\n";
// Display a slice of the valarray.
std::cout << "vi[slice(0,3,3)]\n\n"
<< vi[std::slice(0,3,3)] << "\n\n";
// Unfortunately, slice_arrays were not designed to be
// easily combined. An explicit conversion to valarray
// is required, leaving us with this awkward casting
// syntax.
vi[std::slice (0,3,3)] =
static_cast<valarray_t>(vi[std::slice (1,3,3)]) +
static_cast<valarray_t>(vi[std::slice (2,3,3)]);
// The operation above uses slices ([0-2],3,3) to treat
// our valarray as a 3-dimensional array:
//
// 0 1 2 3 = 1 + 2
// 3 4 5 9 = 4 + 5
// 6 7 8 15 = 7 + 8
// | | | | | |
// | | +-slice(2,3,3) | | +- slice_array for
// | | | | arithmetic operand.
// | +---slice(1,3,3) | +----- slice_array for
// | | arithmetic operand.
// +-----slice(0,3,3) +---slice_array assignment
// refers to valarray.
std::cout << "vi[slice(0,3,3)] = vi[slice(1,3,3)] + "
<< "vi[slice(2,3,3)]\n\n"
<< vi << std::endl;
return 0;
}
Program Output:
original valarray vi [0,1,2,3,4,5,6,7,8] vi[slice(0,3,3)] [0,3,6] vi[slice(0,3,3)] = vi[slice(1,3,3)] + vi[slice(2,3,3)] [3,1,2,9,4,5,15,7,8]
slice, valarray, gslice, gslice_array, mask_array, indirect_array
ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 26.3.5