2013-03-02 13:41:31 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! A radix trie for storing integers in sorted order
|
|
|
|
|
|
|
|
use prelude::*;
|
|
|
|
|
2013-03-05 17:53:43 -06:00
|
|
|
// FIXME: #5244: need to manually update the TrieNode constructor
|
2013-03-22 16:00:15 -05:00
|
|
|
static SHIFT: uint = 4;
|
|
|
|
static SIZE: uint = 1 << SHIFT;
|
|
|
|
static MASK: uint = SIZE - 1;
|
2013-03-02 13:41:31 -06:00
|
|
|
|
|
|
|
enum Child<T> {
|
|
|
|
Internal(~TrieNode<T>),
|
|
|
|
External(uint, T),
|
|
|
|
Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TrieMap<T> {
|
|
|
|
priv root: TrieNode<T>,
|
|
|
|
priv length: uint
|
|
|
|
}
|
|
|
|
|
2013-02-26 13:34:00 -06:00
|
|
|
impl<T> BaseIter<(uint, &'self T)> for TrieMap<T> {
|
2013-03-02 13:41:31 -06:00
|
|
|
/// Visit all key-value pairs in order
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn each(&self, f: &fn(&(uint, &'self T)) -> bool) {
|
2013-03-05 09:10:22 -06:00
|
|
|
self.root.each(f);
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
2013-02-26 13:34:00 -06:00
|
|
|
impl<T> ReverseIter<(uint, &'self T)> for TrieMap<T> {
|
2013-03-02 13:41:31 -06:00
|
|
|
/// Visit all key-value pairs in reverse order
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn each_reverse(&self, f: &fn(&(uint, &'self T)) -> bool) {
|
2013-03-05 09:10:22 -06:00
|
|
|
self.root.each_reverse(f);
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Container for TrieMap<T> {
|
|
|
|
/// Return the number of elements in the map
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn len(&const self) -> uint { self.length }
|
2013-03-02 13:41:31 -06:00
|
|
|
|
|
|
|
/// Return true if the map contains no elements
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn is_empty(&const self) -> bool { self.len() == 0 }
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
2013-03-05 17:53:43 -06:00
|
|
|
impl<T> Mutable for TrieMap<T> {
|
2013-03-02 13:41:31 -06:00
|
|
|
/// Clear the map, removing all values.
|
|
|
|
#[inline(always)]
|
|
|
|
fn clear(&mut self) {
|
|
|
|
self.root = TrieNode::new();
|
|
|
|
self.length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 17:53:43 -06:00
|
|
|
impl<T> Map<uint, T> for TrieMap<T> {
|
2013-03-02 13:41:31 -06:00
|
|
|
/// Return true if the map contains a value for the specified key
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn contains_key(&self, key: &uint) -> bool {
|
2013-03-02 13:41:31 -06:00
|
|
|
self.find(key).is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visit all keys in order
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn each_key(&self, f: &fn(&uint) -> bool) {
|
2013-03-02 13:41:31 -06:00
|
|
|
self.each(|&(k, _)| f(&k))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visit all values in order
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn each_value(&self, f: &fn(&T) -> bool) {
|
2013-03-07 17:44:21 -06:00
|
|
|
self.each(|&(_, v)| f(v))
|
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
|
2013-03-13 16:07:23 -05:00
|
|
|
/// Iterate over the map and mutate the contained values
|
|
|
|
#[inline(always)]
|
|
|
|
fn mutate_values(&mut self, f: &fn(&uint, &mut T) -> bool) {
|
|
|
|
self.root.mutate_values(f);
|
|
|
|
}
|
|
|
|
|
2013-03-24 19:30:35 -05:00
|
|
|
/// Return a reference to the value corresponding to the key
|
2013-03-02 13:41:31 -06:00
|
|
|
#[inline(hint)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn find(&self, key: &uint) -> Option<&'self T> {
|
2013-03-14 13:22:51 -05:00
|
|
|
let mut node: &'self TrieNode<T> = &self.root;
|
2013-03-02 13:41:31 -06:00
|
|
|
let mut idx = 0;
|
|
|
|
loop {
|
|
|
|
match node.children[chunk(*key, idx)] {
|
|
|
|
Internal(ref x) => node = &**x,
|
|
|
|
External(stored, ref value) => {
|
|
|
|
if stored == *key {
|
|
|
|
return Some(value)
|
|
|
|
} else {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Nothing => return None
|
|
|
|
}
|
|
|
|
idx += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a key-value pair into the map. An existing value for a
|
|
|
|
/// key is replaced by the new value. Return true if the key did
|
|
|
|
/// not already exist in the map.
|
|
|
|
#[inline(always)]
|
|
|
|
fn insert(&mut self, key: uint, value: T) -> bool {
|
|
|
|
let ret = insert(&mut self.root.count,
|
|
|
|
&mut self.root.children[chunk(key, 0)],
|
|
|
|
key, value, 1);
|
|
|
|
if ret { self.length += 1 }
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove a key-value pair from the map. Return true if the key
|
|
|
|
/// was present in the map, otherwise false.
|
|
|
|
#[inline(always)]
|
|
|
|
fn remove(&mut self, key: &uint) -> bool {
|
|
|
|
let ret = remove(&mut self.root.count,
|
|
|
|
&mut self.root.children[chunk(*key, 0)],
|
|
|
|
*key, 1);
|
|
|
|
if ret { self.length -= 1 }
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 16:49:55 -05:00
|
|
|
pub impl<T> TrieMap<T> {
|
2013-03-15 20:03:29 -05:00
|
|
|
/// Create an empty TrieMap
|
2013-03-02 13:41:31 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn new() -> TrieMap<T> {
|
2013-03-02 13:41:31 -06:00
|
|
|
TrieMap{root: TrieNode::new(), length: 0}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visit all keys in reverse order
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn each_key_reverse(&self, f: &fn(&uint) -> bool) {
|
2013-03-02 13:41:31 -06:00
|
|
|
self.each_reverse(|&(k, _)| f(&k))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visit all values in reverse order
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn each_value_reverse(&self, f: &fn(&T) -> bool) {
|
2013-03-02 13:41:31 -06:00
|
|
|
self.each_reverse(|&(_, v)| f(v))
|
|
|
|
}
|
2013-03-24 19:30:35 -05:00
|
|
|
|
|
|
|
/// Return a mutable reference to the value corresponding to the key
|
|
|
|
#[inline(always)]
|
|
|
|
fn find_mut(&mut self, key: &uint) -> Option<&'self mut T> {
|
|
|
|
find_mut(&mut self.root.children[chunk(*key, 0)], *key, 1)
|
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TrieSet {
|
|
|
|
priv map: TrieMap<()>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BaseIter<uint> for TrieSet {
|
|
|
|
/// Visit all values in order
|
2013-03-22 15:21:57 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn each(&self, f: &fn(&uint) -> bool) { self.map.each_key(f) }
|
2013-03-22 15:21:57 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ReverseIter<uint> for TrieSet {
|
|
|
|
/// Visit all values in reverse order
|
2013-03-22 15:21:57 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn each_reverse(&self, f: &fn(&uint) -> bool) {
|
2013-03-02 13:41:31 -06:00
|
|
|
self.map.each_key_reverse(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Container for TrieSet {
|
|
|
|
/// Return the number of elements in the set
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn len(&const self) -> uint { self.map.len() }
|
2013-03-02 13:41:31 -06:00
|
|
|
|
|
|
|
/// Return true if the set contains no elements
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn is_empty(&const self) -> bool { self.map.is_empty() }
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Mutable for TrieSet {
|
|
|
|
/// Clear the set, removing all values.
|
|
|
|
#[inline(always)]
|
|
|
|
fn clear(&mut self) { self.map.clear() }
|
|
|
|
}
|
|
|
|
|
2013-03-22 16:40:28 -05:00
|
|
|
pub impl TrieSet {
|
2013-03-15 20:03:29 -05:00
|
|
|
/// Create an empty TrieSet
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn new() -> TrieSet {
|
2013-03-15 20:03:29 -05:00
|
|
|
TrieSet{map: TrieMap::new()}
|
|
|
|
}
|
|
|
|
|
2013-03-02 13:41:31 -06:00
|
|
|
/// Return true if the set contains a value
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn contains(&self, value: &uint) -> bool {
|
2013-03-02 13:41:31 -06:00
|
|
|
self.map.contains_key(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a value to the set. Return true if the value was not already
|
|
|
|
/// present in the set.
|
|
|
|
#[inline(always)]
|
|
|
|
fn insert(&mut self, value: uint) -> bool { self.map.insert(value, ()) }
|
|
|
|
|
|
|
|
/// Remove a value from the set. Return true if the value was
|
|
|
|
/// present in the set.
|
|
|
|
#[inline(always)]
|
|
|
|
fn remove(&mut self, value: &uint) -> bool { self.map.remove(value) }
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TrieNode<T> {
|
|
|
|
count: uint,
|
2013-03-22 15:19:24 -05:00
|
|
|
children: [Child<T> * SIZE]
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
2013-03-05 17:53:43 -06:00
|
|
|
impl<T> TrieNode<T> {
|
2013-03-02 13:41:31 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn new() -> TrieNode<T> {
|
2013-03-05 17:53:43 -06:00
|
|
|
// FIXME: #5244: [Nothing, ..SIZE] should be possible without Copy
|
|
|
|
TrieNode{count: 0,
|
|
|
|
children: [Nothing, Nothing, Nothing, Nothing,
|
|
|
|
Nothing, Nothing, Nothing, Nothing,
|
|
|
|
Nothing, Nothing, Nothing, Nothing,
|
|
|
|
Nothing, Nothing, Nothing, Nothing]}
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> TrieNode<T> {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn each(&self, f: &fn(&(uint, &'self T)) -> bool) -> bool {
|
2013-03-02 13:41:31 -06:00
|
|
|
for uint::range(0, self.children.len()) |idx| {
|
|
|
|
match self.children[idx] {
|
2013-03-05 09:10:22 -06:00
|
|
|
Internal(ref x) => if !x.each(f) { return false },
|
|
|
|
External(k, ref v) => if !f(&(k, v)) { return false },
|
2013-03-02 13:41:31 -06:00
|
|
|
Nothing => ()
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 09:10:22 -06:00
|
|
|
true
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn each_reverse(&self, f: &fn(&(uint, &'self T)) -> bool) -> bool {
|
2013-03-02 13:41:31 -06:00
|
|
|
for uint::range_rev(self.children.len(), 0) |idx| {
|
|
|
|
match self.children[idx - 1] {
|
2013-03-05 09:10:22 -06:00
|
|
|
Internal(ref x) => if !x.each_reverse(f) { return false },
|
|
|
|
External(k, ref v) => if !f(&(k, v)) { return false },
|
2013-03-02 13:41:31 -06:00
|
|
|
Nothing => ()
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 09:10:22 -06:00
|
|
|
true
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
2013-03-13 16:07:23 -05:00
|
|
|
fn mutate_values(&mut self, f: &fn(&uint, &mut T) -> bool) -> bool {
|
2013-03-02 13:41:31 -06:00
|
|
|
for vec::each_mut(self.children) |child| {
|
|
|
|
match *child {
|
2013-03-05 09:10:22 -06:00
|
|
|
Internal(ref mut x) => if !x.mutate_values(f) {
|
|
|
|
return false
|
|
|
|
},
|
2013-03-13 16:07:23 -05:00
|
|
|
External(k, ref mut v) => if !f(&k, v) { return false },
|
2013-03-02 13:41:31 -06:00
|
|
|
Nothing => ()
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 09:10:22 -06:00
|
|
|
true
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this was done via a trait, the key could be generic
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn chunk(n: uint, idx: uint) -> uint {
|
2013-03-05 22:54:19 -06:00
|
|
|
let sh = uint::bits - (SHIFT * (idx + 1));
|
|
|
|
(n >> sh) & MASK
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
2013-03-24 19:30:35 -05:00
|
|
|
fn find_mut<T>(child: &'r mut Child<T>, key: uint, idx: uint) -> Option<&'r mut T> {
|
|
|
|
unsafe { // FIXME(#4903)---requires flow-sensitive borrow checker
|
|
|
|
(match *child {
|
|
|
|
External(_, ref value) => Some(cast::transmute_mut(value)),
|
|
|
|
Internal(ref x) => find_mut(cast::transmute_mut(&x.children[chunk(key, idx)]),
|
|
|
|
key, idx + 1),
|
|
|
|
Nothing => None
|
|
|
|
}).map_consume(|x| cast::transmute_mut_region(x))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-13 16:07:23 -05:00
|
|
|
fn insert<T>(count: &mut uint, child: &mut Child<T>, key: uint, value: T,
|
|
|
|
idx: uint) -> bool {
|
2013-03-05 17:53:43 -06:00
|
|
|
let mut tmp = Nothing;
|
|
|
|
tmp <-> *child;
|
|
|
|
let mut added = false;
|
|
|
|
|
|
|
|
*child = match tmp {
|
2013-03-02 13:41:31 -06:00
|
|
|
External(stored_key, stored_value) => {
|
|
|
|
if stored_key == key {
|
2013-03-05 17:53:43 -06:00
|
|
|
External(stored_key, value)
|
2013-03-02 13:41:31 -06:00
|
|
|
} else {
|
|
|
|
// conflict - split the node
|
|
|
|
let mut new = ~TrieNode::new();
|
|
|
|
insert(&mut new.count,
|
|
|
|
&mut new.children[chunk(stored_key, idx)],
|
|
|
|
stored_key, stored_value, idx + 1);
|
|
|
|
insert(&mut new.count, &mut new.children[chunk(key, idx)], key,
|
|
|
|
value, idx + 1);
|
2013-03-05 17:53:43 -06:00
|
|
|
added = true;
|
|
|
|
Internal(new)
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
}
|
2013-03-05 17:53:43 -06:00
|
|
|
Internal(x) => {
|
|
|
|
let mut x = x;
|
|
|
|
added = insert(&mut x.count, &mut x.children[chunk(key, idx)], key,
|
|
|
|
value, idx + 1);
|
|
|
|
Internal(x)
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
Nothing => {
|
|
|
|
*count += 1;
|
2013-03-05 17:53:43 -06:00
|
|
|
added = true;
|
|
|
|
External(key, value)
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
2013-03-05 17:53:43 -06:00
|
|
|
};
|
|
|
|
added
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn remove<T>(count: &mut uint, child: &mut Child<T>, key: uint,
|
|
|
|
idx: uint) -> bool {
|
|
|
|
let (ret, this) = match *child {
|
|
|
|
External(stored, _) => {
|
|
|
|
if stored == key { (true, true) } else { (false, false) }
|
|
|
|
}
|
|
|
|
Internal(ref mut x) => {
|
|
|
|
let ret = remove(&mut x.count, &mut x.children[chunk(key, idx)],
|
|
|
|
key, idx + 1);
|
|
|
|
(ret, x.count == 0)
|
|
|
|
}
|
|
|
|
Nothing => (false, false)
|
|
|
|
};
|
|
|
|
|
|
|
|
if this {
|
|
|
|
*child = Nothing;
|
|
|
|
*count -= 1;
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn check_integrity<T>(trie: &TrieNode<T>) {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(trie.count != 0);
|
2013-03-02 13:41:31 -06:00
|
|
|
|
|
|
|
let mut sum = 0;
|
|
|
|
|
|
|
|
for trie.children.each |x| {
|
|
|
|
match *x {
|
|
|
|
Nothing => (),
|
|
|
|
Internal(ref y) => {
|
|
|
|
check_integrity(&**y);
|
|
|
|
sum += 1
|
|
|
|
}
|
|
|
|
External(_, _) => { sum += 1 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(sum == trie.count);
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2013-03-24 19:30:35 -05:00
|
|
|
use core::option::{Some, None};
|
2013-03-02 13:41:31 -06:00
|
|
|
use uint;
|
|
|
|
|
2013-03-24 19:30:35 -05:00
|
|
|
#[test]
|
|
|
|
fn test_find_mut() {
|
|
|
|
let mut m = TrieMap::new();
|
|
|
|
fail_unless!(m.insert(1, 12));
|
|
|
|
fail_unless!(m.insert(2, 8));
|
|
|
|
fail_unless!(m.insert(5, 14));
|
|
|
|
let new = 100;
|
|
|
|
match m.find_mut(&5) {
|
|
|
|
None => fail!(), Some(x) => *x = new
|
|
|
|
}
|
|
|
|
assert_eq!(m.find(&5), Some(&new));
|
|
|
|
}
|
|
|
|
|
2013-03-02 13:41:31 -06:00
|
|
|
#[test]
|
|
|
|
fn test_step() {
|
|
|
|
let mut trie = TrieMap::new();
|
|
|
|
let n = 300;
|
|
|
|
|
|
|
|
for uint::range_step(1, n, 2) |x| {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(trie.insert(x, x + 1));
|
|
|
|
fail_unless!(trie.contains_key(&x));
|
2013-03-02 13:41:31 -06:00
|
|
|
check_integrity(&trie.root);
|
|
|
|
}
|
|
|
|
|
|
|
|
for uint::range_step(0, n, 2) |x| {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(!trie.contains_key(&x));
|
|
|
|
fail_unless!(trie.insert(x, x + 1));
|
2013-03-02 13:41:31 -06:00
|
|
|
check_integrity(&trie.root);
|
|
|
|
}
|
|
|
|
|
|
|
|
for uint::range(0, n) |x| {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(trie.contains_key(&x));
|
|
|
|
fail_unless!(!trie.insert(x, x + 1));
|
2013-03-02 13:41:31 -06:00
|
|
|
check_integrity(&trie.root);
|
|
|
|
}
|
|
|
|
|
|
|
|
for uint::range_step(1, n, 2) |x| {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(trie.remove(&x));
|
|
|
|
fail_unless!(!trie.contains_key(&x));
|
2013-03-02 13:41:31 -06:00
|
|
|
check_integrity(&trie.root);
|
|
|
|
}
|
|
|
|
|
|
|
|
for uint::range_step(0, n, 2) |x| {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(trie.contains_key(&x));
|
|
|
|
fail_unless!(!trie.insert(x, x + 1));
|
2013-03-02 13:41:31 -06:00
|
|
|
check_integrity(&trie.root);
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 08:35:45 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_each() {
|
|
|
|
let mut m = TrieMap::new();
|
|
|
|
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(m.insert(3, 6));
|
|
|
|
fail_unless!(m.insert(0, 0));
|
|
|
|
fail_unless!(m.insert(4, 8));
|
|
|
|
fail_unless!(m.insert(2, 4));
|
|
|
|
fail_unless!(m.insert(1, 2));
|
2013-03-05 08:35:45 -06:00
|
|
|
|
|
|
|
let mut n = 0;
|
|
|
|
for m.each |&(k, v)| {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(k == n);
|
|
|
|
fail_unless!(*v == n * 2);
|
2013-03-05 08:35:45 -06:00
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 09:10:22 -06:00
|
|
|
#[test]
|
|
|
|
fn test_each_break() {
|
|
|
|
let mut m = TrieMap::new();
|
|
|
|
|
|
|
|
for uint::range_rev(uint::max_value, uint::max_value - 10000) |x| {
|
|
|
|
m.insert(x, x / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut n = uint::max_value - 9999;
|
|
|
|
for m.each |&(k, v)| {
|
|
|
|
if n == uint::max_value - 5000 { break }
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(n < uint::max_value - 5000);
|
2013-03-05 09:10:22 -06:00
|
|
|
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(k == n);
|
|
|
|
fail_unless!(*v == n / 2);
|
2013-03-05 09:10:22 -06:00
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-05 08:35:45 -06:00
|
|
|
#[test]
|
|
|
|
fn test_each_reverse() {
|
|
|
|
let mut m = TrieMap::new();
|
|
|
|
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(m.insert(3, 6));
|
|
|
|
fail_unless!(m.insert(0, 0));
|
|
|
|
fail_unless!(m.insert(4, 8));
|
|
|
|
fail_unless!(m.insert(2, 4));
|
|
|
|
fail_unless!(m.insert(1, 2));
|
2013-03-05 08:35:45 -06:00
|
|
|
|
|
|
|
let mut n = 4;
|
|
|
|
for m.each_reverse |&(k, v)| {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(k == n);
|
|
|
|
fail_unless!(*v == n * 2);
|
2013-03-05 08:35:45 -06:00
|
|
|
n -= 1;
|
|
|
|
}
|
|
|
|
}
|
2013-03-05 09:10:22 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_each_reverse_break() {
|
|
|
|
let mut m = TrieMap::new();
|
|
|
|
|
|
|
|
for uint::range_rev(uint::max_value, uint::max_value - 10000) |x| {
|
|
|
|
m.insert(x, x / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut n = uint::max_value;
|
|
|
|
for m.each_reverse |&(k, v)| {
|
|
|
|
if n == uint::max_value - 5000 { break }
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(n > uint::max_value - 5000);
|
2013-03-05 09:10:22 -06:00
|
|
|
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(k == n);
|
|
|
|
fail_unless!(*v == n / 2);
|
2013-03-05 09:10:22 -06:00
|
|
|
n -= 1;
|
|
|
|
}
|
|
|
|
}
|
2013-03-15 20:43:54 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_sane_chunk() {
|
|
|
|
let x = 1;
|
|
|
|
let y = 1 << (uint::bits - 1);
|
|
|
|
|
|
|
|
let mut trie = TrieSet::new();
|
|
|
|
|
|
|
|
fail_unless!(trie.insert(x));
|
|
|
|
fail_unless!(trie.insert(y));
|
|
|
|
|
|
|
|
fail_unless!(trie.len() == 2);
|
|
|
|
|
|
|
|
let expected = [x, y];
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
|
|
|
|
for trie.each |x| {
|
|
|
|
fail_unless!(expected[i] == *x);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
2013-03-02 13:41:31 -06:00
|
|
|
}
|