std::gslice - Valarray Generalized Slice Selector in C++
Last Updated :
18 Jan, 2022
Valarray generalized slice selector class represents a valarray generalized slice selector (a multidimensional slice). It does not contain nor refer to any element - it only describes a selection of elements to be used as an index in valarray::operator[].In other words, std::gslice is the selector class that identifies a subset of std::valarray indices defined by a multi-level set of strides and sizes. Objects of type std::gslice can be used as indices with valarray's operator[] to select, for example, columns of a multidimensional array represented as a valarray.Given the starting value s, a list of strides ij and a list of sizes dj, a std::gslice constructed from these values selects the set of indices:
kj = s + ?j(ijdj)
A valarray generalized slice is specified by a starting index, a set of sizes, and a set of strides. It produces a multidimensional combination of slice selections.Syntax:
gslice( std::size_t start, const std::valarray& sizes,
const std::valarray& strides );
Parameters:
- size_t start: index of the first element in the selection.
- size_t (size): number of elements in the selection.
- size_t (stride: span that separates the elements selected
In given Syntax, default constructor is Equivalent to gslice(0, std::valarray(), std::valarray()). This constructor exists only to allow construction of arrays of slices.
Constructs a new slice with parameters start, sizes, strides.
Header file:
<valarray>
Example 1:
start = 1 , size = {2, 3} , stride = {7, 2}
Input : 0 1 2 3 4 5 6 7 8 9 10 11 12 13
Output : 1 3 5 8 10 12
Explanation: 1 + 0*7 + 0*2 = 1,
1 + 0*7 + 1*2 = 3,
1 + 0*7 + 2*2 = 5,
1 + 1*7 + 0*2 = 8,
1 + 1*7 + 1*2 = 10,
1 + 1*7 + 2*2 = 12
Solution : gslice example in C++ Program:
CPP
// C++ Program to test the
// functioning of std::gslice
#include <cstddef> // std::size_t
#include <iostream> // std::cout
#include <valarray> // std::valarray, std::gslice
// Driver Code
int main()
{
// valarray sample of size 14
std::valarray<int> sample(14);
for (int i = 0; i < 14; ++i)
sample[i] = i;
std::size_t start = 1;
std::size_t lengths[] = { 2, 3 };
std::size_t strides[] = { 7, 2 };
// gslice object which can be used directly
std::gslice mygslice(
start, std::valarray<std::size_t>(lengths, 2),
std::valarray<std::size_t>(strides, 2));
// creating data using gslice
std::valarray<int> data = sample[mygslice];
std::cout << "gslice:";
// displaying content of data
for (int i = 0; i < data.size(); i++)
std::cout << ' ' << data[i];
std::cout << '\n';
return 0;
}
Outputgslice: 1 3 5 8 10 12
Example 2:
start = 3 , size = {2,4,3} , strides = {19,4,1}
Input : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Output: 3 4 5 7 8 9 11 12 13 15 16 17 19 20 21 22
Explanation : 3 + 0*19 + 0*4 + 0*1 = 3,
3 + 0*19 + 0*4 + 1*1 = 4,
3 + 0*19 + 0*4 + 2*1 = 5,
3 + 0*19 + 1*4 + 0*1 = 7,
3 + 0*19 + 1*4 + 1*1 = 8,
...
...
3 + 1*19 + 0*4 + 0*1 = 22
Example 2: Demonstrates the use of gslices to address columns of a 3D array and perform some operations.
CPP
// C++ Program to demonstrate use of
// gslice to address columns of 3D array
#include <iostream> // std::cout
#include <valarray> // std::gslice
void test_print(std::valarray<int>& v, int rows, int cols,
int planes)
{
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
for (int z = 0; z < planes; ++z)
std::cout
<< v[r * cols * planes + c * planes + z]
<< ' ';
std::cout << '\n';
}
std::cout << '\n';
}
}
// Driver Code
int main()
{
// 3d array: 2 x 4 x 3 elements
std::valarray<int> v
= { 111, 112, 113, 121, 122, 123, 131, 132,
133, 141, 142, 143, 211, 212, 213, 221,
222, 223, 231, 232, 233, 241, 242, 243 };
// int ar3D [2][4][3]
std::cout << "Initial 2x4x3 array:\n";
test_print(v, 2, 4, 3);
// update every value in the first columns of both
// planes
v[std::gslice(0, { 2, 4 }, { 4 * 3, 3 })] = 1;
// subtract the third column from the
// second column in the 1st plane
v[std::gslice(1, { 1, 4 }, { 4 * 3, 3 })]
-= v[std::gslice(2, { 1, 4 }, { 4 * 3, 3 })];
std::cout << "After column operations: \n";
test_print(v, 2, 4, 3);
return 0;
}
OutputInitial 2x4x3 array:
111 112 113
121 122 123
131 132 133
141 142 143
211 212 213
221 222 223
231 232 233
241 242 243
After column operations:
1 -1 113
1 -1 123
1 -1 133
1 -1 143
1 212 213
1 222 223
1 232 233
1 242 243