1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::hash::{Hash, Hasher};

const SIZE: usize = 1024; // must be small for web/WASM build (for unknown reason)

/// Very stupid/simple key-value cache. TODO: improve
#[derive(Clone)]
pub struct Cache<K, V>([Option<(K, V)>; SIZE]);

impl<K, V> Default for Cache<K, V>
where
    K: Copy,
    V: Copy,
{
    fn default() -> Self {
        Self([None; SIZE])
    }
}

impl<K, V> std::fmt::Debug for Cache<K, V> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Cache")
    }
}

impl<K, V> Cache<K, V>
where
    K: Hash + PartialEq,
{
    pub fn get(&self, key: &K) -> Option<&V> {
        let bucket = (hash(key) % (SIZE as u64)) as usize;
        match &self.0[bucket] {
            Some((k, v)) if k == key => Some(v),
            _ => None,
        }
    }

    pub fn set(&mut self, key: K, value: V) {
        let bucket = (hash(&key) % (SIZE as u64)) as usize;
        self.0[bucket] = Some((key, value));
    }
}

fn hash(value: impl Hash) -> u64 {
    use std::collections::hash_map::DefaultHasher;
    let mut hasher = DefaultHasher::default();
    value.hash(&mut hasher);
    hasher.finish()
}