Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/valarray/gslice"

From cppreference.com
< cpp‎ | numeric‎ | valarray
(+)
 
m (Fixed the link to std::valarray::operator[].)
 
(21 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{cpp/title|gslice}}
 
{{cpp/title|gslice}}
{{cpp/numeric/valarray/sidebar}}
+
{{cpp/numeric/valarray/navbar}}
{{ddcl | header=valarray |
+
{{ddcl|header=valarray|
 
class gslice;
 
class gslice;
 
}}
 
}}
  
{{tt|std::gslice}} is the selector class that identifies a subset of {{cpp|std::valarray}} indexes defined by a multi-level set of strides and sizes. Objects of type {{tt|std::slice}} can be used as indexes with valarray's {{tt|operator[]}} to select, for example, columns of a multidimensional array represented as a {{tt|valarray}}.
+
{{tt|std::gslice}} is the selector class that identifies a subset of {{lc|std::valarray}} indices defined by a multi-level set of strides and sizes. Objects of type {{tt|std::gslice}} can be used as indices with valarray's {{c/core|operator[]}} to select, for example, columns of a multidimensional array represented as a {{tt|valarray}}.
  
Given the starting value {{math|s}}, a list of strides {{math|i{{su|b=j}}}} and a list of sizes {{math|d{{su|b=j}}}}, a {{tt|std::gslice}} constructed from these values selects the set of indices {{math|k{{=}}s+Σ{{su|b=j}}(i{{su|b=j}}d{{su|b=j}})}}.
+
Given the starting value {{math|s}}, a list of strides {{math|i{{su|b=j}}}} and a list of sizes {{math|d{{su|b=j}}}}, a {{tt|std::gslice}} constructed from these values selects the set of indices {{math|k{{su|b=j}}{{=}}s+Σ{{su|b=j}}(i{{su|b=j}}d{{su|b=j}})}}.
 +
 
 +
For example, a gslice with starting index {{tt|3}}, strides {{tt|{19,4,1}}} and lengths {{tt|{2,4,3}}} generates the following set of {{tt|24{{=}}2*4*3}} indices:
 +
 
 +
{{cc|1=
 +
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 + 0*19 + 1*4 + 2*1 = 9,
 +
3 + 0*19 + 2*4 + 0*1 = 11,
 +
...
 +
3 + 1*19 + 3*4 + 1*1 = 35,
 +
3 + 1*19 + 3*4 + 2*1 = 36}}
 +
 
 +
It is possible to construct {{tt|std::gslice}} objects that select some indices more than once: if the above example used the strides {{tt|{1,1,1}<!---->}}, the indices would have been {{tt|{3, 4, 5, 4, 5, 6, ...}<!---->}}. Such gslices may only be used as arguments to the const version of {{rlpt|operator at|std::valarray::operator[]}}, otherwise the behavior is undefined.
  
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list mem ctor | cpp/numeric/valarray/gslice/gslice | constructs a gslice}}
+
{{dsc mem ctor|cpp/numeric/valarray/gslice|inlinemem=true|constructs a generic slice}}
{{dcl list mem fun | cpp/numeric/valarray/gslice/gstart | accesses the start of the gslice}}
+
{{dsc mem fun|cpp/numeric/valarray/gslice|title=start<br>size<br>stride|inlinemem=true|returns the parameters of the slice}}
{{dcl list mem fun | cpp/numeric/valarray/gslice/gstride | accesses the array of strides of the gslice}}
+
{{dsc end}}
{{dcl list mem fun | cpp/numeric/valarray/gslice/gsize | accesses the array of sizees of the gslice}}
+
 
{{dcl list end}}
+
{{member|{{small|std::gslice::}}gslice|
 +
{{dcl begin}}
 +
{{dcl|num=1|
 +
gslice()
 +
}}
 +
{{dcl|num=2|
 +
gslice( std::size_t start, const std::valarray<std::size_t>& sizes,
 +
                          const std::valarray<std::size_t>& strides );
 +
}}
 +
{{dcl|num=3|
 +
gslice( const gslice& other );
 +
}}
 +
{{dcl end}}
 +
 
 +
Constructs a new generic slice.
 +
 
 +
@1@ Default constructor. Equivalent to {{c|gslice(0, std::valarray<std::size_t>(), std::valarray<std::size_t>())}}. This constructor exists only to allow construction of arrays of slices.
 +
 
 +
@2@ Constructs a new slice with parameters {{c|start}}, {{c|sizes}}, {{c|strides}}.
 +
 
 +
@3@ Constructs a copy of {{c|other}}.
 +
 
 +
===Parameters===
 +
{{par begin}}
 +
{{par|start|the position of the first element}}
 +
{{par|sizes|an array that defines the number of elements in each dimension}}
 +
{{par|strides|an array that defines the number of positions between successive elements in each dimension}}
 +
{{par|other|another slice to copy}}
 +
{{par end}}
 +
}}
 +
 
 +
 
 +
{{member|{{small|std::slice::}}start, size, stride|
 +
{{dcl begin}}
 +
{{dcl|num=1|
 +
std::size_t start() const;
 +
}}
 +
{{dcl|num=2|
 +
std::valarray<std::size_t> size() const;
 +
}}
 +
{{dcl|num=3|
 +
std::valarray<std::size_t> stride() const;
 +
}}
 +
{{dcl end}}
 +
 
 +
Returns the parameters passed to the slice on construction - start, sizes and strides respectively.
 +
 
 +
===Parameters===
 +
(none)
 +
 
 +
===Return value===
 +
The parameters of the slice -- start, sizes and strides respectively.
 +
 
 +
===Complexity===
 +
Constant.
 +
}}
  
 
===Example===
 
===Example===
{{example cpp
+
{{example
| demonstrates the use of gslices to address columns of a 3D array
+
|Demonstrates the use of gslices to address columns of a 3D array:
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
 
#include <valarray>
 
#include <valarray>
void test_print(std::valarray<int>& v, int rows, int cols, int planes)
+
 
 +
void test_print(std::valarray<int>& v, int planes, int rows, int cols)
 
{
 
{
     for(int r=0; r<rows; ++r) {
+
     for (int r = 0; r < rows; ++r)
         for(int c=0; c<cols; ++c) {
+
    {
             for(int z=0; z<planes; ++z)
+
         for (int z = 0; z < planes; ++z)
                 std::cout << v[r*cols*planes + c*planes + z] << ' ';
+
        {
             std::cout << '\n';
+
             for (int c = 0; c < cols; ++c)
 +
                 std::cout << v[z * rows * cols + r * cols + c] << ' ';
 +
             std::cout << "  ";
 
         }
 
         }
 
         std::cout << '\n';
 
         std::cout << '\n';
 
     }
 
     }
 
}
 
}
 +
 
int main()
 
int main()
 
{
 
{
 
     std::valarray<int> v = // 3d array: 2 x 4 x 3 elements
 
     std::valarray<int> v = // 3d array: 2 x 4 x 3 elements
    { 111,112,113 , 121,122,123 , 131,132,133 , 141,142,143,
+
        {111,112,113 , 121,122,123 , 131,132,133 , 141,142,143,
      211,212,213 , 221,222,223 , 231,232,233 , 241,242,243};
+
        211,212,213 , 221,222,223 , 231,232,233 , 241,242,243};
 
     // int ar3d[2][4][3]
 
     // int ar3d[2][4][3]
 
     std::cout << "Initial 2x4x3 array:\n";
 
     std::cout << "Initial 2x4x3 array:\n";
 
     test_print(v, 2, 4, 3);
 
     test_print(v, 2, 4, 3);
 
+
   
 
     // update every value in the first columns of both planes
 
     // update every value in the first columns of both planes
     v[std::gslice(0, {2, 4}, {4*3, 3})] = 1; // two level one strides of 12 elements
+
     v[std::gslice(0, {2, 4}, {4 * 3, 3})] = 1; // two level one strides of 12 elements
                                            // then four level two strides of 3 elements
+
                                              // then four level two strides of 3 elements
 
+
   
 
     // subtract the third column from the second column in the 1st plane
 
     // 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})];
+
     v[std::gslice(1, {1, 4}, {4 * 3, 3})] -= v[std::gslice(2, {1, 4}, {4 * 3, 3})];
 
+
   
     std::cout << "After column operations: \n";
+
     std::cout << "\n" "After column operations:\n";
 
     test_print(v, 2, 4, 3);
 
     test_print(v, 2, 4, 3);
 
}
 
}
| output=
+
|output=
 
Initial 2x4x3 array:
 
Initial 2x4x3 array:
111 112 113
+
111 112 113   211 212 213
121 122 123
+
121 122 123   221 222 223
131 132 133
+
131 132 133   231 232 233
141 142 143
+
141 142 143   241 242 243
 
+
211 212 213
+
221 222 223
+
231 232 233
+
241 242 243
+
  
 
After column operations:
 
After column operations:
1 -1 113
+
1 -1 113   1 212 213
1 -1 123
+
1 -1 123   1 222 223
1 -1 133
+
1 -1 133   1 232 233
1 -1 143
+
1 -1 143   1 242 243
 
+
1 212 213
+
1 222 223
+
1 232 233
+
1 242 243
+
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=543|std=C++98|before=it was unclear whether a default constructed generic slice is usable|after=it is usable (as an empty subset)}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/numeric/valarray/dcl list operator_at}}
+
{{dsc inc|cpp/numeric/valarray/dsc operator_at}}
{{dcl list template | cpp/numeric/valarray/dcl list slice}}
+
{{dsc inc|cpp/numeric/valarray/dsc slice}}
{{dcl list template | cpp/numeric/valarray/dcl list gslice_array}}
+
{{dsc inc|cpp/numeric/valarray/dsc gslice_array}}
{{dcl list end}}
+
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 01:12, 27 February 2023

 
 
 
 
Defined in header <valarray>
class gslice;

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).

For example, a gslice with starting index 3, strides {19,4,1} and lengths {2,4,3} generates the following set of 24=2*4*3 indices:

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 + 0*19 + 1*4 + 2*1 = 9,
3 + 0*19 + 2*4 + 0*1 = 11,
...
3 + 1*19 + 3*4 + 1*1 = 35,
3 + 1*19 + 3*4 + 2*1 = 36

It is possible to construct std::gslice objects that select some indices more than once: if the above example used the strides {1,1,1}, the indices would have been {3, 4, 5, 4, 5, 6, ...}. Such gslices may only be used as arguments to the const version of std::valarray::operator[], otherwise the behavior is undefined.

Contents

[edit] Member functions

(constructor)
constructs a generic slice
(public member function)
startsizestride
returns the parameters of the slice
(public member function)

std::gslice::gslice

gslice()
(1)
gslice( std::size_t start, const std::valarray<std::size_t>& sizes,
                           const std::valarray<std::size_t>& strides );
(2)
gslice( const gslice& other );
(3)

Constructs a new generic slice.

1) Default constructor. Equivalent to gslice(0, std::valarray<std::size_t>(), std::valarray<std::size_t>()). This constructor exists only to allow construction of arrays of slices.
2) Constructs a new slice with parameters start, sizes, strides.
3) Constructs a copy of other.

Parameters

start - the position of the first element
sizes - an array that defines the number of elements in each dimension
strides - an array that defines the number of positions between successive elements in each dimension
other - another slice to copy


std::slice::start, size, stride

std::size_t start() const;
(1)
std::valarray<std::size_t> size() const;
(2)
std::valarray<std::size_t> stride() const;
(3)

Returns the parameters passed to the slice on construction - start, sizes and strides respectively.

Parameters

(none)

Return value

The parameters of the slice -- start, sizes and strides respectively.

Complexity

Constant.

[edit] Example

Demonstrates the use of gslices to address columns of a 3D array:

#include <iostream>
#include <valarray>
 
void test_print(std::valarray<int>& v, int planes, int rows, int cols)
{
    for (int r = 0; r < rows; ++r)
    {
        for (int z = 0; z < planes; ++z)
        {
            for (int c = 0; c < cols; ++c)
                std::cout << v[z * rows * cols + r * cols + c] << ' ';
            std::cout << "  ";
        }
        std::cout << '\n';
    }
}
 
int main()
{
    std::valarray<int> v = // 3d array: 2 x 4 x 3 elements
        {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; // two level one strides of 12 elements
                                               // then four level two strides of 3 elements
 
    // 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 << "\n" "After column operations:\n";
    test_print(v, 2, 4, 3);
}

Output:

Initial 2x4x3 array:
111 112 113   211 212 213
121 122 123   221 222 223
131 132 133   231 232 233
141 142 143   241 242 243
 
After column operations:
1 -1 113   1 212 213
1 -1 123   1 222 223
1 -1 133   1 232 233
1 -1 143   1 242 243

[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 generic slice is usable it is usable (as an empty subset)

[edit] See also

get/set valarray element, slice, or mask
(public member function) [edit]
BLAS-like slice of a valarray: starting index, length, stride
(class) [edit]
proxy to a subset of a valarray after applying a gslice
(class template) [edit]