Struct allwords::Alphabet [−][src]
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 ourAlphabet
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!
pub fn all_words(&self, max_len: Option<usize>) -> WordsIterator<'_>ⓘNotable traits for WordsIterator<'a>impl<'a> Iterator for WordsIterator<'a> type Item = String;
pub fn all_words(&self, max_len: Option<usize>) -> WordsIterator<'_>ⓘNotable traits for WordsIterator<'a>impl<'a> Iterator for WordsIterator<'a> type Item = String;
impl<'a> Iterator for WordsIterator<'a> type Item = String;
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 optionalusize
that, if present, will specify the maximum length of the generated string. IfNone
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"]);
pub fn all_words_unbound(&self) -> WordsIterator<'_>ⓘNotable traits for WordsIterator<'a>impl<'a> Iterator for WordsIterator<'a> type Item = String;
pub fn all_words_unbound(&self) -> WordsIterator<'_>ⓘNotable traits for WordsIterator<'a>impl<'a> Iterator for WordsIterator<'a> type Item = String;
impl<'a> Iterator for WordsIterator<'a> type Item = String;
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)`
pub fn all_words_starting_from(
&self,
start_word: String,
max_len: Option<usize>
) -> WordsIterator<'_>ⓘNotable traits for WordsIterator<'a>impl<'a> Iterator for WordsIterator<'a> type Item = String;
pub fn all_words_starting_from(
&self,
start_word: String,
max_len: Option<usize>
) -> WordsIterator<'_>ⓘNotable traits for WordsIterator<'a>impl<'a> Iterator for WordsIterator<'a> type Item = String;
impl<'a> Iterator for WordsIterator<'a> type Item = String;
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
- aString
instance representing the starting string. This string will be returned by the first.next()
call to the iterator).max_len
- an optionalusize
that, if present, will specify the maximum length of the generated string. IfNone
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"]);
pub fn all_words_with_len(
&self,
start_len: usize,
max_len: Option<usize>
) -> WordsIterator<'_>ⓘNotable traits for WordsIterator<'a>impl<'a> Iterator for WordsIterator<'a> type Item = String;
pub fn all_words_with_len(
&self,
start_len: usize,
max_len: Option<usize>
) -> WordsIterator<'_>ⓘNotable traits for WordsIterator<'a>impl<'a> Iterator for WordsIterator<'a> type Item = String;
impl<'a> Iterator for WordsIterator<'a> type Item = String;
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
- ausize
defining the minimum length of the first word emitted by the iterator.max_len
- an optionalusize
that, if present, will specify the maximum length of the generated string. IfNone
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"]);