k-combinations
Efficient iterator over k-element combinations of a slice, with no dependencies.
For slices with at most 64 elements, uses a bitmask (Gosper's hack) for fast iteration. Falls back to index-based iteration for larger slices.
Usage
Add to your project:
use Combinations;
let items = ;
// All pairs
for combo in items.combinations
// [&1, &2], [&1, &3], [&2, &3], [&1, &4], [&2, &4], [&3, &4]
// Works on Vec too
let v = vec!;
let pairs: = v.combinations.collect;
Range of sizes
Iterate over combinations of multiple sizes with combinations_range:
use Combinations;
let items = ;
// All subsets of size 1 or 2
for combo in items.combinations_range
// All 2^n subsets (power set)
assert_eq!;
Zero-allocation iteration
Reuse a buffer across calls with next_into, similar to BufReader::read_line:
use Combinations;
let items = ;
let mut iter = items.combinations;
let mut buf = Vecnew;
while iter.next_into
License
MIT