lru.d.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. export class LRUMap<K,V> {
  2. // Construct a new cache object which will hold up to limit entries.
  3. // When the size == limit, a `put` operation will evict the oldest entry.
  4. //
  5. // If `entries` is provided, all entries are added to the new map.
  6. // `entries` should be an Array or other iterable object whose elements are
  7. // key-value pairs (2-element Arrays). Each key-value pair is added to the new Map.
  8. // null is treated as undefined.
  9. constructor(limit :number, entries? :Iterable<[K,V]>);
  10. // Convenience constructor equivalent to `new LRUMap(count(entries), entries)`
  11. constructor(entries :Iterable<[K,V]>);
  12. // Current number of items
  13. size :number;
  14. // Maximum number of items this map can hold
  15. limit :number;
  16. // Least recently-used entry. Invalidated when map is modified.
  17. oldest :Entry<K,V>;
  18. // Most recently-used entry. Invalidated when map is modified.
  19. newest :Entry<K,V>;
  20. // Replace all values in this map with key-value pairs (2-element Arrays) from
  21. // provided iterable.
  22. assign(entries :Iterable<[K,V]>) : void;
  23. // Put <value> into the cache associated with <key>. Replaces any existing entry
  24. // with the same key. Returns `this`.
  25. set(key :K, value :V) : LRUMap<K,V>;
  26. // Purge the least recently used (oldest) entry from the cache.
  27. // Returns the removed entry or undefined if the cache was empty.
  28. shift() : [K,V] | undefined;
  29. // Get and register recent use of <key>.
  30. // Returns the value associated with <key> or undefined if not in cache.
  31. get(key :K) : V | undefined;
  32. // Check if there's a value for key in the cache without registering recent use.
  33. has(key :K) : boolean;
  34. // Access value for <key> without registering recent use. Useful if you do not
  35. // want to chage the state of the map, but only "peek" at it.
  36. // Returns the value associated with <key> if found, or undefined if not found.
  37. find(key :K) : V | undefined;
  38. // Remove entry <key> from cache and return its value.
  39. // Returns the removed value, or undefined if not found.
  40. delete(key :K) : V | undefined;
  41. // Removes all entries
  42. clear() : void;
  43. // Returns an iterator over all keys, starting with the oldest.
  44. keys() : Iterator<K>;
  45. // Returns an iterator over all values, starting with the oldest.
  46. values() : Iterator<V>;
  47. // Returns an iterator over all entries, starting with the oldest.
  48. entries() : Iterator<[K,V]>;
  49. // Returns an iterator over all entries, starting with the oldest.
  50. [Symbol.iterator]() : Iterator<[K,V]>;
  51. // Call `fun` for each entry, starting with the oldest entry.
  52. forEach(fun :(value :V, key :K, m :LRUMap<K,V>)=>void, thisArg? :any) : void;
  53. // Returns an object suitable for JSON encoding
  54. toJSON() : Array<{key :K, value :V}>;
  55. // Returns a human-readable text representation
  56. toString() : string;
  57. }
  58. // An entry holds the key and value, and pointers to any older and newer entries.
  59. interface Entry<K,V> {
  60. key :K;
  61. value :V;
  62. }