Files
2025-10-25 03:02:53 +03:00

158 lines
4.9 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[class.gslice]
# 29 Numerics library [[numerics]](./#numerics)
## 29.6 Numeric arrays [[numarray]](numarray#class.gslice)
### 29.6.6 The gslice class [class.gslice]
#### [29.6.6.1](#overview) Overview [[class.gslice.overview]](class.gslice.overview)
[🔗](#lib:gslice)
namespace std {class gslice {public: gslice();
gslice(size_t s, const valarray<size_t>& l, const valarray<size_t>& d);
size_t start() const;
valarray<size_t> size() const;
valarray<size_t> stride() const; };}
[1](#overview-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L8619)
This class represents a generalized slice out of an array[.](#overview-1.sentence-1)
Agslice is defined by a starting offset (s),
a set of lengths (lj),
and a set of strides (dj)[.](#overview-1.sentence-2)
The number of lengths shall equal the number of strides[.](#overview-1.sentence-3)
[2](#overview-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L8628)
Agslice represents a mapping from a set of indices (ij),
equal in number to the number of strides, to a single index k[.](#overview-2.sentence-1)
It is useful for building multidimensional array classes using
thevalarray template, which is one-dimensional[.](#overview-2.sentence-2)
The set of one-dimensional index values specified by a gslice are
k=s+∑jijdj where the multidimensional indices ij range in value from
0 to lij−1[.](#overview-2.sentence-3)
[3](#overview-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L8642)
[*Example [1](#overview-example-1)*:
Thegslice specificationstart = 3 length = {2, 4, 3} stride = {19, 4, 1} yields the sequence of one-dimensional indices
k=3+(0,1)×19+(0,1,2,3)×4+(0,1,2)×1 which are ordered as shown in the following table:
```
(i0,i1,i2,k)=
(0,0,0, 3),
(0,0,1, 4),
(0,0,2, 5),
(0,1,0, 7),
(0,1,1, 8),
(0,1,2, 9),
(0,2,0,11),
(0,2,1,12),
(0,2,2,13),
(0,3,0,15),
(0,3,1,16),
(0,3,2,17),
(1,0,0,22),
(1,0,1,23),
(1,3,2,36)
```
That is, the highest-ordered index turns fastest[.](#overview-3.sentence-2)
— *end example*]
[4](#overview-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L8680)
It is possible to have degenerate generalized slices in which an address
is repeated[.](#overview-4.sentence-1)
[5](#overview-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L8684)
[*Example [2](#overview-example-2)*:
If the stride parameters in the previous
example are changed to {1, 1, 1}, the first few elements of the
resulting sequence of indices will be
```
(0,0,0, 3),
(0,0,1, 4),
(0,0,2, 5),
(0,1,0, 4),
(0,1,1, 5),
(0,1,2, 6),
```
— *end example*]
[6](#overview-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L8702)
If a degenerate slice is used as the argument to the
non-const version ofoperator[](const gslice&),
the behavior is undefined[.](#overview-6.sentence-1)
#### [29.6.6.2](#gslice.cons) Constructors [[gslice.cons]](gslice.cons)
[🔗](#lib:gslice,constructor)
`gslice();
gslice(size_t start, const valarray<size_t>& lengths,
const valarray<size_t>& strides);
`
[1](#gslice.cons-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L8720)
The default constructor is equivalent togslice(0, valarray<size_t>(), valarray<size_t>())[.](#gslice.cons-1.sentence-1)
The constructor with arguments builds agslice based on a specification of start, lengths, and strides, as explained
in the previous subclause[.](#gslice.cons-1.sentence-2)
#### [29.6.6.3](#gslice.access) Access functions [[gslice.access]](gslice.access)
[🔗](#lib:start,gslice)
`size_t start() const;
valarray<size_t> size() const;
valarray<size_t> stride() const;
`
[1](#gslice.access-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L8741)
*Returns*: The representation of the
start, lengths, or strides specified for the gslice[.](#gslice.access-1.sentence-1)
[2](#gslice.access-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L8746)
*Complexity*: start() is constant time[.](#gslice.access-2.sentence-1)
size() and stride() are linear in the number of strides[.](#gslice.access-2.sentence-2)