state should have the length of repeat
Input range primitives
Forward range primitive. Allocates using GC.
import mir.ndslice.fuse; import mir.ndslice.topology: iota; import std.string: representation; assert(iota(3).combinations(2).fuse == [[0, 1], [0, 2], [1, 2]]); assert("AB"d.representation.combinations(2).fuse == ["AB"d]); assert("ABC"d.representation.combinations(2).fuse == ["AB"d, "AC"d, "BC"d]);
import mir.algorithm.iteration: equal; import mir.ndslice.slice: sliced; import mir.ndslice.topology: iota; import std.experimental.allocator.mallocator; auto alloc = Mallocator.instance; static immutable expected3r2 = [ 0, 1, 0, 2, 1, 2]; auto r = iota(3); auto rc = alloc.makeCombinations(r.length, 2); assert(expected3r2.sliced(3, 2).equal(rc.indexedRoR(r))); alloc.dispose(rc);
Lazy Forward range of Combinations. It always generates combinations from 0 to n - 1, use indexedRoR to map it to your range.
Generating a new combination is in O(k), the number of combinations is binomial(n, k).