Struct 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.

struct Comb(T) ;

Constructors

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

Fields

NameTypeDescription
chosen T*
diff int
myArray T*
N int
pos uint*
R int
_length size_t

Properties

NameTypeDescription
empty[get] bool
front[get] const(T)[]Gets the current combination.
length[get] size_t
save[get] typeof(this)

Methods

NameDescription
popFront Advances to the next combination. The array returned by front will be overwritten with the new results.
popFrontArray
popFrontNum
setLen

Aliases

NameDescription
CombArray

Note

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

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);