1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
extern crate collections_rs as collections;
pub mod vec;
mod map;
pub use map::MapGraph;
pub trait DirectedWeightedGraph<'g, N, W>
where
N: 'g + Eq,
W: 'g,
{
type AllWeightedEdges: Iterator<Item = (&'g N, &'g N, &'g W)>;
type AllWeightedEdgesMut: Iterator<Item = (&'g N, &'g N, &'g mut W)>;
fn all_edges(&'g self) -> Self::AllWeightedEdges;
fn all_edges_mut(&'g mut self) -> Self::AllWeightedEdgesMut;
fn add_edge(&'g mut self, N, N, W) -> Option<(N, N, W)>;
fn remove_edge(&'g mut self, &N, &N) -> Option<(N, N, W)>;
fn has_edge(&'g self, source: &N, sink: &N) -> bool {
self.edge_weight(source, sink).is_some()
}
fn edge_weight(&'g self, source: &N, sink: &N) -> Option<&W> {
self.all_edges()
.find(|&(n1, n2, _)| n1 == source && n2 == sink)
.map(|(_, _, weight)| weight)
}
fn edge_weight_mut(&'g mut self, source: &N, sink: &N) -> Option<&mut W> {
self.all_edges_mut()
.find(|&(n1, n2, _)| n1 == source && n2 == sink)
.map(|(_, _, weight)| weight)
}
}