Skip to content

Instantly share code, notes, and snippets.

@Tristramg
Created May 1, 2013 11:29
Show Gist options
  • Save Tristramg/5494814 to your computer and use it in GitHub Desktop.
Save Tristramg/5494814 to your computer and use it in GitHub Desktop.
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