Skip to content

Commit

Permalink
Merge pull request #324 from github/fix-similar-paths
Browse files Browse the repository at this point in the history
Prefer higher-priority similar paths
  • Loading branch information
hendrikvanantwerpen authored Oct 17, 2023
2 parents 1e307b5 + d26ad13 commit ac9628f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 7 additions & 5 deletions stack-graphs/src/cycles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

use enumset::EnumSet;
use smallvec::SmallVec;
use std::cmp::Ordering;
use std::collections::HashMap;

use crate::arena::Arena;
Expand Down Expand Up @@ -97,22 +98,23 @@ where
/// Determines whether we should process this path during the path-finding algorithm. If we have seen
/// a path with the same start and end node, and the same pre- and postcondition, then we return false.
/// Otherwise, we return true.
pub fn has_similar_path<Eq>(
pub fn has_similar_path<Cmp>(
&mut self,
_graph: &StackGraph,
arena: &mut P::Arena,
path: &P,
eq: Eq,
cmp: Cmp,
) -> bool
where
Eq: Fn(&mut P::Arena, &P, &P) -> bool,
Cmp: Fn(&mut P::Arena, &P, &P) -> Option<Ordering>,
{
let key = path.key();

let possibly_similar_paths = self.paths.entry(key).or_default();
for other_path in possibly_similar_paths.iter() {
if eq(arena, path, other_path) {
return true;
match cmp(arena, path, other_path) {
Some(ord) if ord != Ordering::Less => return true,
_ => continue,
}
}

Expand Down
15 changes: 14 additions & 1 deletion stack-graphs/src/stitching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
//! [`Database`]: struct.Database.html
//! [`PathStitcher`]: struct.PathStitcher.html

use std::cmp::Ordering;
use std::collections::HashMap;
use std::collections::VecDeque;
#[cfg(feature = "copious-debugging")]
Expand Down Expand Up @@ -897,7 +898,19 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
graph,
partials,
&new_partial_path,
|ps, left, right| left.equals(ps, right),
|ps, left, right| {
if !left.equals(ps, right) {
None
} else {
if left.shadows(ps, right) {
Some(Ordering::Less)
} else if right.shadows(ps, left) {
Some(Ordering::Greater)
} else {
Some(Ordering::Equal)
}
}
},
) {
copious_debugging!(" is rejected: too many similar");
continue;
Expand Down

0 comments on commit ac9628f

Please sign in to comment.