pub use super::{Capacity, Replacement};
pub mod lru;
pub use lru::*;
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RoundRobinReplacement {
n: usize,
}
impl<V, C> Replacement<V, C> for RoundRobinReplacement
where
C: Capacity,
{
#[inline]
fn choose_for_replacement<'a>(
&mut self,
mut candidates: impl ExactSizeIterator<Item = (usize, &'a V)>,
) -> usize
where
V: 'a,
{
let len = candidates.len();
assert!(len > 0);
self.n %= len;
let index = candidates.nth(self.n).unwrap().0;
self.n += 1;
index
}
}
#[cfg(feature = "rand")]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RandomReplacement<R = rand::rngs::StdRng> {
rng: R,
}
#[cfg(feature = "rand")]
impl Default for RandomReplacement<rand::rngs::StdRng> {
#[inline]
fn default() -> Self {
RandomReplacement { rng: rand::make_rng() }
}
}
#[cfg(feature = "rand")]
impl<R> RandomReplacement<R> {
#[inline]
pub fn with_rng(rng: R) -> Self {
RandomReplacement { rng }
}
}
#[cfg(feature = "rand")]
impl<V, C, R> Replacement<V, C> for RandomReplacement<R>
where
C: Capacity,
R: rand::Rng,
{
#[inline]
fn choose_for_replacement<'a>(
&mut self,
candidates: impl Iterator<Item = (usize, &'a V)>,
) -> usize
where
V: 'a,
{
use rand::seq::IteratorRandom;
candidates.choose(&mut self.rng).unwrap().0
}
}