Skip to content

Instantly share code, notes, and snippets.

@eholk
Created August 25, 2011 23:56
Show Gist options
  • Select an option

  • Save eholk/1172347 to your computer and use it in GitHub Desktop.

Select an option

Save eholk/1172347 to your computer and use it in GitHub Desktop.
tag tree_node<@K, @V> {
empty;
node(@K, @V, treemap<K, V>, treemap<K, V>);
}
type treemap<@K, @V> = @mutable tree_node<K, V>;
fn init<@K, @V>() -> treemap<K,V> { @mutable empty }
fn insert<@K, @V>(m : &treemap<K, V>, k : &K, v : &V) {
alt m {
@empty. {
*m = node(@k, @v, @mutable empty, @mutable empty);
}
@node(@kk, _, _, _) {
// We have to name left and right individually, because
// otherwise the alias checker complains.
if k < kk {
alt m {
@node(_, _, left, _) {
insert(left, k, v);
}
}
}
else {
alt m {
@node(_, _, _, right) {
insert(right, k, v);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment