use super::{ContainsAny, Process};
use crate::Node;
use std::hash::Hash;
pub struct DFS<N> {
node: N,
}
impl<N: Node> Process for DFS<N> {
type Node = N;
fn from_node(node: Self::Node) -> Self {
Self { node }
}
}
impl<N, P> ContainsAny<P> for DFS<N>
where
N: Copy + Eq + Hash + Node,
P: Fn(Self::Node) -> bool,
{
fn contains_any(&self, pred: P) -> bool {
type HashSet<K> = std::collections::HashSet<K, ahash::RandomState>;
let mut is_visited = HashSet::default();
let mut to_visit = vec![self.node];
while let Some(node) = to_visit.pop() {
if pred(node) {
return true;
} else if is_visited.insert(node) {
let next = node.outgoing().filter(|node| !is_visited.contains(node));
to_visit.extend(next);
}
}
false
}
}