The OpenD Programming Language

Comb

Generates every possible combination of r elements of the given sequence, or array indices from zero to N, depending on which c'tor is called. Uses an input range interface.

Note: The buffer that is returned by front is recycled across iterations. To duplicate it instead, use map!"a.dup" or map!"a.idup".

Constructors

this
this(uint n, uint r)

Ctor to generate all possible combinations of array indices for a length r array. This is a special-case optimization and is faster than simply using the other ctor to generate all length r combinations from seq(0, length).

this
this(T[] array, uint r)

General ctor. array is a sequence from which to generate the * combinations. r is the length of the combinations to be generated.

Members

Functions

popFront
void popFront()

Advances to the next combination. The array returned by front will be overwritten with the new results.

Properties

empty
bool empty [@property getter]
front
const(T)[] front [@property getter]

Gets the current combination.

length
size_t length [@property getter]
save
typeof(this) save [@property getter]

Bugs

Only supports iterating over up to size_t.max combinations. This was a conscious tradeoff to allow this range to have a length of type size_t, since iterating over such huge combination spaces would be insanely slow anyhow.

Examples

auto comb1 = map!"a.dup"(Comb!(uint)(5, 2));
uint[][] vals;
foreach(c; comb1) {
    vals ~= c;
}
auto sorted = sort(vals);
assert(sorted.canFind([0u,1]));
assert(sorted.canFind([0u,2]));
assert(sorted.canFind([0u,3]));
assert(sorted.canFind([0u,4]));
assert(sorted.canFind([1u,2]));
assert(sorted.canFind([1u,3]));
assert(sorted.canFind([1u,4]));
assert(sorted.canFind([2u,3]));
assert(sorted.canFind([2u,4]));
assert(sorted.canFind([3u,4]));
assert(sorted.length == 10);

Meta