use std::collections::HashMap;
use smallvec::SmallVec;
use crate::arena::Handle;
use crate::graph::Node;
use crate::partial::PartialPath;
use crate::paths::Path;
pub struct CycleDetector<P> {
paths: HashMap<PathKey, SmallVec<[P; 8]>>,
}
#[doc(hidden)]
#[derive(Clone, Eq, Hash, PartialEq)]
pub struct PathKey {
start_node: Handle<Node>,
end_node: Handle<Node>,
}
#[doc(hidden)]
pub trait HasPathKey: Clone {
fn key(&self) -> PathKey;
fn is_shorter_than(&self, other: &Self) -> bool;
}
impl HasPathKey for Path {
fn key(&self) -> PathKey {
PathKey {
start_node: self.start_node,
end_node: self.end_node,
}
}
fn is_shorter_than(&self, other: &Self) -> bool {
self.edges.len() < other.edges.len() && self.symbol_stack.len() <= other.symbol_stack.len()
}
}
impl HasPathKey for PartialPath {
fn key(&self) -> PathKey {
PathKey {
start_node: self.start_node,
end_node: self.end_node,
}
}
fn is_shorter_than(&self, other: &Self) -> bool {
self.edges.len() < other.edges.len()
&& (self.symbol_stack_precondition.len() + self.symbol_stack_postcondition.len())
<= (other.symbol_stack_precondition.len() + other.symbol_stack_postcondition.len())
}
}
const MAX_SIMILAR_PATH_COUNT: usize = 4;
impl<P> CycleDetector<P>
where
P: HasPathKey,
{
pub fn new() -> CycleDetector<P> {
CycleDetector {
paths: HashMap::new(),
}
}
pub fn should_process_path<F>(&mut self, path: &P, cmp: F) -> bool
where
F: FnMut(&P) -> std::cmp::Ordering,
{
let key = path.key();
let paths_with_same_nodes = self.paths.entry(key).or_default();
match paths_with_same_nodes.binary_search_by(cmp) {
Ok(_) => return false,
Err(index) => paths_with_same_nodes.insert(index, path.clone()),
}
let similar_path_count = paths_with_same_nodes
.iter()
.filter(|similar_path| similar_path.is_shorter_than(path))
.count();
return similar_path_count <= MAX_SIMILAR_PATH_COUNT;
}
}