/** * LRU (Least Recently Used) cache implementation. * Based on the Go iavl cache/cache.go implementation. * * Uses a doubly-linked list + Map for O(1) operations: * - add: O(1) * - get: O(1) * - has: O(1) * - remove: O(1) */ /** * Node in the doubly-linked list. */ class ListNode { constructor( public key: K, public value: V, public prev: ListNode | null = null, public next: ListNode | null = null, ) {} } /** * Doubly-linked list for LRU ordering. */ class DoublyLinkedList { private head: ListNode | null = null; private tail: ListNode | null = null; private count = 0; /** * Add node to the front (most recently used). */ addToFront(node: ListNode): void { if (!this.head) { this.head = node; this.tail = node; } else { node.next = this.head; this.head.prev = node; this.head = node; } this.count++; } /** * Remove node from the list. */ remove(node: ListNode): void { if (node.prev) { node.prev.next = node.next; } else { this.head = node.next; } if (node.next) { node.next.prev = node.prev; } else { this.tail = node.prev; } node.prev = null; node.next = null; this.count--; } /** * Move node to front (mark as most recently used). */ moveToFront(node: ListNode): void { if (node === this.head) { return; } this.remove(node); this.addToFront(node); } /** * Remove and return the least recently used node (tail). */ removeLast(): ListNode | null { if (!this.tail) { return null; } const node = this.tail; this.remove(node); return node; } /** * Get the number of nodes in the list. */ size(): number { return this.count; } } /** * Cache interface for generic key-value caching. */ export interface Cache { /** * Adds a value to the cache with the given key. * If the cache is full, evicts the least recently used entry. * If the key already exists, updates the value and moves it to front. * * @param key - The key to cache * @param value - The value to cache * @returns The evicted value if cache was full, undefined otherwise */ add(key: K, value: V): V | undefined /** * Retrieves a value from the cache. * If found, marks the entry as recently used. * * @param key - The key to look up * @returns The cached value, or undefined if not found */ get(key: K): V | undefined /** * Checks if a key exists in the cache. * Does not update LRU ordering. * * @param key - The key to check * @returns true if the key exists, false otherwise */ has(key: K): boolean /** * Removes a key from the cache. * * @param key - The key to remove * @returns The removed value, or undefined if not found */ remove(key: K): V | undefined /** * Gets the current number of entries in the cache. * * @returns The number of cached entries */ size(): number /** * Gets the maximum capacity of the cache. * * @returns The maximum number of entries */ capacity(): number } /** * LRU cache implementation using a doubly-linked list and Map. * * Provides O(1) time complexity for all operations: * - add: O(1) * - get: O(1) * - has: O(1) * - remove: O(1) */ export class LRUCache implements Cache { private dict: Map> = new Map(); private list: DoublyLinkedList = new DoublyLinkedList(); private maxSize: number; /** * Creates a new LRU cache with the specified capacity. * * @param maxSize - Maximum number of entries to cache */ constructor(maxSize: number) { if (maxSize <= 0) { throw new Error("Cache size must be positive"); } this.maxSize = maxSize; } add(key: K, value: V): V | undefined { // Check if key already exists const existingNode = this.dict.get(key); if (existingNode) { // Update existing entry and move to front const oldValue = existingNode.value; existingNode.value = value; this.list.moveToFront(existingNode); return oldValue; } // Create new entry const newNode = new ListNode(key, value); this.dict.set(key, newNode); this.list.addToFront(newNode); // Evict if over capacity if (this.list.size() > this.maxSize) { const evicted = this.list.removeLast(); if (evicted) { this.dict.delete(evicted.key); return evicted.value; } } return undefined; } get(key: K): V | undefined { const node = this.dict.get(key); if (!node) { return undefined; } // Move to front (mark as recently used) this.list.moveToFront(node); return node.value; } has(key: K): boolean { return this.dict.has(key); } remove(key: K): V | undefined { const node = this.dict.get(key); if (!node) { return undefined; } this.dict.delete(key); this.list.remove(node); return node.value; } size(): number { return this.list.size(); } capacity(): number { return this.maxSize; } }