-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlru.ts
executable file
·207 lines (185 loc) · 5.03 KB
/
lru.ts
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
class CacheNode<T> {
public id: string;
public value: T;
public prev: CacheNode<T> | null;
public next: CacheNode<T> | null;
/**
* @class CacheNode
* @classdesc A node in a doubly linked list
*
* @param {string} id - The id of the node
* @param {T} value - The value of the node
*/
constructor(id: string, value: T) {
this.id = id;
this.value = value;
this.prev = null;
this.next = null;
}
}
export class LRUCache<T> {
private head: CacheNode<T> | null;
private tail: CacheNode<T> | null;
private filled: number;
private capacity: number;
private addFromHead: boolean;
/**
* @class LRUCache
* @classdesc An LRU cache implementation using a doubly linked list
*
* @param {number} capacity - The capacity of the cache
* @param {boolean} addFromHead - Whether to add new entries to the head of the cache
*/
public constructor(capacity: number, addFromHead: boolean) {
this.head = null;
this.tail = null;
this.filled = 0;
this.capacity = capacity;
this.addFromHead = addFromHead;
}
/**
* @description Adds a new entry to the cache
*
* @param {string} id - The id of the entry
* @param {T} value - The value of the entry
*/
public add(id: string, value: T) {
let entry: CacheNode<T> = new CacheNode(id, value);
if (this.reachedCapacity()) {
this.ejectLRU();
} else {
this.filled++;
}
if (this.addFromHead && this.head) {
this.addAsHead(entry);
} else if (!this.addFromHead && this.tail) {
this.addAsTail(entry);
} else {
this.head = entry;
this.tail = entry;
}
}
/**
* @description Gets an entry by id
*
* @param {object} value - The value to get
* @param {boolean} promote - Whether the entry should be promoted toward the head of the cache
*
*/
public get(id: string, promote: boolean = false) {
let current = this.head;
let prev: CacheNode<T> | null = null;
while (current) {
if (current.id === id) {
if (promote) {
this.promoteWithPrev(current, prev);
} else {
this.removeFromPosition(current);
}
return current.value;
}
prev = current;
current = current.next;
}
return null;
}
/** Removes the least recently used entry from the cache */
private ejectLRU() {
if (this.addFromHead && this.head && this.head.next) {
this.head = this.head.next;
this.head.prev = null;
} else if (this.tail && this.tail.prev && this.tail.next) {
this.tail = this.tail.prev;
this.tail.next = null;
}
}
/**
* @description Removes the entry from its current position in the cache
*
* @param {CacheNode} entry - The entry to remove
*/
private removeFromPosition(entry: CacheNode<T>) {
// If head is also tail
if (this.head === this.tail) {
this.head = null;
this.tail = null;
}
// If we're dealing with the head
else if (!entry.prev && entry.next) {
this.head = entry.next;
this.head.prev = null;
} else if (entry.next && entry.prev) {
entry.prev.next = entry.next;
entry.next.prev = entry.prev;
}
}
/**
* @description Promotes the entry toward the head of the cache, requiring
* the previous entry as argument
*
* @param {CacheNode} entry - Current entry
* @param {CacheNode | null} prev - Previous entry to the current
*/
private promoteWithPrev(entry: CacheNode<T>, prev: CacheNode<T> | null) {
if (prev) {
const tempId = prev.id;
const tempVal = prev.value;
prev.id = entry.id;
entry.id = tempId;
prev.value = entry.value;
entry.value = tempVal;
}
}
/**
* @description Promotes the entry toward the head of the cache
*
* @param {CacheNode} entry
*/
private promote(entry: CacheNode<T>) {
let current = this.head;
let prev: CacheNode<T> | null = null;
while (current && current.id != entry.id) {
prev = current;
current = current.next;
}
if (prev && current) {
let tempVal = prev.value;
let tempId = prev.id;
prev.value = entry.value;
prev.id = entry.id;
current.value = tempVal;
current.id = tempId;
}
}
/**
* @description Predicate for checking whether cache has reached capacity
* @returns A boolean indicating whether the cache has reached capacity
*/
private reachedCapacity() {
return this.filled >= this.capacity;
}
/**
* @description Adds a new entry to the head of the cache
*
* @param {CacheNode} entry - The entry to add to the head of the cache
*/
private addAsHead(entry: CacheNode<T>) {
if (this.head) {
this.head.prev = entry;
entry.next = this.head;
this.head = entry;
}
}
/**
* @description Adds a new entry to the tail of the cache
*
* @param {CacheNode} entry - The entry to add to the tail of the cache
*/
private addAsTail(entry: CacheNode<T>) {
if (this.tail) {
this.tail.next = entry;
entry.prev = this.tail;
this.tail = entry;
}
}
}