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>

source

pub fn new() -> Self

Returns a new instance of the trie data structure.

source

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());
source

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());
source

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());
source

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);
source

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);
source

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);
source

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());
source

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);
source

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);
source

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));

Trait Implementations§

source§

impl<'a, D: Debug> Debug for Trie<'a, D>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, D> Default for Trie<'a, D>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a, D> RefUnwindSafe for Trie<'a, D>where D: RefUnwindSafe,

§

impl<'a, D> Send for Trie<'a, D>where D: Send,

§

impl<'a, D> Sync for Trie<'a, D>where D: Sync,

§

impl<'a, D> Unpin for Trie<'a, D>where D: Unpin,

§

impl<'a, D> UnwindSafe for Trie<'a, D>where D: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.