Sha256: 8af8cf6660af90da71d04f2e0e4b34a0363f8aff5b9505d52034b92e4b9abfa6

Contents?: true

Size: 1.87 KB

Versions: 61

Compression:

Stored size: 1.87 KB

Contents

module.exports = function(size) {
  return new LruCache(size)
}

function LruCache(size) {
  this.capacity = size | 0
  this.map = Object.create(null)
  this.list = new DoublyLinkedList()
}

LruCache.prototype.get = function(key) {
  var node = this.map[key]
  if (node == null) return undefined
  this.used(node)
  return node.val
}

LruCache.prototype.set = function(key, val) {
  var node = this.map[key]
  if (node != null) {
    node.val = val
  } else {
    if (!this.capacity) this.prune()
    if (!this.capacity) return false
    node = new DoublyLinkedNode(key, val)
    this.map[key] = node
    this.capacity--
  }
  this.used(node)
  return true
}

LruCache.prototype.used = function(node) {
  this.list.moveToFront(node)
}

LruCache.prototype.prune = function() {
  var node = this.list.pop()
  if (node != null) {
    delete this.map[node.key]
    this.capacity++
  }
}


function DoublyLinkedList() {
  this.firstNode = null
  this.lastNode = null
}

DoublyLinkedList.prototype.moveToFront = function(node) {
  if (this.firstNode == node) return

  this.remove(node)

  if (this.firstNode == null) {
    this.firstNode = node
    this.lastNode = node
    node.prev = null
    node.next = null
  } else {
    node.prev = null
    node.next = this.firstNode
    node.next.prev = node
    this.firstNode = node
  }
}

DoublyLinkedList.prototype.pop = function() {
  var lastNode = this.lastNode
  if (lastNode != null) {
    this.remove(lastNode)
  }
  return lastNode
}

DoublyLinkedList.prototype.remove = function(node) {
  if (this.firstNode == node) {
    this.firstNode = node.next
  } else if (node.prev != null) {
    node.prev.next = node.next
  }
  if (this.lastNode == node) {
    this.lastNode = node.prev
  } else if (node.next != null) {
    node.next.prev = node.prev
  }
}


function DoublyLinkedNode(key, val) {
  this.key = key
  this.val = val
  this.prev = null
  this.next = null
}

Version data entries

61 entries across 54 versions & 20 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/aws4/lru.js
disco_app-0.18.0 test/dummy/node_modules/aws4/lru.js
disco_app-0.18.2 test/dummy/node_modules/aws4/lru.js
tang-0.2.1 spec/tang_app/node_modules/aws4/lru.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/aws4/lru.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/aws4/lru.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/aws4/lru.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/aws4/lru.js
tang-0.2.0 spec/tang_app/node_modules/aws4/lru.js
tang-0.1.0 spec/tang_app/node_modules/aws4/lru.js
tang-0.0.9 spec/tang_app/node_modules/aws4/lru.js
enju_library-0.3.8 spec/dummy/node_modules/aws4/lru.js
ilog-0.4.1 node_modules/aws4/lru.js
ilog-0.4.0 node_modules/aws4/lru.js
ilog-0.3.3 node_modules/aws4/lru.js
learn_create-0.0.22 lib/templates/javascript_lab_template/node_modules/aws4/lru.js
jester-data-8.0.0 node_modules/aws4/lru.js
ezii-os-5.2.1 node_modules/aws4/lru.js
ezii-os-2.0.1 node_modules/aws4/lru.js
ezii-os-1.1.0 node_modules/aws4/lru.js