associative_cache/
replacement.rs1pub use super::{Capacity, Replacement};
5
6pub mod lru;
7pub use lru::*;
8
9#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub struct RoundRobinReplacement {
18 n: usize,
19}
20
21impl<V, C> Replacement<V, C> for RoundRobinReplacement
22where
23 C: Capacity,
24{
25 #[inline]
26 fn choose_for_replacement<'a>(
27 &mut self,
28 mut candidates: impl ExactSizeIterator<Item = (usize, &'a V)>,
29 ) -> usize
30 where
31 V: 'a,
32 {
33 let len = candidates.len();
34 assert!(len > 0);
35 self.n %= len;
36 let index = candidates.nth(self.n).unwrap().0;
37 self.n += 1;
38 index
39 }
40}
41
42#[cfg(feature = "rand")]
48#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
49pub struct RandomReplacement<R = rand::rngs::StdRng> {
50 rng: R,
51}
52
53#[cfg(feature = "rand")]
54impl Default for RandomReplacement<rand::rngs::StdRng> {
55 #[inline]
56 fn default() -> Self {
57 RandomReplacement { rng: rand::make_rng() }
58 }
59}
60
61#[cfg(feature = "rand")]
62impl<R> RandomReplacement<R> {
63 #[inline]
75 pub fn with_rng(rng: R) -> Self {
76 RandomReplacement { rng }
77 }
78}
79
80#[cfg(feature = "rand")]
81impl<V, C, R> Replacement<V, C> for RandomReplacement<R>
82where
83 C: Capacity,
84 R: rand::Rng,
85{
86 #[inline]
87 fn choose_for_replacement<'a>(
88 &mut self,
89 candidates: impl Iterator<Item = (usize, &'a V)>,
90 ) -> usize
91 where
92 V: 'a,
93 {
94 use rand::seq::IteratorRandom;
95 candidates.choose(&mut self.rng).unwrap().0
96 }
97}