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

simple no-eviction cache impl

parent 0b16974d
No related branches found
No related tags found
No related merge requests found
use crate::numerics::comp::ApproxComparable;
// size of caches in implementations where that should be known at comptime
pub(crate) const COMPTIME_CACHE_SIZE: usize = 1024;
pub trait ApproximateCache<K, V>
where
K: ApproxComparable,
V: Clone,
{
fn find(&self, key: &K) -> Option<V>;
fn insert(&mut self, key: K, value: V);
fn len(&self) -> usize;
}
use crate::cache::cache::*;
use crate::numerics::comp::*;
/// A cache implementation that checks all entries one-by-one, without eviction
/// # Generic Types
/// K and V for the cache keys and values
/// Comp : a type that implements a comparison method for keys
struct InfiniteLinearCache<K, V> {
lines: Vec<(K, V)>,
tolerance: f32,
}
impl<K, V> ApproximateCache<K, V> for InfiniteLinearCache<K, V>
where
K: ApproxComparable,
V: Clone,
{
// to find a match in an infinite cache, iterate over all cache lines
// and return early if you have something
fn find(&self, key: &K) -> Option<V> {
let x = self
.lines
.iter()
.find(|&(k, _v)| key.roughly_matches(k, self.tolerance));
match x {
None => None,
Some((_u, v)) => Some(v.clone()),
}
}
// to insert a new value in a
fn insert(&mut self, key: K, value: V) {
self.lines.push((key, value));
}
fn len(&self) -> usize {
self.lines.len()
}
}
mod cache;
mod infinite_linear_cache;
#![allow(dead_code)]
mod linalg;
mod cache;
mod numerics;
fn main() {
println!("Hello, world!");
......
use super::vector::F32Vector;
// rust Ord trait has some issues
pub trait ApproxComparable {
fn roughly_matches(&self, instore: &Self, tolerance: f32) -> bool;
}
impl ApproxComparable for f32 {
fn roughly_matches(&self, target: &f32, tolerance: f32) -> bool {
(self - target).abs() < tolerance
}
}
impl<'a> ApproxComparable for F32Vector<'a> {
fn roughly_matches(&self, target: &F32Vector<'a>, tolerance: f32) -> bool {
self.l2_dist(target) < tolerance
}
}
pub mod comp;
mod vector;
File moved
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