-
Notifications
You must be signed in to change notification settings - Fork 0
/
lruCache.js
99 lines (94 loc) · 2.1 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
/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.capacity = capacity
this.queue = new DblLinkedListQueue()
this.cache = new Map()
}
class DblLinkedListQueue {
constructor() {
this.front = null
this.tail = null
this.size = 0
}
enqueue(key, value) {
const node = {key, value, prev: null, next:null}
if(!this.front && !this.tail) {
this.front = this.tail = node
} else {
this.tail.next = node
node.prev = this.tail
this.tail = node
}
this.size++
return node
}
dequeue(){
if(!this.front || !this.tail || this.size < 1)
return null
const node = this.front
if(this.size === 1)
this.front = this.tail = null
else {
node.next.prev = null
this.front = node.next
}
this.size--
return node
}
use(node) {
if(!node || this.tail === node)
return node
if(this.front === node) {
node.next.prev = null
this.front = node.next
} else {
node.prev.next = node.next
node.next.prev = node.prev
}
this.tail.next = node
node.prev = this.tail
this.tail = node
node.next = null
return node
}
}
/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
const node = this.cache.get(key)
if(!node)
return -1
this.queue.use(node)
return node.value
};
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function(key, value) {
// check cache for key
const node = this.cache.get(key)
const size = this.queue.size
if(!node) {
if(size === this.capacity) {
const {key} = this.queue.dequeue()
this.cache.delete(key)
}
const node = this.queue.enqueue(key, value)
this.cache.set(key, node)
} else {
this.queue.use(node)
node.value = value
}
};
/**
* Your LRUCache object will be instantiated and called as such:
* var obj = new LRUCache(capacity)
* var param_1 = obj.get(key)
* obj.put(key,value)
*/