2012-07-04 16:53:12 -05:00
|
|
|
/*!
|
|
|
|
* A key,value store that works on anything.
|
|
|
|
*
|
|
|
|
* This works using a binary search tree. In the first version, it's a
|
|
|
|
* very naive algorithm, but it will probably be updated to be a
|
|
|
|
* red-black tree or something else.
|
|
|
|
*/
|
2012-10-04 21:58:31 -05:00
|
|
|
#[forbid(deprecated_mode)];
|
2011-08-25 19:19:23 -05:00
|
|
|
|
2012-09-04 13:23:53 -05:00
|
|
|
use core::cmp::{Eq, Ord};
|
|
|
|
use core::option::{Some, None};
|
|
|
|
use Option = core::Option;
|
2011-08-25 19:19:23 -05:00
|
|
|
|
2012-10-04 17:18:02 -05:00
|
|
|
pub type TreeMap<K: Copy Eq Ord, V: Copy> = @mut TreeEdge<K, V>;
|
2012-05-23 19:18:31 -05:00
|
|
|
|
2012-10-04 17:18:02 -05:00
|
|
|
type TreeEdge<K: Copy Eq Ord, V: Copy> = Option<@TreeNode<K, V>>;
|
2011-08-25 19:19:23 -05:00
|
|
|
|
2012-10-04 17:18:02 -05:00
|
|
|
struct TreeNode<K: Copy Eq Ord, V: Copy> {
|
2012-05-23 19:18:31 -05:00
|
|
|
key: K,
|
|
|
|
mut value: V,
|
2012-09-04 16:35:43 -05:00
|
|
|
mut left: TreeEdge<K, V>,
|
|
|
|
mut right: TreeEdge<K, V>
|
2012-10-04 17:18:02 -05:00
|
|
|
}
|
2011-10-24 17:25:41 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Create a treemap
|
2012-10-04 17:18:02 -05:00
|
|
|
pub fn TreeMap<K: Copy Eq Ord, V: Copy>() -> TreeMap<K, V> { @mut None }
|
2011-08-25 19:19:23 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Insert a value into the map
|
2012-10-04 21:58:31 -05:00
|
|
|
pub fn insert<K: Copy Eq Ord, V: Copy>(m: &mut TreeEdge<K, V>, k: K, v: V) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match copy *m {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {
|
2012-10-04 17:18:02 -05:00
|
|
|
*m = Some(@TreeNode {key: k,
|
|
|
|
mut value: v,
|
|
|
|
mut left: None,
|
|
|
|
mut right: None});
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2012-05-23 19:18:31 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(node) => {
|
2012-05-23 19:18:31 -05:00
|
|
|
if k == node.key {
|
|
|
|
node.value = v;
|
|
|
|
} else if k < node.key {
|
|
|
|
insert(&mut node.left, k, v);
|
2012-02-15 02:40:42 -06:00
|
|
|
} else {
|
2012-05-23 19:18:31 -05:00
|
|
|
insert(&mut node.right, k, v);
|
2012-02-15 02:40:42 -06:00
|
|
|
}
|
2011-08-25 19:19:23 -05:00
|
|
|
}
|
2012-05-23 19:18:31 -05:00
|
|
|
};
|
2011-08-25 19:19:23 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Find a value based on the key
|
2012-10-04 21:58:31 -05:00
|
|
|
pub fn find<K: Copy Eq Ord, V: Copy>(m: &const TreeEdge<K, V>, k: K)
|
2012-08-27 18:26:35 -05:00
|
|
|
-> Option<V> {
|
2012-08-06 14:34:08 -05:00
|
|
|
match copy *m {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => None,
|
2012-05-23 19:18:31 -05:00
|
|
|
|
2012-07-05 17:58:25 -05:00
|
|
|
// FIXME (#2808): was that an optimization?
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(node) => {
|
2012-05-23 19:18:31 -05:00
|
|
|
if k == node.key {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(node.value)
|
2012-05-23 19:18:31 -05:00
|
|
|
} else if k < node.key {
|
|
|
|
find(&const node.left, k)
|
|
|
|
} else {
|
|
|
|
find(&const node.right, k)
|
|
|
|
}
|
2011-08-25 19:19:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 12:50:02 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Visit all pairs in the map in order.
|
2012-10-04 17:18:02 -05:00
|
|
|
pub fn traverse<K: Copy Eq Ord, V: Copy>(m: &const TreeEdge<K, V>,
|
|
|
|
f: fn((&K), (&V))) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match copy *m {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => (),
|
|
|
|
Some(node) => {
|
2012-05-23 19:18:31 -05:00
|
|
|
traverse(&const node.left, f);
|
|
|
|
// copy of value is req'd as f() requires an immutable ptr
|
2012-09-26 20:12:07 -05:00
|
|
|
f(&node.key, © node.value);
|
2012-05-23 19:18:31 -05:00
|
|
|
traverse(&const node.right, f);
|
2011-08-26 12:50:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-10-04 17:18:02 -05:00
|
|
|
/// Compare two treemaps and return true iff
|
|
|
|
/// they contain same keys and values
|
|
|
|
pub fn equals<K: Copy Eq Ord, V: Copy Eq>(t1: &const TreeEdge<K, V>,
|
|
|
|
t2: &const TreeEdge<K, V>)
|
|
|
|
-> bool {
|
|
|
|
let mut v1 = ~[];
|
|
|
|
let mut v2 = ~[];
|
|
|
|
traverse(t1, |k,v| { v1.push((copy *k, copy *v)) });
|
|
|
|
traverse(t2, |k,v| { v2.push((copy *k, copy *v)) });
|
|
|
|
return v1 == v2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2012-09-21 20:10:45 -05:00
|
|
|
#[legacy_exports];
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[test]
|
2012-09-04 16:35:43 -05:00
|
|
|
fn init_treemap() { let _m = TreeMap::<int, int>(); }
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[test]
|
2012-09-04 16:35:43 -05:00
|
|
|
fn insert_one() { let m = TreeMap(); insert(m, 1, 2); }
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[test]
|
2012-09-04 16:35:43 -05:00
|
|
|
fn insert_two() { let m = TreeMap(); insert(m, 1, 2); insert(m, 3, 4); }
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_find() {
|
2012-09-04 16:35:43 -05:00
|
|
|
let m = TreeMap();
|
2012-01-17 21:05:07 -06:00
|
|
|
insert(m, 1, 2);
|
2012-08-20 14:23:37 -05:00
|
|
|
assert (find(m, 1) == Some(2));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_empty() {
|
2012-09-04 16:35:43 -05:00
|
|
|
let m = TreeMap::<int, int>(); assert (find(m, 1) == None);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_not_found() {
|
2012-09-04 16:35:43 -05:00
|
|
|
let m = TreeMap();
|
2012-01-17 21:05:07 -06:00
|
|
|
insert(m, 1, 2);
|
2012-08-20 14:23:37 -05:00
|
|
|
assert (find(m, 2) == None);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn traverse_in_order() {
|
2012-09-04 16:35:43 -05:00
|
|
|
let m = TreeMap();
|
2012-01-17 21:05:07 -06:00
|
|
|
insert(m, 3, ());
|
|
|
|
insert(m, 0, ());
|
|
|
|
insert(m, 4, ());
|
|
|
|
insert(m, 2, ());
|
|
|
|
insert(m, 1, ());
|
|
|
|
|
2012-03-26 20:35:18 -05:00
|
|
|
let n = @mut 0;
|
2012-10-04 21:58:31 -05:00
|
|
|
fn t(n: @mut int, k: int, _v: ()) {
|
2012-01-17 21:05:07 -06:00
|
|
|
assert (*n == k); *n += 1;
|
|
|
|
}
|
2012-09-26 20:12:07 -05:00
|
|
|
traverse(m, |x,y| t(n, *x, *y));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2012-10-04 17:18:02 -05:00
|
|
|
#[test]
|
|
|
|
fn equality() {
|
|
|
|
let m1 = TreeMap();
|
|
|
|
insert(m1, 3, ());
|
|
|
|
insert(m1, 0, ());
|
|
|
|
insert(m1, 4, ());
|
|
|
|
insert(m1, 2, ());
|
|
|
|
insert(m1, 1, ());
|
|
|
|
let m2 = TreeMap();
|
|
|
|
insert(m2, 2, ());
|
|
|
|
insert(m2, 1, ());
|
|
|
|
insert(m2, 3, ());
|
|
|
|
insert(m2, 0, ());
|
|
|
|
insert(m2, 4, ());
|
|
|
|
|
|
|
|
assert equals(m1, m2);
|
|
|
|
|
|
|
|
let m3 = TreeMap();
|
|
|
|
assert !equals(m1,m3);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
|
|
|
fn u8_map() {
|
2012-09-04 16:35:43 -05:00
|
|
|
let m = TreeMap();
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-08-23 17:44:57 -05:00
|
|
|
let k1 = str::to_bytes(~"foo");
|
|
|
|
let k2 = str::to_bytes(~"bar");
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
insert(m, k1, ~"foo");
|
|
|
|
insert(m, k2, ~"bar");
|
2012-01-17 21:05:07 -06:00
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
assert (find(m, k2) == Some(~"bar"));
|
|
|
|
assert (find(m, k1) == Some(~"foo"));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
2012-05-23 19:18:31 -05:00
|
|
|
}
|