Crate permutate [] [src]

Permutate

Permutate exists as both a library and application for permutating generic lists of lists as well as individual lists using an original Rust-based algorithm which works with references. If the data you are working with is not best-handled with references, this isn't for you. It has been developed primarily for the goal of inclusion within the Rust implementation of the GNU Parallel program, which provides the ability to permutate a list of input lists.

Examples

These are a list of examples on how to use the library to manipulate string-based data. The only thing we need to ensure is that our list of strings is in the &[&[&str]] format.

An individual list

extern crate permutate;
use permutate::Permutator;
use std::io::{self, Write};

fn main() {
    let stdout = io::stdout();
    let mut stdout = stdout.lock();
    let list: &[&str] = &["one", "two", "three", "four"];
    let list = [list];
    let permutator = Permutator::new(&list[..]);

    // NOTE: print! macros are incredibly slow, so printing directly
    // to stdout is faster.
    for permutation in permutator {
        for element in permutation {
            let _ = stdout.write(element.as_bytes());
        }
        let _ = stdout.write(b"\n");
    }
}

An array of arrays: &[&[&str]]

extern crate permutate;
use permutate::Permutator;
use std::io::{self, Write};

fn main() {
    let stdout = io::stdout();
    let mut stdout = stdout.lock();
    let lists = [
        &["one", "two", "three"][..],
        &["four", "five", "six"][..],
        &["seven", "eight", "nine"][..],
    ];
    let permutator = Permutator::new(&lists[..]);

    // NOTE: print! macros are incredibly slow, so printing directly
    // to stdout is faster.
    for permutation in permutator {
        for element in permutation {
            let _ = stdout.write(element.as_bytes());
        }
        let _ = stdout.write(b"\n");
    }
}

A Vector of Vector of Strings: Vec<Vec<String>>

This is the most complicated example to accomplish because you have to convert, essentially, A vector of a vector of vectors into a slice of a slice of a slice, as the String type itself is a vector of characters.

extern crate permutate;
use permutate::Permutator;
use std::io::{self, Write};

fn main() {
    let stdout = io::stdout();
    let mut stdout = stdout.lock();
    let lists: Vec<Vec<String>> = vec![
        vec!["one".to_owned(), "two".to_owned(), "three".to_owned()],
        vec!["four".to_owned(), "five".to_owned(), "six".to_owned()],
        vec!["seven".to_owned(), "eight".to_owned(), "nine".to_owned()],
    ];

    // Convert the `Vec<Vec<String>>` into a `Vec<Vec<&str>>`
    let tmp: Vec<Vec<&str>> = lists.iter()
        .map(|list| list.iter().map(AsRef::as_ref).collect::<Vec<&str>>())
        .collect();

    // Convert the `Vec<Vec<&str>>` into a `Vec<&[&str]>`
    let vector_of_arrays: Vec<&[&str]> = tmp.iter()
        .map(AsRef::as_ref).collect();

    // Pass the `Vec<&[&str]>` as an `&[&[&str]]`
    let permutator = Permutator::new(&vector_of_arrays[..]);

    // NOTE: print! macros are incredibly slow, so printing directly
    // to stdout is faster.
    for permutation in permutator {
        for element in permutation {
            let _ = stdout.write(element.as_bytes());
        }
        let _ = stdout.write(b"\n");
    }
}

Structs

Permutator

The Permutator contains the state of the iterator as well as the references to inputs that are being permutated. The input should be provided as an array of an array of references.