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
use *;
/// A fixed-capacity LRU cache.
///
/// The cache holds at most `capacity` entries. When a
/// `put` would exceed the capacity, the
/// least-recently-used entry is evicted. `get` updates
/// the recency so the just-read entry becomes the most-
/// recently-used.
///
/// All operations are O(1) amortized (`put`, `get`,
/// `remove`, `contains`) except `iter`, which is O(n).
///
/// # Capacity edge cases
///
/// - `capacity = 0` - the cache accepts no entries. Both
/// `put` and `get` behave as no-ops (well, `get` still
/// evicts because there's nothing to evict; `put`
/// silently drops the entry).
/// - `capacity = 1` - the cache holds exactly one entry.
/// Every `put` evicts the previous entry.
///
/// # Lombok `New` derivation
///
/// `#[derive(New)]` generates `LruCache::new(capacity)` —
/// the `map` and `order` fields are skipped with
/// `#[new(skip)]` so Lombok falls back to
/// `<HashMap as Default>::default()` and
/// `<VecDeque as Default>::default()` (which both call
/// `new()` internally), preserving the canonical
/// single-argument call site.