use std::collections::HashSet;
pub(super) trait VisitSet {
fn insert(&mut self, id: u32) -> bool;
}
impl VisitSet for HashSet<u32> {
#[inline]
fn insert(&mut self, id: u32) -> bool {
HashSet::insert(self, id)
}
}
pub(super) struct VisitedList {
stamps: Vec<u32>,
epoch: u32,
}
impl VisitedList {
pub(super) fn new(n: usize) -> Self {
Self {
stamps: vec![0; n],
epoch: 1,
}
}
#[inline]
pub(super) fn clear(&mut self) {
self.epoch = self.epoch.wrapping_add(1);
if self.epoch == 0 {
self.stamps.iter_mut().for_each(|s| *s = 0);
self.epoch = 1;
}
}
}
impl VisitSet for VisitedList {
#[inline]
fn insert(&mut self, id: u32) -> bool {
let slot = &mut self.stamps[id as usize];
if *slot == self.epoch {
false
} else {
*slot = self.epoch;
true
}
}
}