[class.gslice.overview]

# 29 Numerics library [[numerics]](./#numerics)

## 29.6 Numeric arrays [[numarray]](numarray#class.gslice.overview)

### 29.6.6 The gslice class [[class.gslice]](class.gslice#overview)

#### 29.6.6.1 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](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L8619)

This class represents a generalized slice out of an array[.](#1.sentence-1)

Agslice is defined by a starting offset (s),
a set of lengths (lj),
and a set of strides (dj)[.](#1.sentence-2)

The number of lengths shall equal the number of strides[.](#1.sentence-3)

[2](#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[.](#2.sentence-1)

It is useful for building multidimensional array classes using
thevalarray template, which is one-dimensional[.](#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[.](#2.sentence-3)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L8642)

[*Example [1](#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[.](#3.sentence-2)

â *end example*]

[4](#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[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/numerics.tex#L8684)

[*Example [2](#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](#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[.](#6.sentence-1)
