The OpenD Programming Language

sliced

Creates an n-dimensional slice-shell over an iterator.

  1. auto sliced(Iterator iterator, size_t[N] lengths)
    sliced
    (
    size_t N
    Iterator
    )
    (
    return scope Iterator iterator
    ,
    size_t[N] lengths...
    )
    if (
    !__traits(isStaticArray, Iterator) &&
    N
    &&
    !is(Iterator : Slice!(_Iterator, _N, kind),
    _Iterator
    size_t _N
    SliceKind kind
    )
    )
  2. Slice!(T*) sliced(T[] array)
  3. Slice!(Iterator, N, kind) sliced(Slice!(Iterator, 1, kind) slice, size_t[N] lengths)

Parameters

iterator Iterator

An iterator, a pointer, or an array.

lengths size_t[N]

A list of lengths for each dimension

Return Value

Type: auto

n-dimensional slice

Examples

Random access range primitives for slices over user defined types

struct MyIota
{
    //`[index]` operator overloading
    auto opIndex(size_t index) @safe nothrow
    {
        return index;
    }

    auto lightConst()() const @property { return MyIota(); }
    auto lightImmutable()() immutable @property { return MyIota(); }
}

import mir.ndslice.iterator: FieldIterator;
alias Iterator = FieldIterator!MyIota;
alias S = Slice!(Iterator, 2);
import std.range.primitives;
static assert(hasLength!S);
static assert(hasSlicing!S);
static assert(isRandomAccessRange!S);

auto slice = Iterator().sliced(20, 10);
assert(slice[1, 2] == 12);
auto sCopy = slice.save;
assert(slice[1, 2] == 12);

Meta