LRU.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var LinkedListNode = /** @class */ (function () {
  4. function LinkedListNode(key, value) {
  5. this.key = key;
  6. this.value = value;
  7. }
  8. return LinkedListNode;
  9. }());
  10. var LRUCache = /** @class */ (function () {
  11. function LRUCache(size) {
  12. this.nodeMap = {};
  13. this.size = 0;
  14. if (typeof size !== 'number' || size < 1) {
  15. throw new Error('Cache size can only be positive number');
  16. }
  17. this.sizeLimit = size;
  18. }
  19. Object.defineProperty(LRUCache.prototype, "length", {
  20. get: function () {
  21. return this.size;
  22. },
  23. enumerable: true,
  24. configurable: true
  25. });
  26. LRUCache.prototype.prependToList = function (node) {
  27. if (!this.headerNode) {
  28. this.tailNode = node;
  29. }
  30. else {
  31. this.headerNode.prev = node;
  32. node.next = this.headerNode;
  33. }
  34. this.headerNode = node;
  35. this.size++;
  36. };
  37. LRUCache.prototype.removeFromTail = function () {
  38. if (!this.tailNode) {
  39. return undefined;
  40. }
  41. var node = this.tailNode;
  42. var prevNode = node.prev;
  43. if (prevNode) {
  44. prevNode.next = undefined;
  45. }
  46. node.prev = undefined;
  47. this.tailNode = prevNode;
  48. this.size--;
  49. return node;
  50. };
  51. LRUCache.prototype.detachFromList = function (node) {
  52. if (this.headerNode === node) {
  53. this.headerNode = node.next;
  54. }
  55. if (this.tailNode === node) {
  56. this.tailNode = node.prev;
  57. }
  58. if (node.prev) {
  59. node.prev.next = node.next;
  60. }
  61. if (node.next) {
  62. node.next.prev = node.prev;
  63. }
  64. node.next = undefined;
  65. node.prev = undefined;
  66. this.size--;
  67. };
  68. LRUCache.prototype.get = function (key) {
  69. if (this.nodeMap[key]) {
  70. var node = this.nodeMap[key];
  71. this.detachFromList(node);
  72. this.prependToList(node);
  73. return node.value;
  74. }
  75. };
  76. LRUCache.prototype.remove = function (key) {
  77. if (this.nodeMap[key]) {
  78. var node = this.nodeMap[key];
  79. this.detachFromList(node);
  80. delete this.nodeMap[key];
  81. }
  82. };
  83. LRUCache.prototype.put = function (key, value) {
  84. if (this.nodeMap[key]) {
  85. this.remove(key);
  86. }
  87. else if (this.size === this.sizeLimit) {
  88. var tailNode = this.removeFromTail();
  89. var key_1 = tailNode.key;
  90. delete this.nodeMap[key_1];
  91. }
  92. var newNode = new LinkedListNode(key, value);
  93. this.nodeMap[key] = newNode;
  94. this.prependToList(newNode);
  95. };
  96. LRUCache.prototype.empty = function () {
  97. var keys = Object.keys(this.nodeMap);
  98. for (var i = 0; i < keys.length; i++) {
  99. var key = keys[i];
  100. var node = this.nodeMap[key];
  101. this.detachFromList(node);
  102. delete this.nodeMap[key];
  103. }
  104. };
  105. return LRUCache;
  106. }());
  107. exports.LRUCache = LRUCache;