|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Represents a node in the doubly linked list. |
| 11 | + */ |
| 12 | +interface Node<Key, Value> { |
| 13 | + key: Key; |
| 14 | + value: Value; |
| 15 | + prev: Node<Key, Value> | undefined; |
| 16 | + next: Node<Key, Value> | undefined; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * A Least Recently Used (LRU) cache implementation. |
| 21 | + * |
| 22 | + * This cache stores a fixed number of key-value pairs, and when the cache exceeds its capacity, |
| 23 | + * the least recently accessed items are evicted. |
| 24 | + * |
| 25 | + * @template Key - The type of the cache keys. |
| 26 | + * @template Value - The type of the cache values. |
| 27 | + */ |
| 28 | +export class LRUCache<Key, Value> { |
| 29 | + /** |
| 30 | + * The maximum number of items the cache can hold. |
| 31 | + */ |
| 32 | + capacity: number; |
| 33 | + |
| 34 | + /** |
| 35 | + * Internal storage for the cache, mapping keys to their associated nodes in the linked list. |
| 36 | + */ |
| 37 | + private readonly cache = new Map<Key, Node<Key, Value>>(); |
| 38 | + |
| 39 | + /** |
| 40 | + * Head of the doubly linked list, representing the most recently used item. |
| 41 | + */ |
| 42 | + private head: Node<Key, Value> | undefined; |
| 43 | + |
| 44 | + /** |
| 45 | + * Tail of the doubly linked list, representing the least recently used item. |
| 46 | + */ |
| 47 | + private tail: Node<Key, Value> | undefined; |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a new LRUCache instance. |
| 51 | + * @param capacity The maximum number of items the cache can hold. |
| 52 | + */ |
| 53 | + constructor(capacity: number) { |
| 54 | + this.capacity = capacity; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Gets the value associated with the given key. |
| 59 | + * @param key The key to retrieve the value for. |
| 60 | + * @returns The value associated with the key, or undefined if the key is not found. |
| 61 | + */ |
| 62 | + get(key: Key): Value | undefined { |
| 63 | + const node = this.cache.get(key); |
| 64 | + if (node) { |
| 65 | + this.moveToHead(node); |
| 66 | + |
| 67 | + return node.value; |
| 68 | + } |
| 69 | + |
| 70 | + return undefined; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Puts a key-value pair into the cache. |
| 75 | + * If the key already exists, the value is updated. |
| 76 | + * If the cache is full, the least recently used item is evicted. |
| 77 | + * @param key The key to insert or update. |
| 78 | + * @param value The value to associate with the key. |
| 79 | + */ |
| 80 | + put(key: Key, value: Value): void { |
| 81 | + const cachedNode = this.cache.get(key); |
| 82 | + if (cachedNode) { |
| 83 | + // Update existing node |
| 84 | + cachedNode.value = value; |
| 85 | + this.moveToHead(cachedNode); |
| 86 | + |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Create a new node |
| 91 | + const newNode: Node<Key, Value> = { key, value, prev: undefined, next: undefined }; |
| 92 | + this.cache.set(key, newNode); |
| 93 | + this.addToHead(newNode); |
| 94 | + |
| 95 | + if (this.cache.size > this.capacity) { |
| 96 | + // Evict the LRU item |
| 97 | + const tail = this.removeTail(); |
| 98 | + if (tail) { |
| 99 | + this.cache.delete(tail.key); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Adds a node to the head of the linked list. |
| 106 | + * @param node The node to add. |
| 107 | + */ |
| 108 | + private addToHead(node: Node<Key, Value>): void { |
| 109 | + node.next = this.head; |
| 110 | + node.prev = undefined; |
| 111 | + |
| 112 | + if (this.head) { |
| 113 | + this.head.prev = node; |
| 114 | + } |
| 115 | + |
| 116 | + this.head = node; |
| 117 | + |
| 118 | + if (!this.tail) { |
| 119 | + this.tail = node; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Removes a node from the linked list. |
| 125 | + * @param node The node to remove. |
| 126 | + */ |
| 127 | + private removeNode(node: Node<Key, Value>): void { |
| 128 | + if (node.prev) { |
| 129 | + node.prev.next = node.next; |
| 130 | + } else { |
| 131 | + this.head = node.next; |
| 132 | + } |
| 133 | + |
| 134 | + if (node.next) { |
| 135 | + node.next.prev = node.prev; |
| 136 | + } else { |
| 137 | + this.tail = node.prev; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Moves a node to the head of the linked list. |
| 143 | + * @param node The node to move. |
| 144 | + */ |
| 145 | + private moveToHead(node: Node<Key, Value>): void { |
| 146 | + this.removeNode(node); |
| 147 | + this.addToHead(node); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Removes the tail node from the linked list. |
| 152 | + * @returns The removed tail node, or undefined if the list is empty. |
| 153 | + */ |
| 154 | + private removeTail(): Node<Key, Value> | undefined { |
| 155 | + const node = this.tail; |
| 156 | + if (node) { |
| 157 | + this.removeNode(node); |
| 158 | + } |
| 159 | + |
| 160 | + return node; |
| 161 | + } |
| 162 | +} |
0 commit comments