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

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

#1307 in Algorithms

Download history 255/week @ 2024-01-02 208/week @ 2024-01-09 321/week @ 2024-01-16 229/week @ 2024-01-23 271/week @ 2024-01-30 312/week @ 2024-02-06 398/week @ 2024-02-13 352/week @ 2024-02-20 221/week @ 2024-02-27 413/week @ 2024-03-05 392/week @ 2024-03-12 284/week @ 2024-03-19 218/week @ 2024-03-26 297/week @ 2024-04-02 522/week @ 2024-04-09 302/week @ 2024-04-16

1,379 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