Files
cppdraft_translate/cppdraft/class/gslice/overview.md
2025-10-25 03:02:53 +03:00

3.4 KiB
Raw Blame History

[class.gslice.overview]

29 Numerics library [numerics]

29.6 Numeric arrays [numarray]

29.6.6 The gslice class [class.gslice]

29.6.6.1 Overview [class.gslice.overview]

🔗

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

#

This class represents a generalized slice out of an array.

Agslice is defined by a starting offset (s), a set of lengths (lj), and a set of strides (dj).

The number of lengths shall equal the number of strides.

2

#

Agslice represents a mapping from a set of indices (ij), equal in number to the number of strides, to a single index k.

It is useful for building multidimensional array classes using thevalarray template, which is one-dimensional.

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.

3

#

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

— end example]

4

#

It is possible to have degenerate generalized slices in which an address is repeated.

5

#

[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

#

If a degenerate slice is used as the argument to the non-const version ofoperator[](const gslice&), the behavior is undefined.