|
|
use std::hash::Hash; |
|
|
|
|
|
use rustc_hash::FxHashSet; |
|
|
|
|
|
use super::VisitedNodes; |
|
|
|
|
|
|
|
|
|
|
|
pub trait GraphStore: Send { |
|
|
type Node: Send; |
|
|
type Handle: Clone + Send; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn insert( |
|
|
&mut self, |
|
|
from_handle: Option<Self::Handle>, |
|
|
node: GraphNode<Self::Node>, |
|
|
) -> Option<(Self::Handle, &Self::Node)>; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash, Ord, PartialOrd)] |
|
|
pub struct GraphNode<Node>(pub(super) Node); |
|
|
|
|
|
impl<Node> GraphNode<Node> { |
|
|
|
|
|
pub fn into_node(self) -> Node { |
|
|
self.0 |
|
|
} |
|
|
|
|
|
|
|
|
pub fn node(&self) -> &Node { |
|
|
&self.0 |
|
|
} |
|
|
|
|
|
|
|
|
pub fn node_mut(&mut self) -> &mut Node { |
|
|
&mut self.0 |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
pub struct SkipDuplicates<StoreImpl> |
|
|
where |
|
|
StoreImpl: GraphStore, |
|
|
{ |
|
|
store: StoreImpl, |
|
|
visited: FxHashSet<StoreImpl::Node>, |
|
|
} |
|
|
|
|
|
impl<StoreImpl> SkipDuplicates<StoreImpl> |
|
|
where |
|
|
StoreImpl: GraphStore, |
|
|
{ |
|
|
pub fn new(store: StoreImpl) -> Self { |
|
|
Self { |
|
|
store, |
|
|
visited: FxHashSet::default(), |
|
|
} |
|
|
} |
|
|
|
|
|
pub fn new_with_visited_nodes(store: StoreImpl, visited: FxHashSet<StoreImpl::Node>) -> Self { |
|
|
Self { store, visited } |
|
|
} |
|
|
} |
|
|
|
|
|
impl<StoreImpl> GraphStore for SkipDuplicates<StoreImpl> |
|
|
where |
|
|
StoreImpl: GraphStore, |
|
|
StoreImpl::Node: Eq + std::hash::Hash + Clone, |
|
|
{ |
|
|
type Node = StoreImpl::Node; |
|
|
type Handle = StoreImpl::Handle; |
|
|
|
|
|
fn insert( |
|
|
&mut self, |
|
|
from_handle: Option<Self::Handle>, |
|
|
node: GraphNode<StoreImpl::Node>, |
|
|
) -> Option<(Self::Handle, &StoreImpl::Node)> { |
|
|
if !self.visited.contains(node.node()) { |
|
|
self.visited.insert(node.node().clone()); |
|
|
self.store.insert(from_handle, node) |
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
self.store.insert(from_handle, node); |
|
|
None |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl<StoreImpl> SkipDuplicates<StoreImpl> |
|
|
where |
|
|
StoreImpl: GraphStore, |
|
|
{ |
|
|
|
|
|
pub fn into_inner(self) -> StoreImpl { |
|
|
self.store |
|
|
} |
|
|
|
|
|
|
|
|
pub fn into_inner_with_visited(self) -> (StoreImpl, VisitedNodes<StoreImpl::Node>) { |
|
|
(self.store, VisitedNodes(self.visited)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
pub struct SkipDuplicatesWithKey<StoreImpl, Key, KeyExtractor> |
|
|
where |
|
|
StoreImpl: GraphStore, |
|
|
Key: Send + Eq + Hash, |
|
|
KeyExtractor: Send + Fn(&StoreImpl::Node) -> &Key, |
|
|
{ |
|
|
store: StoreImpl, |
|
|
visited: FxHashSet<Key>, |
|
|
key_extractor: KeyExtractor, |
|
|
} |
|
|
|
|
|
impl<StoreImpl, Key, KeyExtractor> SkipDuplicatesWithKey<StoreImpl, Key, KeyExtractor> |
|
|
where |
|
|
StoreImpl: GraphStore, |
|
|
Key: Send + Eq + std::hash::Hash + Clone, |
|
|
KeyExtractor: Send + Fn(&StoreImpl::Node) -> &Key, |
|
|
{ |
|
|
pub fn new(store: StoreImpl, key_extractor: KeyExtractor) -> Self { |
|
|
Self { |
|
|
store, |
|
|
visited: FxHashSet::default(), |
|
|
key_extractor, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl<StoreImpl, Key, KeyExtractor> GraphStore |
|
|
for SkipDuplicatesWithKey<StoreImpl, Key, KeyExtractor> |
|
|
where |
|
|
StoreImpl: GraphStore, |
|
|
StoreImpl::Node: Eq + std::hash::Hash + Clone, |
|
|
Key: Send + Eq + std::hash::Hash + Clone, |
|
|
KeyExtractor: Send + Fn(&StoreImpl::Node) -> &Key, |
|
|
{ |
|
|
type Node = StoreImpl::Node; |
|
|
type Handle = StoreImpl::Handle; |
|
|
|
|
|
fn insert( |
|
|
&mut self, |
|
|
from_handle: Option<Self::Handle>, |
|
|
node: GraphNode<StoreImpl::Node>, |
|
|
) -> Option<(Self::Handle, &StoreImpl::Node)> { |
|
|
let key = (self.key_extractor)(node.node()); |
|
|
|
|
|
if !self.visited.contains(key) { |
|
|
self.visited.insert(key.clone()); |
|
|
self.store.insert(from_handle, node) |
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
self.store.insert(from_handle, node); |
|
|
None |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl<StoreImpl, Key, KeyExtractor> SkipDuplicatesWithKey<StoreImpl, Key, KeyExtractor> |
|
|
where |
|
|
StoreImpl: GraphStore, |
|
|
Key: Send + Eq + std::hash::Hash + Clone, |
|
|
KeyExtractor: Send + Fn(&StoreImpl::Node) -> &Key, |
|
|
{ |
|
|
|
|
|
pub fn into_inner(self) -> StoreImpl { |
|
|
self.store |
|
|
} |
|
|
|
|
|
|
|
|
pub fn into_inner_with_visited(self) -> (StoreImpl, VisitedNodes<Key>) { |
|
|
(self.store, VisitedNodes(self.visited)) |
|
|
} |
|
|
} |
|
|
|