All files / src/internal/engines array-list-engine.ts

100% Statements 64/64
100% Branches 7/7
100% Functions 15/15
100% Lines 50/50

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                                266820x       266820x 266820x       109877x       421858x       1446451x       249802x       249802x 249763x   39x       1269609x       387890x 387890x       6631x 6631x 6631x       2436x 2436x 18547x 2436x 2436x       626x 626x 626x   1786x 4846x 1786x 626x       4862x 4862x 4862x 2022935x 4862x 4862x 4862x       397x 397x 397x 447x 397x 397x       60x 60x 60x 404x   60x 60x       1205x 1205x       856x      
// The engine behind `ArrayList`: the list operations over a native array. The public class holds
// one engine and delegates to it, so nothing here reaches the published types.
 
import type { List } from '../../interfaces/list'
import { checkIndex } from '../checks'
import { AbstractList } from '../skeletons/abstract-list'
import { reversedOf, subListOf } from '../views/list-views'
 
/**
 * A list stored in a native array. Elements move with plain index loops (no `splice` or spread),
 * so the array never gets holes and no argument-count limit applies.
 *
 * @internal
 */
export class ArrayListEngine<E> extends AbstractList<E> {
  /** The elements, in order. Never handed out; replaced only while the list is being built. */
  items: E[] = []
  private readonly facade: List<E>
 
  constructor(facade: List<E>) {
    super()
    this.facade = facade
  }
 
  override get owner(): List<E> {
    return this.facade
  }
 
  get [Symbol.toStringTag](): string {
    return 'ArrayList'
  }
 
  get size(): number {
    return this.items.length
  }
 
  override get(index: number): E {
    const items = this.items
    // A valid index is a whole number in 0..length - 1, and an array's length is below 2^32, so
    // `index >>> 0 === index` holds for every valid index; anything else goes to the full check,
    // which throws.
    if (typeof index === 'number' && index >>> 0 === index && index < items.length) {
      return items[index] as E
    }
    return items[checkIndex(index, items.length, this, 'get')] as E
  }
 
  getAt(index: number): E {
    return this.items[index] as E
  }
 
  setAt(index: number, element: E): void {
    this.items[index] = element
    this.changes.replaced()
  }
 
  override add(element: E): this {
    this.items.push(element)
    this.changes.structural()
    return this
  }
 
  insertAt(index: number, element: E): void {
    const items = this.items
    items.push(element)
    for (let j = items.length - 1; j > index; j--) items[j] = items[j - 1] as E
    items[index] = element
    this.changes.structural()
  }
 
  insertItems(index: number, elements: readonly E[]): void {
    const items = this.items
    const count = elements.length
    const oldLength = items.length
    // Grow with real elements first, so the array never has holes.
    for (let k = 0; k < count; k++) items.push(elements[k] as E)
    for (let j = oldLength - 1; j >= index; j--) items[j + count] = items[j] as E
    for (let k = 0; k < count; k++) items[index + k] = elements[k] as E
    this.changes.structural()
  }
 
  removeAtIndex(index: number): E {
    const items = this.items
    const element = items[index] as E
    const last = items.length - 1
    for (let j = index; j < last; j++) items[j] = items[j + 1] as E
    items.length = last
    this.changes.structural()
    return element
  }
 
  removeRange(from: number, to: number): void {
    const items = this.items
    const count = to - from
    const length = items.length
    for (let j = to; j < length; j++) items[j - count] = items[j] as E
    items.length = length - count
    this.changes.structural()
  }
 
  protected override removeMarked(flags: readonly boolean[]): void {
    const items = this.items
    let kept = 0
    for (let i = 0; i < items.length; i++) {
      if (flags[i] !== true) items[kept++] = items[i] as E
    }
    items.length = kept
    this.changes.structural()
  }
 
  subList(from: number, to: number): List<E> {
    this.checkSubList(from, to)
    return subListOf(this, from, to)
  }
 
  reversed(): List<E> {
    return reversedOf(this)
  }
}