Skip to content
Snippets Groups Projects
Commit a9fd2461 authored by Mathis "what could possibly go wrong" Randl's avatar Mathis "what could possibly go wrong" Randl
Browse files

fifo + formatting

parent abc433b1
No related branches found
No related tags found
No related merge requests found
use std::collections::VecDeque;
use crate::{caching::approximate_cache::ApproximateCache, numerics::comp::ApproxComparable};
pub struct FifoCache<K, V> {
max_capacity: usize,
items: VecDeque<(K, V)>,
tolerance: f32,
}
impl<K, V> ApproximateCache<K, V> for FifoCache<K, V>
where
K: ApproxComparable,
V: Clone,
{
fn find(&mut self, key: &K) -> Option<V> {
let candidate = self
.items
.iter()
.min_by(|&(x, _), &(y, _)| key.fuzziness(x).partial_cmp(&key.fuzziness(y)).unwrap())?;
let (c_key, c_value) = candidate;
if c_key.roughly_matches(key, self.tolerance) {
Some(c_value.clone())
} else {
None
}
}
fn insert(&mut self, key: K, value: V) {
self.items.push_back((key, value));
if self.items.len() > self.max_capacity {
self.items.pop_front();
}
}
fn len(&self) -> usize {
self.items.iter().len()
}
}
pub mod fifo_cache;
......@@ -113,8 +113,7 @@ mod tests {
#[test]
fn test_lru_cache_basic_operations() {
fn test_lru_cache_basic_operations_best_match<const BEST_MATCH: bool>() {
let mut cache: LRUCache<i16, i16, BEST_MATCH> =
LRUCache::new(2, TEST_TOLERANCE);
let mut cache: LRUCache<i16, i16, BEST_MATCH> = LRUCache::new(2, TEST_TOLERANCE);
cache.insert(1, 1); // Cache is {1=1}
cache.insert(2, 2); // Cache is {1=1, 2=2}
assert_eq!(cache.find(&1), Some(1)); // Returns 1, Cache is {2=2, 1=1}
......@@ -132,8 +131,7 @@ mod tests {
#[test]
fn test_lru_cache_eviction_order() {
fn test_lru_cache_eviction_order_best_match<const BEST_MATCH: bool>() {
let mut cache: LRUCache<i16, i16, BEST_MATCH> =
LRUCache::new(3, TEST_TOLERANCE);
let mut cache: LRUCache<i16, i16, BEST_MATCH> = LRUCache::new(3, TEST_TOLERANCE);
cache.insert(1, 1); // Cache is {1=1}
cache.insert(2, 2); // Cache is {1=1, 2=2}
cache.insert(3, 3); // Cache is {1=1, 2=2, 3=3}
......@@ -152,8 +150,7 @@ mod tests {
#[test]
fn test_lru_cache_overwrite() {
fn test_lru_cache_overwrite_best_match<const BEST_MATCH: bool>() {
let mut cache: LRUCache<i16, i16, BEST_MATCH> =
LRUCache::new(2, TEST_TOLERANCE);
let mut cache: LRUCache<i16, i16, BEST_MATCH> = LRUCache::new(2, TEST_TOLERANCE);
cache.insert(1, 1); // Cache is {1=1}
cache.insert(2, 2); // Cache is {1=1, 2=2}
cache.insert(1, 10); // Overwrites key 1, Cache is {2=2, 1=10}
......@@ -169,8 +166,7 @@ mod tests {
#[test]
fn test_lru_cache_capacity_one() {
fn test_lru_cache_capacity_one_best_match<const BEST_MATCH: bool>() {
let mut cache: LRUCache<i16, i16, BEST_MATCH> =
LRUCache::new(1, TEST_TOLERANCE);
let mut cache: LRUCache<i16, i16, BEST_MATCH> = LRUCache::new(1, TEST_TOLERANCE);
cache.insert(1, 1); // Cache is {1=1}
assert_eq!(cache.find(&1), Some(1)); // Returns 1
cache.insert(2, 2); // Evicts key 1, Cache is {2=2}
......
pub mod lru_cache;
mod linked_list;
mod list_node;
pub mod lru_cache;
pub mod lru;
\ No newline at end of file
pub mod fifo;
pub mod lru;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment