1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
use iterator::TrieIterator; use std::{mem, str}; type Bitmap = u16; #[derive(Copy, Clone, Debug)] struct FlagsIndex(u32); impl FlagsIndex { #[inline] fn new(flags: u8, index: usize) -> FlagsIndex { debug_assert!(flags & 0x3 == flags); FlagsIndex(((index as u32) << 2) | (flags as u32)) } #[inline] fn flags_get(&self) -> u8 { (self.0 & 0x3) as u8 } #[inline] fn index_get(&self) -> usize { (self.0 >> 2) as usize } } #[derive(Debug)] pub struct Leaf<TK: PartialEq + AsRef<[u8]>, TV> { key: TK, val: TV, } #[derive(Debug)] pub struct Branch<TK: PartialEq + AsRef<[u8]>, TV> { twigs: Vec<Node<TK, TV>>, flags_index: FlagsIndex, bitmap: Bitmap, } #[derive(Debug)] pub enum Node<TK: PartialEq + AsRef<[u8]>, TV> { Leaf(Leaf<TK, TV>), Branch(Branch<TK, TV>), Empty, } #[derive(Default, Debug)] pub struct Trie<TK: PartialEq + AsRef<[u8]>, TV> { root: Option<Node<TK, TV>>, } impl<TK: PartialEq + AsRef<[u8]>, TV> Branch<TK, TV> { #[inline] fn twigoff(&self, b: Bitmap) -> usize { (self.bitmap & (b - 1)).count_ones() as usize } } impl<TK: PartialEq + AsRef<[u8]>, TV> Node<TK, TV> { #[inline] fn flags_index_get(&self) -> (u8, usize) { let branch = match *self { Node::Branch(ref branch) => branch, _ => unsafe { debug_unreachable!() }, }; let flags_index = branch.flags_index; (flags_index.flags_get(), flags_index.index_get()) } #[inline] fn is_branch(&self) -> bool { match *self { Node::Branch(_) => true, _ => false, } } #[inline] fn twigbit(&self, key: &[u8]) -> Bitmap { let key_len = key.len(); let (flags, index) = self.flags_index_get(); let i = index as usize; if i >= key_len { return 1; } Node::<TK, TV>::nibbit(key[i], flags) } #[inline] fn has_twig(&self, bit: Bitmap) -> bool { let branch = match *self { Node::Branch(ref branch) => branch, _ => unsafe { debug_unreachable!() }, }; (branch.bitmap & bit) != 0 } #[inline] fn twigoff(&self, b: Bitmap) -> usize { match *self { Node::Branch(ref branch) => branch.twigoff(b), _ => unsafe { debug_unreachable!() }, } } #[inline] fn twig(&self, i: usize) -> &Node<TK, TV> { let branch = match *self { Node::Branch(ref branch) => branch, _ => unsafe { debug_unreachable!() }, }; &branch.twigs[i] } #[inline] fn twig_mut(&mut self, i: usize) -> &mut Node<TK, TV> { let branch = match *self { Node::Branch(ref mut branch) => branch, _ => unsafe { debug_unreachable!() }, }; &mut branch.twigs[i] } #[inline] fn twigoff_max(&self, b: Bitmap) -> (usize, usize) { let branch = match *self { Node::Branch(ref branch) => branch, _ => unsafe { debug_unreachable!() }, }; let off = self.twigoff(b); let max = branch.bitmap.count_ones() as usize; (off, max) } #[inline] fn nibbit(k: u8, flags: u8) -> Bitmap { let mask = ((flags.wrapping_sub(2)) ^ 0x0f) & 0xff; let shift = (2 - flags) << 2; (1 as Bitmap) << ((k & mask) >> shift) } pub fn next_ge<'s>(self: &'s Node<TK, TV>, key: &[u8]) -> Option<(&'s TK, &'s TV)> { if self.is_branch() { let (s, m) = self.twigoff_max(self.twigbit(key)); for s in s..m { if let ret @ Some(_) = self.twig(s).next_ge(key) { return ret; } } return None; } let leaf = match *self { Node::Leaf(ref leaf) => leaf, _ => unsafe { debug_unreachable!() }, }; Some((&leaf.key, &leaf.val)) } pub fn next_gt<'s>(self: &'s Node<TK, TV>, key: &[u8]) -> Option<(&'s TK, &'s TV)> { if self.is_branch() { let (s, m) = self.twigoff_max(self.twigbit(key)); for s in s..m { if let ret @ Some(_) = Self::next_gt(self.twig(s), key) { return ret; } } return None; } let leaf = match *self { Node::Leaf(ref leaf) => leaf, _ => unsafe { debug_unreachable!() }, }; if leaf.key.as_ref() == key { None } else { Some((&leaf.key, &leaf.val)) } } } impl<TK: PartialEq + AsRef<[u8]>, TV> Trie<TK, TV> { pub fn get(&self, key: &TK) -> Option<&TV> { let key = key.as_ref(); if self.root.is_none() { return None; } if key.len() == 0 { return None; } let mut t = self.root.as_ref().unwrap(); while t.is_branch() { let b = t.twigbit(key); if !t.has_twig(b) { return None; } t = t.twig(t.twigoff(b)); } let leaf = match *t { Node::Leaf(ref leaf) => leaf, _ => unsafe { debug_unreachable!() }, }; if leaf.key.as_ref() != key { return None; } Some(&leaf.val) } pub fn insert(&mut self, key: TK, val: TV) -> bool { let key_len = match key.as_ref().len() { 0 => panic!("key cannot be empty"), key_len if key_len > 0xffffff => panic!("key is too long"), key_len => key_len, }; if self.root.is_none() { self.root = Some(Node::Leaf(Leaf { key: key, val: val, })); return true; } let mut t: *mut Node<TK, TV> = self.root.as_mut().unwrap(); let t = unsafe { while (&*t).is_branch() { let b = (&*t).twigbit(key.as_ref()); let i = if (&*t).has_twig(b) { (&*t).twigoff(b) } else { 0 }; t = (&mut *t).twig_mut(i); } &mut *t }; let leaf = match *t { Node::Leaf(ref mut leaf) => leaf, _ => unsafe { debug_unreachable!() }, }; let leaf_key = &leaf.key; let mut i = 0; let mut x = 0; let (mut k1, mut k2) = (0, 0); let leaf_key_len = leaf_key.as_ref().len(); while i <= key_len { k1 = Self::_get_with_implicit_zero(key.as_ref(), key_len, i); k2 = Self::_get_with_implicit_zero(leaf_key.as_ref(), leaf_key_len, i); x = k1 ^ k2; if x != 0 { break; } i += 1; } if x == 0 { leaf.val = val; return false; } let f = if (x & 0xf0) != 0 { 1 } else { 2 }; let mut t: *mut Node<TK, TV> = self.root.as_mut().unwrap(); let (t, grow_branch) = unsafe { let mut grow_branch = false; while (&*t).is_branch() { let (flags, index) = (&*t).flags_index_get(); if i == index && f == flags { grow_branch = true; break; } if (i == index && f < flags) || i < index { break; } let b = (&*t).twigbit(key.as_ref()); debug_assert!((&*t).has_twig(b)); t = (&mut *t).twig_mut((&*t).twigoff(b)); } (&mut *t, grow_branch) }; let new_node = Node::Leaf(Leaf { key: key, val: val, }); let b1 = Node::<TK, TV>::nibbit(k1, f); if grow_branch { Self::_grow_branch(t, b1, new_node); } else { let b2 = Node::<TK, TV>::nibbit(k2, f); Self::_new_branch(t, b1, b2, f, i, new_node); } true } #[inline] fn _get_with_implicit_zero(key: &[u8], key_len: usize, i: usize) -> u8 { if i < key_len { key[i] } else { 0 } } fn _new_branch(t: &mut Node<TK, TV>, b1: Bitmap, b2: Bitmap, f: u8, i: usize, new_node: Node<TK, TV>) { let twigs: Vec<Node<TK, TV>> = Vec::with_capacity(2); let mut new_t = Branch { twigs: twigs, flags_index: FlagsIndex::new(f, i), bitmap: b1 | b2, }; let t_save = mem::replace(t, Node::Empty); if new_t.twigoff(b1) == 0 { new_t.twigs.push(new_node); new_t.twigs.push(t_save); } else { new_t.twigs.push(t_save); new_t.twigs.push(new_node); } *t = Node::Branch(new_t); } fn _grow_branch(t: &mut Node<TK, TV>, b1: Bitmap, new_node: Node<TK, TV>) { debug_assert!(!t.has_twig(b1)); let branch = match *t { Node::Branch(ref mut branch) => branch, _ => unsafe { debug_unreachable!() }, }; let s = branch.twigoff(b1); branch.twigs.insert(s, new_node); branch.bitmap |= b1; } pub fn remove(&mut self, key: &TK) -> Option<TV> { if self.root.is_none() || key.as_ref().len() == 0 { return None; } let mut t: *mut Node<TK, TV> = self.root.as_mut().unwrap(); let (t, p, b): (&mut Node<TK, TV>, _, _) = unsafe { let mut b = 0; let mut p = None; while (&*t).is_branch() { b = (&*t).twigbit(key.as_ref()); if !(&*t).has_twig(b) { return None; } p = Some(t); t = (&mut *t).twig_mut((&*t).twigoff(b)); } (&mut *t, p, b) }; match *t { Node::Leaf(ref leaf) => { if leaf.key != *key { return None; } } _ => unsafe { debug_unreachable!() }, } let val = match mem::replace(t, Node::Empty) { Node::Leaf(leaf) => leaf.val, _ => unsafe { debug_unreachable!() }, }; let t2: &mut Node<TK, TV> = match p { None => return Some(val), Some(t2) => unsafe { &mut *t2 }, }; let (s, m) = t.twigoff_max(b); if m == 2 { *t2 = mem::replace(t2.twig_mut(1 - s), Node::Empty); } else { let branch = match *t2 { Node::Branch(ref mut branch) => branch, _ => unsafe { debug_unreachable!() }, }; branch.twigs.remove(s); branch.twigs.shrink_to_fit(); branch.bitmap &= !b; } Some(val) } pub fn prefix_iter<'s>(&'s self, key: &'s TK) -> TrieIterator<TK, TV> { TrieIterator::new(self.root.as_ref().unwrap(), key.as_ref(), false) } }