All files / src/internal/views set-from-map.ts

100% Statements 31/31
100% Branches 2/2
100% Functions 19/19
100% Lines 30/30

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                                          1118x 1118x 1118x       641x       183x       9214x       9x       386x       15252x       836x       3412x       1788x 1788x         714x 714x 714x 714x 2015x 2012x   697x       549x       20x             29x 29x 42x         25x 25x       93x       89x       125x      
// The set a map backs: its members are the map's keys. Adding goes through the map's
// `getOrInsert`; reading, iterating and removing go through the map's key set, so the map's key
// equality, order, eviction and iteration mode apply unchanged.
 
import type { Cursor } from '../../interfaces/collection'
import type { MapCollection } from '../../interfaces/map'
import type { SetCollection } from '../../interfaces/set'
import { type HashFunction, hashCodeSymbol } from '../equality'
import { apply } from '../intrinsics'
import { AbstractSet } from '../skeletons/abstract-set'
 
/**
 * A live set over a map's keys; every member maps to `true` when added through the set.
 *
 * @internal
 */
export class SetFromMap<E> extends AbstractSet<E> {
  private readonly map: MapCollection<E, boolean>
  private readonly members: SetCollection<E>
 
  constructor(map: MapCollection<E, boolean>) {
    super()
    this.map = map
    this.members = map.keySet()
  }
 
  get owner(): SetCollection<E> {
    return this
  }
 
  get [Symbol.toStringTag](): string {
    return 'SetFromMap'
  }
 
  get size(): number {
    return this.map.size
  }
 
  override isEmpty(): boolean {
    return this.map.isEmpty()
  }
 
  override get revision(): number {
    return this.map.revision
  }
 
  [Symbol.iterator](): IterableIterator<E> {
    return this.members[Symbol.iterator]()
  }
 
  cursor(): Cursor<E> {
    return this.members.cursor()
  }
 
  protected hasMember(element: E, _method: string): boolean {
    return this.members.has(element)
  }
 
  add(element: E): this {
    this.map.getOrInsert(element, true)
    return this
  }
 
  /** Adds one element at a time; `true` when some element was absent just before its add. */
  override addAll(elements: Iterable<E>): boolean {
    const items = this.readAll(elements, 'addAll')
    const map = this.map
    let added = false
    for (const item of items) {
      if (!map.has(item)) added = true
      map.getOrInsert(item, true)
    }
    return added
  }
 
  override delete(element: E): boolean {
    return this.members.delete(element)
  }
 
  override clear(): void {
    this.members.clear()
  }
 
  override forEach(
    action: (element: E, same: E, set: SetCollection<E>) => void,
    thisArg?: unknown,
  ): void {
    this.requireFunction(action, 'forEach', 'action')
    this.members.forEach((element) => {
      apply(action, thisArg, [element, element, this])
    })
  }
 
  override removeIf(predicate: (element: E) => unknown): boolean {
    this.requireFunction(predicate, 'removeIf', 'predicate')
    return this.members.removeIf(predicate)
  }
 
  override removeAll(elements: Iterable<E>): boolean {
    return this.members.removeAll(this.readAll(elements, 'removeAll'))
  }
 
  override retainAll(elements: Iterable<E>): boolean {
    return this.members.retainAll(this.readAll(elements, 'retainAll'))
  }
 
  protected memberHashes(hash: HashFunction): number {
    return this.members[hashCodeSymbol](hash)
  }
}