Report a bug
If you spot a problem with this page, click here to create a Github issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.ndslice.iterator

This is a submodule of mir.ndslice.
Iterator is a type with a pointer like behavior. An ndslice can be created on top of an iterator using sliced .

Iterators

Iterator Name Used By
BytegroupIterator bytegroup .
FieldIterator slicedField , bitwise , ndiota , and others.
FlattenedIterator flattened 
IndexIterator indexed 
IotaIterator iota 
MapIterator map 
RetroIterator retro 
SliceIterator map  in composition with MapIterator for packed slices.
SlideIterator diff , pairwise , and slide .
StairsIterator stairs 
StrideIterator stride 
ZipIterator zip 
Authors:
Ilya Yaroshenko
struct IotaIterator(I) if (isIntegral!I || isPointer!I);
Step counter.
IotaIterator is used by iota .
Examples:
IotaIterator!int iota;
assert(*iota == 0);

// iteration
++iota;
assert(*iota == 1);

assert(iota[2] == 3);
assert(iota[-1] == 0);

--iota;
assert(*iota == 0);

// opBinary
assert(*(iota + 2) == 2);
assert(*(iota - 3) == -3);
assert((iota - 3) - iota == -3);

// construction
assert(*IotaIterator!int(3) == 3);
assert(iota - 1 < iota);
Examples:
int[32] data;
auto iota = IotaIterator!(int*)(data.ptr);
assert(*iota == data.ptr);

// iteration
++iota;
assert(*iota == 1 + data.ptr);

assert(iota[2] == 3 + data.ptr);
assert(iota[-1] == 0 + data.ptr);

--iota;
assert(*iota == 0 + data.ptr);

// opBinary
assert(*(iota + 2) == 2 + data.ptr);
assert(*(iota - 3) == -3 + data.ptr);
assert((iota - 3) - iota == -3);

// construction
assert(*IotaIterator!(int*)(data.ptr) == data.ptr);
assert(iota - 1 < iota);
I _index;
struct RetroIterator(Iterator);
Reverse directions for an iterator.
RetroIterator is used by retro .
Examples:
IotaIterator!int iota;
RetroIterator!(IotaIterator!int) retro;

++iota;
--retro;
assert(*retro == *iota);

--iota;
++retro;
assert(*retro == *iota);

assert(retro[-7] == iota[7]);

iota += 100;
retro -= 100;
assert(*retro == *iota);

iota -= 100;
retro += 100;
assert(*retro == *iota);

assert(*(retro + 10) == *(iota - 10));

assert(retro - 1 < retro);

assert((retro - 5) - retro == -5);

iota = IotaIterator!int(3);
retro = RetroIterator!(IotaIterator!int)(iota);
assert(*retro == *iota);
Iterator _iterator;
template __map(alias fun)
struct StrideIterator(Iterator);
Iterates an iterator with a fixed strides.
StrideIterator is used by stride .
Examples:
IotaIterator!int iota;
StrideIterator!(IotaIterator!int) stride;
stride._stride = -3;

iota -= stride._stride;
--stride;
assert(*stride == *iota);

iota += stride._stride;
++stride;
assert(*stride == *iota);

assert(stride[7] == iota[7 * stride._stride]);

iota -= 100 * stride._stride;
stride -= 100;
assert(*stride == *iota);

iota += 100 * stride._stride;
stride += 100;
assert(*stride == *iota);

assert(*(stride + 10) == *(iota + 10 * stride._stride));

assert(stride - 1 < stride);

assert((stride - 5) - stride == -5);

iota = IotaIterator!int(3);
stride = StrideIterator!(IotaIterator!int)(3, iota);
assert(*stride == *iota);
ptrdiff_t _stride;
Iterator _iterator;
template __map(alias fun)
struct ZipIterator(Iterators...) if (Iterators.length > 1);
Iterates multiple iterators in lockstep.
ZipIterator is used by zip .
Examples:
double[10] data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
alias ItA = IotaIterator!int;
alias ItB = double*;
alias ItZ = ZipIterator!(ItA, ItB);
auto zip = ItZ(ItA(3), data.ptr);
assert((*zip).a == 3);
assert((*zip).b == 1);

// iteration
++zip;
assert((*zip).a == 3 + 1);
assert((*zip).b == 1 + 1);
assert(&(*zip).b() == data.ptr + 1);

assert(zip[4].a == 3 + 5);
assert(zip[4].b == 1 + 5);
assert(&zip[4].b() == data.ptr + 5);

--zip;
assert((*zip).a == 3);
assert((*zip).b == 1);

assert((*(zip + 2)).a == 3 + 2);
assert((*(zip - 3)).a == 3 + -3);
assert((*(zip + 2)).b == 1 + 2);
assert((*(zip + 3 - 3)).b == 1);
assert((zip - 3).opBinary!"-"(zip) == -3);

assert(zip == zip);
assert(zip - 1 < zip);
Iterators _iterators;
struct MapIterator(Iterator, alias fun);
MapIterator is used by map .
Iterator _iterator;
template __map(alias fun1)
auto mapIterator(alias fun, Iterator)(Iterator iterator);
Creates a mapped iterator. Uses __map if possible.
struct BytegroupIterator(Iterator, size_t count, DestinationType) if (count);
BytegroupIterator is used by Bytegroup  and bytegroup .
Iterator _iterator;
struct SlideIterator(Iterator, size_t params, alias fun) if (params > 1);
SlideIterator is used by diff  and slide .
Examples:
import mir.functional: naryFun;
auto data = [1, 3, 8, 18];
auto diff = SlideIterator!(int*, 2, naryFun!"b - a")(data.ptr);
assert(*diff == 2);
assert(diff[1] == 5);
assert(diff[2] == 10);
Iterator _iterator;
template __map(alias fun1)
struct IndexIterator(Iterator, Field);
Iterates a field using an iterator.
IndexIterator is used by TODO.
Iterator _iterator;
Field _field;
template __map(alias fun)
struct SliceIterator(SliceKind kind, size_t[] packs, Iterator);
Iterates on top of another iterator and returns a slice as a multidimensional window at the current position.
SliceIterator is used by map  for packed slices.
alias Elem = Slice!(kind, packs, Iterator);
size_t[Elem.N] _lengths;
ptrdiff_t[Elem.S] _strides;
Iterator _iterator;
struct FieldIterator(Field);
Creates an iterator on top of a field.
FieldIterator is used by slicedField , bitwise , ndiota , and others.
ptrdiff_t _index;
Field _field;
template __map(alias fun)
const _Slice!() opSlice(size_t dimension)(size_t i, size_t j);
auto opIndex()(_Slice!() sl);
Returns:
_field[_index + sl.i .. _index + sl.j].
struct FlattenedIterator(SliceKind kind, size_t[] packs, Iterator) if (packs[0] > 1 && (kind == Universal || kind == Canonical));
Creates an iterator on top of all elements in a slice.
FieldIterator is used by bitwise , ndiota , and others.
ptrdiff_t[packs[0]] _indexes;
Slice!(kind, packs, Iterator) _slice;
template __map(alias fun)
auto opIndexAssign(E)(auto ref E elem, size_t index);
struct StairsIterator(Iterator);
StairsIterator is used by stairs .
Examples:
// 0
// 1 2
// 3 4 5
// 6 7 8 9
// 10 11 12 13 14
auto it = StairsIterator!(IotaIterator!size_t)(1, IotaIterator!size_t());
assert(*it == [0]);
assert(it[4] == [10, 11, 12, 13, 14]);
assert(*(it + 4) == [10, 11, 12, 13, 14]);
++it;
assert(*it == [1, 2]);
it += 3;
assert(*it == [10, 11, 12, 13, 14]);
assert(it[-3] == [1, 2]);
assert(*(it - 3) == [1, 2]);
assert(it + 1 > it);
assert(it + 1 - 1 == it);
assert(it + 3 - it == 3);
--it;
assert(*it == [6, 7, 8, 9]);
size_t _length;
Iterator _iterator;
Slice!(Contiguous, [1], Iterator) opUnary(string op : "*")();
Slice!(Contiguous, [1], Iterator) opIndex()(ptrdiff_t index);