#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

#1292 in Algorithms

Download history 361/week @ 2023-12-17 106/week @ 2023-12-24 212/week @ 2023-12-31 205/week @ 2024-01-07 279/week @ 2024-01-14 248/week @ 2024-01-21 331/week @ 2024-01-28 251/week @ 2024-02-04 448/week @ 2024-02-11 345/week @ 2024-02-18 190/week @ 2024-02-25 396/week @ 2024-03-03 401/week @ 2024-03-10 295/week @ 2024-03-17 239/week @ 2024-03-24 265/week @ 2024-03-31

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