File size: 5,979 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::hash::Hash;

use rustc_hash::FxHashSet;

use super::VisitedNodes;

/// A graph store is a data structure that will be built up during a graph
/// traversal. It is used to store the results of the traversal.
pub trait GraphStore: Send {
    type Node: Send;
    type Handle: Clone + Send;

    // TODO(alexkirsz) An `entry(from_handle) -> Entry` API would be more
    // efficient, as right now we're getting the same key multiple times.
    /// Inserts a node into the graph store, and returns a handle to it.
    ///
    /// If this method returns `None`, the node edges will not be visited.
    fn insert(
        &mut self,
        from_handle: Option<Self::Handle>,
        node: GraphNode<Self::Node>,
    ) -> Option<(Self::Handle, &Self::Node)>;
}

/// Utility type to ensure that GraphStore::insert can only ever be called from
/// within this module, as a GraphNode can't be constructed outside of it.
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, Ord, PartialOrd)]
pub struct GraphNode<Node>(pub(super) Node);

impl<Node> GraphNode<Node> {
    /// Consumes this `GraphNode` and returns the underlying node.
    pub fn into_node(self) -> Node {
        self.0
    }

    /// Returns a reference the underlying node.
    pub fn node(&self) -> &Node {
        &self.0
    }

    /// Returns a mutable reference the underlying node.
    pub fn node_mut(&mut self) -> &mut Node {
        &mut self.0
    }
}

/// A [`GraphStore`] wrapper that skips nodes that have already been
/// visited.
///
/// This is necessary to avoid repeated work when traversing non-tree
/// graphs (i.e. where a node can have more than one incoming edge).
#[derive(Debug)]
pub struct SkipDuplicates<StoreImpl>
where
    StoreImpl: GraphStore,
{
    store: StoreImpl,
    visited: FxHashSet<StoreImpl::Node>,
}

impl<StoreImpl> SkipDuplicates<StoreImpl>
where
    StoreImpl: GraphStore,
{
    pub fn new(store: StoreImpl) -> Self {
        Self {
            store,
            visited: FxHashSet::default(),
        }
    }

    pub fn new_with_visited_nodes(store: StoreImpl, visited: FxHashSet<StoreImpl::Node>) -> Self {
        Self { store, visited }
    }
}

impl<StoreImpl> GraphStore for SkipDuplicates<StoreImpl>
where
    StoreImpl: GraphStore,
    StoreImpl::Node: Eq + std::hash::Hash + Clone,
{
    type Node = StoreImpl::Node;
    type Handle = StoreImpl::Handle;

    fn insert(
        &mut self,
        from_handle: Option<Self::Handle>,
        node: GraphNode<StoreImpl::Node>,
    ) -> Option<(Self::Handle, &StoreImpl::Node)> {
        if !self.visited.contains(node.node()) {
            self.visited.insert(node.node().clone());
            self.store.insert(from_handle, node)
        } else {
            // Always insert the node into the store, even if we've already
            // visited it. This is necessary to ensure that the store sees all
            // edges.
            self.store.insert(from_handle, node);
            None
        }
    }
}

impl<StoreImpl> SkipDuplicates<StoreImpl>
where
    StoreImpl: GraphStore,
{
    /// Consumes the wrapper and returns the underlying store.
    pub fn into_inner(self) -> StoreImpl {
        self.store
    }

    /// Consumes the wrapper and returns the underlying store along with the visited nodes.
    pub fn into_inner_with_visited(self) -> (StoreImpl, VisitedNodes<StoreImpl::Node>) {
        (self.store, VisitedNodes(self.visited))
    }
}

/// A [`GraphStore`] wrapper that skips nodes that have already been
/// visited, based on a key extracted from the node.
///
/// This is necessary to avoid repeated work when traversing non-tree
/// graphs (i.e. where a node can have more than one incoming edge).
#[derive(Debug)]
pub struct SkipDuplicatesWithKey<StoreImpl, Key, KeyExtractor>
where
    StoreImpl: GraphStore,
    Key: Send + Eq + Hash,
    KeyExtractor: Send + Fn(&StoreImpl::Node) -> &Key,
{
    store: StoreImpl,
    visited: FxHashSet<Key>,
    key_extractor: KeyExtractor,
}

impl<StoreImpl, Key, KeyExtractor> SkipDuplicatesWithKey<StoreImpl, Key, KeyExtractor>
where
    StoreImpl: GraphStore,
    Key: Send + Eq + std::hash::Hash + Clone,
    KeyExtractor: Send + Fn(&StoreImpl::Node) -> &Key,
{
    pub fn new(store: StoreImpl, key_extractor: KeyExtractor) -> Self {
        Self {
            store,
            visited: FxHashSet::default(),
            key_extractor,
        }
    }
}

impl<StoreImpl, Key, KeyExtractor> GraphStore
    for SkipDuplicatesWithKey<StoreImpl, Key, KeyExtractor>
where
    StoreImpl: GraphStore,
    StoreImpl::Node: Eq + std::hash::Hash + Clone,
    Key: Send + Eq + std::hash::Hash + Clone,
    KeyExtractor: Send + Fn(&StoreImpl::Node) -> &Key,
{
    type Node = StoreImpl::Node;
    type Handle = StoreImpl::Handle;

    fn insert(
        &mut self,
        from_handle: Option<Self::Handle>,
        node: GraphNode<StoreImpl::Node>,
    ) -> Option<(Self::Handle, &StoreImpl::Node)> {
        let key = (self.key_extractor)(node.node());

        if !self.visited.contains(key) {
            self.visited.insert(key.clone());
            self.store.insert(from_handle, node)
        } else {
            // Always insert the node into the store, even if we've already
            // visited it. This is necessary to ensure that the store sees all
            // edges.
            self.store.insert(from_handle, node);
            None
        }
    }
}

impl<StoreImpl, Key, KeyExtractor> SkipDuplicatesWithKey<StoreImpl, Key, KeyExtractor>
where
    StoreImpl: GraphStore,
    Key: Send + Eq + std::hash::Hash + Clone,
    KeyExtractor: Send + Fn(&StoreImpl::Node) -> &Key,
{
    /// Consumes the wrapper and returns the underlying store.
    pub fn into_inner(self) -> StoreImpl {
        self.store
    }

    /// Consumes the wrapper and returns the underlying store along with the visited nodes.
    pub fn into_inner_with_visited(self) -> (StoreImpl, VisitedNodes<Key>) {
        (self.store, VisitedNodes(self.visited))
    }
}