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
use super::*;
impl<K, V> LruCache<K, V>
where
K: Clone + Eq + Hash,
{
/// Returns the current number of entries in the
/// cache.
///
/// # Returns
///
/// - `usize` - The number of items in the collection.
pub fn len(&self) -> usize {
self.get_map().len()
}
/// Returns `true` if the cache is empty.
///
/// # Returns
///
/// - `bool` - `true` when the collection is empty.
pub fn is_empty(&self) -> bool {
self.get_map().is_empty()
}
/// Returns `true` if the cache is at capacity.
///
/// # Returns
///
/// - `bool` - A boolean.
pub fn is_full(&self) -> bool {
self.get_map().len() >= *self.get_capacity()
}
/// Returns `true` if the cache contains a value for
/// the given key. Does NOT update the recency (use
/// `get` for that).
///
/// # Arguments
///
/// - `&K` - Shared reference to a `K`.
///
/// # Returns
///
/// - `bool` - A boolean.
pub fn contains(&self, key: &K) -> bool {
self.get_map().contains_key(key)
}
/// Returns the value for the given key, updating the
/// recency so the entry becomes the most-recently-
/// used.
///
/// Returns `None` if the key is not in the cache.
///
/// # Arguments
///
/// - `&K` - Shared reference to a `K`.
///
/// # Returns
///
/// - `Option<V>` - The current value (or a snapshot thereof).
pub fn get(&mut self, key: &K) -> Option<&V> {
if self.get_map().contains_key(key) {
// Promote the key to the front of the
// order deque. Remove its existing position
// first (if any) to avoid duplicates.
self.get_mut_order().retain(|k: &K| k != key);
self.get_mut_order().push_front(key.clone());
self.get_map().get(key)
} else {
None
}
}
/// Returns the value for the given key without
/// updating the recency. Useful for "is this cached?"
/// checks that should not affect eviction order.
///
/// # Arguments
///
/// - `&K` - Shared reference to a `K`.
///
/// # Returns
///
/// - `Option<V>` - `Some(...)` on success, `None` otherwise.
pub fn peek(&self, key: &K) -> Option<&V> {
self.get_map().get(key)
}
/// Inserts a key-value pair into the cache. If the
/// key is already present, the existing value is
/// replaced (and the entry becomes the most-
/// recently-used). If the cache is at capacity and
/// the key is new, the least-recently-used entry is
/// evicted first.
///
/// Returns the evicted entry, if any.
///
/// # Arguments
///
/// - `K: Clone + Eq + Hash` - A generic type parameter.
/// - `V` - A `V` parameter.
///
/// # Returns
///
/// - `Option<(K, V)>` - `Some(...)` on success, `None` otherwise.
pub fn put(&mut self, key: K, value: V) -> Option<(K, V)> {
// Capacity of 0 — silently drop.
if *self.get_capacity() == 0 {
return None;
}
// Updating an existing key.
if self.get_map().contains_key(&key) {
self.get_mut_map().insert(key.clone(), value);
// Promote the key to the front of the
// order deque. Remove its existing position
// first (if any) to avoid duplicates.
self.get_mut_order().retain(|k: &K| k != &key);
self.get_mut_order().push_front(key);
return None;
}
// Inserting a new key. Evict if at capacity.
let evicted: Option<(K, V)> = if self.get_map().len() >= *self.get_capacity() {
let victim_key: K = self.get_mut_order().pop_back()?;
let victim_value: V = self.get_mut_map().remove(&victim_key)?;
Some((victim_key, victim_value))
} else {
None
};
self.get_mut_map().insert(key.clone(), value);
self.get_mut_order().push_front(key);
evicted
}
/// Removes the entry for the given key, returning
/// the removed value if any.
///
/// # Arguments
///
/// - `&K` - Shared reference to a `K`.
///
/// # Returns
///
/// - `Option<V>` - `Some(...)` on success, `None` otherwise.
pub fn remove(&mut self, key: &K) -> Option<V> {
self.get_mut_order().retain(|k: &K| k != key);
self.get_mut_map().remove(key)
}
/// Removes every entry from the cache.
pub fn clear(&mut self) {
self.get_mut_map().clear();
self.get_mut_order().clear();
}
/// Returns an iterator over the entries in
/// most-recently-used-first order.
///
/// # Returns
///
/// - `impl Iterator<Item` - A `impl Iterator<Item` value.
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> {
// We can't return the VecDeque order directly
// because the entries would be in order-deque
// order, not MRU-first order. Actually they
// ARE in MRU-first order — the VecDeque's
// front is MRU. So iterating and mapping through
// the map gives us MRU-first order.
self.get_order()
.iter()
.filter_map(|k: &K| self.get_map().get_key_value(k))
}
/// Returns an iterator over the keys in
/// most-recently-used-first order.
///
/// # Returns
///
/// - `impl Iterator<Item` - A `impl Iterator<Item` value.
pub fn keys(&self) -> impl Iterator<Item = &K> {
self.get_order().iter()
}
/// Returns an iterator over the values in
/// most-recently-used-first order.
///
/// # Returns
///
/// - `impl Iterator<Item` - A `impl Iterator<Item` value.
pub fn values(&self) -> impl Iterator<Item = &V> {
self.get_order()
.iter()
.filter_map(|k: &K| self.get_map().get(k))
}
/// Resizes the cache to a new capacity.
///
/// If the new capacity is smaller than the current
/// size, the least-recently-used entries are
/// evicted until the cache fits.
///
/// # Arguments
///
/// - `usize` - A non-negative integer (`usize`).
pub fn resize(&mut self, new_capacity: usize) {
self.set_capacity(new_capacity);
while self.get_map().len() > *self.get_capacity() {
if let Some(victim_key) = self.get_mut_order().pop_back() {
self.get_mut_map().remove(&victim_key);
} else {
break;
}
}
}
}