use cranpose_core::collections::recent_hashes::RecentHashes;
const WAYS: usize = 4;
pub(super) struct ReturningTexts {
hashes: RecentHashes<WAYS>,
most: usize,
puts: usize,
passes: u64,
}
impl ReturningTexts {
pub(super) const fn new(most: usize) -> Self {
Self {
hashes: RecentHashes::new(0),
most,
puts: 0,
passes: 0,
}
}
pub(super) fn contains(&self, hash: u64) -> bool {
self.hashes.contains(hash)
}
pub(super) fn put(&mut self, hash: u64) {
self.puts += 1;
if self.puts > self.hashes.slots() / 2 && self.hashes.slots() < self.most {
self.grow();
}
self.hashes.put(hash);
}
#[cold]
#[inline(never)]
fn grow(&mut self) {
self.hashes
.grow_to((self.hashes.slots() * 2).min(self.most));
}
pub(super) fn begin_pass(&mut self, idle_passes: u64) {
self.passes += 1;
if self.passes >= idle_passes {
self.passes = 0;
self.puts = 0;
}
}
pub(super) fn clear(&mut self) {
*self = Self::new(self.most);
}
}