rotary-permutator

Crates.iorotary-permutator
lib.rsrotary-permutator
version0.1.11
created_at2025-03-17 23:32:39.256998+00
updated_at2025-03-20 09:12:53.300171+00
descriptionPermutations iterator
homepagehttps://github.com/ishrut/rotary-permutator
repositoryhttps://github.com/ishrut/rotary-permutator
max_upload_size
id1595989
size9,735
ruthis (ishrut)

documentation

README

rotary-permutator - A library to generate permutations.

The algorithm is inspired from mechanical rotary permutation generators like the cryptex. It generates permutations of a given length with repetition from a list of chars or variants of an enum. It implements the Iterator trait for usability.

CharRotor - It is a struct that is initialised and then generates the permutations and returns them as Vec<char>. EnumRotor - The macro expands to implementation functions of the enum and generates a RotorEngine<T> struct which implements the iterator.

For EnumRotor the dafault trait has to be implemented. It is the starting state of the machine.

Examples

To generate permutations from a list of chars and a given size of permutations:

use rotary_permutator::CharRotor;

fn main() {
    let mut char_rotor = CharRotor::init(vec!['x', 'y', 'z'], 3);
    while let Some(permutations) = char_rotor.next() {
        println!("{:?}", permutations);
    }
}

output:

['x', 'x', 'x']
['x', 'x', 'y']
['x', 'x', 'z']
['x', 'y', 'x']
['x', 'y', 'y']
['x', 'y', 'z']
['x', 'z', 'x']
['x', 'z', 'y']
['x', 'z', 'z']
['y', 'x', 'x']
['y', 'x', 'y']
['y', 'x', 'z']
['y', 'y', 'x']
['y', 'y', 'y']
['y', 'y', 'z']
['y', 'z', 'x']
['y', 'z', 'y']
['y', 'z', 'z']
['z', 'x', 'x']
['z', 'x', 'y']
['z', 'x', 'z']
['z', 'y', 'x']
['z', 'y', 'y']
['z', 'y', 'z']
['z', 'z', 'x']
['z', 'z', 'y']
['z', 'z', 'z']

Permutations from variants of an enum.

use rotary_permutator::EnumRotor;

#[derive(EnumRotor, Debug, Clone, Default)]
pub enum Levels {
    #[default]
    High,
    Normal,
    Low,
}

fn main() {
    let mut rotor_engine = Levels::init_rotor_engine(3);
    let mut count = 0;
    while let Some(perm) = rotor_engine.next() {
        println!("{:?}", perm);
        count += 1;
    }
    println!("total: {}", count);
}

output:

[High, High, High]
[High, High, Normal]
[High, High, Low]
[High, Normal, High]
[High, Normal, Normal]
[High, Normal, Low]
[High, Low, High]
[High, Low, Normal]
[High, Low, Low]
[Normal, High, High]
[Normal, High, Normal]
[Normal, High, Low]
[Normal, Normal, High]
[Normal, Normal, Normal]
[Normal, Normal, Low]
[Normal, Low, High]
[Normal, Low, Normal]
[Normal, Low, Low]
[Low, High, High]
[Low, High, Normal]
[Low, High, Low]
[Low, Normal, High]
[Low, Normal, Normal]
[Low, Normal, Low]
[Low, Low, High]
[Low, Low, Normal]
[Low, Low, Low]
total: 27
Commit count: 7

cargo fmt