| 1 | extern crate yada; |
| 2 | |
| 3 | use yada::builder::DoubleArrayBuilder; |
| 4 | use yada::DoubleArray; |
| 5 | |
| 6 | fn main() { |
| 7 | // make a keyset which have key-value pairs |
| 8 | let keyset = &[ |
| 9 | ("a".as_bytes(), 0), |
| 10 | ("ab".as_bytes(), 1), |
| 11 | ("abc".as_bytes(), 2), |
| 12 | ("b".as_bytes(), 3), |
| 13 | ("bc".as_bytes(), 4), |
| 14 | ("c".as_bytes(), 5), |
| 15 | ]; |
| 16 | |
| 17 | // build a double-array trie binary |
| 18 | let da_bytes = DoubleArrayBuilder::build(keyset); |
| 19 | assert!(da_bytes.is_some()); |
| 20 | |
| 21 | // create a double-array trie instance |
| 22 | let da = DoubleArray::new(da_bytes.unwrap()); |
| 23 | |
| 24 | // exact match search |
| 25 | for (key, value) in keyset { |
| 26 | assert_eq!(da.exact_match_search(key), Some(*value as u32)); |
| 27 | } |
| 28 | assert_eq!(da.exact_match_search("aa".as_bytes()), None); |
| 29 | assert_eq!(da.exact_match_search("aba".as_bytes()), None); |
| 30 | assert_eq!(da.exact_match_search("abb".as_bytes()), None); |
| 31 | assert_eq!(da.exact_match_search("abcd".as_bytes()), None); |
| 32 | assert_eq!(da.exact_match_search("ba".as_bytes()), None); |
| 33 | assert_eq!(da.exact_match_search("bb".as_bytes()), None); |
| 34 | assert_eq!(da.exact_match_search("bcd".as_bytes()), None); |
| 35 | assert_eq!(da.exact_match_search("ca".as_bytes()), None); |
| 36 | |
| 37 | // common prefix search |
| 38 | assert_eq!( |
| 39 | da.common_prefix_search("a".as_bytes()).collect::<Vec<_>>(), |
| 40 | vec![(0, 1)] // match "a" |
| 41 | ); |
| 42 | assert_eq!( |
| 43 | da.common_prefix_search("abc".as_bytes()) |
| 44 | .collect::<Vec<_>>(), |
| 45 | vec![(0, 1), (1, 2), (2, 3)] // match "a", "ab", "abc" |
| 46 | ); |
| 47 | assert_eq!( |
| 48 | da.common_prefix_search("abcd".as_bytes()) |
| 49 | .collect::<Vec<_>>(), |
| 50 | vec![(0, 1), (1, 2), (2, 3)] // match "a", "ab", "abc" |
| 51 | ); |
| 52 | assert_eq!( |
| 53 | da.common_prefix_search("bcd".as_bytes()) |
| 54 | .collect::<Vec<_>>(), |
| 55 | vec![(3, 1), (4, 2)] // match "b", "bc" |
| 56 | ); |
| 57 | assert_eq!( |
| 58 | da.common_prefix_search("d".as_bytes()).collect::<Vec<_>>(), |
| 59 | vec![] // don't match |
| 60 | ); |
| 61 | } |