All files / src/collections hash-set.ts

100% Statements 45/45
100% Branches 4/4
100% Functions 33/33
100% Lines 43/43

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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613                                          106x                                                       6x                                                                                                                                                     3032x 3032x 3032x                                         30x 24x 24x 24x 24x                                     452x               45x               876x               152x         40406x               1379x         17x                               20x                                           122486x 122486x                                       21983x                                       39378x                                         70x                                           69x                                         66x                                       43x                                 1304x                               35x                               7x                               10x                                       50x                                   4455x                                     2947x                               1729x                                   5x 5x 5x                                       576x         327x                 5681x                               498x                                 17726x                 36x      
/**
 * `HashSet`: a set that behaves like native `Set` and can compare members by value.
 *
 * @packageDocumentation
 */
 
import type { Cursor } from '../interfaces/collection'
import type { SetCollection } from '../interfaces/set'
import { brandedHasInstance, SET_COLLECTION_BRAND } from '../internal/brand'
import { describeValue, isIterable } from '../internal/checks'
import { HashSetEngine } from '../internal/engines/hash-set-engine'
import { readEquality, readSetArguments } from '../internal/engines/hash-support'
import {
  type Equivalence,
  equalsSymbol,
  type HashFunction,
  hashCodeSymbol,
  type KeyOf,
} from '../internal/equality'
import { COLLECTION_BRAND, INSPECT } from '../internal/output'
 
const HASH_SET_BRAND: unique symbol = /*#__PURE__*/ Symbol.for(
  'javascript-collections-framework/1/HashSet',
)
 
/**
 * Options for a `HashSet`. At most one of `key` and `equivalence` may be given; with neither, members
 * compare with SameValueZero, as in native `Set`.
 *
 * @example
 * ```ts
 * const byId: HashSetOptions<{ id: number }> = { key: (user) => user.id }
 * const byValue: HashSetOptions<Point> = { equivalence: valueEquivalence }
 * ```
 */
export interface HashSetOptions<E> {
  /**
   * Derives the primitive key a member is found by; two values are the same member when their keys
   * are SameValueZero-equal. Called once per value per operation, never again for a stored member.
   */
  key?: KeyOf<E>
  /**
   * Decides membership by its `equals` and `hash`. `valueEquivalence` honours the
   * `equalsSymbol`/`hashCodeSymbol` protocol; `sameValueZeroEquivalence` is the default behaviour.
   */
  equivalence?: Equivalence<E>
}
 
function notIterable(where: string, value: unknown): TypeError {
  return new TypeError(`${where}: elements is not iterable (${describeValue(value)})`)
}
 
/**
 * A set that behaves like native `Set` — insertion order, SameValueZero membership (`NaN` is one
 * member, `-0` is stored as `0`), `add` returning the set, live iteration — and adds value
 * equality on request, structural `equals`, and the shared collection API (`removeIf`,
 * `removeAll`, `retainAll`, cursors, `toJSON`).
 *
 * Give a `key` function to treat values with the same derived key as one member, or an
 * `equivalence` (such as `valueEquivalence`) to decide membership by its `equals` and `hash`.
 * The original values are stored and returned, never the keys. `null` and `undefined` are allowed.
 *
 * Iteration is live, as with native `Set`: members deleted before they are reached are skipped,
 * members added during iteration are visited, and nothing throws `ConcurrentModificationError`.
 * Changing a member's key or hash while it is in the set leaves lookups for it unspecified; it can
 * still be removed by a cursor, `removeIf` or `clear`.
 *
 * Two sets are equal when they have the same size and each has every member of the other, in any
 * order. The class is not designed to be subclassed.
 *
 * @example
 * ```ts
 * const tags = new HashSet(['a', 'b'])
 * tags.add('a').add('c') // HashSet(3) {'a', 'b', 'c'}
 *
 * const users = new HashSet<{ id: number }>({ key: (user) => user.id })
 * users.add({ id: 1 }).add({ id: 1 })
 * users.size // 1
 * ```
 *
 * @sealed
 */
export class HashSet<E> implements SetCollection<E> {
  private engine: HashSetEngine<E>
 
  /**
   * Creates a set holding each distinct element of `elements` once, in first-seen order; empty
   * when `elements` is omitted, `undefined` or `null` (as `new Set(null)` is). A string is
   * iterable, so `new HashSet('ab')` holds `'a'` and `'b'`.
   *
   * Time: O(m) average for `m` elements, plus the `key` or `equivalence` calls.
   *
   * @param elements - The initial elements.
   * @param options - `key` or `equivalence`; `null` means none.
   * @throws TypeError when `elements` is neither iterable nor `null`/`undefined`, when `options`
   *   is not an object, when both `key` and `equivalence` are given, when `key` is not a function,
   *   or when `equivalence` lacks an `equals` or `hash` function.
   *
   * @example
   * ```ts
   * new HashSet([1, 2, 1]) // HashSet(2) {1, 2}
   * new HashSet(points, { equivalence: valueEquivalence })
   * ```
   */
  constructor(elements?: Iterable<E> | null, options?: HashSetOptions<E> | null)
  /**
   * Creates an empty set with the given options.
   *
   * Time: O(1).
   *
   * @param options - `key` or `equivalence`.
   * @throws TypeError when both `key` and `equivalence` are given, when `key` is not a function,
   *   or when `equivalence` lacks an `equals` or `hash` function.
   *
   * @example
   * ```ts
   * const byName = new HashSet<{ name: string }>({ key: (item) => item.name })
   * ```
   */
  constructor(options: HashSetOptions<E>)
  constructor(
    elementsOrOptions?: Iterable<E> | HashSetOptions<E> | null,
    options?: HashSetOptions<E> | null,
  ) {
    const { elements, equality } = readSetArguments<E>(elementsOrOptions, options, 'HashSet')
    this.engine = new HashSetEngine<E>(this, equality)
    if (elements !== undefined) this.engine.addInitial(elements, 'HashSet')
  }
 
  /**
   * Returns a new set of the distinct elements of `elements`, in first-seen order, with the given
   * options.
   *
   * Time: O(m) average for `m` elements, plus the `key` or `equivalence` calls.
   *
   * @param elements - The elements.
   * @param options - `key` or `equivalence`; `null` means none.
   * @returns A new set.
   * @throws TypeError when `elements` is not iterable (`null` and `undefined` included), or the
   *   options are invalid (as for the constructor).
   *
   * @example
   * ```ts
   * HashSet.from(['a', 'A'], { key: (s) => s.toLowerCase() }) // HashSet(1) {'a'}
   * ```
   */
  static from<T>(elements: Iterable<T>, options?: HashSetOptions<T> | null): HashSet<T> {
    if (!isIterable(elements)) throw notIterable('HashSet.from', elements)
    const set = new HashSet<T>()
    set.engine = new HashSetEngine<T>(set, readEquality<T>(options, 'HashSet.from'))
    set.engine.addInitial(elements, 'HashSet.from')
    return set
  }
 
  /**
   * Returns whether `value` is a `HashSet`, including one created by another copy of the library
   * with the same major version.
   *
   * Time: O(1).
   *
   * @param value - The value on the left of `instanceof`.
   * @returns `true` when `value` is a `HashSet`.
   *
   * @example
   * ```ts
   * new HashSet() instanceof HashSet // true
   * ```
   */
  static [Symbol.hasInstance](value: unknown): boolean {
    // biome-ignore lint/complexity/noThisInStatic: `this` is the class on the right of `instanceof`
    return brandedHasInstance(this, HashSet, HASH_SET_BRAND, value)
  }
 
  /**
   * Marks instances of this class so `instanceof` recognises them across copies of the package.
   * Its key is not exported, so the getter is not called directly. Time: O(1).
   */
  get [HASH_SET_BRAND](): true {
    return true
  }
 
  /**
   * Marks sets so `isSetCollection` recognises them across copies of the package. Its key is not
   * exported, so the getter is not called directly. Time: O(1).
   */
  get [SET_COLLECTION_BRAND](): true {
    return true
  }
 
  /**
   * Marks collections so `isCollection` recognises them across copies of the package. Its key is
   * not exported, so the getter is not called directly. Time: O(1).
   */
  get [COLLECTION_BRAND](): true {
    return true
  }
 
  /** The number of members. Time: O(1). */
  get size(): number {
    return this.engine.size
  }
 
  /**
   * A number that changes on every change to the members and never on a read or on a call that
   * changes nothing. Its exact values are unspecified. Time: O(1).
   */
  get revision(): number {
    return this.engine.revision
  }
 
  /** `'HashSet'`, for `Object.prototype.toString`. Time: O(1). */
  get [Symbol.toStringTag](): string {
    return 'HashSet'
  }
 
  /**
   * Returns whether the set has no members.
   *
   * Time: O(1).
   *
   * @returns `size === 0`.
   *
   * @example
   * ```ts
   * new HashSet().isEmpty() // true
   * ```
   */
  isEmpty(): boolean {
    return this.engine.isEmpty()
  }
 
  /**
   * Adds `element` unless a member equals it, and returns the set, so calls chain. A member already
   * present keeps its value and its place in the order.
   *
   * Time: O(1) average, plus one `key` or `hash` call and the `equals` calls.
   *
   * @param element - The element to add (`-0` is stored as `0`).
   * @returns The set.
   * @throws TypeError when `key` returns an object or a function, `equivalence.equals` returns a
   *   non-boolean or `equivalence.hash` a non-finite number.
   * @throws ConcurrentModificationError when a `key`, `equals` or `hash` function adds or removes
   *   members; the set keeps those changes and adds nothing itself.
   *
   * @example
   * ```ts
   * new HashSet<number>().add(1).add(1).add(2) // HashSet(2) {1, 2}
   * ```
   */
  add(element: E): this {
    this.engine.add(element)
    return this
  }
 
  /**
   * Returns whether a member equals `element`.
   *
   * Time: O(1) average, plus one `key` or `hash` call and the `equals` calls.
   *
   * @param element - The value to look for.
   * @returns `true` when a member equals it.
   * @throws TypeError when `key`, `equals` or `hash` returns an invalid result (as for `add`).
   * @throws ConcurrentModificationError when a `key`, `equals` or `hash` function adds or removes
   *   members.
   *
   * @example
   * ```ts
   * new HashSet([Number.NaN]).has(Number.NaN) // true
   * ```
   */
  has(element: E): boolean {
    return this.engine.has(element)
  }
 
  /**
   * Removes the member equal to `element`.
   *
   * Time: O(1) average, plus one `key` or `hash` call and the `equals` calls.
   *
   * @param element - The value to remove.
   * @returns `true` when a member was removed.
   * @throws TypeError when `key`, `equals` or `hash` returns an invalid result (as for `add`).
   * @throws ConcurrentModificationError when a `key`, `equals` or `hash` function adds or removes
   *   members; the set removes nothing itself.
   *
   * @example
   * ```ts
   * new HashSet([1, 2]).delete(1) // true
   * ```
   */
  delete(element: E): boolean {
    return this.engine.delete(element)
  }
 
  /**
   * Adds every element of `elements`, in order, after reading them all. Elements added before an
   * error stay.
   *
   * Time: O(m) average for `m` elements, plus the calls.
   *
   * @param elements - The elements to add.
   * @returns `true` when any element was added.
   * @throws TypeError when `elements` is not iterable, or a function returns an invalid result.
   * @throws ConcurrentModificationError when a `key`, `equals` or `hash` function adds or removes
   *   members.
   *
   * @example
   * ```ts
   * new HashSet([1]).addAll([1, 2]) // true
   * ```
   */
  addAll(elements: Iterable<E>): boolean {
    return this.engine.addAll(elements)
  }
 
  /**
   * Removes every member equal, under this set's equality, to an element of `elements`. The
   * elements are read first and looked up through a temporary index built with this set's `key`
   * or `equivalence`; an error while deciding removes nothing.
   *
   * Time: O(n + m) average, plus one `key` or `hash` call per element and the `equals` calls.
   *
   * @param elements - The elements to remove.
   * @returns `true` when any member was removed.
   * @throws TypeError when `elements` is not iterable, or a function returns an invalid result.
   * @throws ConcurrentModificationError when a `key`, `equals` or `hash` function adds or removes
   *   members.
   *
   * @example
   * ```ts
   * new HashSet([1, 2, 3]).removeAll([2, 9]) // true; HashSet(2) {1, 3}
   * ```
   */
  removeAll(elements: Iterable<E>): boolean {
    return this.engine.removeAll(elements)
  }
 
  /**
   * Removes every member not equal, under this set's equality, to an element of `elements`, as
   * `removeAll` decides.
   *
   * Time: O(n + m) average, plus one `key` or `hash` call per element and the `equals` calls.
   *
   * @param elements - The elements to keep.
   * @returns `true` when any member was removed.
   * @throws TypeError when `elements` is not iterable, or a function returns an invalid result.
   * @throws ConcurrentModificationError when a `key`, `equals` or `hash` function adds or removes
   *   members.
   *
   * @example
   * ```ts
   * new HashSet([1, 2, 3]).retainAll([2]) // true; HashSet(1) {2}
   * ```
   */
  retainAll(elements: Iterable<E>): boolean {
    return this.engine.retainAll(elements)
  }
 
  /**
   * Removes every member for which `predicate` returns a truthy value. Every member is decided
   * first; if `predicate` throws, or adds or removes members, nothing is removed.
   *
   * Time: O(n) plus the calls.
   *
   * @param predicate - Called with each member.
   * @returns `true` when any member was removed.
   * @throws TypeError when `predicate` is not a function.
   * @throws ConcurrentModificationError when `predicate` adds or removes members.
   *
   * @example
   * ```ts
   * new HashSet([1, 2, 3, 4]).removeIf((n) => n % 2 === 0) // true; HashSet(2) {1, 3}
   * ```
   */
  removeIf(predicate: (element: E) => unknown): boolean {
    return this.engine.removeIf(predicate)
  }
 
  /**
   * Removes every member. An open iteration ends, and members added afterwards are visited, as
   * with native `Set`.
   *
   * Time: O(1) with an `equivalence`; one native `clear` otherwise (at most O(n)).
   *
   * @example
   * ```ts
   * const set = new HashSet([1, 2])
   * set.clear()
   * set.size // 0
   * ```
   */
  clear(): void {
    this.engine.clear()
  }
 
  /**
   * Returns an iterator over the members in insertion order, as `values()` does.
   *
   * Time: O(1) to create; O(1) per step.
   *
   * @returns A new live iterator.
   *
   * @example
   * ```ts
   * [...new HashSet(['a', 'b']).keys()] // ['a', 'b']
   * ```
   */
  keys(): IterableIterator<E> {
    return this.engine.keys()
  }
 
  /**
   * Returns an iterator over the members in insertion order.
   *
   * Time: O(1) to create; O(1) per step.
   *
   * @returns A new live iterator.
   *
   * @example
   * ```ts
   * [...new HashSet(['a', 'b']).values()] // ['a', 'b']
   * ```
   */
  values(): IterableIterator<E> {
    return this.engine.values()
  }
 
  /**
   * Returns an iterator over `[member, member]` pairs in insertion order, as native `Set` does.
   *
   * Time: O(1) to create; O(1) per step.
   *
   * @returns A new live iterator.
   *
   * @example
   * ```ts
   * [...new HashSet(['a']).entries()] // [['a', 'a']]
   * ```
   */
  entries(): IterableIterator<[E, E]> {
    return this.engine.entries()
  }
 
  /**
   * Calls `action(member, member, set)` for each member in insertion order, with `this` set to
   * `thisArg`, as native `Set` does. Members deleted before they are reached are skipped; members
   * added during the loop are visited.
   *
   * Time: O(n) plus the calls.
   *
   * @param action - Called with each member twice and the set.
   * @param thisArg - The `this` value for `action`.
   * @throws TypeError when `action` is not a function.
   *
   * @example
   * ```ts
   * new HashSet(['a', 'b']).forEach((tag) => console.log(tag))
   * ```
   */
  forEach(action: (element: E, same: E, set: SetCollection<E>) => void, thisArg?: unknown): void {
    this.engine.forEach(action, thisArg)
  }
 
  /**
   * Returns an iterator over the members in insertion order. It is live, as a native `Set`
   * iterator is: deleted members not yet reached are skipped, added members are visited, and
   * `clear()` ends it.
   *
   * Time: O(1) to create; O(1) per step.
   *
   * @returns A new live iterator.
   *
   * @example
   * ```ts
   * for (const tag of new HashSet(['a', 'b'])) console.log(tag)
   * ```
   */
  [Symbol.iterator](): IterableIterator<E> {
    return this.engine[Symbol.iterator]()
  }
 
  /**
   * Returns a live cursor in insertion order; `remove()` removes the member `next()` returned,
   * without calling `key`, `equals` or `hash` while that member is still stored. A member
   * `hasNext()` has reached is returned even if it was deleted meanwhile.
   *
   * Time: O(1) to create; `next` O(1); `remove` O(1) average.
   *
   * @returns A new cursor.
   *
   * @example
   * ```ts
   * const cursor = set.cursor()
   * while (cursor.hasNext()) if (cursor.next() < 0) cursor.remove()
   * ```
   */
  cursor(): Cursor<E> {
    return this.engine.cursor()
  }
 
  /**
   * Returns a new array of the members in insertion order.
   *
   * Time: O(n).
   *
   * @returns A new array.
   *
   * @example
   * ```ts
   * new HashSet([2, 1, 2]).toArray() // [2, 1]
   * ```
   */
  toArray(): E[] {
    return this.engine.toArray()
  }
 
  /**
   * Returns a new `HashSet` with the same members, order and `key` or `equivalence`, independent
   * of this one. Stored keys and hashes are copied; no user function is called.
   *
   * Time: O(n).
   *
   * @returns The copy.
   *
   * @example
   * ```ts
   * const copy = set.copy()
   * copy.add(9) // set is unchanged
   * ```
   */
  copy(): HashSet<E> {
    const set = new HashSet<E>()
    set.engine = this.engine.copy(set)
    return set
  }
 
  /**
   * Returns whether `other` is a set of the same size in which every member of this set is
   * present, and which has every member of this set — whatever the insertion order, and the same
   * both ways round. Lists, queues, maps, arrays and native `Set` are never equal to it.
   *
   * Time: O(n) average, plus the `has` calls on both sets.
   *
   * @param other - Any value.
   * @returns `true` when `other` is an equal set.
   *
   * @example
   * ```ts
   * new HashSet([1, 2]).equals(new HashSet([2, 1])) // true
   * new HashSet([1, 2]).equals(new Set([1, 2]))     // false
   * ```
   */
  equals(other: unknown): boolean {
    return this.engine.equals(other)
  }
 
  /** The value-equality protocol; delegates to `equals`. Time: as `equals`. */
  [equalsSymbol](other: unknown): boolean {
    return this.engine.equals(other)
  }
 
  /**
   * The value-equality protocol: an order-independent combination of the members' hashes, the
   * same for equal sets that use the same `key` function, the same `equivalence`, or neither.
   * Values are unspecified. Time: O(n) plus the `hash` calls.
   */
  [hashCodeSymbol](hash: HashFunction): number {
    return this.engine[hashCodeSymbol](hash)
  }
 
  /**
   * Returns the class name, size and members in insertion order, e.g. `HashSet(2) {1, 'a'}`.
   *
   * Time: O(length of the output) for flat contents.
   *
   * @returns The text.
   *
   * @example
   * ```ts
   * String(new HashSet([1, 'a'])) // "HashSet(2) {1, 'a'}"
   * ```
   */
  toString(): string {
    return this.engine.toString()
  }
 
  /**
   * Returns a new array of the members in insertion order, so `JSON.stringify` writes the set as an
   * array (a native `Set` is written as `{}`).
   *
   * Time: O(n).
   *
   * @returns A new array.
   *
   * @example
   * ```ts
   * JSON.stringify(new HashSet([1, 2])) // '[1,2]'
   * ```
   */
  toJSON(): E[] {
    return this.engine.toJSON()
  }
 
  /** Node's inspect hook: `HashSet(2) { 1, 2 }` in `console.log`. Time: as `toString`. */
  [INSPECT](
    depth: number | null,
    options: object,
    inspect: (value: unknown, options: object) => string,
  ): string {
    return this.engine[INSPECT](depth, options, inspect)
  }
}