Struct allwords::Alphabet[][src]

pub struct Alphabet {
    pub next_char_map: HashMap<char, Option<char>>,
    pub first_char: char,
}
Expand description

A representation of an alphabet

Fields

next_char_map: HashMap<char, Option<char>>

An hashmap used to track what’s the next character for every given character. The last caracter will point to None.

first_char: char

The first character in the alphabet

Implementations

Creates a new alphabet starting from the unique characters found in a given string.

This function will extract all the unique characters found in order in the given string. It will return an Err if there are less than 2 unique characters in the given string.

Arguments
  • alphabet_str - A string-like instance that contains the sequence of characters that we want to use to initialize our Alphabet instance
Returns

It returns a Result containing the new Alphabet instance in case of success.

Examples

Creates an alphabet using characters from 'a' to 'f':

use allwords::{Alphabet};

let alphabet = Alphabet::from_chars_in_str("abcdef").unwrap();

Passing an empty string or a string with less than 2 unique chars will return an error:

use allwords::{Alphabet};

let a = Alphabet::from_chars_in_str("zzzzzzzzzzzzzz");

match a {
    Ok(_) => panic!("An alphabet was created when we expected an error"),
    Err(e) => assert_eq!(
        e,
        String::from("Invalid alphabet string. Found less than 2 unique chars")
    ),
};

Since Alphabet implements str::FromStr, you can also do the following:

use allwords::{Alphabet};

let value = "abcdef";
let alphabet = value.parse::<Alphabet>().unwrap(); // long life to the turbofish!

Creates an iterator that will generate all the words for a given alphabet. You can optionally specifify a maximum length, after which, the iterator will terminate.

Arguments
  • max_len - an optional usize that, if present, will specify the maximum length of the generated string. If None the iterator will be endless.
Returns

An instance of a WordsIterator.

Examples

Creates an iterator with max_len = 2 for a given alphabet:

use allwords::{Alphabet};

let alphabet = Alphabet::from_chars_in_str("01").unwrap();
let iterator = alphabet.all_words(Some(2));
let words: Vec<String> = iterator.collect();
assert_eq!(words, vec!["0", "1", "00", "01", "10", "11"]);

A shortcut for creating an unbound (endless) iterator for the given alphabet.

Examples
use allwords::{Alphabet};

let alphabet = Alphabet::from_chars_in_str("01").unwrap();
let iterator = alphabet.all_words_unbound(); // equivalent to `alphabet.all_words(None)`

Creates an iterator that will generate all the words for a given alphabet starting from a given word. This method can be useful in case you want to restart a partially completed iteration from another execution or if you want to distribute computation across indepentend processes or threads.

Note: this method does not check that the starting word complies with the alphabet. If there are characters in the string that are NOT present in the alphabet, the iterator will consider these characters as last character and restart the sequence from the first character in the alphabet.

Arguments
  • start_word - a String instance representing the starting string. This string will be returned by the first .next() call to the iterator).
  • max_len - an optional usize that, if present, will specify the maximum length of the generated string. If None the iterator will be endless.
Returns

An instance of a WordsIterator.

Examples

Creates an iterator with max_len = 2 starting from “01” for a given alphabet:

use allwords::{Alphabet};

let alphabet = Alphabet::from_chars_in_str("01").unwrap();
let iterator = alphabet.all_words_starting_from(String::from("01"), Some(2));
let words: Vec<String> = iterator.collect();
assert_eq!(words, vec!["01", "10", "11"]);

Creates an iterator that will generate all the words for a given alphabet starting from the first word with a given minimum length.

Arguments
  • start_len - a usize defining the minimum length of the first word emitted by the iterator.
  • max_len - an optional usize that, if present, will specify the maximum length of the generated string. If None the iterator will be endless.
Returns

An instance of a WordsIterator.

Examples

Creates an iterator for all the words with length bethween 2 and 3 chars:

use allwords::{Alphabet};

let alphabet = Alphabet::from_chars_in_str("01").unwrap();
let iterator = alphabet.all_words_with_len(2, Some(3));
let words: Vec<String> = iterator.collect();
assert_eq!(words, vec!["00", "01", "10", "11", "000", "001", "010", "011", "100", "101", "110", "111"]);

Trait Implementations

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.