Skip to content

Instantly share code, notes, and snippets.

@germanescobar
Created September 23, 2023 00:46
Show Gist options
  • Save germanescobar/e5b6b591062df11c2f67de7787ed66ae to your computer and use it in GitHub Desktop.
Save germanescobar/e5b6b591062df11c2f67de7787ed66ae to your computer and use it in GitHub Desktop.
var Node = function(key, value, next, previous) {
this.key = key
this.value = value
this.next = next
this.previous = previous
}
/**
* @param {number} capacity
*/
var LRUCache = function(capacity) {
this.capacity = capacity
this.head = null
this.tail = null
this.index = {}
this.size = 0
};
/**
* @param {number} key
* @return {number}
*/
LRUCache.prototype.get = function(key) {
const node = this.index[key]
if (!node) return -1
// actualizamos las referencias si no es la cabeza de la lista
this.moveToFront(node)
return node.value
};
LRUCache.prototype.moveToFront = function(node) {
if (node.previous) {
// si es la cola, debemos actualizarla
if (!node.next) this.tail = node.previous
// interconectamos el anterior con el siguiente
node.previous.next = node.next
if (node.next) node.next.previous = node.previous
// actualizamos el nodo para que quede de head
this.head.previous = node
node.next = this.head
node.previous = null
this.head = node
}
}
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LRUCache.prototype.put = function(key, value) {
if (this.index[key]) {
const node = this.index[key]
node.value = value
this.moveToFront(node)
} else {
if (this.size === this.capacity) this.evict()
const node = new Node(key, value, this.head)
if (this.head) {
this.head.previous = node
this.head = node
} else {
this.head = node
this.tail = node
}
this.index[key] = node
this.size++
}
};
LRUCache.prototype.evict = function() {
delete this.index[this.tail.key]
this.tail = this.tail.previous
if (!this.tail) {
this.head = null
} else {
this.tail.next = null
}
this.size--
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment