-
Notifications
You must be signed in to change notification settings - Fork 9
/
lrucache.js
257 lines (214 loc) · 5.35 KB
/
lrucache.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
//////
// ListEntry
function ListEntry(data) {
this.__ll__ = {};
this.__ll__.next = null;
this.__ll__.prev = null;
this.__ll__.data = data;
}
ListEntry.prototype.Next = function () { return this.__ll__.next };
ListEntry.prototype.Prev = function () { return this.__ll__.prev };
ListEntry.prototype.GetData = function () { return this.__ll__.data };
//////
// LinkedList
function LinkedList() {
this.first = null;
this.last = null;
this.entries = 0;
}
LinkedList.prototype.AppendLast = function (e) {
if (!e.__ll__) {
throw new Error("Object is no list entry");
}
if (this.last) {
// modify end of list
this.last.__ll__.next = e; // make successor of last eentry
e.__ll__.prev = this.last; // make last entry successor of new entry
this.last = e; // make entry the new last entry
} else {
// only list entry, modify first and last
this.first = e;
this.last = e;
}
this.entries++;
}
LinkedList.prototype.InsertFirst = function (e) {
if (!e.__ll__) {
throw new Error("Object is no list entry");
}
if (this.first) {
this.first.__ll__.prev = e; // make predecessor of first entry
e.__ll__.next = this.first; // make first entry predecessor of new entry
this.first = e; // make new entry the new first entry
} else {
// only list entry, modify first and last
this.first = e;
this.last = e;
}
this.entries++;
}
LinkedList.prototype.InsertAfter = function (e, existing) {
if (!e.__ll__) {
throw new Error("Object is no list entry!");
}
if (!existing.__ll__.prev && !existing.__ll__.next) {
throw new Error("Predecessor is not in list!");
}
var old_next = existing.__ll__.next; // keep old next entry
existing.__ll__.next = e; // make new entry the successor of the existing entry
e.__ll__.prev = existing; // and make the existing entry the predecessor of the new
if (old_next) { // check if there was a next entry
old_next.__ll__.prev = e; // yes-> link new entry and old next entry
e.__ll__.next = old_next;
} else {
this.last = e; // no-> make entry the new last list entry
}
this.entries++;
}
LinkedList.prototype.GetFirst = function () {
return this.first;
}
LinkedList.prototype.GetLast = function () {
return this.last;
}
LinkedList.prototype.Size = function () {
return this.entries;
}
LinkedList.prototype.Remove = function (e) {
if (!e.__ll__) {
throw new Error("Object is no list entry!");
}
if (!e.__ll__.prev && !e.__ll__.next && (this.first !== e)) {
throw new Error("Element is not in list!");
}
if (e.__ll__.next) {
var next_entry = e.__ll__.next;
next_entry.__ll__.prev = e.__ll__.prev;
} else {
this.last = e.__ll__.prev;
}
if (e.__ll__.prev) {
var prev_entry = e.__ll__.prev;
prev_entry.__ll__.next = e.__ll__.next;
} else {
this.first = e.__ll__.next;
}
e.__ll__.next = null;
e.__ll__.prev = null;
this.entries--;
}
//////
// LRUCache
function LRUCache(maxSize) {
this.maxSize = maxSize;
this.objects = {};
this.lruList = new LinkedList();
}
LRUCache.prototype.Size = function () {
return this.lruList.Size();
}
LRUCache.prototype.Get = function (key) {
if (this.objects[key]) {
var entry = this.objects[key];
this.lruList.Remove(entry.list); // remove from LRU
this.lruList.InsertFirst(entry.list); // queue as first LRU entry
return entry.data;
} else {
return null;
}
}
LRUCache.prototype.Remove = function (key) {
if (this.objects[key]) {
var entry = this.objects[key];
this.lruList.Remove(entry.list); // remove from LRU
delete this.objects[key]; // and from cache
}
}
LRUCache.prototype.Put = function (key, val) {
var entry;
var le;
if (this.objects[key]) {
// entry exists
entry = this.objects[key];
le = entry.list;
this.lruList.Remove(le);
} else {
// new entry
entry = {
"data": val,
"list": new ListEntry(key)
};
le = entry.list;
}
while (this.Size() > this.maxSize - 1) {
this.Remove(this.lruList.GetLast().GetData());
}
this.objects[key] = entry; // insert cache entry object
this.lruList.InsertFirst(le); // queue as first LRU entry
}
// export functions and version
exports.__VERSION__ = 1;
exports.ListEntry = ListEntry;
exports.LinkedList = LinkedList;
exports.LRUCache = LRUCache;
/*
function Setup() {
var c = new LRUCache(5);
for (var i = 0; i < 10; i++) {
c.Put("" + i, "" + i);
}
Println(c.Size());
printList(c.lruList);
Println(c.Get("5"));
Println(c.Get("6"));
Println(c.Get("3"));
Println(c.Get("2"));
printList(c.lruList);
Stop();
}
function SetupLL() {
var ll = new LinkedList();
for (var i = 0; i < 5; i++) {
var e = new ListEntry("" + i);
ll.AppendLast(e);
}
printList(ll);
ll.InsertFirst(new ListEntry("A"));
ll.AppendLast(new ListEntry("B"));
printList(ll);
var e = ll.GetFirst();
e = e.Next();
e = e.Next();
e = e.Next();
Println("Insert after " + e.GetData());
ll.InsertAfter(new ListEntry("X"), e);
printList(ll);
Println("removing " + e.GetData());
ll.Remove(e);
printList(ll);
ll.Remove(ll.GetFirst());
printList(ll);
ll.Remove(ll.GetLast());
printList(ll);
printList_rev(ll);
Stop();
}
function Loop() { }
function Input() { }
function printList(ll) {
Println("Iterating: " + ll.Size());
var it = ll.GetFirst();
while (it) {
Println(" " + it.GetData());
it = it.Next();
}
}
function printList_rev(ll) {
Println("Reverse Iterating: " + ll.Size());
var it = ll.GetLast();
while (it) {
Println(" " + it.GetData());
it = it.Prev();
}
}
*/