supa_mdx_lint/utils/
lru.rs

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use std::{
    borrow::Borrow,
    collections::{HashMap, VecDeque},
    hash::Hash,
};

pub(crate) struct LruCache<K, V>
where
    K: Eq + Hash + Clone,
{
    capacity: usize,
    inner: HashMap<K, V>,
    usage_queue: VecDeque<K>,
}

impl<K, V> std::fmt::Debug for LruCache<K, V>
where
    K: std::fmt::Debug + Eq + Hash + Clone,
    V: std::fmt::Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LruCache")
            .field("capacity", &self.capacity)
            .field("inner", &self.inner)
            .field("usage_queue", &self.usage_queue)
            .finish()
    }
}

impl<K, V> Default for LruCache<K, V>
where
    K: Eq + Hash + Clone,
{
    fn default() -> Self {
        Self {
            capacity: 10,
            inner: HashMap::default(),
            usage_queue: VecDeque::default(),
        }
    }
}

impl<K, V> LruCache<K, V>
where
    K: Eq + Hash + Clone,
{
    pub(crate) fn contains_key<Q>(&self, key: &Q) -> bool
    where
        K: Borrow<Q>,
        Q: Hash + Eq + ?Sized,
    {
        self.inner.contains_key(key)
    }

    pub(crate) fn insert(&mut self, key: K, value: V) -> Option<V> {
        if self.inner.contains_key(&key) {
            self.usage_queue.retain(|k| k != &key);
        } else if self.inner.len().saturating_add(1) > self.capacity {
            if let Some(lru_key) = self.usage_queue.pop_front() {
                self.inner.remove(&lru_key);
            }
        }

        self.usage_queue.push_back(key.clone());
        self.inner.insert(key, value)
    }

    pub(crate) fn get(&mut self, key: &K) -> Option<&V> {
        if self.inner.contains_key(key) {
            self.usage_queue.retain(|k| k != key);
            self.usage_queue.push_back(key.clone());
        }
        self.inner.get(key)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_lru_cache_basic() {
        let mut cache = LruCache::<String, i32>::default();

        // Insert some items
        assert_eq!(cache.insert("a".to_string(), 1), None);
        assert_eq!(cache.insert("b".to_string(), 2), None);
        assert_eq!(cache.insert("c".to_string(), 3), None);

        // Check if keys exist
        assert!(cache.contains_key("a"));
        assert!(cache.contains_key("b"));
        assert!(cache.contains_key("c"));

        // Overwrite existing key
        assert_eq!(cache.insert("b".to_string(), 22), Some(2));
        assert!(cache.contains_key("b"));
        assert_eq!(cache.get(&"b".to_string()), Some(&22));
    }

    #[test]
    fn test_lru_cache_eviction() {
        let mut cache = LruCache::<String, i32>::default();
        cache.capacity = 3;

        // Fill the cache
        cache.insert("a".to_string(), 1);
        cache.insert("b".to_string(), 2);
        cache.insert("c".to_string(), 3);

        // All three items should be in the cache
        assert!(cache.contains_key("a"));
        assert!(cache.contains_key("b"));
        assert!(cache.contains_key("c"));

        // Adding a new item should evict the least recently used item (a)
        cache.insert("d".to_string(), 4);
        assert!(!cache.contains_key("a"));
        assert!(cache.contains_key("b"));
        assert!(cache.contains_key("c"));
        assert!(cache.contains_key("d"));

        // Using an item moves it to the back of the queue
        // Access b (now b is most recently used)
        cache.insert("b".to_string(), 22);

        // Adding another item should evict c now
        cache.insert("e".to_string(), 5);
        assert!(!cache.contains_key("a"));
        assert!(cache.contains_key("b"));
        assert!(!cache.contains_key("c"));
        assert!(cache.contains_key("d"));
        assert!(cache.contains_key("e"));
    }
}