2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
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
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-09-04 13:23:53 -05:00
|
|
|
use core::cmp::{Eq, Ord};
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::option::{Some, None};
|
2011-08-30 10:38:14 -05:00
|
|
|
|
2012-10-02 14:02:59 -05:00
|
|
|
pub 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-10-02 14:02:59 -05:00
|
|
|
pub 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
|
2013-05-29 18:59:33 -05:00
|
|
|
pub fn insert<K:Eq + Ord,V>(m: Treemap<K, V>, k: K, v: V) -> Treemap<K, V> {
|
2012-08-06 14:34:08 -05:00
|
|
|
@match m {
|
2013-05-08 10:38:39 -05:00
|
|
|
@Empty => Node(@k, @v, @Empty, @Empty),
|
2013-05-29 18:59:33 -05:00
|
|
|
@Node(kk, vv, left, right) => cond!(
|
|
|
|
(k < *kk) { Node(kk, vv, insert(left, k, v), right) }
|
|
|
|
(k == *kk) { Node(kk, @v, left, right) }
|
|
|
|
_ { Node(kk, vv, left, insert(right, k, v)) }
|
2013-05-08 10:38:39 -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
|
2013-02-20 19:07:17 -06:00
|
|
|
pub 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 {
|
2013-05-08 10:38:39 -05:00
|
|
|
Empty => None,
|
2013-05-29 18:59:33 -05:00
|
|
|
Node(kk, v, left, right) => cond!(
|
|
|
|
(k == *kk) { Some(copy *v) }
|
2013-05-10 00:01:27 -05:00
|
|
|
(k < *kk) { find(left, k) }
|
2013-05-08 10:38:39 -05:00
|
|
|
_ { 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.
|
2013-03-07 16:38:38 -06:00
|
|
|
pub fn traverse<K, V: Copy>(m: Treemap<K, V>, f: &fn(&K, &V)) {
|
2012-08-06 14:34:08 -05:00
|
|
|
match *m {
|
2013-05-08 10:38:39 -05:00
|
|
|
Empty => (),
|
|
|
|
// Previously, this had what looked like redundant
|
|
|
|
// matches to me, so I changed it. but that may be a
|
|
|
|
// de-optimization -- tjc
|
|
|
|
Node(@ref k, @ref v, left, right) => {
|
2013-06-21 19:08:35 -05:00
|
|
|
traverse(left, |k,v| f(k,v));
|
2013-05-08 10:38:39 -05:00
|
|
|
f(k, v);
|
2013-06-21 19:08:35 -05:00
|
|
|
traverse(right, |k,v| f(k,v));
|
2013-05-08 10:38:39 -05:00
|
|
|
}
|
2011-08-30 10:38:14 -05:00
|
|
|
}
|
|
|
|
}
|