#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

#903 in Algorithms

Download history 136/week @ 2024-11-16 157/week @ 2024-11-23 376/week @ 2024-11-30 265/week @ 2024-12-07 311/week @ 2024-12-14 123/week @ 2024-12-21 170/week @ 2024-12-28 251/week @ 2025-01-04 256/week @ 2025-01-11 275/week @ 2025-01-18 236/week @ 2025-01-25 445/week @ 2025-02-01 165/week @ 2025-02-08 299/week @ 2025-02-15 275/week @ 2025-02-22 221/week @ 2025-03-01

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