LRU.js
3.09 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var LinkedListNode = /** @class */ (function () {
function LinkedListNode(key, value) {
this.key = key;
this.value = value;
}
return LinkedListNode;
}());
var LRUCache = /** @class */ (function () {
function LRUCache(size) {
this.nodeMap = {};
this.size = 0;
if (typeof size !== 'number' || size < 1) {
throw new Error('Cache size can only be positive number');
}
this.sizeLimit = size;
}
Object.defineProperty(LRUCache.prototype, "length", {
get: function () {
return this.size;
},
enumerable: true,
configurable: true
});
LRUCache.prototype.prependToList = function (node) {
if (!this.headerNode) {
this.tailNode = node;
}
else {
this.headerNode.prev = node;
node.next = this.headerNode;
}
this.headerNode = node;
this.size++;
};
LRUCache.prototype.removeFromTail = function () {
if (!this.tailNode) {
return undefined;
}
var node = this.tailNode;
var prevNode = node.prev;
if (prevNode) {
prevNode.next = undefined;
}
node.prev = undefined;
this.tailNode = prevNode;
this.size--;
return node;
};
LRUCache.prototype.detachFromList = function (node) {
if (this.headerNode === node) {
this.headerNode = node.next;
}
if (this.tailNode === node) {
this.tailNode = node.prev;
}
if (node.prev) {
node.prev.next = node.next;
}
if (node.next) {
node.next.prev = node.prev;
}
node.next = undefined;
node.prev = undefined;
this.size--;
};
LRUCache.prototype.get = function (key) {
if (this.nodeMap[key]) {
var node = this.nodeMap[key];
this.detachFromList(node);
this.prependToList(node);
return node.value;
}
};
LRUCache.prototype.remove = function (key) {
if (this.nodeMap[key]) {
var node = this.nodeMap[key];
this.detachFromList(node);
delete this.nodeMap[key];
}
};
LRUCache.prototype.put = function (key, value) {
if (this.nodeMap[key]) {
this.remove(key);
}
else if (this.size === this.sizeLimit) {
var tailNode = this.removeFromTail();
var key_1 = tailNode.key;
delete this.nodeMap[key_1];
}
var newNode = new LinkedListNode(key, value);
this.nodeMap[key] = newNode;
this.prependToList(newNode);
};
LRUCache.prototype.empty = function () {
var keys = Object.keys(this.nodeMap);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var node = this.nodeMap[key];
this.detachFromList(node);
delete this.nodeMap[key];
}
};
return LRUCache;
}());
exports.LRUCache = LRUCache;