All files / src/interfaces sorted-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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
/**
 * The sorted-map interfaces: maps kept in the order of their keys, with the ends, nearest-entry
 * queries and live range views.
 *
 * @packageDocumentation
 */
 
import type { Comparator } from '../functions/comparators'
import type { SequencedCollection } from './list'
import type { MapCollection, MapEntry } from './map'
import type { ReadonlySequencedMap, SequencedMap } from './sequenced-map'
import type { SequencedSet } from './sequenced-set'
import type { BoundOptions, ReadonlySortedSet, SortedSet, SubSetOptions } from './sorted-set'
 
/**
 * The read-only view of a sorted map: a map whose mappings are kept in the order of its keys under
 * a comparator, with the first and last entries, the entries nearest a key and live range views.
 * Two keys are the same key when the comparator returns `0` for them.
 *
 * "Before" and "after" follow the map's own order, which is descending on a reversed view.
 * Questions about keys alone go through `keySet()`, a sorted set: `map.keySet().floor(k)`. A sorted
 * map is a sequenced map whose order is decided by its keys.
 *
 * @example
 * ```ts
 * function priceAt(levels: ReadonlySortedMap<number, string>, at: number): string | undefined {
 *   return levels.floorEntry(at)?.value
 * }
 * ```
 */
export interface ReadonlySortedMap<K, V> extends ReadonlySequencedMap<K, V> {
  /**
   * The number of mappings. Time: O(1) on a map; O(k + log n) on a range view holding k of the
   * map's n mappings, which counts the mappings in range.
   */
  readonly size: number
  /**
   * Returns whether there are no mappings.
   *
   * Time: O(1) on a map; O(log n) on a range view, which looks for one mapping in range.
   *
   * @returns `size === 0`.
   */
  isEmpty(): boolean
  /**
   * Returns an iterator over new `[key, value]` arrays in key order.
   *
   * Time: O(log n) to create; O(1) amortised per step.
   *
   * @returns A new iterator.
   * @throws ConcurrentModificationError (from `next()`) when mappings are added or removed after
   *   the iterator is created.
   */
  [Symbol.iterator](): IterableIterator<[K, V]>
  /**
   * Returns an iterator over the keys in order.
   *
   * Time: O(log n) to create; O(1) amortised per step.
   *
   * @returns A new iterator.
   */
  keys(): IterableIterator<K>
  /**
   * Returns an iterator over the values in key order.
   *
   * Time: O(log n) to create; O(1) amortised per step.
   *
   * @returns A new iterator.
   */
  values(): IterableIterator<V>
  /**
   * Returns an iterator over new `[key, value]` arrays in key order.
   *
   * Time: O(log n) to create; O(1) amortised per step.
   *
   * @returns A new iterator.
   */
  entries(): IterableIterator<[K, V]>
  /**
   * Returns the comparator that orders the keys, or `undefined` for natural order; a reversed view
   * returns a function that orders in reverse.
   *
   * Time: O(1).
   *
   * @returns The comparator, or `undefined`.
   */
  comparator(): Comparator<K> | undefined
  /**
   * Returns the entry of the first key, or `undefined` when there is none.
   *
   * Time: O(log n).
   *
   * @returns A live entry, or `undefined`.
   */
  firstEntry(): MapEntry<K, V> | undefined
  /**
   * Returns the entry of the last key, or `undefined` when there is none.
   *
   * Time: O(log n).
   *
   * @returns A live entry, or `undefined`.
   */
  lastEntry(): MapEntry<K, V> | undefined
  /**
   * Returns the entry of the greatest key before `key`, or `undefined` when there is none.
   *
   * Time: O(log n).
   *
   * @param key - The key to search from; it need not be present.
   * @returns A live entry, or `undefined`.
   */
  lowerEntry(key: K): MapEntry<K, V> | undefined
  /**
   * Returns the entry of the greatest key equal to or before `key`, or `undefined` when there is
   * none.
   *
   * Time: O(log n).
   *
   * @param key - The key to search from; it need not be present.
   * @returns A live entry, or `undefined`.
   */
  floorEntry(key: K): MapEntry<K, V> | undefined
  /**
   * Returns the entry of the least key equal to or after `key`, or `undefined` when there is none.
   *
   * Time: O(log n).
   *
   * @param key - The key to search from; it need not be present.
   * @returns A live entry, or `undefined`.
   */
  ceilingEntry(key: K): MapEntry<K, V> | undefined
  /**
   * Returns the entry of the least key after `key`, or `undefined` when there is none.
   *
   * Time: O(log n).
   *
   * @param key - The key to search from; it need not be present.
   * @returns A live entry, or `undefined`.
   */
  higherEntry(key: K): MapEntry<K, V> | undefined
  /**
   * Returns a live view of the mappings whose keys run from `from` (inclusive by default) to `to`
   * (exclusive by default).
   *
   * Time: O(log n).
   *
   * @param from - The low end, in this map's order.
   * @param to - The high end, in this map's order.
   * @param options - The inclusive flags.
   * @returns The view.
   */
  subMap(from: K, to: K, options?: SubSetOptions | null): ReadonlySortedMap<K, V>
  /**
   * Returns a live view of the mappings whose keys are before `to` (exclusive by default).
   *
   * Time: O(log n).
   *
   * @param to - The high end, in this map's order.
   * @param options - The inclusive flag.
   * @returns The view.
   */
  headMap(to: K, options?: BoundOptions | null): ReadonlySortedMap<K, V>
  /**
   * Returns a live view of the mappings whose keys are `from` (inclusive by default) or after it.
   *
   * Time: O(log n).
   *
   * @param from - The low end, in this map's order.
   * @param options - The inclusive flag.
   * @returns The view.
   */
  tailMap(from: K, options?: BoundOptions | null): ReadonlySortedMap<K, V>
  /**
   * Returns a live view of this map in the opposite key order.
   *
   * Time: O(1).
   *
   * @returns The reversed view.
   */
  reversed(): ReadonlySortedMap<K, V>
  /**
   * Returns a live view of the keys as a sorted set, in this map's order.
   *
   * Time: O(1).
   *
   * @returns A new view.
   */
  keySet(): ReadonlySortedSet<K>
}
 
/**
 * A map kept in the order of its keys under a comparator, with the ends, nearest-entry queries,
 * removal of the first and last mappings, and live range views. Two keys are the same key when
 * the comparator returns `0` for them. Setting a key outside a range view's range through the view
 * throws `IllegalArgumentError`. As a sequenced map it has ends and a reversed view, but `putFirst`
 * and `putLast` throw `UnsupportedOperationError`: each mapping's place is decided by its key.
 *
 * @example
 * ```ts
 * function takeBefore<K, V>(map: SortedMap<K, V>, limit: K): Array<[K, V]> {
 *   const head = map.headMap(limit)
 *   const taken = head.toJSON()
 *   head.clear()
 *   return taken
 * }
 * ```
 */
export interface SortedMap<K, V> extends SequencedMap<K, V>, ReadonlySortedMap<K, V> {
  /**
   * Returns an iterator over new `[key, value]` arrays in key order.
   *
   * Time: O(log n) to create; O(1) amortised per step.
   *
   * @returns A new iterator.
   * @throws ConcurrentModificationError (from `next()`) when mappings are added or removed after
   *   the iterator is created.
   */
  [Symbol.iterator](): IterableIterator<[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: O(log n).
   *
   * @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.
   *
   * Time: O(log n).
   *
   * @returns The entry of the removed mapping, or `undefined`.
   */
  pollLastEntry(): MapEntry<K, V> | undefined
  /**
   * Always throws: a sorted map places each mapping by its key. Use `set`. Nothing is checked or
   * changed.
   *
   * Time: O(1).
   *
   * @param key - Ignored.
   * @param value - Ignored.
   * @returns Never returns.
   * @throws UnsupportedOperationError always.
   */
  putFirst(key: K, value: V): this
  /**
   * Always throws: a sorted map places each mapping by its key. Use `set`. Nothing is checked or
   * changed.
   *
   * Time: O(1).
   *
   * @param key - Ignored.
   * @param value - Ignored.
   * @returns Never returns.
   * @throws UnsupportedOperationError always.
   */
  putLast(key: K, value: V): this
  /**
   * Returns a live view of the mappings whose keys run from `from` (inclusive by default) to `to`
   * (exclusive by default); changes through the view reach this map.
   *
   * Time: O(log n).
   *
   * @param from - The low end, in this map's order.
   * @param to - The high end, in this map's order.
   * @param options - The inclusive flags.
   * @returns The view.
   */
  subMap(from: K, to: K, options?: SubSetOptions | null): SortedMap<K, V>
  /**
   * Returns a live view of the mappings whose keys are before `to` (exclusive by default); changes
   * through the view reach this map.
   *
   * Time: O(log n).
   *
   * @param to - The high end, in this map's order.
   * @param options - The inclusive flag.
   * @returns The view.
   */
  headMap(to: K, options?: BoundOptions | null): SortedMap<K, V>
  /**
   * Returns a live view of the mappings whose keys are `from` (inclusive by default) or after it;
   * changes through the view reach this map.
   *
   * Time: O(log n).
   *
   * @param from - The low end, in this map's order.
   * @param options - The inclusive flag.
   * @returns The view.
   */
  tailMap(from: K, options?: BoundOptions | null): SortedMap<K, V>
  /**
   * Returns a live view of this map in the opposite key order; changes through the view reach
   * this map.
   *
   * Time: O(1).
   *
   * @returns The reversed view.
   */
  reversed(): SortedMap<K, V>
  /**
   * Returns a live view of the keys as a sorted set, in this map's order; removing a key removes
   * its mapping, and adding throws `UnsupportedOperationError`.
   *
   * Time: O(1).
   *
   * @returns A new view.
   */
  keySet(): SortedSet<K>
  /**
   * Returns a live view of the mappings as entries, in key order, with the ends of this map;
   * 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 key order, with the ends of this map; 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 key 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.
   * @throws ConcurrentModificationError when `action` adds or removes mappings.
   */
  forEach(action: (value: V, key: K, map: MapCollection<K, V>) => void, thisArg?: unknown): void
}