All files / src/internal/skeletons abstract-linked-set.ts

98.61% Statements 213/216
98.97% Branches 97/98
100% Functions 38/38
98.88% Lines 178/180

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                                                                  6451x                                                         220441x 220441x 220441x         1308283x 1308283x 1308283x 610x 610x 610x 610x   1308283x         1875647x               83x       473326x 473326x       504588x 504588x 322388x       1470x 1470x 1242x         142659x         44362x 44362x       659924x 659924x 275090x 275090x 12737x                 95157x 95157x 95157x 95157x 95157x 95157x 70672x 70672x 70672x   24424x 24424x               26491x 26491x 26491x 26491x 26491x 20347x 20347x 20347x   6144x 6144x         37069x 37069x 37069x 31454x 31454x       127503x 127503x 93772x 84415x 127462x       7074x 3318x 7068x       9299x 9299x 8481x 3250x 9294x         101695x 101695x   100971x 100971x 100971x 100971x 100971x 100971x   724x       724x 1655x         4959x 4959x 4959x 4959x 4959x 13186x             9950x     4926x         21489x 21489x 1984x   19505x       5774x 5774x 4662x         5229x 5229x 270x   4959x       5506x       9186x 9186x 4117x 4117x 4117x       6252x       34488x 34488x 34488x       45099x       53410x 53410x 17708x 16520x 16520x 16520x 16520x   35702x 35702x     53410x 23598x 23598x       2640x 2640x 458x 370x 370x 370x 370x   2182x 1778x 1778x       9394x 9394x   1522x   440x       7872x 7872x     1390x 1390x 816x               3241x       3268x               6509x 31891x 6467x 6467x 1672x 1672x 4655x 1672x   4795x 4795x 4795x 15248x   4766x 7665x 3504x 3504x               2000x 2000x 459x 459x 1914x 459x 459x   1541x 1541x 7511x   1541x         2131x 3081x 1606x       5731x   121x 206x 109x   5610x      
// The skeleton of a linked hash set and its reversed view: every operation is written once, in the
// set's own order, over a linked hash table and a direction. The set walks the table from the
// front; the reversed view walks the same table from the back, so its front is the set's back.
// Each operation derives the argument's key or hash once, finds the member, and only then changes
// the table.
//
// A set with SameValueZero members starts in the native layout (`state.flat`, see flat-layout.ts):
// its members live directly in a native `Set`, and the calls a native `Set` can answer are
// answered from it. Every other call reads `table`, which first makes the linked hash table from
// the native `Set`, once; the reversed view always does.
 
import { NoSuchElementError } from '../../errors/errors'
import type { Cursor } from '../../interfaces/collection'
import type { SequencedSet } from '../../interfaces/sequenced-set'
import { SEQUENCED_COLLECTION_BRAND } from '../brand'
import {
  type FlatLayout,
  FlatSetIterator,
  frontKey,
  isFront,
  tableFromSet,
} from '../engines/flat-layout'
import { type HashEquality, normalise, Operation } from '../engines/hash-support'
import type { LinkedHashTable, LinkedRecord } from '../engines/linked-hash-table'
import { MISSING } from '../engines/map-stores'
import type { HashFunction } from '../equality'
import { LiveCursor } from '../iteration'
import type { ChangeCounter } from '../revision'
import { AbstractSet } from './abstract-set'
 
type SetRecord<E> = LinkedRecord<E, undefined>
 
function keyOf<E>(record: SetRecord<E>): E {
  return record.key
}
 
/**
 * What a linked hash set and its reversed view share: the layout (the native `Set` or the table),
 * the change counter and the member equality.
 *
 * @internal
 */
export interface LinkedSetState<E> extends FlatLayout<Set<E>, E, undefined> {
  readonly changes: ChangeCounter
  readonly equality: HashEquality<E>
}
 
/**
 * Base class of a linked hash set and its reversed view.
 *
 * Subclasses give the tag, the public owner and `reversed()`; the state and the direction are
 * fixed at construction.
 *
 * @internal
 */
export abstract class AbstractLinkedSet<E> extends AbstractSet<E> implements SequencedSet<E> {
  /** The state, shared by a set and its reversed view. */
  readonly state: LinkedSetState<E>
  /** Whether this set walks the table from the back. */
  readonly fromBack: boolean
 
  constructor(state: LinkedSetState<E>, fromBack: boolean) {
    super(state.changes)
    this.state = state
    this.fromBack = fromBack
  }
 
  /** The table, shared by a set and its reversed view; made from the native layout on first use. */
  get table(): LinkedHashTable<E, undefined> {
    const state = this.state
    const flat = state.flat
    if (flat !== undefined) {
      state.table = tableFromSet(flat, state.equality)
      state.flat = undefined
      state.front = undefined
      state.frontKey = MISSING
    }
    return state.table as LinkedHashTable<E, undefined>
  }
 
  /** The native `Set` while the layout is native and this is the set, not its reversed view. */
  private flatOf(): Set<E> | undefined {
    return this.fromBack ? undefined : this.state.flat
  }
 
  abstract reversed(): SequencedSet<E>
  abstract override get owner(): SequencedSet<E>
 
  /** Marks every sequenced collection for `isSequencedCollection`, across loaded copies. */
  get [SEQUENCED_COLLECTION_BRAND](): true {
    return true
  }
 
  get size(): number {
    const flat = this.flatOf()
    return flat !== undefined ? flat.size : this.table.size
  }
 
  [Symbol.iterator](): IterableIterator<E> {
    const flat = this.flatOf()
    if (flat !== undefined) return new FlatSetIterator<E>(this.state, flat, false)
    return this.table.keyIterator(this.fromBack)
  }
 
  override entries(): IterableIterator<[E, E]> {
    const flat = this.flatOf()
    if (flat !== undefined) return new FlatSetIterator<[E, E]>(this.state, flat, true)
    return super.entries()
  }
 
  /** Starts an operation whose errors name `<tag>.<method>`. */
  protected operation(method: string): Operation {
    return new Operation(this.changes, `${this.className}.${method}`)
  }
 
  /** Finds the member equal to `value` (already normalised), with one `key` or `hash` call. */
  private findRecord(value: E, op: Operation): SetRecord<E> | undefined {
    const table = this.table
    return table.find(value, table.probe(value, op), op)
  }
 
  protected hasMember(element: E, method: string): boolean {
    const flat = this.flatOf()
    if (flat !== undefined) return flat.has(element)
    const table = this.table
    if (table.plain) return table.findPlain(normalise(element)) !== undefined
    return this.findRecord(normalise(element), this.operation(method)) !== undefined
  }
 
  /**
   * Inserts `element` at this set's front or back unless a member equals it; a present member is
   * moved to that end when `move` is set and kept where it is otherwise. Returns whether `element`
   * was inserted.
   */
  private place(element: E, op: Operation, ownFront: boolean, move: boolean): boolean {
    const value = normalise(element)
    const table = this.table
    const probe = table.probe(value, op)
    const found = table.find(value, probe, op)
    const front = ownFront !== this.fromBack
    if (found === undefined) {
      table.insert(value, probe, undefined, front)
      this.changes.structural()
      return true
    }
    if (move && table.move(found, front)) this.changes.structural()
    return false
  }
 
  /**
   * `place` on a plain table: one native read, then at most one native write. Runs no user code,
   * so no operation is needed.
   */
  private placePlain(element: E, ownFront: boolean, move: boolean): boolean {
    const value = normalise(element)
    const table = this.table
    const found = table.findPlain(value)
    const front = ownFront !== this.fromBack
    if (found === undefined) {
      table.insert(value, value, undefined, front)
      this.changes.structural()
      return true
    }
    if (move && table.move(found, front)) this.changes.structural()
    return false
  }
 
  /** Adds `element` at the back of the native layout with one native call; returns whether it was new. */
  private addFlat(flat: Set<E>, element: E): boolean {
    const size = flat.size
    flat.add(element)
    if (flat.size === size) return false
    this.changes.structural()
    return true
  }
 
  add(element: E): this {
    const flat = this.flatOf()
    if (flat !== undefined) this.addFlat(flat, element)
    else if (this.table.plain) this.placePlain(element, false, false)
    else this.place(element, this.operation('add'), false, false)
    return this
  }
 
  addFirst(element: E): this {
    if (this.table.plain) this.placePlain(element, true, true)
    else this.place(element, this.operation('addFirst'), true, true)
    return this
  }
 
  addLast(element: E): this {
    const flat = this.flatOf()
    if (flat !== undefined && !flat.has(element)) this.addFlat(flat, element)
    else if (this.table.plain) this.placePlain(element, false, true)
    else this.place(element, this.operation('addLast'), false, true)
    return this
  }
 
  /** Adds each element of `elements` at the back, in order, naming `where` in errors. */
  addInitial(elements: Iterable<E>, where: string): void {
    const state = this.state
    if (state.flat !== undefined) {
      // A new set: one native construction, counted as one add per new member.
      const built = new Set<E>(elements)
      state.flat = built
      const changes = this.changes
      changes.revision += built.size
      changes.structure += built.size
      return
    }
    Iif (this.table.plain) {
      for (const element of elements) this.placePlain(element, false, false)
      return
    }
    for (const element of elements) {
      this.place(element, new Operation(this.changes, where), false, false)
    }
  }
 
  override addAll(elements: Iterable<E>): boolean {
    const items = this.readAll(elements, 'addAll')
    const flat = this.flatOf()
    const plain = flat !== undefined || this.table.plain
    let added = false
    for (const item of items) {
      if (
        flat !== undefined
          ? this.addFlat(flat, item)
          : plain
            ? this.placePlain(item, false, false)
            : this.place(item, this.operation('addAll'), false, false)
      ) {
        added = true
      }
    }
    return added
  }
 
  /** The record at this set's front (`back` false) or back, or the empty-set error. */
  private end(back: boolean, method: string): SetRecord<E> {
    const record = this.table.first(back !== this.fromBack)
    if (record === undefined) {
      throw new NoSuchElementError(`${this.className}.${method}: the set is empty`)
    }
    return record
  }
 
  getFirst(): E {
    const flat = this.flatOf()
    if (flat !== undefined) return this.front(flat, 'getFirst')
    return this.end(false, 'getFirst').key
  }
 
  /** The first member of the native layout, or the empty-set error. */
  private front(flat: Set<E>, method: string): E {
    const key = frontKey(this.state, flat, false)
    if (key === MISSING) {
      throw new NoSuchElementError(`${this.className}.${method}: the set is empty`)
    }
    return key as E
  }
 
  getLast(): E {
    return this.end(true, 'getLast').key
  }
 
  removeFirst(): E {
    const flat = this.flatOf()
    if (flat === undefined) return this.removeRecord(this.end(false, 'removeFirst')).key
    const key = this.front(flat, 'removeFirst')
    this.deleteElement(key, 'removeFirst')
    return key
  }
 
  removeLast(): E {
    return this.removeRecord(this.end(true, 'removeLast')).key
  }
 
  private removeRecord(record: SetRecord<E>): SetRecord<E> {
    this.table.remove(record)
    this.changes.structural()
    return record
  }
 
  override delete(element: E): boolean {
    return this.deleteElement(element, 'delete')
  }
 
  private deleteElement(element: E, method: string): boolean {
    const flat = this.flatOf()
    if (flat !== undefined) {
      if (!flat.delete(element)) return false
      const state = this.state
      if (isFront(state, element)) state.frontKey = MISSING
      this.changes.structural()
      return true
    }
    const table = this.table
    const record = table.plain
      ? table.findPlain(normalise(element))
      : this.findRecord(normalise(element), this.operation(method))
    if (record === undefined) return false
    this.removeRecord(record)
    return true
  }
 
  override clear(): void {
    const flat = this.flatOf()
    if (flat !== undefined) {
      if (flat.size === 0) return
      flat.clear()
      this.state.frontKey = MISSING
      this.changes.structural()
      return
    }
    if (this.table.size === 0) return
    this.table.clear()
    this.changes.structural()
  }
 
  cursor(): Cursor<E> {
    const flat = this.flatOf()
    if (flat !== undefined) {
      // Removal by member, in whatever layout the set is in by then.
      return new LiveCursor<E>(
        new FlatSetIterator<E>(this.state, flat, false),
        (member) => this.deleteElement(member, 'cursor'),
        `${this.className}.cursor`,
      )
    }
    const table = this.table
    return new LiveCursor<E, SetRecord<E>>(
      table.iterator(this.fromBack),
      (record) => {
        const current = table.current(record)
        if (current !== undefined) this.removeRecord(current)
        else this.deleteElement(record.key, 'cursor')
      },
      `${this.className}.cursor`,
      keyOf,
    )
  }
 
  override removeAll(elements: Iterable<E>): boolean {
    return this.removeMatching('removeAll', elements, true)
  }
 
  override retainAll(elements: Iterable<E>): boolean {
    return this.removeMatching('retainAll', elements, false)
  }
 
  /**
   * Builds a lookup of `elements` under this set's equality (user code runs only here and while
   * probing it, in this set's order), decides every member, then removes the decided ones.
   */
  private removeMatching(method: string, elements: Iterable<E>, remove: boolean): boolean {
    const items = this.readAll(elements, method)
    for (let i = 0; i < items.length; i++) items[i] = normalise(items[i] as E)
    const flat = this.flatOf()
    if (flat !== undefined) {
      const lookup = new Set<E>(items)
      const doomed: E[] = []
      for (const member of flat) if (lookup.has(member) === remove) doomed.push(member)
      return this.removeMembers(doomed, method)
    }
    const matches = this.table.lookup(items, this.operation(method))
    const doomed: SetRecord<E>[] = []
    for (const record of this.table.records(this.fromBack)) {
      if (matches(record) === remove) doomed.push(record)
    }
    if (doomed.length === 0) return false
    for (const record of doomed) this.table.remove(record)
    this.changes.structural()
    return true
  }
 
  /**
   * Removes the members marked in `flags` (this set's order) by stored entry. Called only when at
   * least one member is marked.
   */
  protected override removeMarked(flags: readonly boolean[]): void {
    const flat = this.flatOf()
    if (flat !== undefined) {
      const doomed: E[] = []
      let i = 0
      for (const member of flat) if (flags[i++] === true) doomed.push(member)
      this.removeMembers(doomed, 'removeIf')
      return
    }
    const records = this.table.records(this.fromBack)
    for (let i = 0; i < records.length; i++) {
      if (flags[i] === true) this.table.remove(records[i] as SetRecord<E>)
    }
    this.changes.structural()
  }
 
  /** Removes `members` (all present) from the native layout; returns whether there were any. */
  private removeMembers(members: E[], method: string): boolean {
    if (members.length === 0) return false
    for (let i = 0; i < members.length; i++) this.deleteElement(members[i] as E, method)
    return true
  }
 
  protected memberHashes(hash: HashFunction): number {
    if (this.flatOf() !== undefined) {
      // Walked live: a member's hash may run code that changes the layout.
      let sum = 0
      for (const member of this) sum = (sum + hash(member)) | 0
      return sum
    }
    return this.table.keyHashes(hash)
  }
}