All files / src/internal/views reversed-deque.ts

100% Statements 22/22
100% Branches 2/2
100% Functions 16/16
100% Lines 20/20

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                                  50945x 50945x       13585x       34394x       19948x       2726x       2669x       34212x       2192x       10353x       2990x       2922x       2196x 2196x 9011x 2196x       346x       158x       1192x       21x      
// The reversed view of a deque: its front is the source's back. Every operation maps straight onto
// the source (always the engine, because reversing the view returns the source), so views never
// nest. It shares the source's change counter and never goes stale.
 
import type { Deque } from '../../interfaces/queue'
import type { Where } from '../checks'
import { AbstractDeque, type DequeWalker } from '../skeletons/abstract-deque'
 
/**
 * A live view of a deque in reverse order.
 *
 * @internal
 */
export class ReversedDeque<E> extends AbstractDeque<E> {
  private readonly source: AbstractDeque<E>
 
  constructor(source: AbstractDeque<E>) {
    super(source.changeCounter)
    this.source = source
  }
 
  get [Symbol.toStringTag](): string {
    return 'ReversedDeque'
  }
 
  get size(): number {
    return this.source.size
  }
 
  override get storesEmpty(): boolean {
    return this.source.storesEmpty
  }
 
  peekFront(): E {
    return this.source.peekBack()
  }
 
  peekBack(): E {
    return this.source.peekFront()
  }
 
  walker(fromBack: boolean): DequeWalker<E> {
    return this.source.walker(!fromBack)
  }
 
  insertFront(element: E, where: Where, method = ''): void {
    this.source.insertBack(element, where, method)
  }
 
  insertBack(element: E, where: Where, method = ''): void {
    this.source.insertFront(element, where, method)
  }
 
  takeFront(): E {
    return this.source.takeBack()
  }
 
  takeBack(): E {
    return this.source.takeFront()
  }
 
  compact(flags: readonly boolean[]): void {
    const size = this.source.size
    const mirrored: boolean[] = []
    for (let i = 0; i < size; i++) mirrored.push(flags[size - 1 - i] === true)
    this.source.compact(mirrored)
  }
 
  clearAll(): void {
    this.source.clearAll()
  }
 
  override releaseStorage(): void {
    this.source.releaseStorage()
  }
 
  ensureRoom(count: number, where: string): void {
    this.source.ensureRoom(count, where)
  }
 
  reversed(): Deque<E> {
    return this.source.owner
  }
}