Skip to content

Commit 8b1a4b9

Browse files
authored
* fix TheAlgorithms#844 * formatted code according to standard.js
1 parent 4aac366 commit 8b1a4b9

File tree

1 file changed

+17
-66
lines changed

1 file changed

+17
-66
lines changed

Cache/LRUCache.js

Lines changed: 17 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,39 @@
1-
class DoubleLinkedListNode {
2-
// Double Linked List Node built specifically for LRU Cache
3-
constructor (key, val) {
4-
this.key = key
5-
this.val = val
6-
this.next = null
7-
this.prev = null
8-
}
9-
}
10-
11-
class DoubleLinkedList {
12-
// Double Linked List built specifically for LRU Cache
13-
constructor () {
14-
this.head = new DoubleLinkedListNode(null, null)
15-
this.rear = new DoubleLinkedListNode(null, null)
16-
this.head.next = this.rear
17-
this.rear.prev = this.head
18-
}
19-
20-
add (node) {
21-
// Adds the given node to the end of the list (before rear)
22-
const temp = this.rear.prev
23-
temp.next = node
24-
node.prev = temp
25-
this.rear.prev = node
26-
node.next = this.rear
27-
}
28-
29-
remove (node) {
30-
// Removes and returns the given node from the list
31-
const tempLast = node.prev
32-
const tempNext = node.next
33-
node.prev = null
34-
node.next = null
35-
tempLast.next = tempNext
36-
tempNext.prev = tempLast
37-
38-
return node
39-
}
40-
}
41-
421
class LRUCache {
432
// LRU Cache to store a given capacity of data
443
constructor (capacity) {
45-
this.list = new DoubleLinkedList()
4+
this.cache = new Map()
465
this.capacity = capacity
47-
this.numKeys = 0
486
this.hits = 0
497
this.miss = 0
50-
this.cache = {}
518
}
529

5310
cacheInfo () {
5411
// Return the details for the cache instance [hits, misses, capacity, current_size]
55-
return `CacheInfo(hits=${this.hits}, misses=${this.miss}, capacity=${this.capacity}, current size=${this.numKeys})`
12+
return `CacheInfo(hits=${this.hits}, misses=${this.miss}, capacity=${this.capacity}, current size=${this.cache.size})`
5613
}
5714

5815
set (key, value) {
59-
// Sets the value for the input key and updates the Double Linked List
60-
if (!(key in this.cache)) {
61-
if (this.numKeys >= this.capacity) {
62-
const keyToDelete = this.list.head.next.key
63-
this.list.remove(this.cache[keyToDelete])
64-
delete this.cache[keyToDelete]
65-
this.numKeys -= 1
66-
}
67-
this.cache[key] = new DoubleLinkedListNode(key, value)
68-
this.list.add(this.cache[key])
69-
this.numKeys += 1
70-
} else {
71-
const node = this.list.remove(this.cache[key])
72-
node.val = value
73-
this.list.add(node)
16+
// Sets the value for the input key and if the key exists it updates the existing key
17+
if (this.cache.size === this.capacity) {
18+
// delete oldest key existing in map
19+
this.cache.delete(this.cache.keys().next().value)
7420
}
21+
this.cache.set(key, value)
7522
}
7623

7724
get (key) {
78-
// Returns the value for the input key and updates the Double Linked List. Returns null if key is not present in cache
79-
if (key in this.cache) {
25+
// Returns the value for the input key. Returns null if key is not present in cache
26+
if (this.cache.has(key)) {
27+
const value = this.cache.get(key)
28+
// refresh the cache to update the order of key
29+
this.cache.delete(key)
30+
this.cache.set(key, value)
8031
this.hits += 1
81-
this.list.add(this.list.remove(this.cache[key]))
82-
return this.cache[key].val
32+
return value
33+
} else {
34+
this.miss += 1
35+
return null
8336
}
84-
this.miss += 1
85-
return null
8637
}
8738
}
8839

0 commit comments

Comments
 (0)