use crate::node::Node;
use std::collections::{HashSet};
use std::default::Default;
use std::fmt;
#[derive(Default, Debug)]
pub struct SuffixTrie {
pub dictionary: Option<Vec<String>>,
main_node: Option<Node>,
}
impl SuffixTrie {
pub fn new() -> Self {
Self::default()
}
pub fn add_words<T: AsRef<str>>(&mut self, words: Vec<T>) {
for word in words {
self.add_word(word.as_ref().to_string());
}
}
pub fn add_word(&mut self, word: String) {
self.dictionary
.get_or_insert_with(Vec::new)
.push(word.clone());
self.main_node
.get_or_insert_with(Node::new)
.add_suffix(word);
}
pub fn find_prefixes(&self, prefix: &str) -> Option<HashSet<String>> {
self.main_node
.as_ref()?
.find_by_prefix(prefix)
.map(|suffixes| suffixes.into_iter().collect())
}
}
impl fmt::Display for SuffixTrie {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(dict) = &self.dictionary {
write!(f, "{words}", words = dict.join("\n"))
} else {
write!(f, "dictionary is empty")
}
}
}