1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Ordered containers with integer keys, implemented as radix tries (`TrieSet` and `TrieMap` types)
15 use util::{swap, replace};
18 // FIXME: #5244: need to manually update the TrieNode constructor
19 static SHIFT: uint = 4;
20 static SIZE: uint = 1 << SHIFT;
21 static MASK: uint = SIZE - 1;
24 Internal(~TrieNode<T>),
30 pub struct TrieMap<T> {
31 priv root: TrieNode<T>,
35 impl<T> Container for TrieMap<T> {
36 /// Return the number of elements in the map
38 fn len(&self) -> uint { self.length }
41 impl<T> Mutable for TrieMap<T> {
42 /// Clear the map, removing all values.
45 self.root = TrieNode::new();
50 impl<T> Map<uint, T> for TrieMap<T> {
51 /// Return a reference to the value corresponding to the key
53 fn find<'a>(&'a self, key: &uint) -> Option<&'a T> {
54 let mut node: &'a TrieNode<T> = &self.root;
57 match node.children[chunk(*key, idx)] {
58 Internal(ref x) => node = &**x,
59 External(stored, ref value) => {
66 Nothing => return None
73 impl<T> MutableMap<uint, T> for TrieMap<T> {
74 /// Return a mutable reference to the value corresponding to the key
76 fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut T> {
77 find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1)
80 /// Insert a key-value pair from the map. If the key already had a value
81 /// present in the map, that value is returned. Otherwise None is returned.
82 fn swap(&mut self, key: uint, value: T) -> Option<T> {
83 let ret = insert(&mut self.root.count,
84 &mut self.root.children[chunk(key, 0)],
86 if ret.is_none() { self.length += 1 }
90 /// Removes a key from the map, returning the value at the key if the key
91 /// was previously in the map.
92 fn pop(&mut self, key: &uint) -> Option<T> {
93 let ret = remove(&mut self.root.count,
94 &mut self.root.children[chunk(*key, 0)],
96 if ret.is_some() { self.length -= 1 }
102 /// Create an empty TrieMap
104 pub fn new() -> TrieMap<T> {
105 TrieMap{root: TrieNode::new(), length: 0}
108 /// Visit all key-value pairs in reverse order
110 pub fn each_reverse<'a>(&'a self, f: |&uint, &'a T| -> bool) -> bool {
111 self.root.each_reverse(f)
114 /// Visit all key-value pairs in order
116 pub fn each<'a>(&'a self, f: |&uint, &'a T| -> bool) -> bool {
120 /// Visit all keys in order
122 pub fn each_key(&self, f: |&uint| -> bool) -> bool {
123 self.each(|k, _| f(k))
126 /// Visit all values in order
128 pub fn each_value<'a>(&'a self, f: |&'a T| -> bool) -> bool {
129 self.each(|_, v| f(v))
132 /// Iterate over the map and mutate the contained values
134 pub fn mutate_values(&mut self, f: |&uint, &mut T| -> bool) -> bool {
135 self.root.mutate_values(f)
138 /// Visit all keys in reverse order
140 pub fn each_key_reverse(&self, f: |&uint| -> bool) -> bool {
141 self.each_reverse(|k, _| f(k))
144 /// Visit all values in reverse order
146 pub fn each_value_reverse(&self, f: |&T| -> bool) -> bool {
147 self.each_reverse(|_, v| f(v))
150 /// Get an iterator over the key-value pairs in the map
151 pub fn iter<'a>(&'a self) -> TrieMapIterator<'a, T> {
153 stack: ~[self.root.children.iter()],
154 remaining_min: self.length,
155 remaining_max: self.length
159 // If `upper` is true then returns upper_bound else returns lower_bound.
161 fn bound<'a>(&'a self, key: uint, upper: bool) -> TrieMapIterator<'a, T> {
162 let mut node: &'a TrieNode<T> = &self.root;
164 let mut it = TrieMapIterator {
167 remaining_max: self.length
170 let children = &node.children;
171 let child_id = chunk(key, idx);
172 match children[child_id] {
175 it.stack.push(children.slice_from(child_id + 1).iter());
177 External(stored, _) => {
178 if stored < key || (upper && stored == key) {
179 it.stack.push(children.slice_from(child_id + 1).iter());
181 it.stack.push(children.slice_from(child_id).iter());
186 it.stack.push(children.slice_from(child_id + 1).iter());
194 /// Get an iterator pointing to the first key-value pair whose key is not less than `key`.
195 /// If all keys in the map are less than `key` an empty iterator is returned.
196 pub fn lower_bound<'a>(&'a self, key: uint) -> TrieMapIterator<'a, T> {
197 self.bound(key, false)
200 /// Get an iterator pointing to the first key-value pair whose key is greater than `key`.
201 /// If all keys in the map are not greater than `key` an empty iterator is returned.
202 pub fn upper_bound<'a>(&'a self, key: uint) -> TrieMapIterator<'a, T> {
203 self.bound(key, true)
207 impl<T> FromIterator<(uint, T)> for TrieMap<T> {
208 fn from_iterator<Iter: Iterator<(uint, T)>>(iter: &mut Iter) -> TrieMap<T> {
209 let mut map = TrieMap::new();
215 impl<T> Extendable<(uint, T)> for TrieMap<T> {
216 fn extend<Iter: Iterator<(uint, T)>>(&mut self, iter: &mut Iter) {
217 for (k, v) in *iter {
223 #[allow(missing_doc)]
225 priv map: TrieMap<()>
228 impl Container for TrieSet {
229 /// Return the number of elements in the set
231 fn len(&self) -> uint { self.map.len() }
234 impl Mutable for TrieSet {
235 /// Clear the set, removing all values.
237 fn clear(&mut self) { self.map.clear() }
241 /// Create an empty TrieSet
243 pub fn new() -> TrieSet {
244 TrieSet{map: TrieMap::new()}
247 /// Return true if the set contains a value
249 pub fn contains(&self, value: &uint) -> bool {
250 self.map.contains_key(value)
253 /// Add a value to the set. Return true if the value was not already
254 /// present in the set.
256 pub fn insert(&mut self, value: uint) -> bool {
257 self.map.insert(value, ())
260 /// Remove a value from the set. Return true if the value was
261 /// present in the set.
263 pub fn remove(&mut self, value: &uint) -> bool {
264 self.map.remove(value)
267 /// Visit all values in order
269 pub fn each(&self, f: |&uint| -> bool) -> bool { self.map.each_key(f) }
271 /// Visit all values in reverse order
273 pub fn each_reverse(&self, f: |&uint| -> bool) -> bool {
274 self.map.each_key_reverse(f)
277 /// Get an iterator over the values in the set
279 pub fn iter<'a>(&'a self) -> TrieSetIterator<'a> {
280 TrieSetIterator{iter: self.map.iter()}
283 /// Get an iterator pointing to the first value that is not less than `val`.
284 /// If all values in the set are less than `val` an empty iterator is returned.
285 pub fn lower_bound<'a>(&'a self, val: uint) -> TrieSetIterator<'a> {
286 TrieSetIterator{iter: self.map.lower_bound(val)}
289 /// Get an iterator pointing to the first value that key is greater than `val`.
290 /// If all values in the set are not greater than `val` an empty iterator is returned.
291 pub fn upper_bound<'a>(&'a self, val: uint) -> TrieSetIterator<'a> {
292 TrieSetIterator{iter: self.map.upper_bound(val)}
296 impl FromIterator<uint> for TrieSet {
297 fn from_iterator<Iter: Iterator<uint>>(iter: &mut Iter) -> TrieSet {
298 let mut set = TrieSet::new();
304 impl Extendable<uint> for TrieSet {
305 fn extend<Iter: Iterator<uint>>(&mut self, iter: &mut Iter) {
314 children: [Child<T>, ..SIZE]
317 impl<T> TrieNode<T> {
319 fn new() -> TrieNode<T> {
320 // FIXME: #5244: [Nothing, ..SIZE] should be possible without implicit
323 children: [Nothing, Nothing, Nothing, Nothing,
324 Nothing, Nothing, Nothing, Nothing,
325 Nothing, Nothing, Nothing, Nothing,
326 Nothing, Nothing, Nothing, Nothing]}
330 impl<T> TrieNode<T> {
331 fn each<'a>(&'a self, f: |&uint, &'a T| -> bool) -> bool {
332 for elt in self.children.iter() {
334 Internal(ref x) => if !x.each(|i,t| f(i,t)) { return false },
335 External(k, ref v) => if !f(&k, v) { return false },
342 fn each_reverse<'a>(&'a self, f: |&uint, &'a T| -> bool) -> bool {
343 for elt in self.children.rev_iter() {
345 Internal(ref x) => if !x.each_reverse(|i,t| f(i,t)) { return false },
346 External(k, ref v) => if !f(&k, v) { return false },
353 fn mutate_values<'a>(&'a mut self, f: |&uint, &mut T| -> bool) -> bool {
354 for child in self.children.mut_iter() {
356 Internal(ref mut x) => if !x.mutate_values(|i,t| f(i,t)) {
359 External(k, ref mut v) => if !f(&k, v) { return false },
367 // if this was done via a trait, the key could be generic
369 fn chunk(n: uint, idx: uint) -> uint {
370 let sh = uint::bits - (SHIFT * (idx + 1));
374 fn find_mut<'r, T>(child: &'r mut Child<T>, key: uint, idx: uint) -> Option<&'r mut T> {
376 External(stored, ref mut value) if stored == key => Some(value),
377 External(..) => None,
378 Internal(ref mut x) => find_mut(&mut x.children[chunk(key, idx)], key, idx + 1),
383 fn insert<T>(count: &mut uint, child: &mut Child<T>, key: uint, value: T,
384 idx: uint) -> Option<T> {
385 let mut tmp = Nothing;
387 swap(&mut tmp, child);
390 External(stored_key, stored_value) => {
391 if stored_key == key {
392 ret = Some(stored_value);
393 External(stored_key, value)
395 // conflict - split the node
396 let mut new = ~TrieNode::new();
397 insert(&mut new.count,
398 &mut new.children[chunk(stored_key, idx)],
399 stored_key, stored_value, idx + 1);
400 ret = insert(&mut new.count, &mut new.children[chunk(key, idx)],
401 key, value, idx + 1);
407 ret = insert(&mut x.count, &mut x.children[chunk(key, idx)], key,
420 fn remove<T>(count: &mut uint, child: &mut Child<T>, key: uint,
421 idx: uint) -> Option<T> {
422 let (ret, this) = match *child {
423 External(stored, _) if stored == key => {
424 match replace(child, Nothing) {
425 External(_, value) => (Some(value), true),
429 External(..) => (None, false),
430 Internal(ref mut x) => {
431 let ret = remove(&mut x.count, &mut x.children[chunk(key, idx)],
435 Nothing => (None, false)
445 /// Forward iterator over a map
446 pub struct TrieMapIterator<'a, T> {
447 priv stack: ~[vec::VecIterator<'a, Child<T>>],
448 priv remaining_min: uint,
449 priv remaining_max: uint
452 impl<'a, T> Iterator<(uint, &'a T)> for TrieMapIterator<'a, T> {
453 fn next(&mut self) -> Option<(uint, &'a T)> {
454 while !self.stack.is_empty() {
455 match self.stack[self.stack.len() - 1].next() {
461 Internal(ref node) => {
462 self.stack.push(node.children.iter());
464 External(key, ref value) => {
465 self.remaining_max -= 1;
466 if self.remaining_min > 0 {
467 self.remaining_min -= 1;
469 return Some((key, value));
480 fn size_hint(&self) -> (uint, Option<uint>) {
481 (self.remaining_min, Some(self.remaining_max))
485 /// Forward iterator over a set
486 pub struct TrieSetIterator<'a> {
487 priv iter: TrieMapIterator<'a, ()>
490 impl<'a> Iterator<uint> for TrieSetIterator<'a> {
491 fn next(&mut self) -> Option<uint> {
492 self.iter.next().map(|(key, _)| key)
495 fn size_hint(&self) -> (uint, Option<uint>) {
496 self.iter.size_hint()
501 pub fn check_integrity<T>(trie: &TrieNode<T>) {
502 assert!(trie.count != 0);
506 for x in trie.children.iter() {
510 check_integrity(&**y);
513 External(_, _) => { sum += 1 }
517 assert_eq!(sum, trie.count);
524 use iter::range_step;
529 let mut m = TrieMap::new();
530 assert!(m.insert(1, 12));
531 assert!(m.insert(2, 8));
532 assert!(m.insert(5, 14));
534 match m.find_mut(&5) {
535 None => fail!(), Some(x) => *x = new
537 assert_eq!(m.find(&5), Some(&new));
541 fn test_find_mut_missing() {
542 let mut m = TrieMap::new();
543 assert!(m.find_mut(&0).is_none());
544 assert!(m.insert(1, 12));
545 assert!(m.find_mut(&0).is_none());
546 assert!(m.insert(2, 8));
547 assert!(m.find_mut(&0).is_none());
552 let mut trie = TrieMap::new();
555 for x in range_step(1u, n, 2) {
556 assert!(trie.insert(x, x + 1));
557 assert!(trie.contains_key(&x));
558 check_integrity(&trie.root);
561 for x in range_step(0u, n, 2) {
562 assert!(!trie.contains_key(&x));
563 assert!(trie.insert(x, x + 1));
564 check_integrity(&trie.root);
567 for x in range(0u, n) {
568 assert!(trie.contains_key(&x));
569 assert!(!trie.insert(x, x + 1));
570 check_integrity(&trie.root);
573 for x in range_step(1u, n, 2) {
574 assert!(trie.remove(&x));
575 assert!(!trie.contains_key(&x));
576 check_integrity(&trie.root);
579 for x in range_step(0u, n, 2) {
580 assert!(trie.contains_key(&x));
581 assert!(!trie.insert(x, x + 1));
582 check_integrity(&trie.root);
588 let mut m = TrieMap::new();
590 assert!(m.insert(3, 6));
591 assert!(m.insert(0, 0));
592 assert!(m.insert(4, 8));
593 assert!(m.insert(2, 4));
594 assert!(m.insert(1, 2));
599 assert_eq!(*v, n * 2);
606 fn test_each_break() {
607 let mut m = TrieMap::new();
609 for x in range(uint::max_value - 10000, uint::max_value).invert() {
613 let mut n = uint::max_value - 10000;
615 if n == uint::max_value - 5000 { false } else {
616 assert!(n < uint::max_value - 5000);
619 assert_eq!(*v, n / 2);
627 fn test_each_reverse() {
628 let mut m = TrieMap::new();
630 assert!(m.insert(3, 6));
631 assert!(m.insert(0, 0));
632 assert!(m.insert(4, 8));
633 assert!(m.insert(2, 4));
634 assert!(m.insert(1, 2));
637 m.each_reverse(|k, v| {
639 assert_eq!(*v, n * 2);
646 fn test_each_reverse_break() {
647 let mut m = TrieMap::new();
649 for x in range(uint::max_value - 10000, uint::max_value).invert() {
653 let mut n = uint::max_value - 1;
654 m.each_reverse(|k, v| {
655 if n == uint::max_value - 5000 { false } else {
656 assert!(n > uint::max_value - 5000);
659 assert_eq!(*v, n / 2);
668 let mut m = TrieMap::new();
669 assert_eq!(m.swap(1, 2), None);
670 assert_eq!(m.swap(1, 3), Some(2));
671 assert_eq!(m.swap(1, 4), Some(3));
676 let mut m = TrieMap::new();
678 assert_eq!(m.pop(&1), Some(2));
679 assert_eq!(m.pop(&1), None);
683 fn test_from_iter() {
684 let xs = ~[(1u, 1i), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
686 let map: TrieMap<int> = xs.iter().map(|&x| x).collect();
688 for &(k, v) in xs.iter() {
689 assert_eq!(map.find(&k), Some(&v));
694 fn test_iteration() {
695 let empty_map : TrieMap<uint> = TrieMap::new();
696 assert_eq!(empty_map.iter().next(), None);
698 let first = uint::max_value - 10000;
699 let last = uint::max_value;
701 let mut map = TrieMap::new();
702 for x in range(first, last).invert() {
703 map.insert(x, x / 2);
707 for (k, &v) in map.iter() {
708 assert_eq!(k, first + i);
709 assert_eq!(v, k / 2);
712 assert_eq!(i, last - first);
717 let empty_map : TrieMap<uint> = TrieMap::new();
718 assert_eq!(empty_map.lower_bound(0).next(), None);
719 assert_eq!(empty_map.upper_bound(0).next(), None);
725 let mut map : TrieMap<uint> = TrieMap::new();
726 for x in range_step(0u, last, step) {
727 assert!(x % step == 0);
728 map.insert(x, value);
731 for i in range(0u, last - step) {
732 let mut lb = map.lower_bound(i);
733 let mut ub = map.upper_bound(i);
734 let next_key = i - i % step + step;
735 let next_pair = (next_key, &value);
737 assert_eq!(lb.next(), Some((i, &value)));
739 assert_eq!(lb.next(), Some(next_pair));
741 assert_eq!(ub.next(), Some(next_pair));
744 let mut lb = map.lower_bound(last - step);
745 assert_eq!(lb.next(), Some((last - step, &value)));
746 let mut ub = map.upper_bound(last - step);
747 assert_eq!(ub.next(), None);
749 for i in range(last - step + 1, last) {
750 let mut lb = map.lower_bound(i);
751 assert_eq!(lb.next(), None);
752 let mut ub = map.upper_bound(i);
753 assert_eq!(ub.next(), None);
765 fn test_sane_chunk() {
767 let y = 1 << (uint::bits - 1);
769 let mut trie = TrieSet::new();
771 assert!(trie.insert(x));
772 assert!(trie.insert(y));
774 assert_eq!(trie.len(), 2);
776 let expected = [x, y];
781 assert_eq!(expected[i], *x);
788 fn test_from_iter() {
789 let xs = ~[9u, 8, 7, 6, 5, 4, 3, 2, 1];
791 let set: TrieSet = xs.iter().map(|&x| x).collect();
794 assert!(set.contains(x));