Crate permute

Source
Expand description

§permute

crate.io version badge docs.rs status badge github actions status badge

Generate permutations of a slice in a memory-efficient and deterministic manner, using Heap’s algorithm.

For instance, printing all the permutations of the sequence [“red”, “green”, “blue”]:

use permute::permutations_of;

for permutation in permutations_of(&["red", "green", "blue"]) {
    for element in permutation {
        print!("{}, ", element);
    }
    println!("");
}

Based on the ordering provided by Heap’s algorithm, it’s guaranteed that this program will produce:

red, green, blue,
green, red, blue,
blue, red, green,
red, blue, green,
green, blue, red,
blue, green, red,

This crate also provides the ArbitraryTandemControlIter, which allows iterating over a slice using a slice of indices - that’s how Heap’s algorithm is implemented here.

Structs§

ArbitraryTandemControlIterator
An iterator over a slice that uses another iterator to control the next element in the sequence, in any arbitrary order, without copying.

Functions§

permutations_of
Produce an iterator over iterators, each one of which yields one permutation of the provided slice. No copying of elements of the slice occurs.
permute
Generate permutations of a given Vec by copying.