flattened

A contiguous 1-dimensional slice of all elements of a slice. flattened iterates existing data. The order of elements is preserved.

flattened can be generalized with other selectors.

Parameters

slice Slice!(Iterator, N)

slice to be iterated

Return Value

Type: Slice!Iterator

contiguous 1-dimensional slice of elements of the slice

Examples

Regular slice

assert(iota(4, 5).flattened == iota(20));
assert(iota(4, 5).canonical.flattened == iota(20));
assert(iota(4, 5).universal.flattened == iota(20));

Packed slice

import mir.ndslice.slice;
import mir.ndslice.dynamic;
assert(iota(3, 4, 5, 6, 7).pack!2.flattened[1] == iota([6, 7], 6 * 7));

Properties

auto elems = iota(3, 4).universal.flattened;

elems.popFrontExactly(2);
assert(elems.front == 2);
/// `_index` is available only for canonical and universal ndslices.
assert(elems._iterator._indices == [0, 2]);

elems.popBackExactly(2);
assert(elems.back == 9);
assert(elems.length == 8);

Index property

import mir.ndslice.slice;
auto slice = new long[20].sliced(5, 4);

for (auto elems = slice.universal.flattened; !elems.empty; elems.popFront)
{
    ptrdiff_t[2] index = elems._iterator._indices;
    elems.front = index[0] * 10 + index[1] * 3;
}
assert(slice ==
    [[ 0,  3,  6,  9],
     [10, 13, 16, 19],
     [20, 23, 26, 29],
     [30, 33, 36, 39],
     [40, 43, 46, 49]]);

Random access and slicing

import mir.ndslice.allocation: slice;
import mir.ndslice.slice: sliced;

auto elems = iota(4, 5).slice.flattened;

elems = elems[11 .. $ - 2];

assert(elems.length == 7);
assert(elems.front == 11);
assert(elems.back == 17);

foreach (i; 0 .. 7)
    assert(elems[i] == i + 11);

// assign an element
elems[2 .. 6] = -1;
assert(elems[2 .. 6] == repeat(-1, 4));

// assign an array
static ar = [-1, -2, -3, -4];
elems[2 .. 6] = ar;
assert(elems[2 .. 6] == ar);

// assign a slice
ar[] *= 2;
auto sl = ar.sliced(ar.length);
elems[2 .. 6] = sl;
assert(elems[2 .. 6] == sl);

Meta