All files / src/internal/engines binary-heap-engine.ts

99.7% Statements 337/338
100% Branches 152/152
100% Functions 46/46
99.65% Lines 286/287

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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593                                                          105x 105x 105x       947772x 947772x           7681984x         798448x       798448x 798448x       1720807x                         30105x 30105x   30105x           30105x 30105x 30105x       928294x             839180x 839009x 839009x 839009x   171x 171x 171x 171x       23390x   50x 50x 50x   23340x 23340x 23340x 23340x 330x 330x   22997x   23327x                         769055x 769055x         1538110x   2942697x 2942697x         769055x 769055x   769055x                 769055x 769055x 769055x       1639671x       5512995x         52083x         1810290x         50x 163x                     3805767x     1787261x   3758474x   3758474x     3805767x 20x   3750827x         220488x 220488x 220488x 584735x 584735x 423179x   212984x               193489x 193489x 193489x 193489x 1369216x 1369216x 1231231x 1231231x 567381x   1231215x 1175727x 1175727x   193454x         234857x 234857x 234857x 423173x 423173x 423173x   234857x         175738x 175738x 175738x 1175711x 1175711x 1175711x   175738x         273651x 273651x   249715x 249715x 249715x   23936x 182384x 182384x 182384x 340541x 340541x 266459x 266459x       115433x   266437x 158157x 158157x   182333x               716005x 716005x 716005x 716005x 450284x 157824x 450284x   574977x 265714x   715988x 715988x         52066x 52066x 52066x           308543x 308543x 308543x 61372x 61372x 61372x   247171x 18063x 18063x 18063x   229108x 32936x 32936x   16234x 16234x 16234x 16234x     212874x 212874x       26235x 26235x 39208x 37848x 24319x       250722x 250722x 250722x 250722x 250722x 21928x 217143x 217143x     217143x 195270x 217143x           27657x       486548x 486548x 486548x 470946x 470946x 31124x 31124x 31124x 31124x   439822x 439822x 279017x 279017x 16157x 279017x 279017x   160805x 160805x 160805x 160805x 160805x 160805x               37858x 37858x 37858x 5174x 5174x 5174x 5174x   32684x 32684x 32684x 32684x 14948x 14948x 14948x 14948x   17716x 17716x 17716x 17716x 17716x       29661x 29551x 79079x 25301x       39120x 39118x 39118x 2708998x 14468x 14468x     24650x         28089x 28089x 125453x 28089x 28089x 28046x 28046x 28046x       7934x 5570x 5570x 5570x           249079x       798448x       30105x       26352x                       61372x 61372x 120745x 120745x 120745x 65461x 65461x   61372x       18063x 18063x 27136x 27136x 27136x 14586x 14586x   18063x         262860x       16157x       413099x 413099x 4088068x 4088068x 3784630x 3784630x 3784630x 3776609x 3776609x 1805062x 1805062x     3784630x 3674969x 3674969x   413099x       20888x 20888x 49512x 49512x 36475x 36475x 36475x 30324x 30324x 12511x 12511x     36475x 28624x 28624x   20888x         292460x 292460x 150239x         157824x 157824x 4731x      
// The engine behind `PriorityQueue`: a binary heap in a plain array. The public class holds one
// engine and delegates to it, so nothing here reaches the published types.
//
// Every repair of the heap is planned before it is written: the comparator only ever sees the heap
// as it was before the call, and an error it throws (or a change it makes to the queue) leaves the
// array untouched.
//
// A queue in natural order whose elements are all numbers, or all strings, is a plain heap: two
// such values compare without running user code and without throwing, so nothing can interrupt a
// repair or change the queue during one. Its `add`, `poll` and build sift in place with `<`, making
// the same comparisons in the same order as the planned repairs, so the comparison bounds hold.
 
import { ConcurrentModificationError } from '../../errors/errors'
import type { Comparator } from '../../functions/comparators'
import type { Cursor } from '../../interfaces/collection'
import type { Queue } from '../../interfaces/queue'
import { type Labelled, labelOf, requireElement, type Where } from '../checks'
import { guardedCompare } from '../compare'
import { sameValueZero } from '../equality'
import { done, FailFastCursor, FailFastIterator } from '../iteration'
import { compareNatural, isPlainKind } from '../natural-order'
import type { ChangeCounter } from '../revision'
import { AbstractCollection } from '../skeletons/abstract-collection'
 
/** Where the former last element went when an element was removed from inside the heap. */
type Moved = 'none' | 'down' | 'stayed' | 'up'
 
/** The kind of a plain heap's elements: `MIXED` when the heap is empty, has a comparator, or holds
 * anything other than only numbers or only strings. */
const MIXED = 0
const NUMBERS = 1
const STRINGS = 2
 
/** The plain kind of `value`: `NUMBERS`, `STRINGS` or `MIXED`. */
function plainKindOf(value: unknown): number {
  const type = typeof value
  return type === 'number' ? NUMBERS : type === 'string' ? STRINGS : MIXED
}
 
/** Natural order of two numbers as a test: `a` before `b`, with `NaN` after every other number. */
function numberBefore(a: number, b: number): boolean {
  // biome-ignore lint/suspicious/noSelfCompare: `x === x` is false only for NaN.
  return a < b || (b !== b && a === a)
}
 
/** A fail-fast iterator over the heap array in index order. */
class HeapIterator<E> extends FailFastIterator<E> {
  private index = 0
  private readonly heap: BinaryHeapEngine<E>
 
  constructor(heap: BinaryHeapEngine<E>, changes: ChangeCounter, where: string) {
    super(changes, where)
    this.heap = heap
  }
 
  protected step(): IteratorResult<E> {
    return this.index < this.heap.size
      ? { done: false, value: this.heap.elementAt(this.index++) }
      : done()
  }
}
 
/**
 * A fail-fast cursor over the heap array in index order. Removing an element moves the former last
 * element into the hole: if it lands at or below the hole, the hole holds an element not yet
 * visited, so the cursor steps back onto it; if it rises above the hole, into the part already
 * visited, the cursor keeps it in `pending` and returns it after the array is walked.
 */
class HeapCursor<E> extends FailFastCursor<E> {
  private index = 0
  private lastIndex = -1
  private pending: E[] | undefined
  private pendingIndex = 0
  private lastPending: { readonly value: E } | undefined
  private readonly heap: BinaryHeapEngine<E>
  private readonly site: string
 
  constructor(heap: BinaryHeapEngine<E>, changes: ChangeCounter, where: string) {
    super(changes, where)
    this.heap = heap
    this.site = where
  }
 
  protected hasMore(): boolean {
    return (
      this.index < this.heap.size ||
      (this.pending !== undefined && this.pendingIndex < this.pending.length)
    )
  }
 
  protected advance(): E {
    if (this.index < this.heap.size) {
      this.lastIndex = this.index
      this.lastPending = undefined
      return this.heap.elementAt(this.index++)
    }
    const value = (this.pending as E[])[this.pendingIndex++] as E
    this.lastIndex = -1
    this.lastPending = { value }
    return value
  }
 
  protected removeLast(): void {
    if (this.lastPending !== undefined) {
      // Every element still in the array has been visited, so where the move lands is irrelevant.
      this.heap.removeAt(this.heap.indexOfSame(this.lastPending.value), this.site)
      this.lastPending = undefined
      return
    }
    const index = this.lastIndex
    const last = this.heap.elementAt(this.heap.size - 1)
    const moved = this.heap.removeAt(index, this.site)
    if (moved === 'up') {
      if (this.pending === undefined) this.pending = []
      this.pending.push(last)
    } else {
      this.index = index
    }
    this.lastIndex = -1
  }
}
 
/**
 * A binary min-heap in an array: the parent of slot `i` is slot `(i - 1) >> 1`, and no element is
 * less than its parent under the queue's order. A removed element's slot is dropped, so the array
 * never keeps it alive.
 *
 * @internal
 */
export class BinaryHeapEngine<E> extends AbstractCollection<E> {
  /** The labels of `add` and `poll`, each built once per queue, the first time a comparison needs it. */
  private readonly addSite: Labelled = this.site('add')
  private readonly pollSite: Labelled = this.site('poll')
 
  /** A label source for `method` that builds its label once, the first time it is needed. */
  private site(method: string): Labelled {
    let label: string | undefined
    return {
      errorLabel: () => {
        if (label === undefined) label = this.errorLabel(method)
        return label
      },
    }
  }
 
  protected readonly iterationMode = 'fail-fast' as const
  private heap: E[] = []
  /** `NUMBERS` or `STRINGS` while the queue is a plain heap of that kind, else `MIXED`. */
  private kind = MIXED
  private readonly order: Comparator<E> | undefined
  private readonly facade: Queue<E>
 
  /**
   * @param facade - The public queue, for identity.
   * @param order - The comparator, or `undefined` for natural order.
   */
  constructor(facade: Queue<E>, order: Comparator<E> | undefined) {
    super()
    this.facade = facade
    this.order = order
  }
 
  get [Symbol.toStringTag](): string {
    return 'PriorityQueue'
  }
 
  get size(): number {
    return this.heap.length
  }
 
  /** The comparator given at construction, or `undefined` for natural order. */
  get comparator(): Comparator<E> | undefined {
    return this.order
  }
 
  /** Returns the element in slot `index`. */
  elementAt(index: number): E {
    return this.heap[index] as E
  }
 
  /** Returns the slot holding exactly `element` (`Object.is`), or -1. */
  indexOfSame(element: E): number {
    const heap = this.heap
    for (let i = 0; i < heap.length; i++) if (Object.is(heap[i], element)) return i
    return -1
  }
 
  // ── Comparing and planning ──
 
  /**
   * Compares `a` and `b` under the queue's order, then checks that the comparator did not add or
   * remove elements: `expected` is the structure count when the outer operation started.
   */
  private compare(a: E, b: E, where: Where, expected: number): number {
    if (this.order === undefined) {
      // Two numbers or two strings run no user code and cannot throw, so nothing can have changed
      // the queue and no label is needed.
      if (isPlainKind(a, b)) return compareNatural(a, b, '')
    }
    const label = labelOf(where, '')
    const result =
      this.order === undefined
        ? compareNatural(a, b, label)
        : guardedCompare(this.order, a, b, label)
    if (this.changes.structure !== expected) {
      throw new ConcurrentModificationError(`${label}: the queue was changed by the comparator`)
    }
    return result
  }
 
  /** The slot `x` rises to from the hole at `index`, comparing it with each parent. */
  private planUp(x: E, index: number, where: Where, expected: number): number {
    const heap = this.heap
    let i = index
    while (i > 0) {
      const parent = (i - 1) >> 1
      if (this.compare(x, heap[parent] as E, where, expected) >= 0) break
      i = parent
    }
    return i
  }
 
  /**
   * The children that move up, in order, when `x` sinks from the hole at `index` in a heap of
   * `length` slots: at each level the smaller child moves up while it is less than `x`.
   */
  private planDown(x: E, index: number, length: number, where: Where, expected: number): number[] {
    const heap = this.heap
    const path: number[] = []
    let i = index
    for (;;) {
      let child = 2 * i + 1
      if (child >= length) break
      const right = child + 1
      if (right < length && this.compare(heap[right] as E, heap[child] as E, where, expected) < 0) {
        child = right
      }
      if (this.compare(heap[child] as E, x, where, expected) >= 0) break
      path.push(child)
      i = child
    }
    return path
  }
 
  /** Moves each parent from `from` up to `to` one level down, then places `x` at `to`. */
  private applyUp(x: E, from: number, to: number): void {
    const heap = this.heap
    let i = from
    while (i > to) {
      const parent = (i - 1) >> 1
      heap[i] = heap[parent] as E
      i = parent
    }
    heap[to] = x
  }
 
  /** Moves each child on `path` one level up into the hole at `from`, then places `x` at its end. */
  private applyDown(x: E, from: number, path: readonly number[]): void {
    const heap = this.heap
    let hole = from
    for (let k = 0; k < path.length; k++) {
      const child = path[k] as number
      heap[hole] = heap[child] as E
      hole = child
    }
    heap[hole] = x
  }
 
  /** Orders `items` into a heap in place (bottom-up), checking the structure after each call. */
  private heapify(items: E[], where: Where, expected: number): void {
    const length = items.length
    if (length === 1 && this.order === undefined) {
      // Alone, the element is compared with itself so an incomparable value is still refused.
      const only = items[0] as E
      this.compare(only, only, where, expected)
      return
    }
    for (let start = (length >> 1) - 1; start >= 0; start--) {
      const x = items[start] as E
      let i = start
      for (;;) {
        let child = 2 * i + 1
        if (child >= length) break
        const right = child + 1
        if (
          right < length &&
          this.compare(items[right] as E, items[child] as E, where, expected) < 0
        ) {
          child = right
        }
        if (this.compare(items[child] as E, x, where, expected) >= 0) break
        items[i] = items[child] as E
        i = child
      }
      items[i] = x
    }
  }
 
  // ── Building ──
 
  /** Replaces the contents of a new, empty queue with `items`, checked and ordered. */
  build(items: E[], where: string): void {
    const kind = this.order === undefined && items.length > 0 ? plainKindOf(items[0]) : MIXED
    let plain = kind !== MIXED
    for (let i = 1; plain && i < items.length; i++) plain = typeof items[i] === typeof items[0]
    if (plain) {
      if (kind === NUMBERS) heapifyNumbers(items as unknown as number[])
      else heapifyStrings(items as unknown as string[])
      this.kind = kind
    } else {
      for (let i = 0; i < items.length; i++) requireElement(items[i], where)
      this.heapify(items, where, this.changes.structure)
    }
    this.heap = items
    if (items.length > 0) this.changes.structural()
  }
 
  /** Fills a new, empty queue with a copy of `source`'s array, which is already a heap. */
  copyFrom(source: BinaryHeapEngine<E>): void {
    this.heap = source.heap.slice()
    this.kind = source.kind
    if (this.heap.length > 0) this.changes.structural()
  }
 
  // ── Adding ──
 
  add(element: E): this {
    const heap = this.heap
    const kind = this.kind
    if (kind === NUMBERS && typeof element === 'number') {
      insertNumber(heap as unknown as number[], element)
      this.changes.structural()
      return this
    }
    if (kind === STRINGS && typeof element === 'string') {
      insertString(heap as unknown as string[], element)
      this.changes.structural()
      return this
    }
    if (heap.length === 0 && this.order === undefined) {
      const first = plainKindOf(element)
      if (first !== MIXED) {
        // A number or a string compares with itself, so the self-check of a lone element passes.
        heap.push(element)
        this.kind = first
        this.changes.structural()
        return this
      }
    }
    this.insert(element, this.addSite)
    return this
  }
 
  override addAll(elements: Iterable<E>): boolean {
    const where = `${this.className}.addAll`
    const items = this.readAll(elements, 'addAll')
    for (let i = 0; i < items.length; i++) requireElement(items[i], where)
    for (let i = 0; i < items.length; i++) this.insert(items[i] as E, where)
    return items.length > 0
  }
 
  private insert(x: E, where: Where): void {
    requireElement(x, where)
    const expected = this.changes.structure
    const length = this.heap.length
    let to = 0
    if (length > 0) to = this.planUp(x, length, where, expected)
    else if (this.order === undefined) this.compare(x, x, where, expected)
    this.heap.push(x)
    this.applyUp(x, length, to)
    // The first element of a queue in natural order sets its kind; a later one of another kind (it
    // passed the comparisons, so it is neither a number nor a string here) makes it mixed.
    if (length === 0) this.kind = this.order === undefined ? plainKindOf(x) : MIXED
    else if (this.kind !== plainKindOf(x)) this.kind = MIXED
    this.changes.structural()
  }
 
  // ── Removing ──
 
  peek(): E | undefined {
    return this.heap.length === 0 ? undefined : this.heap[0]
  }
 
  poll(): E | undefined {
    const heap = this.heap
    const length = heap.length
    if (length === 0) return undefined
    const top = heap[0] as E
    if (length === 1) {
      heap.pop()
      this.kind = MIXED
      this.changes.structural()
      return top
    }
    const kind = this.kind
    if (kind !== MIXED) {
      const x = heap.pop() as E
      if (kind === NUMBERS) siftDownNumbers(heap as unknown as number[], x as unknown as number)
      else siftDownStrings(heap as unknown as string[], x as unknown as string)
      this.changes.structural()
      return top
    }
    const x = heap[length - 1] as E
    const path = this.planDown(x, 0, length - 1, this.pollSite, this.changes.structure)
    heap.pop()
    this.applyDown(x, 0, path)
    this.changes.structural()
    return top
  }
 
  /**
   * Removes the element in slot `index`, filling the hole with the last element, and reports where
   * that element went relative to the hole.
   */
  removeAt(index: number, where: string): Moved {
    const heap = this.heap
    const last = heap.length - 1
    if (index === last) {
      heap.pop()
      if (last === 0) this.kind = MIXED
      this.changes.structural()
      return 'none'
    }
    const expected = this.changes.structure
    const x = heap[last] as E
    const path = this.planDown(x, index, last, where, expected)
    if (path.length > 0) {
      heap.pop()
      this.applyDown(x, index, path)
      this.changes.structural()
      return 'down'
    }
    const to = this.planUp(x, index, where, expected)
    heap.pop()
    this.applyUp(x, index, to)
    this.changes.structural()
    return to < index ? 'up' : 'stayed'
  }
 
  override has(element: E): boolean {
    if (element === null || element === undefined) return false
    const heap = this.heap
    for (let i = 0; i < heap.length; i++) if (sameValueZero(heap[i], element)) return true
    return false
  }
 
  override delete(element: E): boolean {
    if (element === null || element === undefined) return false
    const heap = this.heap
    for (let i = 0; i < heap.length; i++) {
      if (sameValueZero(heap[i], element)) {
        this.removeAt(i, `${this.className}.delete`)
        return true
      }
    }
    return false
  }
 
  /** Rebuilds the kept elements in a scratch array and switches to it only when that succeeds. */
  protected override removeMarked(flags: readonly boolean[], method: string): void {
    const heap = this.heap
    const kept: E[] = []
    for (let i = 0; i < heap.length; i++) if (flags[i] !== true) kept.push(heap[i] as E)
    const where = `${this.className}.${method}`
    if (kept.length > 1) this.heapify(kept, where, this.changes.structure)
    this.heap = kept
    if (kept.length === 0) this.kind = MIXED
    this.changes.structural()
  }
 
  override clear(): void {
    if (this.heap.length === 0) return
    this.heap = []
    this.kind = MIXED
    this.changes.structural()
  }
 
  // ── Reading ──
 
  override toArray(): E[] {
    return this.heap.slice()
  }
 
  [Symbol.iterator](): IterableIterator<E> {
    return new HeapIterator(this, this.changes, `${this.className}[Symbol.iterator]().next`)
  }
 
  cursor(): Cursor<E> {
    return new HeapCursor(this, this.changes, `${this.className}.cursor`)
  }
 
  override equals(other: unknown): boolean {
    return other === this || other === this.facade
  }
}
 
// ── Plain heaps ──
//
// Each sift compares as the planned repairs do: going down, the right child with the left, then the
// smaller child with the element sinking; going up, the element rising with each parent. Numbers and
// strings each have their own loops, so every comparison site sees one type.
 
/** Adds `x` from a new slot at the end, moving each parent it is before one level down. */
function insertNumber(heap: number[], x: number): void {
  let i = heap.length
  while (i > 0) {
    const parent = (i - 1) >> 1
    const p = heap[parent] as number
    if (!numberBefore(x, p)) break
    heap[i] = p
    i = parent
  }
  heap[i] = x
}
 
function insertString(heap: string[], x: string): void {
  let i = heap.length
  while (i > 0) {
    const parent = (i - 1) >> 1
    const p = heap[parent] as string
    if (!(x < p)) break
    heap[i] = p
    i = parent
  }
  heap[i] = x
}
 
/** Places `x` from the hole at the root, moving the smaller child up while it is before `x`. */
function siftDownNumbers(heap: number[], x: number): void {
  siftNumbers(heap, x, 0, heap.length)
}
 
function siftDownStrings(heap: string[], x: string): void {
  siftStrings(heap, x, 0, heap.length)
}
 
function siftNumbers(heap: number[], x: number, from: number, length: number): void {
  let i = from
  for (;;) {
    let child = 2 * i + 1
    if (child >= length) break
    let c = heap[child] as number
    const right = child + 1
    if (right < length) {
      const r = heap[right] as number
      if (numberBefore(r, c)) {
        child = right
        c = r
      }
    }
    if (!numberBefore(c, x)) break
    heap[i] = c
    i = child
  }
  heap[i] = x
}
 
function siftStrings(heap: string[], x: string, from: number, length: number): void {
  let i = from
  for (;;) {
    let child = 2 * i + 1
    if (child >= length) break
    let c = heap[child] as string
    const right = child + 1
    if (right < length) {
      const r = heap[right] as string
      if (r < c) {
        child = right
        c = r
      }
    }
    if (!(c < x)) break
    heap[i] = c
    i = child
  }
  heap[i] = x
}
 
/** Orders `items` into a heap in place, bottom-up. */
function heapifyNumbers(items: number[]): void {
  const length = items.length
  for (let start = (length >> 1) - 1; start >= 0; start--) {
    siftNumbers(items, items[start] as number, start, length)
  }
}
 
function heapifyStrings(items: string[]): void {
  const length = items.length
  for (let start = (length >> 1) - 1; start >= 0; start--) {
    siftStrings(items, items[start] as string, start, length)
  }
}