Created
May 1, 2013 11:29
-
-
Save Tristramg/5494814 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern mod core; | |
use core::hashmap::HashMap; | |
use core::hashmap::linear_map_with_capacity; | |
// #[crate_type = "lib"]; | |
trait Graph<Node, Edge> { | |
fn children(&self, Node, &fn(&(Edge, Node)) -> bool); // iterate on outgoing edges | |
} | |
impl<E> Graph<int,E> for HashMap<int, ~[(E,int)]> { | |
fn children(&self, n:int, f : &fn(&(E,int)) -> bool) { | |
match self.find(&n) { | |
None => (), | |
Some(list) => list.each(f) | |
}; | |
} | |
} | |
#[test] | |
fn test_1() { | |
let g : ~HashMap<int, ~[(int,int)]> = ~linear_map_with_capacity(15); | |
let g2 : ~Graph<int,int> = g as ~Graph<int,int>; | |
g2.children(5, |&(e,x)| { io::println(fmt!("got edge from 5: %d -> %d", e, x)); true }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment