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
use crate::dep_graph::DepNodeIndex;
use smallvec::SmallVec;
use std::hash::{Hash, Hasher};
use std::ops::Deref;

#[derive(Default, Debug)]
pub(crate) struct EdgesVec {
    max: u32,
    edges: SmallVec<[DepNodeIndex; EdgesVec::INLINE_CAPACITY]>,
}

impl Hash for EdgesVec {
    #[inline]
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        Hash::hash(&self.edges, hasher)
    }
}

impl EdgesVec {
    pub(crate) const INLINE_CAPACITY: usize = 8;

    #[inline]
    pub(crate) fn new() -> Self {
        Self::default()
    }

    #[inline]
    pub(crate) fn push(&mut self, edge: DepNodeIndex) {
        self.max = self.max.max(edge.as_u32());
        self.edges.push(edge);
    }

    #[inline]
    pub(crate) fn max_index(&self) -> u32 {
        self.max
    }
}

impl Deref for EdgesVec {
    type Target = [DepNodeIndex];

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.edges.as_slice()
    }
}

impl FromIterator<DepNodeIndex> for EdgesVec {
    #[inline]
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = DepNodeIndex>,
    {
        let mut vec = EdgesVec::new();
        for index in iter {
            vec.push(index)
        }
        vec
    }
}

impl Extend<DepNodeIndex> for EdgesVec {
    #[inline]
    fn extend<T>(&mut self, iter: T)
    where
        T: IntoIterator<Item = DepNodeIndex>,
    {
        for elem in iter {
            self.push(elem);
        }
    }
}