Difference between revisions of "cpp/numeric/valarray/slice array"
From cppreference.com
m (rewording) |
Andreas Krug (Talk | contribs) m (fmt) |
||
Line 2: | Line 2: | ||
{{cpp/numeric/valarray/slice_array/navbar}} | {{cpp/numeric/valarray/slice_array/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl header | valarray}} | + | {{dcl header|valarray}} |
− | {{dcl | | + | {{dcl| |
template< class T > class slice_array; | template< class T > class slice_array; | ||
}} | }} | ||
Line 12: | Line 12: | ||
===Member types=== | ===Member types=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc hitem | Type | Definition}} | + | {{dsc hitem|Type|Definition}} |
− | {{dsc | {{tt|value_type}} | {{tt|T}} }} | + | {{dsc|{{tt|value_type}}|{{tt|T}}}} |
{{dsc end}} | {{dsc end}} | ||
===Member functions=== | ===Member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/numeric/valarray/array/dsc constructor | slice_array}} | + | {{dsc inc|cpp/numeric/valarray/array/dsc constructor|slice_array}} |
− | {{dsc inc | cpp/numeric/valarray/array/dsc destructor | slice_array}} | + | {{dsc inc|cpp/numeric/valarray/array/dsc destructor|slice_array}} |
− | {{dsc inc | cpp/numeric/valarray/array/dsc operator{{=}} | slice_array}} | + | {{dsc inc|cpp/numeric/valarray/array/dsc operator{{=}}|slice_array}} |
− | {{dsc inc | cpp/numeric/valarray/array/dsc operator_arith | slice_array}} | + | {{dsc inc|cpp/numeric/valarray/array/dsc operator_arith|slice_array}} |
{{dsc end}} | {{dsc end}} | ||
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | |code= | |
#include <iostream> | #include <iostream> | ||
#include <valarray> | #include <valarray> | ||
− | class Matrix { | + | class Matrix |
+ | { | ||
int dim; | int dim; | ||
std::valarray<int> data; | std::valarray<int> data; | ||
public: | public: | ||
explicit Matrix(int dim, int init = 0) | explicit Matrix(int dim, int init = 0) | ||
− | : dim{dim}, data(init, dim*dim) { } | + | : dim{dim}, data(init, dim*dim) {} |
void clear(int value = 0) { data = value; } | void clear(int value = 0) { data = value; } | ||
void identity() { clear(); diagonal() = 1; } | void identity() { clear(); diagonal() = 1; } | ||
int& operator()(int x, int y) { return data[dim * y + x]; } | int& operator()(int x, int y) { return data[dim * y + x]; } | ||
− | std::slice_array<int> diagonal() { | + | std::slice_array<int> diagonal() |
− | return data[std::slice(0, dim, dim+1)]; | + | { |
+ | return data[std::slice(0, dim, dim + 1)]; | ||
} | } | ||
− | std::slice_array<int> secondary_diagonal() { | + | std::slice_array<int> secondary_diagonal() |
− | return data[std::slice(dim-1, dim, dim-1)]; | + | { |
+ | return data[std::slice(dim - 1, dim, dim - 1)]; | ||
} | } | ||
− | std::slice_array<int> row(std::size_t row) { | + | std::slice_array<int> row(std::size_t row) |
− | return data[std::slice(dim*row, dim, 1)]; | + | { |
+ | return data[std::slice(dim * row, dim, 1)]; | ||
} | } | ||
− | std::slice_array<int> column(std::size_t col) { | + | std::slice_array<int> column(std::size_t col) |
+ | { | ||
return data[std::slice(col, dim, dim)]; | return data[std::slice(col, dim, dim)]; | ||
} | } | ||
Line 55: | Line 60: | ||
}; | }; | ||
− | template <unsigned dim = 3, unsigned max = 8> class MatrixStack { | + | template<unsigned dim = 3, unsigned max = 8> class MatrixStack |
+ | { | ||
std::valarray<int> stack; | std::valarray<int> stack; | ||
unsigned count = 0; | unsigned count = 0; | ||
public: | public: | ||
− | MatrixStack() : stack(dim*dim*max) {} | + | MatrixStack() : stack(dim * dim * max) {} |
− | void print_all() const { | + | void print_all() const |
+ | { | ||
std::valarray<int> row(dim*count); | std::valarray<int> row(dim*count); | ||
− | for (unsigned r = 0; r != dim; ++r) | + | for (unsigned r = 0; r != dim; ++r) // screen row |
− | row = stack[std::gslice(r*dim, {count, dim}, {dim*dim, 1})]; | + | { |
+ | row = stack[std::gslice(r * dim, {count, dim}, {dim * dim, 1})]; | ||
for (unsigned i = 0; i != row.size(); ++i) | for (unsigned i = 0; i != row.size(); ++i) | ||
− | std::cout << row[i] << ((i+1) % dim ? " " : " │ "); | + | std::cout << row[i] << ((i + 1) % dim ? " " : " │ "); |
std::cout << '\n'; | std::cout << '\n'; | ||
} | } | ||
} | } | ||
− | void push_back(Matrix const& m) { | + | void push_back(Matrix const& m) |
− | if (count < max) { | + | { |
+ | if (count < max) | ||
+ | { | ||
stack[std::slice(count * dim * dim, dim * dim, 1)] | stack[std::slice(count * dim * dim, dim * dim, 1)] | ||
= m.data[std::slice(0, dim * dim, 1)]; | = m.data[std::slice(0, dim * dim, 1)]; | ||
Line 91: | Line 101: | ||
stack.push_back(m); | stack.push_back(m); | ||
− | for (int i = 0; i != dim; ++i) { | + | for (int i = 0; i != dim; ++i) |
+ | { | ||
m.clear(); | m.clear(); | ||
m.row(i) = i + 1; | m.row(i) = i + 1; | ||
Line 97: | Line 108: | ||
} | } | ||
− | for (int i = 0; i != dim; ++i) { | + | for (int i = 0; i != dim; ++i) |
+ | { | ||
m.clear(); | m.clear(); | ||
m.column(i) = i + 1; | m.column(i) = i + 1; | ||
Line 113: | Line 125: | ||
stack.print_all(); | stack.print_all(); | ||
} | } | ||
− | + | |output= | |
1 0 0 │ 1 1 3 │ 1 1 1 │ 0 0 0 │ 0 0 0 │ 1 0 0 │ 0 2 0 │ 0 0 3 │ 0 0 0 │ 0 7 0 │ | 1 0 0 │ 1 1 3 │ 1 1 1 │ 0 0 0 │ 0 0 0 │ 1 0 0 │ 0 2 0 │ 0 0 3 │ 0 0 0 │ 0 7 0 │ | ||
0 1 0 │ 1 3 1 │ 0 0 0 │ 2 2 2 │ 0 0 0 │ 1 0 0 │ 0 2 0 │ 0 0 3 │ 4 5 6 │ 0 8 0 │ | 0 1 0 │ 1 3 1 │ 0 0 0 │ 2 2 2 │ 0 0 0 │ 1 0 0 │ 0 2 0 │ 0 0 3 │ 4 5 6 │ 0 8 0 │ | ||
Line 121: | Line 133: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/numeric/valarray/dsc gslice_array}} | + | {{dsc inc|cpp/numeric/valarray/dsc gslice_array}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|ja|zh}} | {{langlinks|ja|zh}} |
Latest revision as of 10:02, 17 October 2023
Defined in header <valarray>
|
||
template< class T > class slice_array; |
||
std::slice_array
is a helper template used by the valarray subscript operator with std::slice argument. It has reference semantics to a subset of the array specified by the std::slice object.
Contents |
[edit] Member types
Type | Definition |
value_type
|
T
|
[edit] Member functions
constructs a slice_array (public member function) | |
destroys a slice_array (public member function) | |
assigns contents (public member function) | |
performs arithmetic operation on the array referred by slice. (public member function) |
[edit] Example
Run this code
#include <iostream> #include <valarray> class Matrix { int dim; std::valarray<int> data; public: explicit Matrix(int dim, int init = 0) : dim{dim}, data(init, dim*dim) {} void clear(int value = 0) { data = value; } void identity() { clear(); diagonal() = 1; } int& operator()(int x, int y) { return data[dim * y + x]; } std::slice_array<int> diagonal() { return data[std::slice(0, dim, dim + 1)]; } std::slice_array<int> secondary_diagonal() { return data[std::slice(dim - 1, dim, dim - 1)]; } std::slice_array<int> row(std::size_t row) { return data[std::slice(dim * row, dim, 1)]; } std::slice_array<int> column(std::size_t col) { return data[std::slice(col, dim, dim)]; } template<unsigned, unsigned> friend class MatrixStack; }; template<unsigned dim = 3, unsigned max = 8> class MatrixStack { std::valarray<int> stack; unsigned count = 0; public: MatrixStack() : stack(dim * dim * max) {} void print_all() const { std::valarray<int> row(dim*count); for (unsigned r = 0; r != dim; ++r) // screen row { row = stack[std::gslice(r * dim, {count, dim}, {dim * dim, 1})]; for (unsigned i = 0; i != row.size(); ++i) std::cout << row[i] << ((i + 1) % dim ? " " : " │ "); std::cout << '\n'; } } void push_back(Matrix const& m) { if (count < max) { stack[std::slice(count * dim * dim, dim * dim, 1)] = m.data[std::slice(0, dim * dim, 1)]; ++count; } } }; int main() { constexpr int dim = 3; Matrix m{dim}; MatrixStack<dim,12> stack; m.identity(); stack.push_back(m); m.clear(1); m.secondary_diagonal() = 3; stack.push_back(m); for (int i = 0; i != dim; ++i) { m.clear(); m.row(i) = i + 1; stack.push_back(m); } for (int i = 0; i != dim; ++i) { m.clear(); m.column(i) = i + 1; stack.push_back(m); } m.clear(); m.row(1) = std::valarray<int>{4, 5, 6}; stack.push_back(m); m.clear(); m.column(1) = std::valarray<int>{7, 8, 9}; stack.push_back(m); stack.print_all(); }
Output:
1 0 0 │ 1 1 3 │ 1 1 1 │ 0 0 0 │ 0 0 0 │ 1 0 0 │ 0 2 0 │ 0 0 3 │ 0 0 0 │ 0 7 0 │ 0 1 0 │ 1 3 1 │ 0 0 0 │ 2 2 2 │ 0 0 0 │ 1 0 0 │ 0 2 0 │ 0 0 3 │ 4 5 6 │ 0 8 0 │ 0 0 1 │ 3 1 1 │ 0 0 0 │ 0 0 0 │ 3 3 3 │ 1 0 0 │ 0 2 0 │ 0 0 3 │ 0 0 0 │ 0 9 0 │
[edit] See also
proxy to a subset of a valarray after applying a gslice (class template) |