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

1 auto comb1 = map!"a.dup"(Comb!(uint)(5, 2));
2 uint[][] vals;
3 foreach(c; comb1) {
4     vals ~= c;
5 }
6 auto sorted = sort(vals);
7 assert(sorted.canFind([0u,1]));
8 assert(sorted.canFind([0u,2]));
9 assert(sorted.canFind([0u,3]));
10 assert(sorted.canFind([0u,4]));
11 assert(sorted.canFind([1u,2]));
12 assert(sorted.canFind([1u,3]));
13 assert(sorted.canFind([1u,4]));
14 assert(sorted.canFind([2u,3]));
15 assert(sorted.canFind([2u,4]));
16 assert(sorted.canFind([3u,4]));
17 assert(sorted.length == 10);

Meta