| 1 | pub mod builder; |
| 2 | pub mod unit; |
| 3 | |
| 4 | use crate::unit::{Unit, UnitID, UNIT_SIZE}; |
| 5 | use std::convert::TryInto; |
| 6 | use std::ops::Deref; |
| 7 | |
| 8 | /// A double array trie. |
| 9 | #[derive(Clone)] |
| 10 | pub struct DoubleArray<T>(pub T) |
| 11 | where |
| 12 | T: Deref<Target = [u8]>; |
| 13 | |
| 14 | impl<T> DoubleArray<T> |
| 15 | where |
| 16 | T: Deref<Target = [u8]>, |
| 17 | { |
| 18 | /// Creates a new `DoubleArray` with a byte slice. |
| 19 | pub fn new(bytes: T) -> Self { |
| 20 | Self { 0: bytes } |
| 21 | } |
| 22 | |
| 23 | /// Finds a value associated with a `key`. |
| 24 | pub fn exact_match_search<K>(&self, key: K) -> Option<u32> |
| 25 | where |
| 26 | K: AsRef<[u8]>, |
| 27 | { |
| 28 | self.exact_match_search_bytes(key.as_ref()) |
| 29 | } |
| 30 | |
| 31 | fn exact_match_search_bytes(&self, key: &[u8]) -> Option<u32> { |
| 32 | // traverse from root node |
| 33 | let mut node_pos = 0 as UnitID; |
| 34 | let mut unit = self.get_unit(node_pos)?; |
| 35 | |
| 36 | for &c in key.iter().take(key.len()) { |
| 37 | assert!(!unit.is_leaf()); |
| 38 | assert_ne!(c, 0); // assumes characters don't have NULL ('\0') |
| 39 | |
| 40 | // try to traverse node |
| 41 | node_pos = (unit.offset() ^ node_pos as u32 ^ c as u32) as UnitID; |
| 42 | unit = self.get_unit(node_pos)?; |
| 43 | |
| 44 | if unit.label() != c as u32 { |
| 45 | return None; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if !unit.has_leaf() { |
| 50 | return None; |
| 51 | } |
| 52 | |
| 53 | // traverse node by NULL ('\0') |
| 54 | let node_pos = (unit.offset() ^ node_pos as u32 ^ 0u32) as UnitID; |
| 55 | unit = self.get_unit(node_pos)?; |
| 56 | assert!(unit.is_leaf()); |
| 57 | assert!(unit.value() < (1 << 31)); |
| 58 | |
| 59 | Some(unit.value()) |
| 60 | } |
| 61 | |
| 62 | /// Finds all values and it's key length which have a common prefix with a `key`. |
| 63 | pub fn common_prefix_search<'b, K>( |
| 64 | &'b self, |
| 65 | key: &'b K, |
| 66 | ) -> impl Iterator<Item = (u32, usize)> + 'b |
| 67 | where |
| 68 | K: AsRef<[u8]>, |
| 69 | K: ?Sized, |
| 70 | { |
| 71 | self.common_prefix_search_bytes(key.as_ref()) |
| 72 | } |
| 73 | |
| 74 | fn common_prefix_search_bytes<'b>( |
| 75 | &'b self, |
| 76 | key: &'b [u8], |
| 77 | ) -> impl Iterator<Item = (u32, usize)> + 'b { |
| 78 | CommonPrefixSearch { |
| 79 | key, |
| 80 | double_array: self, |
| 81 | unit_id: 0, |
| 82 | key_pos: 0, |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | #[inline(always)] |
| 87 | fn get_unit(&self, index: usize) -> Option<Unit> { |
| 88 | let b = unsafe { |
| 89 | // This unsafe method call does not lead unexpected transitions |
| 90 | // when a double array was built properly. |
| 91 | self.0 |
| 92 | .get_unchecked(index * UNIT_SIZE..(index + 1) * UNIT_SIZE) |
| 93 | }; |
| 94 | match b.try_into() { |
| 95 | Ok(bytes) => Some(Unit::from_u32(u32::from_le_bytes(bytes))), |
| 96 | Err(_) => None, |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /// An iterator that finds all values with a common prefix. |
| 102 | pub struct CommonPrefixSearch<'k, 'd, T> |
| 103 | where |
| 104 | T: Deref<Target = [u8]>, |
| 105 | { |
| 106 | key: &'k [u8], |
| 107 | double_array: &'d DoubleArray<T>, |
| 108 | unit_id: UnitID, |
| 109 | key_pos: usize, |
| 110 | } |
| 111 | |
| 112 | impl<T> Iterator for CommonPrefixSearch<'_, '_, T> |
| 113 | where |
| 114 | T: Deref<Target = [u8]>, |
| 115 | { |
| 116 | type Item = (u32, usize); |
| 117 | |
| 118 | #[inline(always)] |
| 119 | fn next(&mut self) -> Option<Self::Item> { |
| 120 | while self.key_pos < self.key.len() { |
| 121 | let unit = self.double_array.get_unit(self.unit_id)?; |
| 122 | |
| 123 | let c = *self.key.get(self.key_pos)?; |
| 124 | self.key_pos += 1; |
| 125 | |
| 126 | self.unit_id = (unit.offset() ^ self.unit_id as u32 ^ c as u32) as UnitID; |
| 127 | let unit = self.double_array.get_unit(self.unit_id)?; |
| 128 | if unit.label() != c as u32 { |
| 129 | return None; |
| 130 | } |
| 131 | if unit.has_leaf() { |
| 132 | let leaf_pos = unit.offset() ^ self.unit_id as u32; |
| 133 | let leaf_unit = self.double_array.get_unit(leaf_pos as UnitID)?; |
| 134 | return Some((leaf_unit.value(), self.key_pos)); |
| 135 | } |
| 136 | } |
| 137 | None |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | #[cfg(test)] |
| 142 | mod tests { |
| 143 | use crate::builder::DoubleArrayBuilder; |
| 144 | use crate::DoubleArray; |
| 145 | |
| 146 | #[test] |
| 147 | fn test_build_search() { |
| 148 | let keyset = &[ |
| 149 | ("a".as_bytes(), 0), |
| 150 | ("ab".as_bytes(), 1), |
| 151 | ("aba".as_bytes(), 2), |
| 152 | ("ac".as_bytes(), 3), |
| 153 | ("acb".as_bytes(), 4), |
| 154 | ("acc".as_bytes(), 5), |
| 155 | ("ad".as_bytes(), 6), |
| 156 | ("ba".as_bytes(), 7), |
| 157 | ("bb".as_bytes(), 8), |
| 158 | ("bc".as_bytes(), 9), |
| 159 | ("c".as_bytes(), 10), |
| 160 | ("caa".as_bytes(), 11), |
| 161 | ]; |
| 162 | |
| 163 | let da_bytes = DoubleArrayBuilder::build(keyset); |
| 164 | assert!(da_bytes.is_some()); |
| 165 | |
| 166 | let da = DoubleArray::new(da_bytes.unwrap()); |
| 167 | |
| 168 | for (key, value) in keyset { |
| 169 | assert_eq!(da.exact_match_search(key), Some(*value as u32)); |
| 170 | } |
| 171 | assert_eq!(da.exact_match_search("aa".as_bytes()), None); |
| 172 | assert_eq!(da.exact_match_search("abc".as_bytes()), None); |
| 173 | assert_eq!(da.exact_match_search("b".as_bytes()), None); |
| 174 | assert_eq!(da.exact_match_search("ca".as_bytes()), None); |
| 175 | |
| 176 | assert_eq!( |
| 177 | da.common_prefix_search("a".as_bytes()).collect::<Vec<_>>(), |
| 178 | vec![(0, 1)] |
| 179 | ); |
| 180 | assert_eq!( |
| 181 | da.common_prefix_search("aa".as_bytes()).collect::<Vec<_>>(), |
| 182 | vec![(0, 1)] |
| 183 | ); |
| 184 | assert_eq!( |
| 185 | da.common_prefix_search("abbb".as_bytes()) |
| 186 | .collect::<Vec<_>>(), |
| 187 | vec![(0, 1), (1, 2)] |
| 188 | ); |
| 189 | assert_eq!( |
| 190 | da.common_prefix_search("abaa".as_bytes()) |
| 191 | .collect::<Vec<_>>(), |
| 192 | vec![(0, 1), (1, 2), (2, 3)] |
| 193 | ); |
| 194 | assert_eq!( |
| 195 | da.common_prefix_search("caa".as_bytes()) |
| 196 | .collect::<Vec<_>>(), |
| 197 | vec![(10, 1), (11, 3)] |
| 198 | ); |
| 199 | assert_eq!( |
| 200 | da.common_prefix_search("d".as_bytes()).collect::<Vec<_>>(), |
| 201 | vec![] |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | #[test] |
| 206 | fn test_exact_match_search_corner_case() { |
| 207 | // A corner case of `exact_match_search()`. |
| 208 | // See https://github.com/takuyaa/yada/pull/28 for more details. |
| 209 | let keyset = &[ |
| 210 | ("a".as_bytes(), 97), |
| 211 | ("ab".as_bytes(), 1), |
| 212 | ("de".as_bytes(), 2), |
| 213 | ]; |
| 214 | |
| 215 | let da_bytes = DoubleArrayBuilder::build(keyset); |
| 216 | assert!(da_bytes.is_some()); |
| 217 | |
| 218 | let da = DoubleArray::new(da_bytes.unwrap()); |
| 219 | |
| 220 | for (key, value) in keyset { |
| 221 | assert_eq!(da.exact_match_search(key), Some(*value as u32)); |
| 222 | } |
| 223 | assert_eq!(da.exact_match_search("dasss"), None); |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn test_clone_and_search() { |
| 228 | let keyset = &[ |
| 229 | ("a".as_bytes(), 0), |
| 230 | ("ab".as_bytes(), 1), |
| 231 | ("aba".as_bytes(), 2), |
| 232 | ("ac".as_bytes(), 3), |
| 233 | ("acb".as_bytes(), 4), |
| 234 | ("acc".as_bytes(), 5), |
| 235 | ("ad".as_bytes(), 6), |
| 236 | ("ba".as_bytes(), 7), |
| 237 | ("bb".as_bytes(), 8), |
| 238 | ("bc".as_bytes(), 9), |
| 239 | ("c".as_bytes(), 10), |
| 240 | ("caa".as_bytes(), 11), |
| 241 | ]; |
| 242 | |
| 243 | let da_bytes = DoubleArrayBuilder::build(keyset); |
| 244 | assert!(da_bytes.is_some()); |
| 245 | |
| 246 | let da_orig = DoubleArray::new(da_bytes.unwrap()); |
| 247 | let da = da_orig.clone(); |
| 248 | |
| 249 | for (key, value) in keyset { |
| 250 | assert_eq!(da.exact_match_search(key), Some(*value as u32)); |
| 251 | } |
| 252 | assert_eq!(da.exact_match_search("aa".as_bytes()), None); |
| 253 | assert_eq!(da.exact_match_search("abc".as_bytes()), None); |
| 254 | assert_eq!(da.exact_match_search("b".as_bytes()), None); |
| 255 | assert_eq!(da.exact_match_search("ca".as_bytes()), None); |
| 256 | |
| 257 | assert_eq!( |
| 258 | da.common_prefix_search("a".as_bytes()).collect::<Vec<_>>(), |
| 259 | vec![(0, 1)] |
| 260 | ); |
| 261 | assert_eq!( |
| 262 | da.common_prefix_search("aa".as_bytes()).collect::<Vec<_>>(), |
| 263 | vec![(0, 1)] |
| 264 | ); |
| 265 | assert_eq!( |
| 266 | da.common_prefix_search("abbb".as_bytes()) |
| 267 | .collect::<Vec<_>>(), |
| 268 | vec![(0, 1), (1, 2)] |
| 269 | ); |
| 270 | assert_eq!( |
| 271 | da.common_prefix_search("abaa".as_bytes()) |
| 272 | .collect::<Vec<_>>(), |
| 273 | vec![(0, 1), (1, 2), (2, 3)] |
| 274 | ); |
| 275 | assert_eq!( |
| 276 | da.common_prefix_search("caa".as_bytes()) |
| 277 | .collect::<Vec<_>>(), |
| 278 | vec![(10, 1), (11, 3)] |
| 279 | ); |
| 280 | assert_eq!( |
| 281 | da.common_prefix_search("d".as_bytes()).collect::<Vec<_>>(), |
| 282 | vec![] |
| 283 | ); |
| 284 | } |
| 285 | } |