All files / src/collections linked-hash-set.ts

100% Statements 54/54
100% Branches 4/4
100% Functions 40/40
100% Lines 53/53

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 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784                                                    106x                                                                                                                                                                                                                           106501x 106501x 106501x                                               100029x 7x       100022x 100022x 100022x 100022x                                     434x               46x               103972x               229x               200x         266138x               207583x         21x                               2031x                                           125096x 125096x                                             5937x 5937x                                             9001x 9001x                                       339164x                                       44169x                                 5412x                                   4344x                                 8075x                                   5947x                                           10716x                                         3311x                                           3213x                                         3243x                                         3368x                                 2619x                                 875x                                 853x                                 867x                                     3222x                                       111047x                                       6084x                               22974x                                   3227x 3227x 3227x                                     103517x         312x                 5613x                               100606x                               117725x                 40x      
/**
 * `LinkedHashSet`: a set in insertion order that can be read and changed at both ends.
 *
 * @packageDocumentation
 */
 
import type { Cursor } from '../interfaces/collection'
import type { SequencedSet } from '../interfaces/sequenced-set'
import type { SetCollection } from '../interfaces/set'
import {
  brandedHasInstance,
  SEQUENCED_COLLECTION_BRAND,
  SET_COLLECTION_BRAND,
} from '../internal/brand'
import { describeValue, isIterable } from '../internal/checks'
import { readEquality, readSetArguments } from '../internal/engines/hash-support'
import { LinkedHashSetEngine } from '../internal/engines/linked-hash-set-engine'
import {
  type Equivalence,
  equalsSymbol,
  type HashFunction,
  hashCodeSymbol,
  type KeyOf,
} from '../internal/equality'
import { COLLECTION_BRAND, INSPECT } from '../internal/output'
 
const LINKED_HASH_SET_BRAND: unique symbol = /*#__PURE__*/ Symbol.for(
  'javascript-collections-framework/1/LinkedHashSet',
)
 
/**
 * Options for a `LinkedHashSet`. At most one of `key` and `equivalence` may be given; with neither,
 * members compare with SameValueZero, as in native `Set`.
 *
 * @example
 * ```ts
 * const byId: LinkedHashSetOptions<{ id: number }> = { key: (user) => user.id }
 * const byValue: LinkedHashSetOptions<Point> = { equivalence: valueEquivalence }
 * ```
 */
export interface LinkedHashSetOptions<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>
}
 
/**
 * A set in insertion order that behaves like native `Set` — SameValueZero membership (`NaN` is one
 * member, `-0` is stored as `0`), `add` returning the set and keeping a present member where it is,
 * live iteration — and can also be read and changed at both ends: `getFirst`, `getLast`,
 * `removeFirst` and `removeLast`, `addFirst` and `addLast`, which insert an absent member at that
 * end or move the present one there, and a live `reversed()` view. Each takes O(1) amortised
 * (average for those that look up or remove).
 *
 * A set without the `key` or `equivalence` option keeps its members in a native `Set` until the
 * first call that needs linked order: `addFirst`, `addLast` of a member, `getLast`, `removeLast`,
 * or any call through `reversed()`. That call costs O(n), once; this is why the ends, `addFirst`
 * and `addLast` are amortised.
 *
 * The `key` and `equivalence` options work as they do for `HashSet`; 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 inserted at the end the loop is heading to are visited, members inserted at the end it
 * started from are not, and nothing throws `ConcurrentModificationError`. A move acts as a delete
 * followed by an insertion at the new end, so `for (const x of set) set.addLast(x)` never ends,
 * exactly as `for (const x of set) { set.delete(x); set.add(x) }` never ends on a native `Set`.
 * An open iterator or cursor keeps the members removed since its last step reachable until it
 * advances or is dropped; release iterators that are no longer needed.
 *
 * Members are found by their `key` result or `hash` when they are stored. If that changes while
 * a member is in the set, lookups, `addFirst`, `addLast` and `delete` give unspecified results for
 * it; `removeFirst`, `removeLast`, a cursor's `remove`, `removeIf` and `clear` still remove it.
 *
 * Two sets are equal when they have the same size and each has every member of the other, in any
 * order. A `LinkedHashSet` is not a `HashSet` (`instanceof HashSet` is `false`); both are sets.
 * The class is not designed to be subclassed.
 *
 * @example
 * ```ts
 * const recent = new LinkedHashSet(['a', 'b', 'c'])
 * recent.addLast('a')   // LinkedHashSet(3) {'b', 'c', 'a'}
 * recent.removeFirst()  // 'b'
 * [...recent.reversed()] // ['a', 'c']
 * ```
 *
 * @sealed
 */
export class LinkedHashSet<E> implements SequencedSet<E> {
  private engine: LinkedHashSetEngine<E>
 
  /**
   * Creates a set holding each distinct element of `elements` once, in first-seen order; empty
   * when `elements` is omitted, `undefined` or `null`. Options are never taken from a source set.
   *
   * 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 LinkedHashSet([3, 1, 3]) // LinkedHashSet(2) {3, 1}
   * new LinkedHashSet(points, { equivalence: valueEquivalence })
   * ```
   */
  constructor(elements?: Iterable<E> | null, options?: LinkedHashSetOptions<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 LinkedHashSet<{ name: string }>({ key: (item) => item.name })
   * ```
   */
  constructor(options: LinkedHashSetOptions<E>)
  constructor(
    elementsOrOptions?: Iterable<E> | LinkedHashSetOptions<E> | null,
    options?: LinkedHashSetOptions<E> | null,
  ) {
    const { elements, equality } = readSetArguments<E>(elementsOrOptions, options, 'LinkedHashSet')
    this.engine = new LinkedHashSetEngine<E>(this, equality)
    if (elements !== undefined) this.engine.addInitial(elements, 'LinkedHashSet')
  }
 
  /**
   * 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
   * LinkedHashSet.from('banana') // LinkedHashSet(3) {'b', 'a', 'n'}
   * ```
   */
  static from<T>(
    elements: Iterable<T>,
    options?: LinkedHashSetOptions<T> | null,
  ): LinkedHashSet<T> {
    if (!isIterable(elements)) {
      throw new TypeError(
        `LinkedHashSet.from: elements is not iterable (${describeValue(elements)})`,
      )
    }
    const set = new LinkedHashSet<T>()
    set.engine = new LinkedHashSetEngine<T>(set, readEquality<T>(options, 'LinkedHashSet.from'))
    set.engine.addInitial(elements, 'LinkedHashSet.from')
    return set
  }
 
  /**
   * Returns whether `value` is a `LinkedHashSet`, including one created by another copy of the
   * library with the same major version. A reversed view is not one.
   *
   * Time: O(1).
   *
   * @param value - The value on the left of `instanceof`.
   * @returns `true` when `value` is a `LinkedHashSet`.
   *
   * @example
   * ```ts
   * new LinkedHashSet() instanceof LinkedHashSet // true
   * ```
   */
  static [Symbol.hasInstance](value: unknown): boolean {
    // biome-ignore lint/complexity/noThisInStatic: `this` is the class on the right of `instanceof`
    return brandedHasInstance(this, LinkedHashSet, LINKED_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 [LINKED_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 sequenced collections so `isSequencedCollection` recognises them across copies of the
   * package. Its key is not exported, so the getter is not called directly. Time: O(1).
   */
  get [SEQUENCED_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 insertion, removal or move 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
  }
 
  /** `'LinkedHashSet'`, for `Object.prototype.toString`. Time: O(1). */
  get [Symbol.toStringTag](): string {
    return 'LinkedHashSet'
  }
 
  /**
   * Returns whether the set has no members.
   *
   * Time: O(1).
   *
   * @returns `size === 0`.
   *
   * @example
   * ```ts
   * new LinkedHashSet().isEmpty() // true
   * ```
   */
  isEmpty(): boolean {
    return this.engine.isEmpty()
  }
 
  /**
   * Adds `element` at the end 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 inserts, removes
   *   or moves members; the set keeps those changes and adds nothing itself.
   *
   * @example
   * ```ts
   * new LinkedHashSet([1, 2]).add(1).add(3) // LinkedHashSet(3) {1, 2, 3}
   * ```
   */
  add(element: E): this {
    this.engine.add(element)
    return this
  }
 
  /**
   * Inserts `element` at the front, or moves the member equal to it to the front (the stored
   * member is kept, not replaced by `element`), and returns the set. Nothing changes when that
   * member is already first.
   *
   * Time: O(1) amortised average, plus one `key` or `hash` call and the `equals` calls (the first
   * call that needs linked order costs O(n), once).
   *
   * @param element - The element to insert or move (`-0` is stored as `0`).
   * @returns The set.
   * @throws TypeError when `key`, `equals` or `hash` returns an invalid result (as for `add`).
   * @throws ConcurrentModificationError when a `key`, `equals` or `hash` function inserts, removes
   *   or moves members; the set inserts and moves nothing itself.
   *
   * @example
   * ```ts
   * new LinkedHashSet([1, 2, 3]).addFirst(3) // LinkedHashSet(3) {3, 1, 2}
   * ```
   */
  addFirst(element: E): this {
    this.engine.addFirst(element)
    return this
  }
 
  /**
   * Inserts `element` at the end, or moves the member equal to it to the end (the stored member is
   * kept, not replaced by `element`), and returns the set. Nothing changes when that member is
   * already last.
   *
   * Time: O(1) amortised average, plus one `key` or `hash` call and the `equals` calls (the first
   * call that needs linked order costs O(n), once).
   *
   * @param element - The element to insert or move (`-0` is stored as `0`).
   * @returns The set.
   * @throws TypeError when `key`, `equals` or `hash` returns an invalid result (as for `add`).
   * @throws ConcurrentModificationError when a `key`, `equals` or `hash` function inserts, removes
   *   or moves members; the set inserts and moves nothing itself.
   *
   * @example
   * ```ts
   * new LinkedHashSet([1, 2, 3]).addLast(1) // LinkedHashSet(3) {2, 3, 1}
   * ```
   */
  addLast(element: E): this {
    this.engine.addLast(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 inserts, removes
   *   or moves members.
   *
   * @example
   * ```ts
   * new LinkedHashSet([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 inserts, removes
   *   or moves members; the set removes nothing itself.
   *
   * @example
   * ```ts
   * new LinkedHashSet([1, 2]).delete(1) // true
   * ```
   */
  delete(element: E): boolean {
    return this.engine.delete(element)
  }
 
  /**
   * Returns the first member.
   *
   * Time: O(1) amortised. Calls no `key`, `equals` or `hash` function.
   *
   * @returns The first member.
   * @throws NoSuchElementError when the set is empty.
   *
   * @example
   * ```ts
   * new LinkedHashSet(['a', 'b']).getFirst() // 'a'
   * ```
   */
  getFirst(): E {
    return this.engine.getFirst()
  }
 
  /**
   * Returns the last member.
   *
   * Time: O(1) amortised (the first call that needs linked order costs O(n), once). Calls no
   * `key`, `equals` or `hash` function.
   *
   * @returns The last member.
   * @throws NoSuchElementError when the set is empty.
   *
   * @example
   * ```ts
   * new LinkedHashSet(['a', 'b']).getLast() // 'b'
   * ```
   */
  getLast(): E {
    return this.engine.getLast()
  }
 
  /**
   * Removes and returns the first member.
   *
   * Time: O(1) amortised average. Calls no `key`, `equals` or `hash` function.
   *
   * @returns The removed member.
   * @throws NoSuchElementError when the set is empty; nothing changes.
   *
   * @example
   * ```ts
   * new LinkedHashSet(['a', 'b']).removeFirst() // 'a'
   * ```
   */
  removeFirst(): E {
    return this.engine.removeFirst()
  }
 
  /**
   * Removes and returns the last member.
   *
   * Time: O(1) amortised average (the first call that needs linked order costs O(n), once). Calls
   * no `key`, `equals` or `hash` function.
   *
   * @returns The removed member.
   * @throws NoSuchElementError when the set is empty; nothing changes.
   *
   * @example
   * ```ts
   * new LinkedHashSet(['a', 'b']).removeLast() // 'b'
   * ```
   */
  removeLast(): E {
    return this.engine.removeLast()
  }
 
  /**
   * Returns a live view of this set in the opposite order. Changes through either show in both;
   * the view's `getFirst`, `removeFirst` and `addFirst` act on this set's last member or end, and
   * its `add` inserts an absent member at the view's end, which is this set's front. Reversing the
   * view returns this set.
   *
   * Time: O(1). Every call through the view needs linked order: the first such call on the set
   * costs O(n), once, and the view's operations cost what the set's cost, amortised.
   *
   * @returns The reversed view, printed as `ReversedLinkedHashSet`.
   *
   * @example
   * ```ts
   * const set = new LinkedHashSet([1, 2, 3])
   * const newestFirst = set.reversed() // ReversedLinkedHashSet(3) {3, 2, 1}
   * newestFirst.add(0)                 // set is now {0, 1, 2, 3}
   * ```
   */
  reversed(): SequencedSet<E> {
    return this.engine.reversed()
  }
 
  /**
   * Adds every element of `elements` at the end, in order, after reading them all; members already
   * present stay where they are. 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 inserts, removes
   *   or moves members.
   *
   * @example
   * ```ts
   * new LinkedHashSet([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 inserts, removes
   *   or moves members.
   *
   * @example
   * ```ts
   * new LinkedHashSet([1, 2, 3]).removeAll([2, 9]) // true; LinkedHashSet(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 inserts, removes
   *   or moves members.
   *
   * @example
   * ```ts
   * new LinkedHashSet([1, 2, 3]).retainAll([2]) // true; LinkedHashSet(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, in order; if `predicate` throws, or inserts, removes or moves 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` inserts, removes or moves members.
   *
   * @example
   * ```ts
   * new LinkedHashSet([1, 2, 3, 4]).removeIf((n) => n % 2 === 0) // true; LinkedHashSet(2) {1, 3}
   * ```
   */
  removeIf(predicate: (element: E) => unknown): boolean {
    return this.engine.removeIf(predicate)
  }
 
  /**
   * Removes every member. An open iteration starts over before the first member, so members added
   * at the end 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 LinkedHashSet([1, 2])
   * set.clear()
   * set.size // 0
   * ```
   */
  clear(): void {
    this.engine.clear()
  }
 
  /**
   * Returns an iterator over the members in order, as `values()` does.
   *
   * Time: O(1) to create; O(1) per step, amortised over the steps and the changes made while it
   * is open.
   *
   * @returns A new live iterator.
   *
   * @example
   * ```ts
   * [...new LinkedHashSet(['a', 'b']).keys()] // ['a', 'b']
   * ```
   */
  keys(): IterableIterator<E> {
    return this.engine.keys()
  }
 
  /**
   * Returns an iterator over the members in order.
   *
   * Time: O(1) to create; O(1) per step, amortised over the steps and the changes made while it
   * is open.
   *
   * @returns A new live iterator.
   *
   * @example
   * ```ts
   * [...new LinkedHashSet(['a', 'b']).values()] // ['a', 'b']
   * ```
   */
  values(): IterableIterator<E> {
    return this.engine.values()
  }
 
  /**
   * Returns an iterator over `[member, member]` pairs in order, as native `Set` does.
   *
   * Time: O(1) to create; O(1) per step, amortised over the steps and the changes made while it
   * is open.
   *
   * @returns A new live iterator.
   *
   * @example
   * ```ts
   * [...new LinkedHashSet(['a']).entries()] // [['a', 'a']]
   * ```
   */
  entries(): IterableIterator<[E, E]> {
    return this.engine.entries()
  }
 
  /**
   * Calls `action(member, member, set)` for each member in order, with `this` set to `thisArg`, as
   * native `Set` does. The loop is live, as `[Symbol.iterator]` describes.
   *
   * 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 LinkedHashSet(['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 order. It is live, as a native `Set` iterator is:
   * members deleted before they are reached are skipped, members inserted at the end are visited,
   * members inserted at the front are not, and a moved member counts as deleted and inserted at its
   * new end. After `clear()` it continues with members added later; once done it stays done.
   *
   * Time: O(1) to create; O(1) per step, amortised over the steps and the changes made while it
   * is open.
   *
   * @returns A new live iterator.
   *
   * @example
   * ```ts
   * for (const tag of new LinkedHashSet(['a', 'b'])) console.log(tag)
   * ```
   */
  [Symbol.iterator](): IterableIterator<E> {
    return this.engine[Symbol.iterator]()
  }
 
  /**
   * Returns a live cursor in order; `remove()` removes the member `next()` returned, from wherever
   * it now stands, without calling `key`, `equals` or `hash` while that member is still stored. A
   * member `hasNext()` has reached is returned even if it was deleted or moved meanwhile.
   *
   * Time: O(1) to create; `next` O(1) amortised over the steps and the changes made while it is
   * open; `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 order.
   *
   * Time: O(n).
   *
   * @returns A new array.
   *
   * @example
   * ```ts
   * new LinkedHashSet([2, 1, 2]).toArray() // [2, 1]
   * ```
   */
  toArray(): E[] {
    return this.engine.toArray()
  }
 
  /**
   * Returns a new `LinkedHashSet` 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.addFirst(9) // set is unchanged
   * ```
   */
  copy(): LinkedHashSet<E> {
    const set = new LinkedHashSet<E>()
    set.engine = this.engine.copy(set)
    return set
  }
 
  /**
   * Returns whether `other` is a set of the same size which has every member of this set and whose
   * every member this set has — whatever the 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 LinkedHashSet([1, 2]).equals(new HashSet([2, 1])) // true
   * ```
   */
  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 as an equal `HashSet`'s with 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 order, e.g. `LinkedHashSet(2) {1, 'a'}`.
   *
   * Time: O(length of the output) for flat contents.
   *
   * @returns The text.
   *
   * @example
   * ```ts
   * String(new LinkedHashSet([1, 'a'])) // "LinkedHashSet(2) {1, 'a'}"
   * ```
   */
  toString(): string {
    return this.engine.toString()
  }
 
  /**
   * Returns a new array of the members in order, so `JSON.stringify` writes the set as an array.
   *
   * Time: O(n).
   *
   * @returns A new array.
   *
   * @example
   * ```ts
   * JSON.stringify(new LinkedHashSet([2, 1])) // '[2,1]'
   * ```
   */
  toJSON(): E[] {
    return this.engine.toJSON()
  }
 
  /** Node's inspect hook: `LinkedHashSet(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)
  }
}