#permutation #permutations #permute

but-what-about

A permutation & combination calculation crate for rust

1 unstable release

0.1.0 Jun 27, 2021

#2112 in Algorithms

MIT license

14KB
258 lines

but-what-about

The crate is a permutation utility that only supports Heap's algorithm. At the moment it can permute strings(by grapheme and codepoint—though, codepoint based permutation is highly questionable at the moment) and primitive numeric types by their bits. In the future I plan to add support for additional permutation algorithms and implement some combination algorithms as well. I can't say I recomment using the crate for anything more than just fun at the moment. So, do go and have fun.

use heap_permute::PermuteIter;
// Print all of the permutations of a string of three characters
fn main() {
    const STRING: &'static str = "ABC";
    for p in PermuteIter::from(STRING) {
        print!("{}, ", p);
    }
}
// Prints: 
// ABC, BAC, CAB, ACB, BCA, CBA,

lib.rs:

The crate is basically just a permutation utility. With the goal of speed and the smallest possible runtime memory impact possible. And so, the goal of this crate is to be able to calculate and obtain a permutation of a value as soon as possible.

Examples

Get all the permutations of a string of three characters:

use heap_permute::PermuteIter;

// Print all of the permutations of a string of three characters
fn main() {
    const STRING: &'static str = "ABC";

    for p in PermuteIter::from(STRING) {
        println!("{}", p);
    }
}
// Prints: 
// ABC
// BAC
// CAB
// ACB
// BCA
// CBA

Now time for something more interesting, let's permute the bits in a u8:

use heap_permute::PermuteIter;

fn main() {
    for p in PermuteIter::from(0b1010_1001 as u8) {
        println!("0b{:08b} ({})", p, p);
    }
}
// Prints:
// 0b10101001 (169)
// 0b10101010 (170)
// 0b10101010 (170)
// 0b10101001 (169)
// 0b10101100 (172)
// 0b10101100 (172)
// 0b10100101 (165)
// 0b10100110 (166)
// 0b10100011 (163)
// 0b10100011 (163)
// ...

As it happens, the crate supports permutation for all of the primitive numeric rust types.

Dependencies

~140KB