2011-08-25 19:19:23 -05:00
|
|
|
/*
|
2011-10-24 17:25:41 -05:00
|
|
|
Module: treemap
|
|
|
|
|
2011-08-25 19:19:23 -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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2011-12-20 14:29:59 -06:00
|
|
|
import core::option::{some, none};
|
|
|
|
import option = core::option::t;
|
2011-08-25 19:19:23 -05:00
|
|
|
|
|
|
|
export treemap;
|
|
|
|
export init;
|
|
|
|
export insert;
|
|
|
|
export find;
|
2011-08-26 12:50:02 -05:00
|
|
|
export traverse;
|
2011-08-25 19:19:23 -05:00
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
/* Section: Types */
|
2011-08-25 19:19:23 -05:00
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
/*
|
|
|
|
Type: treemap
|
|
|
|
*/
|
2011-10-25 08:56:55 -05:00
|
|
|
type treemap<K, V> = @mutable tree_node<K, V>;
|
2011-08-25 19:19:23 -05:00
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
/*
|
|
|
|
Tag: tree_node
|
|
|
|
*/
|
2012-01-19 21:29:21 -06:00
|
|
|
enum tree_node<K, V> { empty, node(@K, @V, treemap<K, V>, treemap<K, V>) }
|
2011-10-24 17:25:41 -05:00
|
|
|
|
|
|
|
/* Section: Operations */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function: init
|
|
|
|
|
|
|
|
Create a treemap
|
|
|
|
*/
|
2011-10-25 08:56:55 -05:00
|
|
|
fn init<K, V>() -> treemap<K, V> { @mutable empty }
|
2011-08-25 19:19:23 -05:00
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
/*
|
|
|
|
Function: insert
|
|
|
|
|
|
|
|
Insert a value into the map
|
|
|
|
*/
|
2012-01-05 08:35:37 -06:00
|
|
|
fn insert<K: copy, V: copy>(m: treemap<K, V>, k: K, v: V) {
|
2011-08-25 19:19:23 -05:00
|
|
|
alt m {
|
2012-01-19 00:37:22 -06:00
|
|
|
@empty { *m = node(@k, @v, @mutable empty, @mutable empty); }
|
2011-08-25 19:19:23 -05:00
|
|
|
@node(@kk, _, _, _) {
|
2011-09-02 17:34:58 -05:00
|
|
|
|
2011-08-25 19:19:23 -05:00
|
|
|
// We have to name left and right individually, because
|
|
|
|
// otherwise the alias checker complains.
|
|
|
|
if k < kk {
|
2011-09-02 17:34:58 -05:00
|
|
|
alt m { @node(_, _, left, _) { insert(left, k, v); } }
|
|
|
|
} else { alt m { @node(_, _, _, right) { insert(right, k, v); } } }
|
2011-08-25 19:19:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
/*
|
|
|
|
Function: find
|
|
|
|
|
|
|
|
Find a value based on the key
|
|
|
|
*/
|
2012-01-05 08:35:37 -06:00
|
|
|
fn find<K: copy, V: copy>(m: treemap<K, V>, k: K) -> option<V> {
|
2011-09-02 17:34:58 -05:00
|
|
|
alt *m {
|
2012-01-19 00:37:22 -06:00
|
|
|
empty { none }
|
2011-09-02 17:34:58 -05:00
|
|
|
node(@kk, @v, _, _) {
|
|
|
|
if k == kk {
|
|
|
|
some(v)
|
|
|
|
} else if k < kk {
|
2011-09-12 04:27:30 -05:00
|
|
|
|
2011-09-02 17:34:58 -05:00
|
|
|
// Again, ugliness to unpack left and right individually.
|
|
|
|
alt *m { node(_, _, left, _) { find(left, k) } }
|
|
|
|
} else { alt *m { node(_, _, _, right) { find(right, k) } } }
|
2011-08-25 19:19:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 12:50:02 -05:00
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
/*
|
|
|
|
Function: traverse
|
|
|
|
|
|
|
|
Visit all pairs in the map in order.
|
|
|
|
*/
|
2011-10-27 18:27:47 -05:00
|
|
|
fn traverse<K, V>(m: treemap<K, V>, f: block(K, V)) {
|
2011-09-02 17:34:58 -05:00
|
|
|
alt *m {
|
2012-01-19 00:37:22 -06:00
|
|
|
empty { }
|
2011-09-02 17:34:58 -05:00
|
|
|
node(k, v, _, _) {
|
|
|
|
let k1 = k, v1 = v;
|
|
|
|
alt *m { node(_, _, left, _) { traverse(left, f); } }
|
|
|
|
f(*k1, *v1);
|
|
|
|
alt *m { node(_, _, _, right) { traverse(right, f); } }
|
2011-08-26 12:50:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn init_treemap() { let _m = init::<int, int>(); }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_one() { let m = init(); insert(m, 1, 2); }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_two() { let m = init(); insert(m, 1, 2); insert(m, 3, 4); }
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn insert_find() {
|
|
|
|
let m = init();
|
|
|
|
insert(m, 1, 2);
|
|
|
|
assert (find(m, 1) == some(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_empty() {
|
|
|
|
let m = init::<int, int>(); assert (find(m, 1) == none);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_not_found() {
|
|
|
|
let m = init();
|
|
|
|
insert(m, 1, 2);
|
|
|
|
assert (find(m, 2) == none);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn traverse_in_order() {
|
|
|
|
let m = init();
|
|
|
|
insert(m, 3, ());
|
|
|
|
insert(m, 0, ());
|
|
|
|
insert(m, 4, ());
|
|
|
|
insert(m, 2, ());
|
|
|
|
insert(m, 1, ());
|
|
|
|
|
|
|
|
let n = @mutable 0;
|
|
|
|
fn t(n: @mutable int, &&k: int, &&_v: ()) {
|
|
|
|
assert (*n == k); *n += 1;
|
|
|
|
}
|
|
|
|
traverse(m, bind t(n, _, _));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn u8_map() {
|
|
|
|
let m = init();
|
|
|
|
|
|
|
|
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"));
|
|
|
|
}
|
|
|
|
}
|