use std::{borrow::Borrow, hash::Hash, num::NonZeroUsize};
use super::linked_slots::{LinkedSlots, Order};
pub struct BoundedLruCache<K, V> {
slots: LinkedSlots<K, V>,
order: Order,
cap: NonZeroUsize,
}
impl<K, V> BoundedLruCache<K, V>
where
K: Eq + Hash,
{
pub fn new(cap: NonZeroUsize) -> Self {
Self {
slots: LinkedSlots::new(),
order: Order::default(),
cap,
}
}
pub fn with_capacity_at_least_one(cap: usize) -> Self {
let cap = NonZeroUsize::new(cap).unwrap_or(NonZeroUsize::MIN);
Self::new(cap)
}
pub fn len(&self) -> usize {
self.slots.len()
}
pub fn is_empty(&self) -> bool {
self.slots.is_empty()
}
pub fn cap(&self) -> NonZeroUsize {
self.cap
}
pub fn set_cap(&mut self, cap: NonZeroUsize) {
self.cap = cap;
while self.len() > cap.get() {
self.pop_lru();
}
}
pub fn clear(&mut self) {
*self = Self::new(self.cap);
}
pub fn contains<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.slots.find(key).is_some()
}
pub fn get<Q>(&mut self, key: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let slot = self.slots.find(key)?;
self.slots.promote(slot, &mut self.order);
Some(&self.slots.slot(slot).value)
}
pub fn peek<Q>(&self, key: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let slot = self.slots.find(key)?;
Some(&self.slots.slot(slot).value)
}
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let slot = self.slots.find(key)?;
self.slots.promote(slot, &mut self.order);
Some(&mut self.slots.slot_mut(slot).value)
}
pub fn find_or_insert_with(&mut self, key: K, value: impl FnOnce() -> V) -> &V {
let slot = match self.slots.find(&key) {
Some(slot) => {
self.slots.promote(slot, &mut self.order);
slot
}
None => self.insert_new(key, value()).0,
};
&self.slots.slot(slot).value
}
pub fn push(&mut self, key: K, value: V) -> Option<(K, V)> {
if let Some(slot) = self.slots.find(&key) {
self.slots.promote(slot, &mut self.order);
let old_value = std::mem::replace(&mut self.slots.slot_mut(slot).value, value);
return Some((key, old_value));
}
self.insert_new(key, value).1
}
#[inline(never)]
fn insert_new(&mut self, key: K, value: V) -> (u32, Option<(K, V)>) {
let evicted = if self.slots.len() == self.cap.get() {
self.pop_lru()
} else {
None
};
let hash = self.slots.hash(&key);
let slot = self.slots.insert(hash, (key, value), self.cap);
self.slots.link_newest(slot, &mut self.order);
(slot, evicted)
}
pub fn put(&mut self, key: K, value: V) -> Option<V> {
self.push(key, value).map(|(_, value)| value)
}
pub fn peek_lru(&self) -> Option<(&K, &V)> {
let entry = self.slots.slot(self.order.oldest?);
Some((&entry.key, &entry.value))
}
pub fn pop_lru(&mut self) -> Option<(K, V)> {
let slot = self.order.oldest?;
let (_, entry) = self.slots.remove(slot, &mut self.order);
Some((entry.key, entry.value))
}
pub fn pop(&mut self, key: &K) -> Option<V> {
self.slots
.take(key, &mut self.order)
.map(|entry| entry.value)
}
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> {
self.slots.iter(self.order)
}
}
#[cfg(test)]
#[path = "tests/bounded_lru_tests.rs"]
mod tests;