Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::treemap - changing types to reflect constraints, adding equality check #3662

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 49 additions & 12 deletions src/libstd/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@ use core::cmp::{Eq, Ord};
use core::option::{Some, None};
use Option = core::Option;

pub type TreeMap<K, V> = @mut TreeEdge<K, V>;
pub type TreeMap<K: Copy Eq Ord, V: Copy> = @mut TreeEdge<K, V>;

type TreeEdge<K, V> = Option<@TreeNode<K, V>>;
type TreeEdge<K: Copy Eq Ord, V: Copy> = Option<@TreeNode<K, V>>;

enum TreeNode<K, V> = {
struct TreeNode<K: Copy Eq Ord, V: Copy> {
key: K,
mut value: V,
mut left: TreeEdge<K, V>,
mut right: TreeEdge<K, V>
};
}

/// Create a treemap
pub fn TreeMap<K, V>() -> TreeMap<K, V> { @mut None }
pub fn TreeMap<K: Copy Eq Ord, V: Copy>() -> TreeMap<K, V> { @mut None }

/// Insert a value into the map
pub fn insert<K: Copy Eq Ord, V: Copy>(m: &mut TreeEdge<K, V>, +k: K, +v: V) {
pub fn insert<K: Copy Eq Ord, V: Copy>(m: &mut TreeEdge<K, V>,
+k: K, +v: V) {
match copy *m {
None => {
*m = Some(@TreeNode({key: k,
mut value: v,
mut left: None,
mut right: None}));
*m = Some(@TreeNode {key: k,
mut value: v,
mut left: None,
mut right: None});
return;
}
Some(node) => {
Expand All @@ -49,7 +50,7 @@ pub fn insert<K: Copy Eq Ord, V: Copy>(m: &mut TreeEdge<K, V>, +k: K, +v: V) {

/// Find a value based on the key
pub fn find<K: Copy Eq Ord, V: Copy>(m: &const TreeEdge<K, V>, +k: K)
-> Option<V> {
-> Option<V> {
match copy *m {
None => None,

Expand All @@ -67,7 +68,8 @@ pub fn find<K: Copy Eq Ord, V: Copy>(m: &const TreeEdge<K, V>, +k: K)
}

/// Visit all pairs in the map in order.
pub fn traverse<K, V: Copy>(m: &const TreeEdge<K, V>, f: fn((&K), (&V))) {
pub fn traverse<K: Copy Eq Ord, V: Copy>(m: &const TreeEdge<K, V>,
f: fn((&K), (&V))) {
match copy *m {
None => (),
Some(node) => {
Expand All @@ -79,6 +81,19 @@ pub fn traverse<K, V: Copy>(m: &const TreeEdge<K, V>, f: fn((&K), (&V))) {
}
}

/// Compare two treemaps and return true iff
/// they contain same keys and values
pub fn equals<K: Copy Eq Ord, V: Copy Eq>(t1: &const TreeEdge<K, V>,
t2: &const TreeEdge<K, V>)
-> bool {
let mut v1 = ~[];
let mut v2 = ~[];
traverse(t1, |k,v| { v1.push((copy *k, copy *v)) });
traverse(t2, |k,v| { v2.push((copy *k, copy *v)) });
return v1 == v2;
}


#[cfg(test)]
mod tests {
#[legacy_exports];
Expand Down Expand Up @@ -127,6 +142,28 @@ mod tests {
traverse(m, |x,y| t(n, *x, *y));
}

#[test]
fn equality() {
let m1 = TreeMap();
insert(m1, 3, ());
insert(m1, 0, ());
insert(m1, 4, ());
insert(m1, 2, ());
insert(m1, 1, ());
let m2 = TreeMap();
insert(m2, 2, ());
insert(m2, 1, ());
insert(m2, 3, ());
insert(m2, 0, ());
insert(m2, 4, ());

assert equals(m1, m2);

let m3 = TreeMap();
assert !equals(m1,m3);

}

#[test]
fn u8_map() {
let m = TreeMap();
Expand Down