{"version":3,"file":"cache.cjs","names":["key: K","value: V","prev: ListNode<K, V> | null","next: ListNode<K, V> | null"],"sources":["../../src/storage/cache.ts"],"sourcesContent":["/**\n * LRU (Least Recently Used) cache implementation.\n * Based on the Go iavl cache/cache.go implementation.\n *\n * Uses a doubly-linked list + Map for O(1) operations:\n * - add: O(1)\n * - get: O(1)\n * - has: O(1)\n * - remove: O(1)\n */\n\n/**\n * Node in the doubly-linked list.\n */\nclass ListNode<K, V> {\n  constructor(\n    public key: K,\n    public value: V,\n    public prev: ListNode<K, V> | null = null,\n    public next: ListNode<K, V> | null = null,\n  ) {}\n}\n\n/**\n * Doubly-linked list for LRU ordering.\n */\nclass DoublyLinkedList<K, V> {\n  private head: ListNode<K, V> | null = null;\n  private tail: ListNode<K, V> | null = null;\n  private count = 0;\n\n  /**\n   * Add node to the front (most recently used).\n   */\n  addToFront(node: ListNode<K, V>): void {\n    if (!this.head) {\n      this.head = node;\n      this.tail = node;\n    }\n    else {\n      node.next = this.head;\n      this.head.prev = node;\n      this.head = node;\n    }\n    this.count++;\n  }\n\n  /**\n   * Remove node from the list.\n   */\n  remove(node: ListNode<K, V>): void {\n    if (node.prev) {\n      node.prev.next = node.next;\n    }\n    else {\n      this.head = node.next;\n    }\n\n    if (node.next) {\n      node.next.prev = node.prev;\n    }\n    else {\n      this.tail = node.prev;\n    }\n\n    node.prev = null;\n    node.next = null;\n    this.count--;\n  }\n\n  /**\n   * Move node to front (mark as most recently used).\n   */\n  moveToFront(node: ListNode<K, V>): void {\n    if (node === this.head) {\n      return;\n    }\n\n    this.remove(node);\n    this.addToFront(node);\n  }\n\n  /**\n   * Remove and return the least recently used node (tail).\n   */\n  removeLast(): ListNode<K, V> | null {\n    if (!this.tail) {\n      return null;\n    }\n\n    const node = this.tail;\n    this.remove(node);\n    return node;\n  }\n\n  /**\n   * Get the number of nodes in the list.\n   */\n  size(): number {\n    return this.count;\n  }\n}\n\n/**\n * Cache interface for generic key-value caching.\n */\nexport interface Cache<K, V> {\n  /**\n   * Adds a value to the cache with the given key.\n   * If the cache is full, evicts the least recently used entry.\n   * If the key already exists, updates the value and moves it to front.\n   *\n   * @param key - The key to cache\n   * @param value - The value to cache\n   * @returns The evicted value if cache was full, undefined otherwise\n   */\n  add(key: K, value: V): V | undefined\n\n  /**\n   * Retrieves a value from the cache.\n   * If found, marks the entry as recently used.\n   *\n   * @param key - The key to look up\n   * @returns The cached value, or undefined if not found\n   */\n  get(key: K): V | undefined\n\n  /**\n   * Checks if a key exists in the cache.\n   * Does not update LRU ordering.\n   *\n   * @param key - The key to check\n   * @returns true if the key exists, false otherwise\n   */\n  has(key: K): boolean\n\n  /**\n   * Removes a key from the cache.\n   *\n   * @param key - The key to remove\n   * @returns The removed value, or undefined if not found\n   */\n  remove(key: K): V | undefined\n\n  /**\n   * Gets the current number of entries in the cache.\n   *\n   * @returns The number of cached entries\n   */\n  size(): number\n\n  /**\n   * Gets the maximum capacity of the cache.\n   *\n   * @returns The maximum number of entries\n   */\n  capacity(): number\n}\n\n/**\n * LRU cache implementation using a doubly-linked list and Map.\n *\n * Provides O(1) time complexity for all operations:\n * - add: O(1)\n * - get: O(1)\n * - has: O(1)\n * - remove: O(1)\n */\nexport class LRUCache<K, V> implements Cache<K, V> {\n  private dict: Map<K, ListNode<K, V>> = new Map();\n  private list: DoublyLinkedList<K, V> = new DoublyLinkedList();\n  private maxSize: number;\n\n  /**\n   * Creates a new LRU cache with the specified capacity.\n   *\n   * @param maxSize - Maximum number of entries to cache\n   */\n  constructor(maxSize: number) {\n    if (maxSize <= 0) {\n      throw new Error(\"Cache size must be positive\");\n    }\n    this.maxSize = maxSize;\n  }\n\n  add(key: K, value: V): V | undefined {\n    // Check if key already exists\n    const existingNode = this.dict.get(key);\n\n    if (existingNode) {\n      // Update existing entry and move to front\n      const oldValue = existingNode.value;\n      existingNode.value = value;\n      this.list.moveToFront(existingNode);\n      return oldValue;\n    }\n\n    // Create new entry\n    const newNode = new ListNode(key, value);\n    this.dict.set(key, newNode);\n    this.list.addToFront(newNode);\n\n    // Evict if over capacity\n    if (this.list.size() > this.maxSize) {\n      const evicted = this.list.removeLast();\n      if (evicted) {\n        this.dict.delete(evicted.key);\n        return evicted.value;\n      }\n    }\n\n    return undefined;\n  }\n\n  get(key: K): V | undefined {\n    const node = this.dict.get(key);\n\n    if (!node) {\n      return undefined;\n    }\n\n    // Move to front (mark as recently used)\n    this.list.moveToFront(node);\n    return node.value;\n  }\n\n  has(key: K): boolean {\n    return this.dict.has(key);\n  }\n\n  remove(key: K): V | undefined {\n    const node = this.dict.get(key);\n\n    if (!node) {\n      return undefined;\n    }\n\n    this.dict.delete(key);\n    this.list.remove(node);\n    return node.value;\n  }\n\n  size(): number {\n    return this.list.size();\n  }\n\n  capacity(): number {\n    return this.maxSize;\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAcA,IAAM,WAAN,MAAqB;CACnB,YACE,AAAOA,KACP,AAAOC,OACP,AAAOC,OAA8B,MACrC,AAAOC,OAA8B,MACrC;EAJO;EACA;EACA;EACA;;;;;;AAOX,IAAM,mBAAN,MAA6B;CAC3B,AAAQ,OAA8B;CACtC,AAAQ,OAA8B;CACtC,AAAQ,QAAQ;;;;CAKhB,WAAW,MAA4B;AACrC,MAAI,CAAC,KAAK,MAAM;AACd,QAAK,OAAO;AACZ,QAAK,OAAO;SAET;AACH,QAAK,OAAO,KAAK;AACjB,QAAK,KAAK,OAAO;AACjB,QAAK,OAAO;;AAEd,OAAK;;;;;CAMP,OAAO,MAA4B;AACjC,MAAI,KAAK,KACP,MAAK,KAAK,OAAO,KAAK;MAGtB,MAAK,OAAO,KAAK;AAGnB,MAAI,KAAK,KACP,MAAK,KAAK,OAAO,KAAK;MAGtB,MAAK,OAAO,KAAK;AAGnB,OAAK,OAAO;AACZ,OAAK,OAAO;AACZ,OAAK;;;;;CAMP,YAAY,MAA4B;AACtC,MAAI,SAAS,KAAK,KAChB;AAGF,OAAK,OAAO,KAAK;AACjB,OAAK,WAAW,KAAK;;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