//! HashTrie Module //! //! This module implement the traits defined in the main module. //! Most importantly it defines a type of `TrieNode` that uses a //! HashMap store children, and is suitable when there the key component //! type is of an arbitrary variety. It also defines string-keyed tries //! by defining the TrieKey trait for strings. use crate::*; use std::collections::{HashMap,BTreeMap}; use std::hash::Hash; pub struct HashNode { item : Option, children : HashMap)>, } impl,VT> TrieNode for HashNode { fn new() -> Self { HashNode { item: None, children:HashMap::new(), } } fn children_count(&self) -> usize { self.children.len() } fn add_child(&mut self, key:KT, n:Self) -> bool { let first = key.ith(0); self.children.insert(first, (key,n)).is_none() } fn add_if_none(&mut self, k:KT) -> &mut Self { let first = k.ith(0); &mut self.children.entry(first).or_insert((k,Self::new())).1 } fn remove_child(&mut self, k:&KCT) -> Option<(KT,Self)> { self.children.remove(k) } fn get_child(&self, k:&KCT) -> Option<(&KT,&Self)> { self.children.get(k).map(|(k,v)|(k,v)) } fn get_mut_child(&mut self, k:&KCT) -> Option<(&KT,&mut Self)> { self.children.get_mut(k).map(|(k,v)|(k as &KT,v)) } fn set_item(&mut self, mut v:Option) -> Option { std::mem::swap(&mut v, &mut self.item); v } fn get_item(&self) -> Option<&VT> { self.item.as_ref() } fn get_mut_item(&mut self) -> Option<&mut VT> { self.item.as_mut() } fn get_item_mut(&mut self) -> &mut Option { &mut self.item } fn eiter<'t>(&'t self) -> impl Iterator where KT:'t, KCT:'t, VT:'t { self.children.iter().map(|(_k,(kk,vv))|(kk,vv)) } fn miter<'t>(&'t mut self) -> impl Iterator where KT:'t, KCT:'t, VT:'t { self.children.iter_mut().map(|(_k,(kk,vv))|(kk as &'t KT,vv)) } } pub struct HashTrie { root : HashNode, size : usize, } impl> RadixTrie for HashTrie { type NodeType = HashNode; fn new() -> Self { HashTrie { root: HashNode::new(), size : 0, } } fn len(&self) -> usize { self.size } fn change_len(&mut self, inc:bool) { if inc { self.size+=1; } else { self.size-=1; } } fn root_mut(&mut self) -> &mut Self::NodeType { &mut self.root } fn root(&self) -> &Self::NodeType { &self.root } } //////// for String Trie impl TrieKey for String { fn empty() -> Self { String::new() } fn ith(&self,i:usize) -> u8 { self.as_bytes()[i] } fn length(&self) -> usize { self.len() } fn add(&self, c:&u8) -> Self { format!("{}{}",self,*c as char) } fn subkey(&self, start:usize, end:usize) -> Self { String::from(&self[start..end]) } fn concat(&self, other:&Self) -> Self { format!("{}{}",self,other) } fn starts_with(&self, other:&Self) -> bool { self.as_str().starts_with(other.as_str()) } } pub type StringTrie = HashTrie; impl StringTrie { pub fn put(&mut self, key:&str, v:VT) -> Option { self.set(String::from(key),v) } pub fn rid(&mut self, key:&str) -> Option { self.remove(&String::from(key)) } pub fn lookup(&self, key:&str) -> Option<&VT> { self.get(&String::from(key)) } pub fn lookup_mut(&mut self, key:&str) -> Option<&mut VT> { self.get_mut(&String::from(key)) } pub fn stream<'t>(&'t mut self, key:&str) -> BTreeMap { self.collect(&String::from(key),0) } } ///////////////////// word trie impl<'t> TrieKey<&'t str> for Vec<&'t str> { fn empty() -> Self { Vec::new() } fn ith(&self,i:usize) -> &'t str { self[i] } fn length(&self) -> usize { self.len() } fn add(&self, c:&&'t str) -> Self { let mut v = self.clone(); v.push(*c); v } fn subkey(&self, start:usize, end:usize) -> Self { self[start..end].iter().map(|x|*x).collect() } fn concat(&self, other:&Self) -> Self { let mut v = self.clone(); let mut vo = other.clone(); v.append(&mut vo); v } fn starts_with(&self, other:&Self) -> bool { if self.len() = HashTrie<&'t str, Vec<&'t str>, VT>; fn printwords(v:&Vec<&str>) { for x in v {print!("{} ",x); } println!(); } pub fn wordtest() { let mut wtrie = WordTrie::new(); wtrie.set(vec!["hi","there"], 1); wtrie.set(vec!["hi","you"], 2); wtrie.set(vec!["hi","you","too"], 6); wtrie.set(vec!["hi","there","you"], 3); wtrie.set(vec!["hi","there","you","and","me"], 4); wtrie.set(vec!["hi","there","me"], 7); wtrie.set(vec!["hey!"],5); let map = wtrie.collect(&vec!["hi","there"],0); for(k,_v) in map.iter() { printwords(k); } }