Jive reference manual
|
Selects parts of array-like objects. More...
#include <jem/base/Slice.h>
Public Member Functions | |
Slice () | |
Creates an empty Slice. More... | |
Slice (int first, int last, int stride=1) | |
Constructs a Slice with a given begin index, end index and stride. More... | |
int | first () const |
Returns the begin index. More... | |
int | last () const |
Returns the end index. More... | |
int | last (int ubound) const |
Returns a clipped end index. More... | |
int | stride () const |
Returns the stride. More... | |
Static Public Attributes | |
static const int | MAX_INDEX |
The largest representable integer. More... | |
Related Functions | |
(Note that these are not member functions.) | |
Slice construction functions. | |
SliceFromTo | slice (int first, int last) |
Constructs a SliceFrom object with a given begin index and end index. More... | |
Slice | slice (int first, int last, int stride) |
Constructs a Slice object with a given begin index, end index and stride. More... | |
SliceTo | slice (Begin, int last) |
Constructs a SliceTo object with a given end index. More... | |
Slice | slice (Begin, int last, int stride) |
Constructs a Slice object with a given end index and stride. More... | |
SliceFrom | slice (int first, End) |
Constructs a SliceFrom object with a given begin index. More... | |
Slice | slice (int first, End, int stride) |
Constructs a Slice object with a given begin index and stride. More... | |
const SliceAll & | slice (Begin, End) |
Returns a SliceAll object. More... | |
Slice | slice (Begin, End, int stride) |
Constructs a Slice object with a given stride. More... | |
const SliceAll & | slice (const SliceAll &all) |
Returns a SliceAll object. More... | |
Slice | slice (const SliceAll &all, int stride) |
Constructs a Slice object with a given stride. More... | |
Slice
objects are used to select parts of arrays, strings, and other array-like objects. Such a selection, or slice, is defined by three integers: a begin index, an end index and a stride. The begin index is a non-negative integer that points to the first element in the slice. The end index is an integer that points one position past the last element in the slice; it is always larger than or equal to the begin index. The stride is a positive integer that specifies the distance between two consecutive elements in the slice. For instance, if the stride is three, a slice contains only each third element of an array-like object. If the stride equals one, the slice is said to be contiguous.
The precise definition of a slice is as follows. Let s represent a slice of an array-like object a, and let first, last and stride denote the begin index, end index and stride, respectively. The elements of the slice are then given by:
s[i] = a[first + i * stride]
for all i for which the following condition is true:
first <= first + i * stride < last
The begin index, end index and stride must select a valid – possibly empty – set of elements from an array-like object. To be precise, the following conditions should be met:
0 <= first <= n and first <= last and stride > 0
where n denotes the length of the array a. The end index may be larger than the size of the array; it will be automatically adjusted so that it becomes less than or equal to the size of the array. In this way one can easily create a slice that extends to the end of an array-like object.
Slices are created by invoking the so-called slice operator of an array-like object. This operator is a member function – usually an overloaded subscripting or function call operator – that accepts a single Slice
object and returns an object representing part of the array-like object. The String
class, for instance, provides an overloaded version of the subscripting operator that returns a sub-string for a given Slice
object.
Slice
objects are normally constructed by calling the non-member function slice()
, which is overloaded for several types of slices (contiguous and non-contiguous slices, for instance). These functions may be passed instances of the special classes Begin
and End
to create slices that start at the beginning of an array, extend to the end of an array, or include all elements of an array. For convenience, there are two pre-defined instances of these classes which are named BEGIN
and END
, respectively. There also exists a constant named ALL
(of type SliceAll
) than can be used to select all elements in an array.
The following example demonstrates how the slice()
function can be used.
Most versions of the overloaded slice()
function do not return an instance of the Slice
class but an instance of a derived class. These derived classes identify special types of slices, such as contiguous slices, and are used to select an optimized version of the slice operator at compile time. For instance, the String
class provides an overloaded slice operator for SliceFrom
objects that takes advantage of the fact that the begin index of a SliceFrom
equals zero.
jem::Slice::Slice | ( | ) |
jem::Slice::Slice | ( | int | first, |
int | last, | ||
int | stride = 1 |
||
) |
int jem::Slice::first | ( | ) | const |
Slice
. int jem::Slice::last | ( | ) | const |
Slice
. int jem::Slice::last | ( | int | ubound | ) | const |
min ( ubound, last() )
int jem::Slice::stride | ( | ) | const |
Slice
.
|
related |
Returns a SliceFromTo
object with a given begin index and end index. Note that the end index may not be larger than the size of the array-like object from which a slice is to be taken.
first | - the begin index. |
last | - the end index. |
SliceFromTo ( first, last )
|
related |
Returns a Slice
object with a given begin index, end index and stride. Note that the end index is allowed to be larger than the size of the array-like object from which a slice is to be taken.
first | - the begin index. |
last | - the end index. |
stride | - the stride. |
Slice ( first, last, stride )
Returns a Slice
object with a given begin index and stride.
first | - the begin index. |
stride | - the stride. |
Slice ( first, Slice::MAX_INDEX, stride )
Returns ALL
(the pre-defined instance of type SliceAll
).
ALL
Returns a Slice
object with a given stride.
stride | - the stride. |
Slice ( 0, Slice::MAX_INDEX, stride )
Returns a Slice
object with a given stride. Note that the first argument is not used; it is only declared for overloading purposes.
all | - a reference to a SliceAll object (not used). |
stride | - the stride. |
Slice ( 0, Slice::MAX_INDEX, stride )
|
static |
The constant MAX_INDEX
has the value of the largest representable integer. It can be used to construct a slice that extends to the end of an array-like object.