Valarray helpers
A numeric array class for representing a BLAS-like slice from an array.
#include <valarray> class slice ;
slice allows you to represent a BLAS-like slice from an array. A BLAS slice contains a starting index, a length, and a stride. The index indicates the first element in the slice, the length determines the number of elements, and the stride indicates the interval between elements in the original array. For instance, the slice (1,3,2) applied to the array (1,2,3,4,5,6,7) produces the array (2,4,6).
When applied to a valarray using the slice subscript operator (see valarray) a slice produces a slice_array. The slice_array gives a view into the original valarray that is tailored to match parameters of the slice. The elements in a slice_array are references to the elements in the original array. This means you need to explicitly copy the slice_array into another valarray in order to have a distinct array.
class slice { public: // constructors slice(); slice(size_t, size_t, size_t); // Accessors size_t start() const; size_t size() const; size_t stride() const; };
slice();
Creates a slice specifying no elements. This constructor is only intended to allow the creation of arrays of slices.
slice(size_t start, size_t length, size_t stride);
Creates a slice with starting index, length, and stride as indicated by the arguments.
slice(const slice&)
Creates a slice with starting index, length, and stride as indicated by the slice argument.
size_t start();
Returns the starting index of the slice.
size_t size();
Returns the length of the slice.
size_t stride();
Returns the stride of the slice.
// // slice.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}; // create a valarray of ints valarray<int> vi(ibuf,10); // print it out cout << vi << endl; // print out a slice cout << valarray<int>(vi[slice(1,3,2)]) << endl; return 0; }
Program Output
[0,1,2,3,4,5,6,7,8,9] [1,3,5]
If your compiler does not support namespaces, then you do not need the using declaration for std.
valarray, slice_array, gslice, gslice_array, mask_array, indirect_array