std::slice
<valarray>
std::slice
is the selector class that identifies a subset of std::valarray similar to BLAS slice. An object of type std::slice
holds three values: the starting index, the stride, and the total number of values in the subset. Objects of type std::slice
can be used as indices with valarray's operator[].
Contents
[edit] Member functions
(public member function)
(public member function)
std::slice::slice
Constructs a new slice.
Parameters
std::slice::start, size, stride
Returns the parameters passed to the slice on construction - start, size and stride respectively.
Parameters
(none)
Return value
The parameters of the slice -- start, size and stride respectively.
Complexity
Constant.
[edit] Non-member functions
(function)
operator==(std::slice)
Checks if the parameters of lhs and rhs - start, size and stride are equal respectively.
This function is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::slice is an associated class of the arguments.
The !=
operator is synthesized from operator==
.
Parameters
Return value
lhs.start() == rhs.start() && lhs.size() == rhs.size() && lhs.stride() == rhs.stride()
[edit] Example
Barebones valarray-backed Matrix class with a trace calculating function.
#include <iostream> #include <valarray> class Matrix { std::valarray <int> data; int dim; public: Matrix(int r, int c) : data(r*c), dim(c) {} int& operator()(int r, int c) { return data[r * dim + c]; } int trace() const { return data[std::slice(0, dim, dim + 1)].sum(); } }; int main() { Matrix m(3, 3); int n = 0; for (int r = 0; r < 3; ++r) for (int c = 0; c < 3; ++c) m(r, c) = ++n; std::cout << "Trace of the matrix (1,2,3) (4,5,6) (7,8,9) is " << m.trace() << '\n'; }
Output:
Trace of the matrix (1,2,3) (4,5,6) (7,8,9) is 15
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 543 | C++98 | it was unclear whether a default constructed slice is usable | it is usable (as an empty subset) |