Struct BoundedLinearCache

Source
pub struct BoundedLinearCache<K, V> { /* private fields */ }
Expand description

BoundedLinearCache is a bounded cache with approximate key matching support.

The cache enforces a maximum capacity, and when the capacity is exceeded, the least recently used (LRU) element is evicted.

§Approximate Key Matching

Keys must implement the ApproxComparable trait, which allows approximate equality comparisons based on the provided tolerance. This enables the cache to retrieve values even when the queried key is not an exact match but is “close enough.”

§Example Usage

use proximipy::caching::bounded::bounded_linear_cache::BoundedLinearCache;
use proximipy::caching::approximate_cache::ApproximateCache;

let mut cache = BoundedLinearCache::new(3, 2.0);

cache.insert(10 as i16, "Value 1");
cache.insert(20, "Value 2");
cache.insert(30, "Value 3");

assert_eq!(cache.find(&11), Some("Value 1"));
assert_eq!(cache.len(), 3);

cache.insert(40, "Value 4"); // Evicts the least recently used (Key(20))
assert!(cache.find(&20).is_none());

§Type Parameters

  • K: The type of the keys, which must implement ApproxComparable, Eq, Hash, and Clone.
  • V: The type of the values, which must implement Clone.

§Methods

  • new(max_capacity: usize, tolerance: f32) -> Self: Creates a new BoundedLinearCache with the specified maximum capacity and tolerance.
  • find(&mut self, key: &K) -> Option<V>: Attempts to find a value matching the given key approximately. Promotes the found key to the head of the list.
  • insert(&mut self, key: K, value: V): Inserts a key-value pair into the cache. Evicts the least recently used item if the cache is full.
  • len(&self) -> usize: Returns the current size of the cache.

Implementations§

Source§

impl<K, V> BoundedLinearCache<K, V>

Source

pub fn new(max_capacity: usize, tolerance: f32) -> Self

Trait Implementations§

Source§

impl<K, V> ApproximateCache<K, V> for BoundedLinearCache<K, V>
where K: ApproxComparable + Eq + Hash + Clone, V: Clone,

Source§

fn find(&mut self, key: &K) -> Option<V>

Source§

fn insert(&mut self, key: K, value: V)

Source§

fn len(&self) -> usize

Source§

fn is_empty(&self) -> bool

Auto Trait Implementations§

§

impl<K, V> Freeze for BoundedLinearCache<K, V>

§

impl<K, V> !RefUnwindSafe for BoundedLinearCache<K, V>

§

impl<K, V> !Send for BoundedLinearCache<K, V>

§

impl<K, V> !Sync for BoundedLinearCache<K, V>

§

impl<K, V> Unpin for BoundedLinearCache<K, V>
where K: Unpin,

§

impl<K, V> !UnwindSafe for BoundedLinearCache<K, V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V