2012-09-25 19:02:22 -05:00
|
|
|
#[warn(deprecated_mode)];
|
2012-08-11 09:08:42 -05:00
|
|
|
|
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-09-04 13:23:53 -05:00
|
|
|
use core::cmp::{Eq, Ord};
|
|
|
|
use option::{Some, None};
|
|
|
|
use option = option;
|
2011-08-30 10:38:14 -05:00
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
export Treemap;
|
2011-08-30 10:38:14 -05:00
|
|
|
export init;
|
|
|
|
export insert;
|
|
|
|
export find;
|
|
|
|
export traverse;
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
type Treemap<K, V> = @TreeNode<K, V>;
|
2011-10-24 17:25:41 -05:00
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
enum TreeNode<K, V> {
|
|
|
|
Empty,
|
|
|
|
Node(@K, @V, @TreeNode<K, V>, @TreeNode<K, V>)
|
2011-08-30 10:38:14 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Create a treemap
|
2012-08-11 09:08:42 -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-09-07 16:52:28 -05:00
|
|
|
fn insert<K: Copy Eq Ord, V: Copy>(m: Treemap<K, V>, +k: K, +v: V)
|
2012-08-31 17:54:01 -05:00
|
|
|
-> Treemap<K, V> {
|
2012-08-06 14:34:08 -05:00
|
|
|
@match m {
|
2012-08-11 09:08:42 -05:00
|
|
|
@Empty => Node(@k, @v, @Empty, @Empty),
|
2012-09-28 02:22:18 -05:00
|
|
|
@Node(@copy kk, vv, left, right) => {
|
2011-09-02 17:34:58 -05:00
|
|
|
if k < kk {
|
2012-08-11 09:08:42 -05:00
|
|
|
Node(@kk, vv, insert(left, k, v), right)
|
2011-09-02 17:34:58 -05:00
|
|
|
} else if k == kk {
|
2012-08-11 09:08:42 -05:00
|
|
|
Node(@kk, @v, left, right)
|
|
|
|
} else { Node(@kk, vv, left, insert(right, k, v)) }
|
2011-09-02 17:34:58 -05:00
|
|
|
}
|
|
|
|
}
|
2011-08-30 10:38:14 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Find a value based on the key
|
2012-09-07 16:52:28 -05:00
|
|
|
fn find<K: Eq Ord, V: Copy>(m: Treemap<K, V>, +k: K) -> Option<V> {
|
2012-08-06 14:34:08 -05:00
|
|
|
match *m {
|
2012-08-11 09:08:42 -05:00
|
|
|
Empty => None,
|
2012-09-28 02:22:18 -05:00
|
|
|
Node(@ref kk, @copy v, left, right) => {
|
|
|
|
if k == *kk {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(v)
|
2012-09-28 02:22:18 -05:00
|
|
|
} else if k < *kk { find(left, move k) } else { find(right, move k) }
|
2011-09-02 17:34:58 -05:00
|
|
|
}
|
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-09-26 17:59:37 -05: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-11 09:08:42 -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-09-28 02:22:18 -05:00
|
|
|
Node(@ref k, @ref v, left, right) => {
|
2012-01-30 23:00:57 -06:00
|
|
|
traverse(left, f);
|
2012-09-28 02:22:18 -05:00
|
|
|
f(k, v);
|
2012-01-30 23:00:57 -06:00
|
|
|
traverse(right, f);
|
2011-08-30 10:38:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|