-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_lru_cache.js
180 lines (150 loc) · 3.48 KB
/
4_lru_cache.js
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
class LRUCacheItem {
constructor(key = null, val = null) {
this.key = key;
this.value = val;
}
}
class LRUCache {
constructor(limit) {
this.limit = limit;
this.length = 0;
this.itemNodes = new List();
this.nodes = {};
}
size() {
return this.length;
}
get(key) {
const item = this.nodes[key];
if (!item) return null;
this.promote(item);
return item.val.value;
}
set(key, val) {
if (this.isFull()) this.prune();
const item = new LRUCacheItem(key, val);
this.nodes[key] = this.itemNodes.push(item);
this.length++;
}
isFull() {
return this.length === this.limit;
}
prune() {
const item = this.itemNodes.shift();
delete this.nodes[item.key];
this.length--;
}
promote(item) {
this.itemNodes.moveToEnd(item);
}
}
// ----------------------------------------
// Given: Doubly Linked List - Do Not Edit!
// ----------------------------------------
class ListNode {
constructor(val, prev = null, next = null) {
this.prev = prev;
this.val = val;
this.next = next;
}
delete() {
if (this.prev) this.prev.next = this.next;
if (this.next) this.next.prev = this.prev;
}
}
class List {
constructor() {
this.head = null;
this.tail = null;
}
// Insert at the head of the list.
unshift(val) {
if (this.head === null && this.tail === null) {
this.head = new ListNode(val);
this.tail = this.head;
} else {
this.head = new ListNode(val, null, this.head);
this.head.next.prev = this.head;
}
return this.head;
}
// Delete at the head of the list.
shift() {
if (this.head === null && this.tail === null) {
return null;
} else {
let head = this.head;
this.head = this.head.next;
head.delete();
return head.val;
}
}
// Insert at the end of the list.
push(val) {
if (this.head === null && this.tail === null) {
this.head = this.tail = new ListNode(val);
} else {
this.tail = new ListNode(val, this.tail, null);
this.tail.prev.next = this.tail;
}
return this.tail;
}
// Delete at the end of the list.
pop() {
if (this.head === null && this.tail === null) {
return null;
} else {
let tail = this.tail;
this.tail = this.tail.prev;
tail.delete();
return tail.val;
}
}
// Move a node to the front of the List
moveToFront(node) {
if (node === this.tail) {
this.pop();
} else if (node === this.head) {
return;
} else {
node.delete();
}
node.prev = node.next = null;
// Don't delegate to shift, since we want to keep the same
// object.
if (this.head === null && this.tail === null) {
this.head = node;
this.tail = node;
} else {
this.head.prev = node;
node.next = this.head;
this.head = node;
}
}
// Move a node to the end of the List
moveToEnd(node) {
if (node === this.head) {
this.shift();
} else if (node === this.tail) {
return;
} else {
node.delete();
}
// Don't delegate to push, since we want to keep the same
// object.
node.prev = null;
node.next = null;
if (this.head === null && this.tail === null) {
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
}
}
}
exports.List = List;
exports.ListNode = ListNode;
exports.LRUCache = LRUCache;
exports.LRUCacheItem = LRUCacheItem;