Debian Codegraph @

core/src/dag_walk.rs

raw
1 // Copyright 2020 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! General-purpose DAG algorithms.
16
17 use std::collections::HashSet;
18 use std::convert::Infallible;
19 use std::hash::Hash;
20 use std::iter;
21
22 use itertools::Itertools as _;
23
24 /// Traverses nodes from `start` in depth-first order.
25 pub fn dfs<T, ID, II, NI>(
26 start: II,
27 id_fn: impl Fn(&T) -> ID,
28 mut neighbors_fn: impl FnMut(&T) -> NI,
29 ) -> impl Iterator<Item = T>
30 where
31 ID: Hash + Eq,
32 II: IntoIterator<Item = T>,
33 NI: IntoIterator<Item = T>,
34 {
35 let neighbors_fn = move |node: &T| to_infallible_iter(neighbors_fn(node));
36 dfs_ok(to_infallible_iter(start), id_fn, neighbors_fn).map(|Ok(node)| node)
37 }
38
39 /// Traverses nodes from `start` in depth-first order.
40 ///
41 /// An `Err` is emitted as a node with no neighbors. Caller may decide to
42 /// short-circuit on it.
43 pub fn dfs_ok<T, ID, E, II, NI>(
44 start: II,
45 id_fn: impl Fn(&T) -> ID,
46 mut neighbors_fn: impl FnMut(&T) -> NI,
47 ) -> impl Iterator<Item = Result<T, E>>
48 where
49 ID: Hash + Eq,
50 II: IntoIterator<Item = Result<T, E>>,
51 NI: IntoIterator<Item = Result<T, E>>,
52 {
53 let mut work: Vec<Result<T, E>> = start.into_iter().collect();
54 let mut visited: HashSet<ID> = HashSet::new();
55 iter::from_fn(move || {
56 loop {
57 let c = match work.pop() {
58 Some(Ok(c)) => c,
59 r @ (Some(Err(_)) | None) => return r,
60 };
61 let id = id_fn(&c);
62 if visited.contains(&id) {
63 continue;
64 }
65 for p in neighbors_fn(&c) {
66 work.push(p);
67 }
68 visited.insert(id);
69 return Some(Ok(c));
70 }
71 })
72 }
73
74 /// Builds a list of nodes reachable from the `start` where neighbors come
75 /// before the node itself.
76 ///
77 /// If the graph has cycle, `cycle_fn()` is called with one of the nodes
78 /// involved in the cycle.
79 pub fn topo_order_forward<T, ID, E, II, NI>(
80 start: II,
81 id_fn: impl Fn(&T) -> ID,
82 mut neighbors_fn: impl FnMut(&T) -> NI,
83 cycle_fn: impl FnOnce(T) -> E,
84 ) -> Result<Vec<T>, E>
85 where
86 ID: Hash + Eq + Clone,
87 II: IntoIterator<Item = T>,
88 NI: IntoIterator<Item = T>,
89 {
90 let neighbors_fn = move |node: &T| to_ok_iter(neighbors_fn(node));
91 topo_order_forward_ok(to_ok_iter(start), id_fn, neighbors_fn, cycle_fn)
92 }
93
94 /// Builds a list of `Ok` nodes reachable from the `start` where neighbors come
95 /// before the node itself.
96 ///
97 /// If `start` or `neighbors_fn()` yields an `Err`, this function terminates and
98 /// returns the error. If the graph has cycle, `cycle_fn()` is called with one
99 /// of the nodes involved in the cycle.
100 pub fn topo_order_forward_ok<T, ID, E, II, NI>(
101 start: II,
102 id_fn: impl Fn(&T) -> ID,
103 mut neighbors_fn: impl FnMut(&T) -> NI,
104 cycle_fn: impl FnOnce(T) -> E,
105 ) -> Result<Vec<T>, E>
106 where
107 ID: Hash + Eq + Clone,
108 II: IntoIterator<Item = Result<T, E>>,
109 NI: IntoIterator<Item = Result<T, E>>,
110 {
111 let mut stack: Vec<(T, bool)> = start.into_iter().map(|r| Ok((r?, false))).try_collect()?;
112 let mut visiting = HashSet::new();
113 let mut emitted = HashSet::new();
114 let mut result = vec![];
115 while let Some((node, neighbors_visited)) = stack.pop() {
116 let id = id_fn(&node);
117 if emitted.contains(&id) {
118 continue;
119 }
120 if !neighbors_visited {
121 if !visiting.insert(id.clone()) {
122 return Err(cycle_fn(node));
123 }
124 let neighbors_iter = neighbors_fn(&node).into_iter();
125 stack.reserve(neighbors_iter.size_hint().0 + 1);
126 stack.push((node, true));
127 for neighbor in neighbors_iter {
128 stack.push((neighbor?, false));
129 }
130 } else {
131 visiting.remove(&id);
132 emitted.insert(id);
133 result.push(node);
134 }
135 }
136 Ok(result)
137 }
138
139 /// Builds a list of nodes reachable from the `start` where neighbors come after
140 /// the node itself.
141 ///
142 /// If the graph has cycle, `cycle_fn()` is called with one of the nodes
143 /// involved in the cycle.
144 pub fn topo_order_reverse<T, ID, E, II, NI>(
145 start: II,
146 id_fn: impl Fn(&T) -> ID,
147 mut neighbors_fn: impl FnMut(&T) -> NI,
148 cycle_fn: impl FnOnce(T) -> E,
149 ) -> Result<Vec<T>, E>
150 where
151 ID: Hash + Eq + Clone,
152 II: IntoIterator<Item = T>,
153 NI: IntoIterator<Item = T>,
154 {
155 let neighbors_fn = move |node: &T| to_ok_iter(neighbors_fn(node));
156 topo_order_reverse_ok(to_ok_iter(start), id_fn, neighbors_fn, cycle_fn)
157 }
158
159 /// Builds a list of `Ok` nodes reachable from the `start` where neighbors come
160 /// after the node itself.
161 ///
162 /// If `start` or `neighbors_fn()` yields an `Err`, this function terminates and
163 /// returns the error. If the graph has cycle, `cycle_fn()` is called with one
164 /// of the nodes involved in the cycle.
165 pub fn topo_order_reverse_ok<T, ID, E, II, NI>(
166 start: II,
167 id_fn: impl Fn(&T) -> ID,
168 neighbors_fn: impl FnMut(&T) -> NI,
169 cycle_fn: impl FnOnce(T) -> E,
170 ) -> Result<Vec<T>, E>
171 where
172 ID: Hash + Eq + Clone,
173 II: IntoIterator<Item = Result<T, E>>,
174 NI: IntoIterator<Item = Result<T, E>>,
175 {
176 let mut result = topo_order_forward_ok(start, id_fn, neighbors_fn, cycle_fn)?;
177 result.reverse();
178 Ok(result)
179 }
180
181 /// Find nodes in the start set that are not reachable from other nodes in the
182 /// start set.
183 pub fn heads<T, ID, II, NI>(
184 start: II,
185 id_fn: impl Fn(&T) -> ID,
186 mut neighbors_fn: impl FnMut(&T) -> NI,
187 ) -> HashSet<T>
188 where
189 T: Hash + Eq + Clone,
190 ID: Hash + Eq,
191 II: IntoIterator<Item = T>,
192 NI: IntoIterator<Item = T>,
193 {
194 let neighbors_fn = move |node: &T| to_infallible_iter(neighbors_fn(node));
195 let Ok(node) = heads_ok(to_infallible_iter(start), id_fn, neighbors_fn);
196 node
197 }
198
199 /// Finds `Ok` nodes in the start set that are not reachable from other nodes in
200 /// the start set.
201 ///
202 /// If `start` or `neighbors_fn()` yields an `Err`, this function terminates and
203 /// returns the error.
204 pub fn heads_ok<T, ID, E, II, NI>(
205 start: II,
206 id_fn: impl Fn(&T) -> ID,
207 mut neighbors_fn: impl FnMut(&T) -> NI,
208 ) -> Result<HashSet<T>, E>
209 where
210 T: Hash + Eq + Clone,
211 ID: Hash + Eq,
212 II: IntoIterator<Item = Result<T, E>>,
213 NI: IntoIterator<Item = Result<T, E>>,
214 {
215 let mut heads: HashSet<T> = start.into_iter().try_collect()?;
216 // Do a BFS until we have only one item left in the frontier. That frontier must
217 // have originated from one of the heads, and since there can't be cycles,
218 // it won't be able to eliminate any other heads.
219 let mut frontier: Vec<T> = heads.iter().cloned().collect();
220 let mut visited: HashSet<ID> = heads.iter().map(&id_fn).collect();
221 let mut root_reached = false;
222 while frontier.len() > 1 || (!frontier.is_empty() && root_reached) {
223 frontier = frontier
224 .iter()
225 .flat_map(|node| {
226 let neighbors = neighbors_fn(node).into_iter().collect_vec();
227 if neighbors.is_empty() {
228 root_reached = true;
229 }
230 neighbors
231 })
232 .try_collect()?;
233 for node in &frontier {
234 heads.remove(node);
235 }
236 frontier.retain(|node| visited.insert(id_fn(node)));
237 }
238 Ok(heads)
239 }
240
241 fn to_ok_iter<T, E>(iter: impl IntoIterator<Item = T>) -> impl Iterator<Item = Result<T, E>> {
242 iter.into_iter().map(Ok)
243 }
244
245 fn to_infallible_iter<T>(
246 iter: impl IntoIterator<Item = T>,
247 ) -> impl Iterator<Item = Result<T, Infallible>> {
248 to_ok_iter(iter)
249 }
250
251 #[cfg(test)]
252 mod tests {
253 use assert_matches::assert_matches;
254 use maplit::hashmap;
255 use maplit::hashset;
256
257 use super::*;
258
259 #[test]
260 fn test_dfs_ok() {
261 let neighbors = hashmap! {
262 'A' => vec![],
263 'B' => vec![Ok('A'), Err('X')],
264 'C' => vec![Ok('B')],
265 };
266 let id_fn = |node: &char| *node;
267 let neighbors_fn = |node: &char| neighbors[node].clone();
268
269 // Self and neighbor nodes shouldn't be lost at the error.
270 let nodes = dfs_ok([Ok('C')], id_fn, neighbors_fn).collect_vec();
271 assert_eq!(nodes, [Ok('C'), Ok('B'), Err('X'), Ok('A')]);
272 }
273
274 #[test]
275 fn test_topo_order_reverse_linear() {
276 // This graph:
277 // o C
278 // o B
279 // o A
280
281 let neighbors = hashmap! {
282 'A' => vec![],
283 'B' => vec!['A'],
284 'C' => vec!['B'],
285 };
286 let id_fn = |node: &char| *node;
287 let neighbors_fn = |node: &char| neighbors[node].clone();
288 let cycle_fn = |id| id;
289
290 let common = topo_order_reverse(vec!['C'], id_fn, neighbors_fn, cycle_fn).unwrap();
291 assert_eq!(common, vec!['C', 'B', 'A']);
292 let common = topo_order_reverse(vec!['C', 'B'], id_fn, neighbors_fn, cycle_fn).unwrap();
293 assert_eq!(common, vec!['C', 'B', 'A']);
294 let common = topo_order_reverse(vec!['B', 'C'], id_fn, neighbors_fn, cycle_fn).unwrap();
295 assert_eq!(common, vec!['C', 'B', 'A']);
296 }
297
298 #[test]
299 fn test_topo_order_reverse_merge() {
300 // This graph:
301 // o F
302 // |\
303 // o | E
304 // | o D
305 // | o C
306 // | o B
307 // |/
308 // o A
309
310 let neighbors = hashmap! {
311 'A' => vec![],
312 'B' => vec!['A'],
313 'C' => vec!['B'],
314 'D' => vec!['C'],
315 'E' => vec!['A'],
316 'F' => vec!['E', 'D'],
317 };
318 let id_fn = |node: &char| *node;
319 let neighbors_fn = |node: &char| neighbors[node].clone();
320 let cycle_fn = |id| id;
321
322 let common = topo_order_reverse(vec!['F'], id_fn, neighbors_fn, cycle_fn).unwrap();
323 assert_eq!(common, vec!['F', 'E', 'D', 'C', 'B', 'A']);
324 let common =
325 topo_order_reverse(vec!['F', 'E', 'C'], id_fn, neighbors_fn, cycle_fn).unwrap();
326 assert_eq!(common, vec!['F', 'D', 'E', 'C', 'B', 'A']);
327 let common =
328 topo_order_reverse(vec!['F', 'D', 'E'], id_fn, neighbors_fn, cycle_fn).unwrap();
329 assert_eq!(common, vec!['F', 'D', 'C', 'B', 'E', 'A']);
330 }
331
332 #[test]
333 fn test_topo_order_reverse_nested_merges() {
334 // This graph:
335 // o I
336 // |\
337 // | o H
338 // | |\
339 // | | o G
340 // | o | F
341 // | | o E
342 // o |/ D
343 // | o C
344 // o | B
345 // |/
346 // o A
347
348 let neighbors = hashmap! {
349 'A' => vec![],
350 'B' => vec!['A'],
351 'C' => vec!['A'],
352 'D' => vec!['B'],
353 'E' => vec!['C'],
354 'F' => vec!['C'],
355 'G' => vec!['E'],
356 'H' => vec!['F', 'G'],
357 'I' => vec!['D', 'H'],
358 };
359 let id_fn = |node: &char| *node;
360 let neighbors_fn = |node: &char| neighbors[node].clone();
361 let cycle_fn = |id| id;
362
363 let common = topo_order_reverse(vec!['I'], id_fn, neighbors_fn, cycle_fn).unwrap();
364 assert_eq!(common, vec!['I', 'D', 'B', 'H', 'F', 'G', 'E', 'C', 'A']);
365 }
366
367 #[test]
368 fn test_topo_order_reverse_nested_merges_bad_order() {
369 // This graph:
370 // o I
371 // |\
372 // | |\
373 // | | |\
374 // | | | o h (h > I)
375 // | | |/|
376 // | | o | G
377 // | |/| o f
378 // | o |/ e (e > I, G)
379 // |/| o D
380 // o |/ C
381 // | o b (b > D)
382 // |/
383 // o A
384
385 let neighbors = hashmap! {
386 'A' => vec![],
387 'b' => vec!['A'],
388 'C' => vec!['A'],
389 'D' => vec!['b'],
390 'e' => vec!['C', 'b'],
391 'f' => vec!['D'],
392 'G' => vec!['e', 'D'],
393 'h' => vec!['G', 'f'],
394 'I' => vec!['C', 'e', 'G', 'h'],
395 };
396 let id_fn = |node: &char| *node;
397 let neighbors_fn = |node: &char| neighbors[node].clone();
398 let cycle_fn = |id| id;
399
400 let common = topo_order_reverse(vec!['I'], id_fn, neighbors_fn, cycle_fn).unwrap();
401 assert_eq!(common, vec!['I', 'h', 'G', 'e', 'C', 'f', 'D', 'b', 'A']);
402 }
403
404 #[test]
405 fn test_topo_order_reverse_merge_bad_fork_order_at_root() {
406 // This graph:
407 // o E
408 // |\
409 // o | D
410 // | o C
411 // | o B
412 // |/
413 // o a (a > D, B)
414
415 let neighbors = hashmap! {
416 'a' => vec![],
417 'B' => vec!['a'],
418 'C' => vec!['B'],
419 'D' => vec!['a'],
420 'E' => vec!['D', 'C'],
421 };
422 let id_fn = |node: &char| *node;
423 let neighbors_fn = |node: &char| neighbors[node].clone();
424 let cycle_fn = |id| id;
425
426 let common = topo_order_reverse(vec!['E'], id_fn, neighbors_fn, cycle_fn).unwrap();
427 assert_eq!(common, vec!['E', 'D', 'C', 'B', 'a']);
428 }
429
430 #[test]
431 fn test_topo_order_reverse_merge_and_linear() {
432 // This graph:
433 // o G
434 // |\
435 // | o F
436 // o | E
437 // | o D
438 // |/
439 // o C
440 // o B
441 // o A
442
443 let neighbors = hashmap! {
444 'A' => vec![],
445 'B' => vec!['A'],
446 'C' => vec!['B'],
447 'D' => vec!['C'],
448 'E' => vec!['C'],
449 'F' => vec!['D'],
450 'G' => vec!['E', 'F'],
451 };
452 let id_fn = |node: &char| *node;
453 let neighbors_fn = |node: &char| neighbors[node].clone();
454 let cycle_fn = |id| id;
455
456 let common = topo_order_reverse(vec!['G'], id_fn, neighbors_fn, cycle_fn).unwrap();
457 assert_eq!(common, vec!['G', 'E', 'F', 'D', 'C', 'B', 'A']);
458 }
459
460 #[test]
461 fn test_topo_order_reverse_merge_and_linear_bad_fork_order() {
462 // This graph:
463 // o G
464 // |\
465 // o | F
466 // o | E
467 // | o D
468 // |/
469 // o c (c > E, D)
470 // o B
471 // o A
472
473 let neighbors = hashmap! {
474 'A' => vec![],
475 'B' => vec!['A'],
476 'c' => vec!['B'],
477 'D' => vec!['c'],
478 'E' => vec!['c'],
479 'F' => vec!['E'],
480 'G' => vec!['F', 'D'],
481 };
482 let id_fn = |node: &char| *node;
483 let neighbors_fn = |node: &char| neighbors[node].clone();
484 let cycle_fn = |id| id;
485
486 let common = topo_order_reverse(vec!['G'], id_fn, neighbors_fn, cycle_fn).unwrap();
487 assert_eq!(common, vec!['G', 'F', 'E', 'D', 'c', 'B', 'A']);
488 }
489
490 #[test]
491 fn test_topo_order_reverse_merge_and_linear_bad_merge_order() {
492 // This graph:
493 // o G
494 // |\
495 // o | f (f > G)
496 // o | e
497 // | o d (d > G)
498 // |/
499 // o C
500 // o B
501 // o A
502
503 let neighbors = hashmap! {
504 'A' => vec![],
505 'B' => vec!['A'],
506 'C' => vec!['B'],
507 'd' => vec!['C'],
508 'e' => vec!['C'],
509 'f' => vec!['e'],
510 'G' => vec!['f', 'd'],
511 };
512 let id_fn = |node: &char| *node;
513 let neighbors_fn = |node: &char| neighbors[node].clone();
514 let cycle_fn = |id| id;
515
516 let common = topo_order_reverse(vec!['G'], id_fn, neighbors_fn, cycle_fn).unwrap();
517 assert_eq!(common, vec!['G', 'f', 'e', 'd', 'C', 'B', 'A']);
518 }
519
520 #[test]
521 fn test_topo_order_reverse_multiple_heads() {
522 // This graph:
523 // o F
524 // |\
525 // o | E
526 // | o D
527 // | | o C
528 // | | |
529 // | | o B
530 // | |/
531 // |/
532 // o A
533
534 let neighbors = hashmap! {
535 'A' => vec![],
536 'B' => vec!['A'],
537 'C' => vec!['B'],
538 'D' => vec!['A'],
539 'E' => vec!['A'],
540 'F' => vec!['E', 'D'],
541 };
542 let id_fn = |node: &char| *node;
543 let neighbors_fn = |node: &char| neighbors[node].clone();
544 let cycle_fn = |id| id;
545
546 let common = topo_order_reverse(vec!['F', 'C'], id_fn, neighbors_fn, cycle_fn).unwrap();
547 assert_eq!(common, vec!['F', 'E', 'D', 'C', 'B', 'A']);
548 }
549
550 #[test]
551 fn test_topo_order_reverse_multiple_roots() {
552 // This graph:
553 // o D
554 // | \
555 // o | C
556 // o B
557 // o A
558
559 let neighbors = hashmap! {
560 'A' => vec![],
561 'B' => vec!['A'],
562 'C' => vec![],
563 'D' => vec!['C', 'B'],
564 };
565 let id_fn = |node: &char| *node;
566 let neighbors_fn = |node: &char| neighbors[node].clone();
567 let cycle_fn = |id| id;
568
569 let common = topo_order_reverse(vec!['D'], id_fn, neighbors_fn, cycle_fn).unwrap();
570 assert_eq!(common, vec!['D', 'C', 'B', 'A']);
571 }
572
573 #[test]
574 fn test_topo_order_reverse_cycle_linear() {
575 // This graph:
576 // o C
577 // o B
578 // o A (to C)
579
580 let neighbors = hashmap! {
581 'A' => vec!['C'],
582 'B' => vec!['A'],
583 'C' => vec!['B'],
584 };
585 let id_fn = |node: &char| *node;
586 let neighbors_fn = |node: &char| neighbors[node].clone();
587 let cycle_fn = |id| id;
588
589 let result: Result<Vec<_>, _> =
590 topo_order_reverse(vec!['C'], id_fn, neighbors_fn, cycle_fn);
591 assert_matches!(result, Err('C' | 'B' | 'A'));
592 }
593
594 #[test]
595 fn test_topo_order_reverse_cycle_to_branchy_sub_graph() {
596 // This graph:
597 // o D
598 // |\
599 // | o C
600 // |/
601 // o B
602 // o A (to C)
603
604 let neighbors = hashmap! {
605 'A' => vec!['C'],
606 'B' => vec!['A'],
607 'C' => vec!['B'],
608 'D' => vec!['B', 'C'],
609 };
610 let id_fn = |node: &char| *node;
611 let neighbors_fn = |node: &char| neighbors[node].clone();
612 let cycle_fn = |id| id;
613
614 let result = topo_order_reverse(vec!['D'], id_fn, neighbors_fn, cycle_fn);
615 assert_matches!(result, Err('C' | 'B' | 'A'));
616 }
617
618 #[test]
619 fn test_topo_order_ok() {
620 let neighbors = hashmap! {
621 'A' => vec![Err('Y')],
622 'B' => vec![Ok('A'), Err('X')],
623 'C' => vec![Ok('B')],
624 };
625 let id_fn = |node: &char| *node;
626 let neighbors_fn = |node: &char| neighbors[node].clone();
627 let cycle_fn = |id| id;
628
629 // Terminates at Err('X') no matter if the sorting order is forward or
630 // reverse. The visiting order matters.
631 let result = topo_order_forward_ok([Ok('C')], id_fn, neighbors_fn, cycle_fn);
632 assert_eq!(result, Err('X'));
633 let result = topo_order_reverse_ok([Ok('C')], id_fn, neighbors_fn, cycle_fn);
634 assert_eq!(result, Err('X'));
635 }
636
637 #[test]
638 fn test_heads_mixed() {
639 // Test the uppercase letters are in the start set
640 //
641 // D F
642 // |/|
643 // C e
644 // |/
645 // b
646 // |
647 // A
648
649 let neighbors = hashmap! {
650 'A' => vec![],
651 'b' => vec!['A'],
652 'C' => vec!['b'],
653 'D' => vec!['C'],
654 'e' => vec!['b'],
655 'F' => vec!['C', 'e'],
656 };
657
658 let actual = heads(
659 vec!['A', 'C', 'D', 'F'],
660 |node| *node,
661 |node| neighbors[node].clone(),
662 );
663 assert_eq!(actual, hashset!['D', 'F']);
664
665 // Check with a different order in the start set
666 let actual = heads(
667 vec!['F', 'D', 'C', 'A'],
668 |node| *node,
669 |node| neighbors[node].clone(),
670 );
671 assert_eq!(actual, hashset!['D', 'F']);
672 }
673
674 #[test]
675 fn test_heads_ok() {
676 let neighbors = hashmap! {
677 'A' => vec![],
678 'B' => vec![Ok('A'), Err('X')],
679 'C' => vec![Ok('B')],
680 };
681 let id_fn = |node: &char| *node;
682 let neighbors_fn = |node: &char| neighbors[node].clone();
683
684 let result = heads_ok([Ok('C')], id_fn, neighbors_fn);
685 assert_eq!(result, Ok(hashset! {'C'}));
686 let result = heads_ok([Ok('B')], id_fn, neighbors_fn);
687 assert_eq!(result, Ok(hashset! {'B'}));
688 let result = heads_ok([Ok('A')], id_fn, neighbors_fn);
689 assert_eq!(result, Ok(hashset! {'A'}));
690 let result = heads_ok([Ok('C'), Ok('B')], id_fn, neighbors_fn);
691 assert_eq!(result, Err('X'));
692 let result = heads_ok([Ok('C'), Ok('A')], id_fn, neighbors_fn);
693 assert_eq!(result, Err('X'));
694 }
695 }