gslice(3C++) - gslice(3C++)
Standard C++ Library Copyright 1998, Rogue Wave Software, Inc.
NAMEgslice
- A numeric array class used to represent a generalized slice from an
array.
SYNOPSIS
#include <valarray>
class gslice ;
DESCRIPTION
gslice_represents a generalized slice from an array. A generalized
slice contains a starting index, a set of lengths and a set of strides.
The number of lengths and strides must be equal. Together the lengths
and strides allow a slice from a multiple dimension array (with the
dimension equal to the number of strides) to be represented on a one
dimensional valarray. The gslice maps a set of n indices (ij), where n
is equal to the number of strides, to a single index k.
When applied to a valarray using the gslice subscript operator (see
valarray) a gslice produces a gslice_array. The gslice_array class cre‐
ates a view into the original valarray that is tailored to match param‐
eters of the gslice. The elements in a gslice_array are references to
the elements in the original array.
INTERFACE
class gslice {
public:
// constructors
gslice();
gslice(size_t, const valarray<size_t>&,
const valarray<size_t>&);
gslice (const gslice&);
// Accessors
size_t start() const;
valarray<size_t> size() const;
valarray<size_t> stride() const;
};
CONSTRUCTORSgslice();
Default constructor creates a gslice specifying no elements.
gslice(size_t start, const valarray<size_t>& length,
const valarray<size_t>& stride);
Creates a slice with starting index, length and stride as indicated by the
arguments.
gslice(const gslice&)
Creates a slice with starting index, length and stride as indicated by the
slice argument.
ACCESSORS
size_t start();
Returns the starting index of the gslice.
valarraysize_t> size();
Returns a valarray<size_t> containing the lengths of the gslice.
Valarray<size_t> stride();
Returns a valarray<size_t> containing the strides of the gslice.
EXAMPLE
//
// gslice.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};
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);
// create length and stride valarrays
valarray<size_t> len(len_buf,3);
valarray<size_t> stride(stride_buf,3);
// print out the valarray
cout << vi << endl;
// Print out all three dimensions (the entire valarray)
cout << valarray<int>(vi[gslice(0,len,stride)]) << endl;
// Print a two dimensional 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] = 1;
cout << valarray<int>(vi[gslice(9,len2,stride2)]) << endl;
// Print another two dimensional slice out of the middle
// but orthogonal to one we just did
stride2[0] = 9;
stride2[1] = 1;
cout << valarray<int>(vi[gslice(3,len2,stride2)]) << endl;
// Print out the last plane in the middle,
// (orthogonal to both of the previous ones)
stride2[0] = 3;
stride2[1] = 9;
cout << valarray<int>(vi[gslice(1,len2,stride2)]) << endl;
// Now how about a diagonal slice?
// upper left front to lower right back
stride2[0] = 3;
stride2[1] = 10;
cout << valarray<int>(vi[gslice(0,len2,stride2)]) << 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,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]
[1,2,3,4,5,6,7,8,9]
[3,4,5,4,5,6,5,6,7]
[1,2,3,4,5,6,7,8,9]
[0,2,4,3,5,7,6,8,10]
WARNINGS
If your compiler does not support namespaces, then you do not need the
using declaration for std.
SEE ALSO
valarray, slice_array, slice, gslice_array, mask_array, indirect_array
Rogue Wave Software 02 Apr 1998 gslice(3C++)