Repository navigation
Expand file tree
/
Copy pathlru-cache.ts
More file actions
115 lines (104 loc) · 3.23 KB
/
Copy pathlru-cache.ts
File metadata and controls
115 lines (104 loc) · 3.23 KB
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
/**
* LRU (least recently used) cache
*
* Idea: keep a hash map from key to node for O(1) lookup, and a doubly linked
* list ordered by recency. Every read or write moves the node to the front.
* When the cache is over capacity, drop the node at the back: it is the one
* nobody has touched for the longest time.
*
* Two sentinel nodes (head and tail) remove all the "is the list empty?"
* special cases from insert and unlink.
*
* (In JavaScript a `Map` already remembers insertion order, so delete + set
* gives the same behaviour in a few lines. The explicit list is shown here
* because it is the version interviews and other languages expect.)
*
* Complexity: get / set / delete O(1); space O(capacity).
*/
class Entry<K, V> {
key: K;
value: V;
prev: Entry<K, V> | null = null;
next: Entry<K, V> | null = null;
constructor(key: K, value: V) {
this.key = key;
this.value = value;
}
}
export class LRUCache<K, V> {
readonly capacity: number;
private map = new Map<K, Entry<K, V>>();
private head: Entry<K, V>;
private tail: Entry<K, V>;
constructor(capacity: number) {
if (!Number.isInteger(capacity) || capacity < 1) {
throw new RangeError("capacity must be a positive integer");
}
this.capacity = capacity;
// Sentinels: their key/value are never read.
this.head = new Entry<K, V>(undefined as K, undefined as V);
this.tail = new Entry<K, V>(undefined as K, undefined as V);
this.head.next = this.tail;
this.tail.prev = this.head;
}
get size(): number {
return this.map.size;
}
has(key: K): boolean {
return this.map.has(key);
}
/** Returns the value and marks the key as most recently used. */
get(key: K): V | undefined {
const entry = this.map.get(key);
if (entry === undefined) return undefined;
this.unlink(entry);
this.pushFront(entry);
return entry.value;
}
/** Inserts or updates a key. Returns the evicted key, if any. */
set(key: K, value: V): K | undefined {
const existing = this.map.get(key);
if (existing !== undefined) {
existing.value = value;
this.unlink(existing);
this.pushFront(existing);
return undefined;
}
const entry = new Entry(key, value);
this.map.set(key, entry);
this.pushFront(entry);
if (this.map.size > this.capacity) {
const oldest = this.tail.prev as Entry<K, V>;
this.unlink(oldest);
this.map.delete(oldest.key);
return oldest.key;
}
return undefined;
}
delete(key: K): boolean {
const entry = this.map.get(key);
if (entry === undefined) return false;
this.unlink(entry);
this.map.delete(key);
return true;
}
/** Keys from most to least recently used. */
keys(): K[] {
const out: K[] = [];
for (let e = this.head.next; e !== null && e !== this.tail; e = e.next) out.push(e.key);
return out;
}
private pushFront(entry: Entry<K, V>): void {
const first = this.head.next as Entry<K, V>;
entry.prev = this.head;
entry.next = first;
first.prev = entry;
this.head.next = entry;
}
private unlink(entry: Entry<K, V>): void {
(entry.prev as Entry<K, V>).next = entry.next;
(entry.next as Entry<K, V>).prev = entry.prev;
entry.prev = null;
entry.next = null;
}
}