// 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 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. /*! * 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. */ use core::cmp::{Eq, Ord}; use core::option::{Some, None}; use core::prelude::*; pub type Treemap = @TreeNode; enum TreeNode { Empty, Node(@K, @V, @TreeNode, @TreeNode) } /// Create a treemap pub fn init() -> Treemap { @Empty } /// Insert a value into the map pub fn insert(m: Treemap, k: K, v: V) -> Treemap { @match m { @Empty => Node(@k, @v, @Empty, @Empty), @Node(@copy kk, vv, left, right) => { 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)) } } } } /// Find a value based on the key pub fn find(m: Treemap, k: K) -> Option { match *m { Empty => None, Node(@ref kk, @copy v, left, right) => { if k == *kk { Some(v) } else if k < *kk { find(left, k) } else { find(right, k) } } } } /// Visit all pairs in the map in order. pub fn traverse(m: Treemap, f: &fn(&K, &V)) { match *m { 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) => { traverse(left, f); f(k, v); traverse(right, f); } } }