mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
* fix #844 * formatted code according to standard.js
This commit is contained in:
@ -1,88 +1,39 @@
|
|||||||
class DoubleLinkedListNode {
|
|
||||||
// Double Linked List Node built specifically for LRU Cache
|
|
||||||
constructor (key, val) {
|
|
||||||
this.key = key
|
|
||||||
this.val = val
|
|
||||||
this.next = null
|
|
||||||
this.prev = null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class DoubleLinkedList {
|
|
||||||
// Double Linked List built specifically for LRU Cache
|
|
||||||
constructor () {
|
|
||||||
this.head = new DoubleLinkedListNode(null, null)
|
|
||||||
this.rear = new DoubleLinkedListNode(null, null)
|
|
||||||
this.head.next = this.rear
|
|
||||||
this.rear.prev = this.head
|
|
||||||
}
|
|
||||||
|
|
||||||
add (node) {
|
|
||||||
// Adds the given node to the end of the list (before rear)
|
|
||||||
const temp = this.rear.prev
|
|
||||||
temp.next = node
|
|
||||||
node.prev = temp
|
|
||||||
this.rear.prev = node
|
|
||||||
node.next = this.rear
|
|
||||||
}
|
|
||||||
|
|
||||||
remove (node) {
|
|
||||||
// Removes and returns the given node from the list
|
|
||||||
const tempLast = node.prev
|
|
||||||
const tempNext = node.next
|
|
||||||
node.prev = null
|
|
||||||
node.next = null
|
|
||||||
tempLast.next = tempNext
|
|
||||||
tempNext.prev = tempLast
|
|
||||||
|
|
||||||
return node
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class LRUCache {
|
class LRUCache {
|
||||||
// LRU Cache to store a given capacity of data
|
// LRU Cache to store a given capacity of data
|
||||||
constructor (capacity) {
|
constructor (capacity) {
|
||||||
this.list = new DoubleLinkedList()
|
this.cache = new Map()
|
||||||
this.capacity = capacity
|
this.capacity = capacity
|
||||||
this.numKeys = 0
|
|
||||||
this.hits = 0
|
this.hits = 0
|
||||||
this.miss = 0
|
this.miss = 0
|
||||||
this.cache = {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheInfo () {
|
cacheInfo () {
|
||||||
// Return the details for the cache instance [hits, misses, capacity, current_size]
|
// Return the details for the cache instance [hits, misses, capacity, current_size]
|
||||||
return `CacheInfo(hits=${this.hits}, misses=${this.miss}, capacity=${this.capacity}, current size=${this.numKeys})`
|
return `CacheInfo(hits=${this.hits}, misses=${this.miss}, capacity=${this.capacity}, current size=${this.cache.size})`
|
||||||
}
|
}
|
||||||
|
|
||||||
set (key, value) {
|
set (key, value) {
|
||||||
// Sets the value for the input key and updates the Double Linked List
|
// Sets the value for the input key and if the key exists it updates the existing key
|
||||||
if (!(key in this.cache)) {
|
if (this.cache.size === this.capacity) {
|
||||||
if (this.numKeys >= this.capacity) {
|
// delete oldest key existing in map
|
||||||
const keyToDelete = this.list.head.next.key
|
this.cache.delete(this.cache.keys().next().value)
|
||||||
this.list.remove(this.cache[keyToDelete])
|
|
||||||
delete this.cache[keyToDelete]
|
|
||||||
this.numKeys -= 1
|
|
||||||
}
|
|
||||||
this.cache[key] = new DoubleLinkedListNode(key, value)
|
|
||||||
this.list.add(this.cache[key])
|
|
||||||
this.numKeys += 1
|
|
||||||
} else {
|
|
||||||
const node = this.list.remove(this.cache[key])
|
|
||||||
node.val = value
|
|
||||||
this.list.add(node)
|
|
||||||
}
|
}
|
||||||
|
this.cache.set(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
get (key) {
|
get (key) {
|
||||||
// Returns the value for the input key and updates the Double Linked List. Returns null if key is not present in cache
|
// Returns the value for the input key. Returns null if key is not present in cache
|
||||||
if (key in this.cache) {
|
if (this.cache.has(key)) {
|
||||||
|
const value = this.cache.get(key)
|
||||||
|
// refresh the cache to update the order of key
|
||||||
|
this.cache.delete(key)
|
||||||
|
this.cache.set(key, value)
|
||||||
this.hits += 1
|
this.hits += 1
|
||||||
this.list.add(this.list.remove(this.cache[key]))
|
return value
|
||||||
return this.cache[key].val
|
} else {
|
||||||
|
this.miss += 1
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
this.miss += 1
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user