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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use crate::linked_list::{LinkedList, ListCursor};
use std::{collections::HashMap, fmt::Debug, hash::Hash};
/// A simple LRU (Least Recently Used) cache implementation with fixed capacity.
///
/// This implementation uses a combination of a linked list for maintaining access order
/// and a hash map for O(1) lookups. The cache has a fixed capacity and automatically
/// evicts the least recently used items when full.
///
/// # Type Parameters
///
/// * `Key`: The type of keys used in the cache. Must implement `Copy`, `Debug`, `Eq`, and `Hash`.
/// * `Value`: The type of values stored in the cache.
///
/// # Examples
///
/// ```
/// use toolbox_rs::lru::LRU;
///
/// // Create a new LRU cache with capacity 2
/// let mut cache = LRU::new_with_capacity(2);
///
/// // Add some items
/// cache.push(&1, "one");
/// cache.push(&2, "two");
///
/// assert_eq!(cache.get(&1), Some(&"one"));
/// assert_eq!(cache.len(), 2);
///
/// // Adding another item will evict the least recently used item (2)
/// cache.push(&3, "three");
/// assert!(!cache.contains(&2));
/// assert!(cache.contains(&1));
/// assert!(cache.contains(&3));
/// ```
pub struct LRU<Key: Copy + Debug + Eq + Hash, Value> {
lru_list: LinkedList<(Key, Value)>,
access_map: HashMap<Key, ListCursor<(Key, Value)>>,
capacity: usize,
}
impl<Key: Copy + Debug + Eq + Hash, Value> LRU<Key, Value> {
/// Creates a new LRU cache with the specified capacity.
///
/// # Arguments
///
/// * `capacity` - The maximum number of key-value pairs the cache can hold
///
/// # Panics
///
/// Panics in debug mode if capacity is 0.
///
/// # Examples
///
/// ```
/// use toolbox_rs::lru::LRU;
///
/// let cache: LRU<i32, &str> = LRU::new_with_capacity(10);
/// assert_eq!(cache.capacity(), 10);
/// assert!(cache.is_empty());
/// ```
pub fn new_with_capacity(capacity: usize) -> Self {
debug_assert!(capacity > 0);
let storage = LinkedList::new();
let indices = HashMap::with_capacity(capacity);
LRU {
lru_list: storage,
access_map: indices,
capacity,
}
}
/// Inserts a key-value pair into the cache.
///
/// If the cache is at capacity, the least recently used item will be evicted.
/// If the key already exists, the value will be updated and the entry will
/// become the most recently used.
///
/// # Arguments
///
/// * `key` - The key to insert
/// * `value` - The value to associate with the key
///
/// # Examples
///
/// ```
/// use toolbox_rs::lru::LRU;
///
/// let mut cache = LRU::new_with_capacity(2);
/// cache.push(&1, "one");
/// cache.push(&2, "two");
/// cache.push(&3, "three"); // This will evict key 1
///
/// assert!(!cache.contains(&1));
/// assert!(cache.contains(&2));
/// assert!(cache.contains(&3));
/// ```
pub fn push(&mut self, key: &Key, value: Value) {
debug_assert!(self.lru_list.len() <= self.capacity);
if let Some(handle) = self.access_map.get(key) {
// Key exists - move to front and update value
let handle = *handle;
self.lru_list.move_to_front(&handle);
// Update the value using a mutable reference
let front = self.lru_list.get_front_mut();
*front = (*key, value);
return;
}
// Key doesn't exist - handle capacity and insert new entry
if self.access_map.len() == self.capacity {
// evict least recently used element
debug_assert!(!self.access_map.is_empty());
if let Some((evicted_key, _)) = self.lru_list.pop_back() {
self.access_map.remove(&evicted_key);
}
}
// Insert new entry
let handle = self.lru_list.push_front((*key, value));
self.access_map.insert(*key, handle);
}
/// Returns true if the cache contains the specified key.
///
/// This operation does not affect the order of items in the cache.
///
/// # Arguments
///
/// * `key` - The key to look up
///
/// # Examples
///
/// ```
/// use toolbox_rs::lru::LRU;
///
/// let mut cache = LRU::new_with_capacity(1);
/// cache.push(&1, "one");
/// assert!(cache.contains(&1));
/// assert!(!cache.contains(&2));
/// ```
pub fn contains(&self, key: &Key) -> bool {
self.access_map.contains_key(key)
}
/// Gets the value associated with the key and marks it as most recently used.
///
/// # Arguments
///
/// * `key` - The key to look up
///
/// # Returns
///
/// Returns `Some(&Value)` if the key exists, or `None` if it doesn't.
///
/// # Examples
///
/// ```
/// use toolbox_rs::lru::LRU;
///
/// let mut cache = LRU::new_with_capacity(2);
/// cache.push(&1, "one");
/// cache.push(&2, "two");
///
/// assert_eq!(cache.get(&1), Some(&"one"));
/// cache.push(&3, "three"); // This will evict key 2, not 1
/// assert!(cache.contains(&1)); // 1 was most recently used
/// assert!(!cache.contains(&2)); // 2 was evicted
/// ```
pub fn get(&mut self, key: &Key) -> Option<&Value> {
if let Some(handle) = self.access_map.get(key) {
self.lru_list.move_to_front(handle);
return Some(&self.lru_list.get_front().1);
}
None
}
/// Returns a reference to the most recently used (front) item in the cache.
///
/// # Returns
///
/// Returns `Option<(&Key, &Value)>` - `Some` with references to the key and value if the cache
/// is not empty, or `None` if the cache is empty.
///
/// # Examples
///
/// ```
/// use toolbox_rs::lru::LRU;
///
/// let mut cache = LRU::new_with_capacity(2);
/// assert_eq!(cache.get_front(), None);
///
/// cache.push(&1, "one");
/// cache.push(&2, "two");
/// assert_eq!(cache.get_front(), Some((&2, &"two")));
/// ```
pub fn get_front(&self) -> Option<(&Key, &Value)> {
if self.is_empty() {
None
} else {
let front = self.lru_list.get_front();
Some((&front.0, &front.1))
}
}
/// Returns a mutable reference to the most recently used (front) item in the cache.
///
/// # Returns
///
/// Returns `Option<(&Key, &mut Value)>` - `Some` with a reference to the key and mutable reference
/// to the value if the cache is not empty, or `None` if the cache is empty.
///
/// # Examples
///
/// ```
/// use toolbox_rs::lru::LRU;
///
/// let mut cache = LRU::new_with_capacity(2);
/// cache.push(&1, String::from("one"));
/// cache.push(&2, String::from("two"));
///
/// if let Some((_, value)) = cache.get_front_mut() {
/// *value = String::from("TWO");
/// }
/// assert_eq!(cache.get(&2), Some(&String::from("TWO")));
/// ```
pub fn get_front_mut(&mut self) -> Option<(&Key, &mut Value)> {
if self.is_empty() {
None
} else {
let front = self.lru_list.get_front_mut();
Some((&front.0, &mut front.1))
}
}
/// Returns the maximum number of items the cache can hold.
///
/// # Examples
///
/// ```
/// use toolbox_rs::lru::LRU;
///
/// let cache: LRU<i32, &str> = LRU::new_with_capacity(5);
/// assert_eq!(cache.capacity(), 5);
/// ```
pub fn capacity(&self) -> usize {
self.capacity
}
/// Returns the current number of items in the cache.
///
/// # Examples
///
/// ```
/// use toolbox_rs::lru::LRU;
///
/// let mut cache = LRU::new_with_capacity(2);
/// assert_eq!(cache.len(), 0);
///
/// cache.push(&1, "one");
/// assert_eq!(cache.len(), 1);
/// ```
pub fn len(&self) -> usize {
assert_eq!(self.lru_list.len(), self.access_map.len());
self.lru_list.len()
}
/// Returns true if the cache contains no items.
///
/// # Examples
///
/// ```
/// use toolbox_rs::lru::LRU;
///
/// let mut cache = LRU::new_with_capacity(2);
/// assert!(cache.is_empty());
///
/// cache.push(&1, "one");
/// assert!(!cache.is_empty());
///
/// cache.clear();
/// assert!(cache.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Removes all items from the cache.
///
/// # Examples
///
/// ```
/// use toolbox_rs::lru::LRU;
///
/// let mut cache = LRU::new_with_capacity(2);
/// cache.push(&1, "one");
/// cache.push(&2, "two");
///
/// cache.clear();
/// assert!(cache.is_empty());
/// assert_eq!(cache.len(), 0);
/// ```
pub fn clear(&mut self) {
self.lru_list.clear();
self.access_map.clear();
}
}
#[cfg(test)]
mod tests {
use super::LRU;
struct SomeTestStruct(i32);
#[test]
fn construct() {
let mut lru = LRU::new_with_capacity(10);
assert_eq!(0, lru.len());
lru.push(&1, SomeTestStruct(1));
assert_eq!(1, lru.len());
lru.push(&2, SomeTestStruct(2));
assert_eq!(2, lru.len());
lru.push(&3, SomeTestStruct(3));
assert_eq!(3, lru.len());
lru.push(&4, SomeTestStruct(4));
assert_eq!(4, lru.len());
lru.push(&5, SomeTestStruct(5));
assert_eq!(5, lru.len());
lru.push(&6, SomeTestStruct(6));
assert_eq!(6, lru.len());
lru.push(&7, SomeTestStruct(7));
assert_eq!(7, lru.len());
lru.push(&8, SomeTestStruct(8));
assert_eq!(8, lru.len());
lru.push(&9, SomeTestStruct(9));
assert_eq!(9, lru.len());
lru.push(&10, SomeTestStruct(10));
assert_eq!(10, lru.len());
assert_eq!(10, lru.capacity());
// access 1, make 2 the oldest element now
let handle = lru.get(&1).unwrap();
assert_eq!(1, handle.0);
// add 11, evict 2
lru.push(&11, SomeTestStruct(11));
assert!(!lru.contains(&2));
assert_eq!(lru.len(), 10);
// get handle for evicted element and verify it is None
let handle = lru.get(&2);
assert!(handle.is_none());
// assert that all other elements are still cached
let keys = vec![1, 3, 4, 5, 6, 7, 8, 9, 10, 11];
keys.into_iter().for_each(|key| {
assert!(lru.contains(&key));
});
}
#[test]
fn clear_is_empty() {
let mut lru = LRU::new_with_capacity(10);
for i in [0, 1, 2, 3] {
lru.push(&i, 2 * i);
}
assert!(!lru.is_empty());
lru.clear();
assert!(lru.is_empty());
}
#[test]
fn test_update_existing_key() {
let mut lru = LRU::new_with_capacity(2);
lru.push(&1, "one");
lru.push(&2, "two");
// Update existing key
lru.push(&1, "ONE");
assert_eq!(lru.get(&1), Some(&"ONE"));
assert_eq!(lru.len(), 2);
}
#[test]
fn test_get_updates_order() {
let mut lru = LRU::new_with_capacity(3);
lru.push(&1, "one");
lru.push(&2, "two");
lru.push(&3, "three");
// Access 1, making it most recently used
assert_eq!(lru.get(&1), Some(&"one"));
// Add new item - should evict 2, not 1
lru.push(&4, "four");
assert!(lru.contains(&1));
assert!(!lru.contains(&2));
assert!(lru.contains(&3));
assert!(lru.contains(&4));
}
#[test]
fn test_capacity_bounds() {
let mut lru = LRU::new_with_capacity(1);
lru.push(&1, "one");
assert_eq!(lru.len(), 1);
assert!(lru.contains(&1));
lru.push(&2, "two");
assert_eq!(lru.len(), 1);
assert!(!lru.contains(&1));
assert!(lru.contains(&2));
}
#[test]
fn test_clear_retains_capacity() {
let mut lru = LRU::new_with_capacity(5);
for i in 0..5 {
lru.push(&i, i.to_string());
}
assert_eq!(lru.len(), 5);
lru.clear();
assert_eq!(lru.len(), 0);
assert_eq!(lru.capacity(), 5);
// Can still add items up to capacity
for i in 0..5 {
lru.push(&i, i.to_string());
}
assert_eq!(lru.len(), 5);
}
#[test]
fn test_get_front_mut() {
let mut lru = LRU::new_with_capacity(3);
assert_eq!(lru.get_front_mut(), None);
lru.push(&1, String::from("one"));
lru.push(&2, String::from("two"));
// Test mutating the front element
if let Some((key, value)) = lru.get_front_mut() {
assert_eq!(key, &2);
assert_eq!(value, "two");
*value = String::from("TWO");
}
// Verify the change was applied
assert_eq!(lru.get(&2), Some(&String::from("TWO")));
// Add another element and verify front changes
lru.push(&3, String::from("three"));
if let Some((key, value)) = lru.get_front_mut() {
assert_eq!(key, &3);
assert_eq!(value, "three");
*value = String::from("THREE");
}
// Verify both modified values are still correct
assert_eq!(lru.get(&2), Some(&String::from("TWO")));
assert_eq!(lru.get(&3), Some(&String::from("THREE")));
}
}