All files / src/internal list-iteration.ts

100% Statements 50/50
100% Branches 14/14
100% Functions 15/15
100% Lines 47/47

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                                                                212684x       212684x 212684x       719803x                                     1330x 1330x             1330x 1330x 1330x 1330x 1330x       275x       8x       24x       8x       1770x 1770x 51x 716x 716x 716x 716x       56x 56x 32x 32x 32x 32x       28x 28x 15x       16x 16x 16x 16x       122x 122x 67x 67x 67x 67x         275x 275x   1x         1992x 1037x             46x              
// Iteration over indexed lists: a fail-fast iterator and a two-way list cursor, written once over
// the index operations every list provides (the array engine and the views alike).
 
import {
  ConcurrentModificationError,
  IllegalStateError,
  NoSuchElementError,
} from '../errors/errors'
import type { ListCursor } from '../interfaces/list'
import { done, FailFastIterator } from './iteration'
import type { ChangeCounter } from './revision'
 
/**
 * The index operations a list provides to its iterators and cursors. Each changing operation
 * records itself in the list's change counter.
 *
 * @internal
 */
export interface IndexedList<E> {
  readonly size: number
  getAt(index: number): E
  setAt(index: number, element: E): void
  insertAt(index: number, element: E): void
  removeAtIndex(index: number): E
}
 
/**
 * A fail-fast iterator over a list by index.
 *
 * @internal
 */
export class IndexIterator<E> extends FailFastIterator<E> {
  private index = 0
  private readonly list: IndexedList<E>
 
  constructor(list: IndexedList<E>, changes: ChangeCounter, where: string) {
    super(changes, where)
    this.list = list
  }
 
  protected step(): IteratorResult<E> {
    return this.index < this.list.size
      ? { done: false, value: this.list.getAt(this.index++) }
      : done()
  }
}
 
/**
 * A two-way cursor over a list. It sits between two elements; `next()` and `previous()` both count
 * as moves that make `set` and `remove` possible, and `insert` or `remove` end that until the next
 * move. Changes made through this cursor keep it valid; any other structural change makes its next
 * `next`, `previous`, `set`, `insert` or `remove` throw `ConcurrentModificationError`. `hasNext`,
 * `hasPrevious`, `nextIndex` and `previousIndex` never throw.
 *
 * @internal
 */
export class IndexListCursor<E> implements ListCursor<E> {
  /** The index of the element `next()` returns. */
  private position: number
  /** The index of the element the last move returned, or `-1` when `set`/`remove` are not allowed. */
  private last = -1
  private moved = false
  private expected: number
  private readonly list: IndexedList<E>
  private readonly changes: ChangeCounter
  private readonly where: string
 
  constructor(list: IndexedList<E>, changes: ChangeCounter, position: number, where: string) {
    this.list = list
    this.changes = changes
    this.position = position
    this.where = where
    this.expected = changes.structure
  }
 
  hasNext(): boolean {
    return this.position < this.size()
  }
 
  hasPrevious(): boolean {
    return this.position > 0
  }
 
  nextIndex(): number {
    return this.position
  }
 
  previousIndex(): number {
    return this.position - 1
  }
 
  next(): E {
    this.check()
    if (this.position >= this.list.size)
      throw new NoSuchElementError(`${this.where}: no more elements`)
    const element = this.list.getAt(this.position)
    this.last = this.position++
    this.moved = true
    return element
  }
 
  previous(): E {
    this.check()
    if (this.position <= 0) throw new NoSuchElementError(`${this.where}: no previous element`)
    const element = this.list.getAt(--this.position)
    this.last = this.position
    this.moved = true
    return element
  }
 
  set(element: E): void {
    this.check()
    if (this.last < 0) throw this.misuse('set')
    this.list.setAt(this.last, element)
  }
 
  insert(element: E): void {
    this.check()
    this.list.insertAt(this.position++, element)
    this.last = -1
    this.expected = this.changes.structure
  }
 
  remove(): void {
    this.check()
    if (this.last < 0) throw this.misuse('remove')
    this.list.removeAtIndex(this.last)
    this.position = this.last
    this.last = -1
    this.expected = this.changes.structure
  }
 
  /** The list's size, or `0` when reading it throws (a stale view): `hasNext` never throws. */
  private size(): number {
    try {
      return this.list.size
    } catch {
      return 0
    }
  }
 
  private check(): void {
    if (this.changes.structure !== this.expected) {
      throw new ConcurrentModificationError(
        `${this.where}: the list was changed other than through this cursor`,
      )
    }
  }
 
  private misuse(method: string): IllegalStateError {
    return new IllegalStateError(
      this.moved
        ? `${this.where}: ${method}() called after insert() or remove() without next() or previous()`
        : `${this.where}: ${method}() called before next()`,
    )
  }
}