Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | numeric‎ | valarray
m (E: 1st comment is incorrect, 2nd one is superfluous)
m (fmt, capitalized 1st letter)
 
(2 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
{{cpp/numeric/valarray/gslice_array/navbar}}
 
{{cpp/numeric/valarray/gslice_array/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | valarray}}
+
{{dcl header|valarray}}
{{dcl |
+
{{dcl|
 
template< class T > class gslice_array;
 
template< class T > class gslice_array;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
{{tt|std::gslice_array}} is a helper template used by {{c|std::gslice}} subscript operator. It has reference semantics to a subset of the array specified by the {{tt|std::gslice}} object.
+
{{tt|std::gslice_array}} is a helper template used by the {{rlp|operator_at|valarray subscript operator}} with {{c|std::gslice}} argument. It has reference semantics to a subset of the array specified by the {{c|std::gslice}} object.
  
 
===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 | gslice_array}}
+
{{dsc inc|cpp/numeric/valarray/array/dsc constructor|gslice_array}}
{{dsc inc | cpp/numeric/valarray/array/dsc destructor | gslice_array}}
+
{{dsc inc|cpp/numeric/valarray/array/dsc destructor|gslice_array}}
{{dsc inc | cpp/numeric/valarray/array/dsc operator{{=}} | gslice_array}}
+
{{dsc inc|cpp/numeric/valarray/array/dsc operator{{=}}|gslice_array}}
{{dsc inc | cpp/numeric/valarray/array/dsc operator_arith | gslice_array}}
+
{{dsc inc|cpp/numeric/valarray/array/dsc operator_arith|gslice_array}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| code=
+
|code=
 
#include <cassert>
 
#include <cassert>
 
#include <cstddef>
 
#include <cstddef>
Line 43: Line 43:
 
     const std::valarray<std::size_t> strides{15, 5, 1};
 
     const std::valarray<std::size_t> strides{15, 5, 1};
 
     const std::gslice gslice = std::gslice(offset, sizes, strides);
 
     const std::gslice gslice = std::gslice(offset, sizes, strides);
     // indices are generated according to the formula:
+
     // Indices are generated according to the formula:
 
     // index[k] = offset + [0,1,2)*15 + [0,1,2,3)*5 + [0,1,2,3,4)*1
 
     // index[k] = offset + [0,1,2)*15 + [0,1,2,3)*5 + [0,1,2,3,4)*1
 
     //          = offset + inner_product(sizes[k], strides);
 
     //          = offset + inner_product(sizes[k], strides);
Line 61: Line 61:
  
 
     const std::valarray<int> indices = data[gslice];
 
     const std::valarray<int> indices = data[gslice];
     for (unsigned i=0; i != indices.size(); ++i) {
+
     for (unsigned i = 0; i != indices.size(); ++i)
 
         std::cout << std::setfill('0') << std::setw(2) << indices[i] << ' ';
 
         std::cout << std::setfill('0') << std::setw(2) << indices[i] << ' ';
    }
 
 
     std::cout << "\nTotal indices: " << indices.size() << '\n';
 
     std::cout << "\nTotal indices: " << indices.size() << '\n';
     assert(indices.size() == x*y*z);
+
     assert(indices.size() == x * y * z);
  
 
     data = 0;
 
     data = 0;
Line 71: Line 70:
 
     gslice_array = 1;
 
     gslice_array = 1;
 
     // Cells that correspond to generated indices = '1', skipped cells = '0'.
 
     // Cells that correspond to generated indices = '1', skipped cells = '0'.
     for (auto i : data) { std::cout << i << ' '; }
+
     for (auto i : data)
 +
        std::cout << i << ' ';
 
     std::cout << "\nSum of ones = " << data.sum() << '\n';
 
     std::cout << "\nSum of ones = " << data.sum() << '\n';
 
}
 
}
| output=
+
|output=
 
01 02 03 04 06 07 08 09 11 12 13 14 16 17 18 19 21 22 23 24 26 27 28 29  
 
01 02 03 04 06 07 08 09 11 12 13 14 16 17 18 19 21 22 23 24 26 27 28 29  
 
Total indices: 24
 
Total indices: 24
Line 83: Line 83:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/numeric/valarray/dsc slice_array}}
+
{{dsc inc|cpp/numeric/valarray/dsc slice_array}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|ja|zh}}
 
{{langlinks|ja|zh}}

Latest revision as of 01:42, 17 October 2023

 
 
 
 
 
Defined in header <valarray>
template< class T > class gslice_array;

std::gslice_array is a helper template used by the valarray subscript operator with std::gslice argument. It has reference semantics to a subset of the array specified by the std::gslice object.

Contents

[edit] Member types

Type Definition
value_type T

[edit] Member functions

constructs a gslice_array
(public member function) [edit]
destroys a gslice_array
(public member function) [edit]
assigns contents
(public member function) [edit]
performs arithmetic operation on the array referred by generic slice.
(public member function) [edit]

[edit] Example

#include <cassert>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <valarray>
 
int main()
{
    std::valarray<int> data(32);
    std::iota(std::begin(data), std::end(data), 0);
 
    const std::size_t offset = 1, z = 2, y = 3, x = 4;
    const std::valarray<std::size_t> sizes{z, y, x};
    const std::valarray<std::size_t> strides{15, 5, 1};
    const std::gslice gslice = std::gslice(offset, sizes, strides);
    // Indices are generated according to the formula:
    // index[k] = offset + [0,1,2)*15 + [0,1,2,3)*5 + [0,1,2,3,4)*1
    //          = offset + inner_product(sizes[k], strides);
    // where sizes[k] = {[0,z), [0,y), [0,x)}, while the rightmost index (x)
    // runs fastest. As a result we have following set of indices:
    //  index[0]  = 1 + 0*15 + 0*5 + 0*1 = 1
    //  index[1]  = 1 + 0*15 + 0*5 + 1*1 = 2
    //  index[2]  = 1 + 0*15 + 0*5 + 2*1 = 3
    //  index[3]  = 1 + 0*15 + 0*5 + 3*1 = 4
    //  index[4]  = 1 + 0*15 + 1*5 + 0*1 = 6
    //  index[5]  = 1 + 0*15 + 1*5 + 1*1 = 7
    //  index[6]  = 1 + 0*15 + 1*5 + 2*1 = 8
    //  index[7]  = 1 + 0*15 + 1*5 + 3*1 = 9
    //  ...
    //  index[22] = 1 + 1*15 + 2*5 + 2*1 = 28
    //  index[23] = 1 + 1*15 + 2*5 + 3*1 = 29
 
    const std::valarray<int> indices = data[gslice];
    for (unsigned i = 0; i != indices.size(); ++i)
        std::cout << std::setfill('0') << std::setw(2) << indices[i] << ' ';
    std::cout << "\nTotal indices: " << indices.size() << '\n';
    assert(indices.size() == x * y * z);
 
    data = 0;
    std::gslice_array<int> gslice_array = data[gslice];
    gslice_array = 1;
    // Cells that correspond to generated indices = '1', skipped cells = '0'.
    for (auto i : data)
        std::cout << i << ' ';
    std::cout << "\nSum of ones = " << data.sum() << '\n';
}

Output:

01 02 03 04 06 07 08 09 11 12 13 14 16 17 18 19 21 22 23 24 26 27 28 29 
Total indices: 24
0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 0 
Sum of ones = 24

[edit] See also

proxy to a subset of a valarray after applying a slice
(class template) [edit]