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
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use self::super::DirectedWeightedGraph;
use std::slice::{Iter, IterMut};

impl<'g, N, W> DirectedWeightedGraph<'g, N, W> for Vec<(N, N, W)>
where
    N: Eq + 'g,
    W: 'g,
{
    type AllWeightedEdges = AllWeightedEdges<'g, N, W>;
    type AllWeightedEdgesMut = AllWeightedEdgesMut<'g, N, W>;

    fn all_edges(&'g self) -> Self::AllWeightedEdges {
        AllWeightedEdges { iter: self.iter() }
    }

    fn all_edges_mut(&'g mut self) -> Self::AllWeightedEdgesMut {
        AllWeightedEdgesMut {
            iter: self.iter_mut(),
        }
    }

    /// Adds an edge to the graph.
    ///
    /// # Complexity
    /// This implementation runs in O(|E|) time. Running in linear time would risk this graph of
    /// becoming a multigraph, where there can be multiple edges between the same nodes.
    fn add_edge(&'g mut self, source: N, sink: N, weight: W) -> Option<(N, N, W)> {
        if let Some(old_weight) = self.edge_weight_mut(&source, &sink) {
            return Some((source, sink, ::std::mem::replace(old_weight, weight)));
        }

        self.push((source, sink, weight));
        None
    }

    fn remove_edge(&'g mut self, source: &N, sink: &N) -> Option<(N, N, W)> {
        for index in 0..self.len() {
            {
                let &(ref current_source, ref current_sink, _) = &self[index];
                if current_source != source || current_sink != sink {
                    continue;
                }
            }
            return Some(self.remove(index));
        }

        None
    }
}

#[derive(Debug, Clone)]
pub struct AllWeightedEdges<'g, N, W>
where
    N: 'g,
    W: 'g,
{
    iter: Iter<'g, (N, N, W)>,
}

impl<'g, N, W> Iterator for AllWeightedEdges<'g, N, W> {
    type Item = (&'g N, &'g N, &'g W);

    fn next(&mut self) -> Option<Self::Item> {
        self.iter
            .next()
            .map(|&(ref source, ref sink, ref weight)| (source, sink, weight))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<'g, N, W> ExactSizeIterator for AllWeightedEdges<'g, N, W> {
    fn len(&self) -> usize {
        self.iter.len()
    }
}

#[derive(Debug)]
pub struct AllWeightedEdgesMut<'g, N, W>
where
    N: 'g,
    W: 'g,
{
    iter: IterMut<'g, (N, N, W)>,
}

impl<'g, N, W> Iterator for AllWeightedEdgesMut<'g, N, W> {
    type Item = (&'g N, &'g N, &'g mut W);

    fn next(&mut self) -> Option<Self::Item> {
        self.iter
            .next()
            .map(|&mut (ref source, ref sink, ref mut weight)| {
                (source, sink, weight)
            })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

#[cfg(test)]
mod tests {
    use DirectedWeightedGraph;

    #[test]
    fn test_add_edge() {
        let mut graph = Vec::new();

        graph.add_edge(0, 1, 1.0);
        graph.add_edge(1, 1, 2.0);
        graph.add_edge(1, 2, 3.0);

        assert!(graph.has_edge(&0, &1));
        assert!(graph.has_edge(&1, &1));
        assert!(graph.has_edge(&1, &2));
        assert!(!graph.has_edge(&0, &0));
        assert!(!graph.has_edge(&0, &2));
        assert!(!graph.has_edge(&1, &0));
        assert!(!graph.has_edge(&1, &4));

        assert_eq!(graph.edge_weight(&0, &1), Some(&1.0));
        assert_eq!(graph.edge_weight(&1, &1), Some(&2.0));
        assert_eq!(graph.edge_weight(&1, &2), Some(&3.0));

        assert_eq!(graph.all_edges().len(), 3);
    }
}