The OpenD Programming Language

CartesianPower

Lazy Forward range of Cartesian Power. It always generates Cartesian Power from 0 to n - 1, use indexedRoR to map it to your range.

Generating a new item is in O(k) (amortized O(1)), the total number of elements is n^k.

Constructors

this
this(size_t n, T[] state)

state should have the length of repeat

Members

Aliases

DeepElement
alias DeepElement = T

Functions

popFront
void popFront()
empty
bool empty [@property getter]

Input range primitives

Properties

front
Slice!(const(T)*) front [@property getter]
length
size_t length [@property getter]

Input range primitives

save
CartesianPower save [@property getter]

Forward range primitive. Allocates using GC.

shape
size_t[2] shape [@property getter]

Examples

import mir.ndslice.fuse;
import mir.ndslice.topology: iota;

assert(iota(2).cartesianPower.fuse == [[0], [1]]);
assert(iota(2).cartesianPower(2).fuse == [[0, 0], [0, 1], [1, 0], [1, 1]]);

auto three_nums_two_bins = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]];
assert(iota(3).cartesianPower(2).fuse == three_nums_two_bins);

import std.string: representation;
assert("AB"d.representation.cartesianPower(2).fuse == ["AA"d, "AB"d, "BA"d, "BB"d]);
import mir.ndslice.topology: iota;
import mir.algorithm.iteration: equal;
import mir.ndslice.slice: sliced;

import std.experimental.allocator.mallocator: Mallocator;
auto alloc = Mallocator.instance;

static immutable expected2r2 = [
    0, 0,
    0, 1,
    1, 0,
    1, 1];
auto r = iota(2);
auto rc = alloc.makeCartesianPower(r.length, 2);
assert(expected2r2.sliced(4, 2).equal(rc.indexedRoR(r)));
alloc.dispose(rc);

See Also

Meta