All files / src/interfaces sequenced-map.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193                                                                                                                                                                                                                                                                                                                                                                                                 
/**
 * The sequenced-map interfaces: maps with a defined order and a first and last mapping.
 *
 * @packageDocumentation
 */
 
import type { ReadonlySequencedCollection, SequencedCollection } from './list'
import type { MapCollection, MapEntry, ReadonlyMapCollection } from './map'
import type { ReadonlySequencedSet, SequencedSet } from './sequenced-set'
 
/**
 * The read-only view of a map with a defined order and a first and last mapping, readable from
 * either end. Entries returned by `firstEntry` and `lastEntry` are live: they read the map's
 * current value of their key.
 *
 * @example
 * ```ts
 * function oldest<K, V>(history: ReadonlySequencedMap<K, V>): K | undefined {
 *   return history.firstEntry()?.key
 * }
 * ```
 */
export interface ReadonlySequencedMap<K, V> extends ReadonlyMapCollection<K, V> {
  /**
   * Returns the entry of the first mapping, or `undefined` when the map is empty. Reading the ends
   * never changes the order.
   *
   * Time: as the implementation documents (O(1) amortised for a `LinkedHashMap`).
   *
   * @returns A live entry, or `undefined`.
   */
  firstEntry(): MapEntry<K, V> | undefined
  /**
   * Returns the entry of the last mapping, or `undefined` when the map is empty. Reading the ends
   * never changes the order.
   *
   * Time: as the implementation documents (O(1) amortised for a `LinkedHashMap`).
   *
   * @returns A live entry, or `undefined`.
   */
  lastEntry(): MapEntry<K, V> | undefined
  /**
   * Returns a live view of this map in the opposite order: changes to either show in the other.
   *
   * Time: O(1).
   *
   * @returns The reversed view.
   */
  reversed(): ReadonlySequencedMap<K, V>
  /**
   * Returns a live view of the keys, in this map's order.
   *
   * Time: O(1).
   *
   * @returns A new view.
   */
  keySet(): ReadonlySequencedSet<K>
  /**
   * Returns a live view of the mappings as entries, in this map's order.
   *
   * Time: O(1).
   *
   * @returns A new view.
   */
  entrySet(): ReadonlySequencedSet<MapEntry<K, V>>
  /**
   * Returns a live view of the values, in this map's order.
   *
   * Time: O(1).
   *
   * @returns A new view.
   */
  valueCollection(): ReadonlySequencedCollection<V>
  /**
   * Calls `action(value, key, map)` for each mapping in order, with `this` set to `thisArg`.
   *
   * Time: O(n) plus the calls.
   *
   * @param action - Called with each value, its key and the map.
   * @param thisArg - The `this` value for `action`.
   * @throws TypeError when `action` is not a function.
   */
  forEach(
    action: (value: V, key: K, map: ReadonlyMapCollection<K, V>) => void,
    thisArg?: unknown,
  ): void
}
 
/**
 * A map with a defined order that can be changed at both ends. `putFirst` and `putLast` insert an
 * absent key at that end or move the present mapping of an equal key there (keeping the stored
 * key), or throw `UnsupportedOperationError` where the map orders its keys itself. Whether
 * iteration is live or fail-fast is stated by each implementation.
 *
 * @example
 * ```ts
 * function touch<K, V>(recent: SequencedMap<K, V>, key: K, value: V, limit: number): void {
 *   recent.putLast(key, value)
 *   while (recent.size > limit) recent.pollFirstEntry()
 * }
 * ```
 */
export interface SequencedMap<K, V> extends MapCollection<K, V>, ReadonlySequencedMap<K, V> {
  /**
   * Removes the first mapping and returns its entry, or returns `undefined` when the map is
   * empty. The entry still reads its value; `setValue` on it throws until the key is set again.
   *
   * Time: as the implementation documents.
   *
   * @returns The entry of the removed mapping, or `undefined`.
   */
  pollFirstEntry(): MapEntry<K, V> | undefined
  /**
   * Removes the last mapping and returns its entry, or returns `undefined` when the map is empty.
   * The entry still reads its value; `setValue` on it throws until the key is set again.
   *
   * Time: as the implementation documents.
   *
   * @returns The entry of the removed mapping, or `undefined`.
   */
  pollLastEntry(): MapEntry<K, V> | undefined
  /**
   * Maps `key` to `value` at the front: inserts the mapping there when the key is absent, or moves
   * the present mapping there (keeping its stored key) and writes its value. Returns the map.
   *
   * Time: as the implementation documents.
   *
   * @param key - The key.
   * @param value - The value.
   * @returns The map.
   * @throws UnsupportedOperationError where the map orders its keys itself.
   */
  putFirst(key: K, value: V): this
  /**
   * Maps `key` to `value` at the end: inserts the mapping there when the key is absent, or moves
   * the present mapping there (keeping its stored key) and writes its value. Returns the map.
   *
   * Time: as the implementation documents.
   *
   * @param key - The key.
   * @param value - The value.
   * @returns The map.
   * @throws UnsupportedOperationError where the map orders its keys itself.
   */
  putLast(key: K, value: V): this
  /**
   * Returns a live view of this map in the opposite order; changes through the view reach this
   * map at the mirrored end.
   *
   * Time: O(1).
   *
   * @returns The reversed view.
   */
  reversed(): SequencedMap<K, V>
  /**
   * Returns a live view of the keys, in this map's order; removing a key removes its mapping, and
   * adding throws `UnsupportedOperationError`.
   *
   * Time: O(1).
   *
   * @returns A new view.
   */
  keySet(): SequencedSet<K>
  /**
   * Returns a live view of the mappings as entries, in this map's order; removing an entry
   * removes its mapping, and adding throws `UnsupportedOperationError`.
   *
   * Time: O(1).
   *
   * @returns A new view.
   */
  entrySet(): SequencedSet<MapEntry<K, V>>
  /**
   * Returns a live view of the values, in this map's order; removing a value removes a mapping
   * with that value, and adding throws `UnsupportedOperationError`.
   *
   * Time: O(1).
   *
   * @returns A new view.
   */
  valueCollection(): SequencedCollection<V>
  /**
   * Calls `action(value, key, map)` for each mapping in order, with `this` set to `thisArg`.
   *
   * Time: O(n) plus the calls.
   *
   * @param action - Called with each value, its key and the map.
   * @param thisArg - The `this` value for `action`.
   * @throws TypeError when `action` is not a function.
   */
  forEach(action: (value: V, key: K, map: MapCollection<K, V>) => void, thisArg?: unknown): void
}