prefixtree-0.1.3/Cargo.toml.orig010064400017500001750000000005351322066662000150060ustar0000000000000000[package] name = "prefixtree" version = "0.1.3" authors = ["Vee Satayamas "] description = "Hash-table-based prefix tree library" homepage = "https://github.com/veer66/prefixtree" repository = "https://github.com/veer66/prefixtree.git" readme = "README.md" keywords = ["text", "library"] license = "MIT" [lib] [dependencies] prefixtree-0.1.3/Cargo.toml0000644000000015510000000000000112510ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "prefixtree" version = "0.1.3" authors = ["Vee Satayamas "] description = "Hash-table-based prefix tree library" homepage = "https://github.com/veer66/prefixtree" readme = "README.md" keywords = ["text", "library"] license = "MIT" repository = "https://github.com/veer66/prefixtree.git" [lib] [dependencies] prefixtree-0.1.3/LICENSE010064400017500001750000000020561322066662000131240ustar0000000000000000MIT License Copyright (c) 2017 Vee Satayamas Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. prefixtree-0.1.3/README.md010066400017500001750000000001101317156140000133600ustar0000000000000000prefixtree ========== prefixtree is a simple prefix tree based HashMap.prefixtree-0.1.3/src/lib.rs010066400017500001750000000040151317441764600140330ustar0000000000000000use std::collections::HashMap; use std::hash::Hash; use std::cmp::Eq; #[derive(Debug, Clone)] pub struct PrefixTree where Elem: Hash + Eq + Clone { pub tab: HashMap<(u32,u32,Elem),(u32,bool,Option)> } impl PrefixTree { pub fn new(sorted_word_list_with_payload: &[(Vec, Payload)]) -> PrefixTree { let mut tab: HashMap<(u32,u32,Elem),(u32,bool,Option)> = HashMap::new(); let word_list_len = sorted_word_list_with_payload.len(); for i in 0..word_list_len { let (ref item, ref payload) = sorted_word_list_with_payload[i]; let item_len = item.len(); let mut row_no: u32 = 0; let mut j = 0; for elem in item.into_iter() { let elem = elem.clone(); let is_terminal = j + 1 == item_len; let child = tab.entry((row_no as u32,j as u32, elem)) .or_insert((i as u32, is_terminal, if is_terminal { Some(payload.clone()) } else { None })); let &mut (_row_no, _, _) = child; row_no = _row_no; j += 1; } } PrefixTree{tab: tab} } pub fn seek(&self, key: &(u32,u32,Elem)) -> Option<&(u32,bool,Option)> { self.tab.get(key) } } pub fn prefix_tree_from_str(sorted_word_list_with_payload: &[(&str, P)]) -> PrefixTree { let sorted_word_list_with_payload: Vec<(Vec,P)> = sorted_word_list_with_payload .into_iter() .map(|&(ref item, ref payload)| (item.chars().collect::>(), payload.clone())) .collect(); PrefixTree::new(&sorted_word_list_with_payload[..]) } prefixtree-0.1.3/tests/seek.rs010066400017500001750000000046571317205210500145620ustar0000000000000000extern crate prefixtree; #[cfg(test)] mod tests { use prefixtree::{PrefixTree, prefix_tree_from_str}; #[test] fn one_char_test() { let sorted_word_list: Vec<(&str, i32)> = vec![("A", 1)]; let prefix_tree = prefix_tree_from_str(&sorted_word_list[..]); assert!(prefix_tree.seek(&(0,0,'A')) == Some(&(0, true, Some(1)))); assert!(prefix_tree.seek(&(0,0,'B')).is_none()); } #[test] fn one_word_test() { let sorted_word_list = vec![("AB", 1)]; let prefix_tree = prefix_tree_from_str(&sorted_word_list[..]); assert!(prefix_tree.seek(&(0,0,'A')) == Some(&(0,false,None))); assert!(prefix_tree.seek(&(0,1,'B')) == Some(&(0,true,Some(1)))); } #[test] fn two_word_test() { let sorted_word_list = vec![("A",1), ("AB",2)]; let prefix_tree = prefix_tree_from_str(&sorted_word_list[..]); assert!(prefix_tree.seek(&(0,0,'A')) == Some(&(0,true,Some(1)))); assert!(prefix_tree.seek(&(0,1,'B')) == Some(&(1,true,Some(2)))); } #[test] fn three_word_test() { let sorted_word_list = vec![("A",1), ("AB",2), ("CXX",3)]; let prefix_tree = prefix_tree_from_str(&sorted_word_list[..]); assert!(prefix_tree.seek(&(0,0,'A')) == Some(&(0,true,Some(1)))); assert!(prefix_tree.seek(&(0,1,'B')) == Some(&(1,true,Some(2)))); assert!(prefix_tree.seek(&(0,0,'C')) == Some(&(2,false,None))); assert!(prefix_tree.seek(&(2,1,'X')) == Some(&(2,false,None))); assert!(prefix_tree.seek(&(2,2,'X')) == Some(&(2,true,Some(3)))); } #[test] fn vec_of_vec_test() { let sorted_list = vec![ (vec![1,2], 1), (vec![1,3], 2) ]; let prefix_tree = PrefixTree::new(&sorted_list); assert!(prefix_tree.seek(&(0,0,1)) == Some(&(0,false,None))); assert!(prefix_tree.seek(&(0,1,2)) == Some(&(0,true,Some(1)))); assert!(prefix_tree.seek(&(0,1,3)) == Some(&(1,true,Some(2)))); } #[test] fn vec_of_str_vec_test() { let sorted_list = vec![ (vec!["CAT", "DOG"], 1), (vec!["CAT", "ZOO"], 2) ]; let prefix_tree = PrefixTree::new(&sorted_list); assert!(prefix_tree.seek(&(0,0,"CAT")) == Some(&(0,false,None))); assert!(prefix_tree.seek(&(0,1,"DOG")) == Some(&(0,true,Some(1)))); assert!(prefix_tree.seek(&(0,1,"ZOO")) == Some(&(1,true,Some(2)))); } }