2012-07-04 16:53:12 -05:00
|
|
|
/*!
|
|
|
|
* A functional 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.
|
|
|
|
*
|
|
|
|
* This is copied and modified from treemap right now. It's missing a lot
|
|
|
|
* of features.
|
|
|
|
*/
|
2011-08-30 10:38:14 -05:00
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
import option::{Some, None};
|
2012-01-31 19:05:20 -06:00
|
|
|
import option = option;
|
2011-08-30 10:38:14 -05:00
|
|
|
|
|
|
|
export treemap;
|
|
|
|
export init;
|
|
|
|
export insert;
|
|
|
|
export find;
|
|
|
|
export traverse;
|
|
|
|
|
2011-10-24 17:25:41 -05:00
|
|
|
type treemap<K, V> = @tree_node<K, V>;
|
|
|
|
|
2012-01-19 17:20:57 -06:00
|
|
|
enum tree_node<K, V> {
|
2012-01-19 21:08:08 -06:00
|
|
|
empty,
|
2012-01-19 21:29:21 -06:00
|
|
|
node(@K, @V, @tree_node<K, V>, @tree_node<K, V>)
|
2011-08-30 10:38:14 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Create a treemap
|
2011-10-25 08:56:55 -05:00
|
|
|
fn init<K, V>() -> treemap<K, V> { @empty }
|
2011-08-30 10:38:14 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// 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) -> treemap<K, V> {
|
2012-08-06 14:34:08 -05:00
|
|
|
@match m {
|
2012-08-03 21:59:04 -05:00
|
|
|
@empty => node(@k, @v, @empty, @empty),
|
|
|
|
@node(@kk, vv, left, right) => {
|
2011-09-02 17:34:58 -05:00
|
|
|
if k < kk {
|
|
|
|
node(@kk, vv, insert(left, k, v), right)
|
|
|
|
} else if k == kk {
|
|
|
|
node(@kk, @v, left, right)
|
|
|
|
} else { node(@kk, vv, left, insert(right, k, v)) }
|
|
|
|
}
|
|
|
|
}
|
2011-08-30 10:38:14 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Find a value based on the key
|
2012-08-20 14:23:37 -05:00
|
|
|
fn find<K, V: copy>(m: treemap<K, V>, k: K) -> Option<V> {
|
2012-08-06 14:34:08 -05:00
|
|
|
match *m {
|
2012-08-20 14:23:37 -05:00
|
|
|
empty => None,
|
2012-08-03 21:59:04 -05:00
|
|
|
node(@kk, @v, left, right) => {
|
2011-09-02 17:34:58 -05:00
|
|
|
if k == kk {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(v)
|
2011-09-02 17:34:58 -05:00
|
|
|
} else if k < kk { find(left, k) } else { find(right, k) }
|
|
|
|
}
|
2011-08-30 10:38:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Visit all pairs in the map in order.
|
2012-01-23 16:59:00 -06:00
|
|
|
fn traverse<K, V: copy>(m: treemap<K, V>, f: fn(K, V)) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match *m {
|
2012-08-03 21:59:04 -05:00
|
|
|
empty => (),
|
2012-01-30 23:00:57 -06:00
|
|
|
/*
|
|
|
|
Previously, this had what looked like redundant
|
|
|
|
matches to me, so I changed it. but that may be a
|
|
|
|
de-optimization -- tjc
|
|
|
|
*/
|
2012-08-03 21:59:04 -05:00
|
|
|
node(@k, @v, left, right) => {
|
2011-09-02 17:34:58 -05:00
|
|
|
// copy v to make aliases work out
|
|
|
|
let v1 = v;
|
2012-01-30 23:00:57 -06:00
|
|
|
traverse(left, f);
|
2011-09-02 17:34:58 -05:00
|
|
|
f(k, v1);
|
2012-01-30 23:00:57 -06:00
|
|
|
traverse(right, f);
|
2011-08-30 10:38:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|