rust/src/libstd/treemap.rs

145 lines
3.2 KiB
Rust
Raw Normal View History

2012-03-07 20:17:30 -06:00
#[doc = "
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-03-07 20:17:30 -06:00
"];
import core::option::{some, none};
import option = core::option;
2012-03-12 17:52:30 -05:00
export treemap;
export insert;
export find;
export traverse;
2012-05-23 19:18:31 -05:00
type treemap<K, V> = @mut tree_edge<K, V>;
type tree_edge<K, V> = option<@tree_node<K, V>>;
2012-05-23 19:18:31 -05:00
enum tree_node<K, V> = {
key: K,
mut value: V,
mut left: tree_edge<K, V>,
mut right: tree_edge<K, V>
};
2012-03-07 20:17:30 -06:00
#[doc = "Create a treemap"]
2012-05-23 19:18:31 -05:00
fn treemap<K, V>() -> treemap<K, V> { @mut none }
2012-03-07 20:17:30 -06:00
#[doc = "Insert a value into the map"]
2012-05-23 19:18:31 -05:00
fn insert<K: copy, V: copy>(m: &mut tree_edge<K, V>, k: K, v: V) {
alt copy *m {
none {
*m = some(@tree_node({key: k,
mut value: v,
mut left: none,
mut right: none}));
ret;
}
some(node) {
if k == node.key {
node.value = v;
} else if k < node.key {
insert(&mut node.left, k, v);
} else {
2012-05-23 19:18:31 -05:00
insert(&mut node.right, k, v);
}
}
2012-05-23 19:18:31 -05:00
};
}
2012-03-07 20:17:30 -06:00
#[doc = "Find a value based on the key"]
2012-05-23 19:18:31 -05:00
fn find<K: copy, V: copy>(m: &const tree_edge<K, V>, k: K) -> option<V> {
alt copy *m {
none { none }
// TODO: was that an optimization?
2012-05-23 19:18:31 -05:00
some(node) {
if k == node.key {
some(node.value)
} else if k < node.key {
find(&const node.left, k)
} else {
find(&const node.right, k)
}
}
}
}
2012-03-07 20:17:30 -06:00
#[doc = "Visit all pairs in the map in order."]
2012-05-23 19:18:31 -05:00
fn traverse<K, V: copy>(m: &const tree_edge<K, V>, f: fn(K, V)) {
alt copy *m {
none { }
some(node) {
traverse(&const node.left, f);
// copy of value is req'd as f() requires an immutable ptr
f(node.key, copy node.value);
traverse(&const node.right, f);
}
}
}
2012-01-17 21:05:07 -06:00
#[cfg(test)]
mod tests {
#[test]
2012-03-12 17:52:30 -05:00
fn init_treemap() { let _m = treemap::<int, int>(); }
2012-01-17 21:05:07 -06:00
#[test]
2012-03-12 17:52:30 -05:00
fn insert_one() { let m = treemap(); insert(m, 1, 2); }
2012-01-17 21:05:07 -06:00
#[test]
2012-03-12 17:52:30 -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-03-12 17:52:30 -05:00
let m = treemap();
2012-01-17 21:05:07 -06:00
insert(m, 1, 2);
assert (find(m, 1) == some(2));
}
#[test]
fn find_empty() {
2012-03-12 17:52:30 -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-03-12 17:52:30 -05:00
let m = treemap();
2012-01-17 21:05:07 -06:00
insert(m, 1, 2);
assert (find(m, 2) == none);
}
#[test]
fn traverse_in_order() {
2012-03-12 17:52:30 -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;
fn t(n: @mut int, &&k: int, &&_v: ()) {
2012-01-17 21:05:07 -06:00
assert (*n == k); *n += 1;
}
2012-06-30 18:19:07 -05:00
traverse(m, |x,y| t(n, x, y));
2012-01-17 21:05:07 -06:00
}
#[test]
fn u8_map() {
2012-03-12 17:52:30 -05:00
let m = treemap();
2012-01-17 21:05:07 -06:00
let k1 = str::bytes("foo");
let k2 = str::bytes("bar");
insert(m, k1, "foo");
insert(m, k2, "bar");
assert (find(m, k2) == some("bar"));
assert (find(m, k1) == some("foo"));
}
2012-05-23 19:18:31 -05:00
}