Class SliceIter

Class Documentation

class SliceIter

A class for iterating through a Slice, realized by actual dimensional bounds.

Given a Slice, either as a literal of a instantiated object, and a set of dimension lengths, this class can be used to iterate through the elements of the slice, returning the current position of the iterator in the bounds of the supplied dimensions. The poistion is return as a one dimensional index to the current position of the iterator in the flattened, contiguous representation of the array. The class can also set the iterator to the beginning or end of the slice and can return the shape of the slice as instantiated on the given dimensions. This involves internally translating negative from-end-of-array indices into the corresponding positive index. Internally, the iterator tracks its position in each index as it traverses a multidimensional slice.

Public Types

using MultiDim = std::vector<size_t>
using Int = Slice::Int

Public Functions

inline SliceIter(const Slice &slice, const MultiDim &dimensions)

Initializes a SliceIter from a SLice and a set of dimensions. The two must have the same cardinality otherwise an invalid argument exception will be thrown. The iterator is initialized at its beginning position, that is the member function atBegin() will return true.

inline bool operator==(const SliceIter &other) const

Checks iterator equality.

Equal SliceIter iterators must have the same dimensionality, the equal dimension lengths for each dimension and equal position in each dimension. All other iterators will compare unequal. An exception to the equal position requirement is that all iterators with the same slice and dimensions will compare equal if the are both at an end position, that is that for both iterators the member function atEnd() returns true.

inline SliceIter &operator++()

Increments the iterator position through the slice.

Returns:

a reference to the incremented iterator.

inline SliceIter operator++(int)

Increments the iterator position through the slice.

Returns:

A copy of the iterator before it was incremented.

inline SliceIter &incrementDim(size_t dim)

Increments the iterator position along a given axis.

Increments the iterator through the slice along a given axis. Calling this function with argument 0 is equivalent to calling the operator++() member function. If the increment takes the position in an axis past the end of the slice in that axis, then the position is reset in that axis and the next axis incremented by one, unless the increment would take the iterator past the end of the entire slice, in which case the final value of the iterator is equal to if the function toEnd() had been called.

SliceIter &operator--()

Reverse iteration is not currently implemented.

SliceIter operator--(int)

Reverse iteration is not currently implemented.

inline Int index() const

Returns the one-dimensional index equivalent to the current position of the iterator in the flattened equivalent of the array dimensions the iterator is based on.

inline const MultiDim position() const

Gets the current position of the iterator as a vector over dimensions.

inline SliceIter &toBegin()

Sets the position to the beginning of the slice. This is the same state as when the iterator is initially constructed.

inline SliceIter &toEnd()

Sets the position to the end of the slice.

inline bool isBegin() const

Returns whether the iterator at its ‘begin’ position.

inline bool isEnd() const

Returns whether the iterator at an ‘end’ position.

inline MultiDim shape() const

Returns the overall shape of the Slice, constrained by the given dimensions.

inline size_t size() const

Returns the total number of elements in the slice.

inline Int start(size_t dim) const

Translates the default and negative bounds into an actual start index for a given dimension.

Parameters:

dim – the dimension for which the start value is requested.

inline Int start() const

Translates the default and negative bounds into an actual start index for the first dimension.

inline Int nElements(size_t dim) const

Calculates the number of elements in the the slice along this dimension.

Parameters:

dim – the dimension for which the number of elements is requested.

inline Int nElements() const

Calculates the number of elements in the the slice along the first dimension.

inline Int step(size_t dim) const

Returns the step value for a given dimension.

inline Int step() const

Returns the step value for the first dimension.

inline std::ostream &print(std::ostream &os) const

Prints the details of the bounds and current position of the iterator.