#permutations #permutation #combinatorics #heaps #rgb #algorithm

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

#779 in Algorithms

Download history 480/week @ 2024-06-17 321/week @ 2024-06-24 204/week @ 2024-07-01 201/week @ 2024-07-08 345/week @ 2024-07-15 235/week @ 2024-07-22 239/week @ 2024-07-29 178/week @ 2024-08-05 181/week @ 2024-08-12 130/week @ 2024-08-19 256/week @ 2024-08-26 263/week @ 2024-09-02 119/week @ 2024-09-09 246/week @ 2024-09-16 227/week @ 2024-09-23 264/week @ 2024-09-30

864 downloads per month
Used in defamed

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