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
use super::Graph;

#[derive(Debug, Clone)]
pub struct EdgeVec(Vec<bool>);
#[derive(Debug, Clone)]
pub struct BitGraph(Vec<EdgeVec>);
impl EdgeVec {
    pub fn iter(&self) -> ::std::slice::Iter<bool> {
        self.0.iter()
    }
    pub fn iter_mut(&mut self) -> ::std::slice::IterMut<bool> {
        self.0.iter_mut()
    }
}
impl Graph for BitGraph {
    type Node = usize;
    fn nb_nodes(&self) -> usize {
        self.0.len()
    }
    fn nodes(&self) -> Box<Iterator<Item = Self::Node>> {
        Box::new((0..self.nb_nodes()).into_iter())
    }
    fn get(&self, from: &Self::Node, to: &Self::Node) -> bool {
        self[*from][*to]
    }
    fn set_edge(&mut self, from: &Self::Node, to: &Self::Node, b: bool) {
        self[*from][*to] = b;
    }
}
impl BitGraph {
    pub fn dependents_mut<'a>(&'a mut self, node: usize) -> Box<'a + Iterator<Item = (usize, &'a mut bool)>> {
        let iter = self[node].iter_mut().enumerate().filter(|&(_, ref b)| **b).map(|(idx, b)| (idx, b));
        Box::new(iter)
    }
}
impl ::std::ops::Index<usize> for BitGraph {
    type Output = EdgeVec;
    fn index(&self, n: usize) -> &Self::Output {
        &self.0[n]
    }
}
impl ::std::ops::IndexMut<usize> for BitGraph {
    fn index_mut(&mut self, n: usize) -> &mut Self::Output {
        &mut self.0[n]
    }
}

impl ::std::ops::Index<usize> for EdgeVec {
    type Output = bool;
    fn index(&self, n: usize) -> &Self::Output {
        &self.0[n]
    }
}
impl ::std::ops::IndexMut<usize> for EdgeVec {
    fn index_mut(&mut self, n: usize) -> &mut Self::Output {
        &mut self.0[n]
    }
}