-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathpathfinding.rs
More file actions
283 lines (247 loc) · 9.13 KB
/
pathfinding.rs
File metadata and controls
283 lines (247 loc) · 9.13 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use crate::errors::{GrimpError, GrimpResult};
use crate::graph::{EMPTY_MODULE_TOKENS, Graph, ModuleToken};
use indexmap::{IndexMap, IndexSet};
use itertools::Itertools;
use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
use slotmap::SecondaryMap;
use std::hash::BuildHasherDefault;
type FxIndexSet<K> = IndexSet<K, BuildHasherDefault<FxHasher>>;
type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
pub fn find_reach(
imports_map: &SecondaryMap<ModuleToken, FxHashSet<ModuleToken>>,
from_modules: &FxHashSet<ModuleToken>,
) -> FxHashSet<ModuleToken> {
let mut seen = FxIndexSet::default();
seen.extend(from_modules.iter().cloned());
let mut i = 0;
while let Some(module) = seen.get_index(i) {
for next_module in imports_map.get(*module).unwrap_or(&EMPTY_MODULE_TOKENS) {
if !seen.contains(next_module) {
seen.insert(*next_module);
}
}
i += 1;
}
&seen.into_iter().collect::<FxHashSet<_>>() - from_modules
}
/// Finds the shortest path, via a bidirectional BFS.
pub fn find_shortest_path(
graph: &Graph,
from_modules: &FxHashSet<ModuleToken>,
to_modules: &FxHashSet<ModuleToken>,
excluded_modules: &FxHashSet<ModuleToken>,
excluded_imports: &FxHashMap<ModuleToken, FxHashSet<ModuleToken>>,
) -> GrimpResult<Option<Vec<ModuleToken>>> {
if !(from_modules & to_modules).is_empty() {
return Err(GrimpError::SharedDescendants);
}
let predecessors: FxIndexMap<ModuleToken, Option<ModuleToken>> = from_modules
.clone()
.into_iter()
.map(|m| (m, None))
.collect();
let successors: FxIndexMap<ModuleToken, Option<ModuleToken>> =
to_modules.clone().into_iter().map(|m| (m, None)).collect();
_find_shortest_path(
graph,
predecessors,
successors,
excluded_modules,
excluded_imports,
false,
)
}
/// Finds the shortest cycle from `modules` to `modules`, via a bidirectional BFS.
pub fn find_shortest_cycle(
graph: &Graph,
modules: &[ModuleToken],
excluded_modules: &FxHashSet<ModuleToken>,
excluded_imports: &FxHashMap<ModuleToken, FxHashSet<ModuleToken>>,
) -> GrimpResult<Option<Vec<ModuleToken>>> {
// Exclude imports internal to `modules`
let mut excluded_imports = excluded_imports.clone();
for (m1, m2) in modules.iter().tuple_combinations() {
excluded_imports.entry(*m1).or_default().insert(*m2);
excluded_imports.entry(*m2).or_default().insert(*m1);
}
let predecessors: FxIndexMap<ModuleToken, Option<ModuleToken>> =
modules.iter().cloned().map(|m| (m, None)).collect();
let successors: FxIndexMap<ModuleToken, Option<ModuleToken>> = predecessors.clone();
_find_shortest_path(
graph,
predecessors,
successors,
excluded_modules,
&excluded_imports,
true,
)
}
fn _find_shortest_path(
graph: &Graph,
mut predecessors: FxIndexMap<ModuleToken, Option<ModuleToken>>,
mut successors: FxIndexMap<ModuleToken, Option<ModuleToken>>,
excluded_modules: &FxHashSet<ModuleToken>,
excluded_imports: &FxHashMap<ModuleToken, FxHashSet<ModuleToken>>,
sorted: bool,
) -> GrimpResult<Option<Vec<ModuleToken>>> {
let mut i_forwards = 0;
let mut i_backwards = 0;
let middle = 'l: loop {
for _ in 0..(predecessors.len() - i_forwards) {
let module = *predecessors.get_index(i_forwards).unwrap().0;
let mut next_modules: Vec<ModuleToken> =
graph.imports.get(module).unwrap().iter().cloned().collect();
if sorted {
next_modules
.sort_by_key(|next_module| graph.get_module(*next_module).unwrap().name());
}
for next_module in next_modules {
if import_is_excluded(&module, &next_module, excluded_modules, excluded_imports) {
continue;
}
if !predecessors.contains_key(&next_module) {
predecessors.insert(next_module, Some(module));
}
if successors.contains_key(&next_module) {
break 'l Some(next_module);
}
}
i_forwards += 1;
}
for _ in 0..(successors.len() - i_backwards) {
let module = *successors.get_index(i_backwards).unwrap().0;
let mut next_modules: Vec<ModuleToken> = graph
.reverse_imports
.get(module)
.unwrap()
.iter()
.cloned()
.collect();
if sorted {
next_modules
.sort_by_key(|next_module| graph.get_module(*next_module).unwrap().name());
}
for next_module in next_modules {
if import_is_excluded(&next_module, &module, excluded_modules, excluded_imports) {
continue;
}
if !successors.contains_key(&next_module) {
successors.insert(next_module, Some(module));
}
if predecessors.contains_key(&next_module) {
break 'l Some(next_module);
}
}
i_backwards += 1;
}
if i_forwards == predecessors.len() && i_backwards == successors.len() {
break 'l None;
}
};
Ok(middle.map(|middle| {
// Path found!
// Build the path.
let mut path = vec![];
let mut node = Some(middle);
while let Some(n) = node {
path.push(n);
node = *predecessors.get(&n).unwrap();
}
path.reverse();
let mut node = *successors.get(path.last().unwrap()).unwrap();
while let Some(n) = node {
path.push(n);
node = *successors.get(&n).unwrap();
}
path
}))
}
fn import_is_excluded(
from_module: &ModuleToken,
to_module: &ModuleToken,
excluded_modules: &FxHashSet<ModuleToken>,
excluded_imports: &FxHashMap<ModuleToken, FxHashSet<ModuleToken>>,
) -> bool {
if excluded_modules.contains(to_module) {
true
} else {
excluded_imports
.get(from_module)
.unwrap_or(&EMPTY_MODULE_TOKENS)
.contains(to_module)
}
}
#[cfg(test)]
mod test_find_shortest_cycle {
use super::*;
#[test]
fn test_finds_cycle_single_module() -> GrimpResult<()> {
let mut graph = Graph::default();
let foo = graph.get_or_add_module("foo").token;
let bar = graph.get_or_add_module("bar").token;
let baz = graph.get_or_add_module("baz").token;
let x = graph.get_or_add_module("x").token;
let y = graph.get_or_add_module("y").token;
let z = graph.get_or_add_module("z").token;
// Shortest cycle
graph.add_import(foo, bar);
graph.add_import(bar, baz);
graph.add_import(baz, foo);
// Longer cycle
graph.add_import(foo, x);
graph.add_import(x, y);
graph.add_import(y, z);
graph.add_import(z, foo);
let path =
find_shortest_cycle(&graph, &[foo], &FxHashSet::default(), &FxHashMap::default())?;
assert_eq!(path, Some(vec![foo, bar, baz, foo]));
graph.remove_import(baz, foo);
let path =
find_shortest_cycle(&graph, &[foo], &FxHashSet::default(), &FxHashMap::default())?;
assert_eq!(path, Some(vec![foo, x, y, z, foo]));
Ok(())
}
#[test]
fn test_returns_none_if_no_cycle() -> GrimpResult<()> {
let mut graph = Graph::default();
let foo = graph.get_or_add_module("foo").token;
let bar = graph.get_or_add_module("bar").token;
let baz = graph.get_or_add_module("baz").token;
graph.add_import(foo, bar);
graph.add_import(bar, baz);
let path =
find_shortest_cycle(&graph, &[foo], &FxHashSet::default(), &FxHashMap::default())?;
assert_eq!(path, None);
Ok(())
}
#[test]
fn test_finds_cycle_multiple_module() -> GrimpResult<()> {
let mut graph = Graph::default();
graph.get_or_add_module("colors");
let red = graph.get_or_add_module("colors.red").token;
let blue = graph.get_or_add_module("colors.blue").token;
let a = graph.get_or_add_module("a").token;
let b = graph.get_or_add_module("b").token;
let c = graph.get_or_add_module("c").token;
let d = graph.get_or_add_module("d").token;
// The computation should not be confused by these two imports internal to `modules`.
graph.add_import(red, blue);
graph.add_import(blue, red);
// This is the part we expect to find.
graph.add_import(red, a);
graph.add_import(a, b);
graph.add_import(b, blue);
// A longer path.
graph.add_import(a, c);
graph.add_import(c, d);
graph.add_import(d, b);
let path = find_shortest_cycle(
&graph,
&Vec::from_iter([red, blue]),
&FxHashSet::default(),
&FxHashMap::default(),
)?;
assert_eq!(path, Some(vec![red, a, b, blue]));
Ok(())
}
}