Struct basic_trie::Trie
source · pub struct Trie<'a, D> { /* private fields */ }Expand description
Trie data structure. Each word has a list of data associated to it. The associated data can be of any type.
Implementations§
source§impl<'a, D> Trie<'a, D>
impl<'a, D> Trie<'a, D>
sourcepub fn insert(&mut self, word: &'a str, associated_data: D)
pub fn insert(&mut self, word: &'a str, associated_data: D)
Insert a word into the trie, with the corresponding data.
use basic_trie::Trie;
let mut trie = Trie::new();
trie.insert("word1", "somedata");
assert_eq!(vec![String::from("word1")], trie.all_words().unwrap());sourcepub fn remove_word(&mut self, word: &str)
pub fn remove_word(&mut self, word: &str)
Removes a word from the trie. If the word is a prefix to some word, some word isn’t removed from the trie. The associated data is also removed.
use basic_trie::Trie;
let mut trie = Trie::new();
trie.insert("word", "somedata");
trie.insert("wording", "somedata2");
trie.remove_word("word");
assert_eq!(vec![String::from("wording")], trie.find_words("word").unwrap());
assert_eq!(vec![&"somedata2"], trie.find_data_of_word("word", true).unwrap());
trie.remove_word("wording");
assert_eq!(None, trie.all_words());sourcepub fn remove_words_from_prefix(&mut self, prefix: &str)
pub fn remove_words_from_prefix(&mut self, prefix: &str)
Removes every word that begins with ‘prefix’.
use basic_trie::Trie;
let mut trie = Trie::new();
trie.insert("eat", "somedata");
trie.insert("eats", "somedata2");
trie.insert("eating", "somedata3");
trie.insert("eatings", "somedata4");
trie.insert("ea", "somedata5");
trie.remove_words_from_prefix("ea");
assert_eq!(vec![String::from("ea")], trie.all_words().unwrap());sourcepub fn find_words(&self, query: &str) -> Option<Vec<String>>
pub fn find_words(&self, query: &str) -> Option<Vec<String>>
Returns an option enum with a vector of owned strings representing all found words that begin with “query”. If no words are found, None is returned.
use basic_trie::Trie;
let mut trie = Trie::new();
trie.insert("word1", "somedata");
trie.insert("word2", "somemoredata");
let all_correct_words = vec![String::from("word1"), String::from("word2")];
let mut found_words = trie.find_words("word").unwrap();
found_words.sort();
assert_eq!(all_correct_words, found_words);sourcepub fn longest_words(&self) -> Vec<String>
pub fn longest_words(&self) -> Vec<String>
Returns the vector of longest words found in the trie.
use basic_trie::Trie;
let mut trie = Trie::new();
trie.insert("shortwrd", "somedata");
trie.insert("verylongword", "somemoredata");
trie.insert("somelongword", "somemoredata");
let longest_words = vec![String::from("somelongword"), String::from("verylongword")];
let mut found_words = trie.longest_words();
found_words.sort();
assert_eq!(longest_words, found_words);sourcepub fn shortest_words(&self) -> Vec<String>
pub fn shortest_words(&self) -> Vec<String>
Returns the vector of shortest words found in the trie.
use basic_trie::Trie;
let mut trie = Trie::new();
trie.insert("shortwrd", "somedata");
trie.insert("rlyshort", "somedata");
trie.insert("verylongword", "somemoredata");
let shortest_word = vec![String::from("rlyshort"), String::from("shortwrd")];
let mut found_words = trie.shortest_words();
found_words.sort();
assert_eq!(shortest_word, found_words);sourcepub fn number_of_words(&self) -> usize
pub fn number_of_words(&self) -> usize
Returns the number of words in the trie.
use basic_trie::Trie;
let mut trie = Trie::new();
trie.insert("word1", "");
trie.insert("word2", "");
trie.insert("word3", "");
trie.insert("word4", "");
let number_of_words = 4;
assert_eq!(number_of_words, trie.number_of_words());sourcepub fn all_words(&self) -> Option<Vec<String>>
pub fn all_words(&self) -> Option<Vec<String>>
Returns an option enum with a vector of owned strings representing all words in the trie. Order is not guaranteed.
use basic_trie::Trie;
let mut trie = Trie::new();
trie.insert("word1", "1");
trie.insert("word2", "2");
trie.insert("word3", "3");
trie.insert("word4", "4");
trie.insert("word5", "5");
let all_words = vec![
String::from("word1"), String::from("word2"), String::from("word3"),
String::from("word4"), String::from("word5")
];
let mut found_words = trie.all_words().unwrap();
found_words.sort();
assert_eq!(all_words, found_words);sourcepub fn find_data_of_word(
&self,
query: &str,
soft_match: bool
) -> Option<Vec<&D>>
pub fn find_data_of_word( &self, query: &str, soft_match: bool ) -> Option<Vec<&D>>
Returns all data associated to “query” wrapped in the Option enum, in case there is no data for the specified arguments. Parameter soft_match dictates if the returned data should be associated only with “query” or with all words that begin with “query”.
use basic_trie::Trie;
let mut trie = Trie::new();
trie.insert("word1", "somedata");
trie.insert("word2", "somemoredata");
let hard_data = vec![&"somedata"];
assert_eq!(hard_data, trie.find_data_of_word("word1", false).unwrap());
let soft_data = vec![&"somedata", &"somemoredata"];
let mut found_data = trie.find_data_of_word("word", true).unwrap();
found_data.sort();
assert_eq!(soft_data, found_data);sourcepub fn clear_word_data(&mut self, word: &'a str)
pub fn clear_word_data(&mut self, word: &'a str)
Clears data of some word. If the word is not found, or there is no data associated with the word, nothing happens.
use basic_trie::Trie;
let mut trie = Trie::new();
trie.insert("word", "data1");
trie.insert("word", "data2");
trie.insert("word", "data3");
trie.clear_word_data("word");
assert_eq!(None, trie.find_data_of_word("word", false));