use std::collections::hash_map::IterMut;
use std::collections::HashMap;
use std::io;
use std::ops::Index;
use std::ops::IndexMut;
use std::slice::SliceIndex;
pub trait Cacheable {
fn dirty(&self) -> bool;
}
#[derive(Debug)]
pub struct VecCache<T: 'static + Copy + Default> {
vec: Box<[T]>,
dirty: bool,
}
impl<T: 'static + Copy + Default> VecCache<T> {
pub fn new(count: usize) -> VecCache<T> {
VecCache {
vec: vec![Default::default(); count].into_boxed_slice(),
dirty: true,
}
}
pub fn from_vec(vec: Vec<T>) -> VecCache<T> {
VecCache {
vec: vec.into_boxed_slice(),
dirty: false,
}
}
pub fn get<I>(&self, index: I) -> Option<&<I as SliceIndex<[T]>>::Output>
where
I: SliceIndex<[T]>,
{
self.vec.get(index)
}
pub fn get_values(&self) -> &[T] {
&self.vec
}
pub fn mark_clean(&mut self) {
self.dirty = false;
}
pub fn len(&self) -> usize {
self.vec.len()
}
}
impl<T: 'static + Copy + Default> Cacheable for VecCache<T> {
fn dirty(&self) -> bool {
self.dirty
}
}
impl<T: 'static + Copy + Default> Index<usize> for VecCache<T> {
type Output = T;
fn index(&self, index: usize) -> &T {
self.vec.index(index)
}
}
impl<T: 'static + Copy + Default> IndexMut<usize> for VecCache<T> {
fn index_mut(&mut self, index: usize) -> &mut T {
self.dirty = true;
self.vec.index_mut(index)
}
}
#[derive(Debug)]
pub struct CacheMap<T: Cacheable> {
capacity: usize,
map: HashMap<usize, T>,
}
impl<T: Cacheable> CacheMap<T> {
pub fn new(capacity: usize) -> Self {
CacheMap {
capacity,
map: HashMap::with_capacity(capacity),
}
}
pub fn contains_key(&self, key: &usize) -> bool {
self.map.contains_key(key)
}
pub fn get(&self, index: &usize) -> Option<&T> {
self.map.get(index)
}
pub fn get_mut(&mut self, index: &usize) -> Option<&mut T> {
self.map.get_mut(index)
}
pub fn iter_mut(&mut self) -> IterMut<usize, T> {
self.map.iter_mut()
}
pub fn insert<F>(&mut self, index: usize, block: T, write_callback: F) -> io::Result<()>
where
F: FnOnce(usize, T) -> io::Result<()>,
{
if self.map.len() == self.capacity {
let to_evict = *self.map.iter().next().unwrap().0;
if let Some(evicted) = self.map.remove(&to_evict) {
if evicted.dirty() {
write_callback(to_evict, evicted)?;
}
}
}
self.map.insert(index, block);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
struct NumCache(pub u64);
impl Cacheable for NumCache {
fn dirty(&self) -> bool {
true
}
}
#[test]
fn evicts_when_full() {
let mut cache = CacheMap::<NumCache>::new(3);
let mut evicted = None;
cache
.insert(0, NumCache(5), |index, _| {
evicted = Some(index);
Ok(())
})
.unwrap();
assert_eq!(evicted, None);
cache
.insert(1, NumCache(6), |index, _| {
evicted = Some(index);
Ok(())
})
.unwrap();
assert_eq!(evicted, None);
cache
.insert(2, NumCache(7), |index, _| {
evicted = Some(index);
Ok(())
})
.unwrap();
assert_eq!(evicted, None);
cache
.insert(3, NumCache(8), |index, _| {
evicted = Some(index);
Ok(())
})
.unwrap();
assert!(evicted.is_some());
let num_items = (0..=3).filter(|k| cache.contains_key(k)).count();
assert_eq!(num_items, 3);
assert!(cache.contains_key(&3));
assert_eq!(cache.get(&3), Some(&NumCache(8)));
}
}