#combinatorics #permutations #algorithm #permutation #heaps

permute

Generate permutations of vectors and slices in a memory-efficient and deterministic manner, using Heap's algorithm

2 unstable releases

0.2.1 Jun 5, 2022
0.1.0 Aug 7, 2019

#901 in Algorithms

Download history 483/week @ 2023-01-19 382/week @ 2023-01-26 263/week @ 2023-02-02 182/week @ 2023-02-09 621/week @ 2023-02-16 337/week @ 2023-02-23 279/week @ 2023-03-02 335/week @ 2023-03-09 227/week @ 2023-03-16 256/week @ 2023-03-23 371/week @ 2023-03-30 291/week @ 2023-04-06 325/week @ 2023-04-13 285/week @ 2023-04-20 306/week @ 2023-04-27 275/week @ 2023-05-04

1,260 downloads per month

AGPL-3.0-or-later

11KB
130 lines

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.

No runtime deps