diff --git a/core/src/caching/bounded/fifo/fifo_cache.rs b/core/src/caching/bounded/fifo/fifo_cache.rs new file mode 100644 index 0000000000000000000000000000000000000000..5c1ed86066346b237f4a73a2e59b875d2da346d4 --- /dev/null +++ b/core/src/caching/bounded/fifo/fifo_cache.rs @@ -0,0 +1,39 @@ +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() + } +} diff --git a/core/src/caching/bounded/fifo/mod.rs b/core/src/caching/bounded/fifo/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..ef2196b0a8519d9f5df2b1922d26a71ca87fb85a --- /dev/null +++ b/core/src/caching/bounded/fifo/mod.rs @@ -0,0 +1 @@ +pub mod fifo_cache; diff --git a/core/src/caching/bounded/lru/lru_cache.rs b/core/src/caching/bounded/lru/lru_cache.rs index b41173008a0534688960b8c8f90e03700eb43c0a..32a6679d7813a7f0bb36e6972c9d1c3fb9cc1970 100644 --- a/core/src/caching/bounded/lru/lru_cache.rs +++ b/core/src/caching/bounded/lru/lru_cache.rs @@ -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} diff --git a/core/src/caching/bounded/lru/mod.rs b/core/src/caching/bounded/lru/mod.rs index 83175224b643b4ac9fb629d6bd3958f134fe558c..272f6adf4c26ef228f4a1d0e7f72d391588f13d1 100644 --- a/core/src/caching/bounded/lru/mod.rs +++ b/core/src/caching/bounded/lru/mod.rs @@ -1,3 +1,3 @@ -pub mod lru_cache; mod linked_list; mod list_node; +pub mod lru_cache; diff --git a/core/src/caching/bounded/mod.rs b/core/src/caching/bounded/mod.rs index 2cafec9fe8d5b666a764b5001669b1d99219383a..8981ce6fd898a1d16bd3c8127d6918902ebfc6bb 100644 --- a/core/src/caching/bounded/mod.rs +++ b/core/src/caching/bounded/mod.rs @@ -1 +1,2 @@ -pub mod lru; \ No newline at end of file +pub mod fifo; +pub mod lru;