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 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 | 106x 4x 7500x 7500x 6635x 6633x 246126x 6633x 14x 13x 11x 11x 11x 52x 52x 10x 10x 10x 459x 50x 46x 47x 101x 161x 14603x 197192x 3x 2266x 204351x 204351x 165361x 165361x 2189x 2189x 104532x 104532x 2298x 11059x 104758x 2233x 2218x 2709x 5705x 1112x 1115x 5188x 3310x 2234x 2303x 2601x 944x 7090x 2251x 7179x 2922x 1537x 4507x 1534x 4431x 5924x 6063x 3056x 25379x 5909x 5962x 41x 87x 2493x 11061x 19x | /**
* `LinkedList`: a double-ended queue stored as a doubly linked list, with node handles.
*
* @packageDocumentation
*/
import type { Cursor } from '../interfaces/collection'
import type { LinkedListNode } from '../interfaces/linked-list-node'
import type { Deque } from '../interfaces/queue'
import {
brandedHasInstance,
DEQUE_BRAND,
QUEUE_BRAND,
SEQUENCED_COLLECTION_BRAND,
} from '../internal/brand'
import { describeValue, isIterable, requireFunction } from '../internal/checks'
import { LinkedListEngine } from '../internal/engines/linked-list-engine'
import { equalsSymbol, type HashFunction, hashCodeSymbol } from '../internal/equality'
import { apply } from '../internal/intrinsics'
import { COLLECTION_BRAND, INSPECT } from '../internal/output'
const LINKED_LIST_BRAND: unique symbol = /*#__PURE__*/ Symbol.for(
'javascript-collections-framework/1/LinkedList',
)
function notIterable(where: string, value: unknown): TypeError {
return new TypeError(`${where}: elements is not iterable (${describeValue(value)})`)
}
/**
* A double-ended queue stored as a doubly linked list, whose node handles allow O(1) insertion and
* removal next to a known element: moving an entry to the front of a cache, inserting next to a
* cursor, unlinking a cancelled task. It works wherever a `Deque` is expected: `add` + `poll` as a
* queue, `push` + `pop` at the end as a stack.
*
* `addFirstNode`, `addLastNode`, `insertBefore` and `insertAfter` return a handle
* (`LinkedListNode`); `removeNode`, `insertBefore` and `insertAfter` take one. Each element has one
* handle for as long as it is in the list. There is no index access; use `ArrayList` for that.
*
* `null` and `undefined` are stored, as in an array, so `poll()`, `peek()` and `pop()` can return a
* stored `undefined`: read `isEmpty()` (or use `removeFirst`, `getFirst` and the other throwing
* forms) to tell "empty" apart. Two lists are equal only when they are the same object; compare
* `toArray()` results to compare contents. Adding or removing while iterating throws
* `ConcurrentModificationError`; walking by handles never does.
*
* The class is not designed to be subclassed.
*
* @example
* ```ts
* const list = new LinkedList<string>()
* const b = list.addLastNode('b')
* list.insertBefore(b, 'a')
* list.removeNode(b) // 'b'
* list.toArray() // ['a']
* ```
*
* @sealed
*/
export class LinkedList<E> implements Deque<E> {
private readonly engine: LinkedListEngine<E>
/**
* Creates a list holding the elements of `elements`, in order (the first is the front); empty
* when `elements` is omitted, `undefined` or `null` (as `new Set(null)` is). `null` and
* `undefined` elements are stored.
*
* Time: O(m) for `m` elements.
*
* @param elements - The initial elements.
* @throws TypeError when `elements` is neither iterable nor `null`/`undefined`.
*
* @example
* ```ts
* new LinkedList(['a', 'b']) // LinkedList(2) ['a', 'b']
* ```
*/
constructor(elements?: Iterable<E> | null) {
this.engine = new LinkedListEngine<E>(this)
if (elements === undefined || elements === null) return
if (!isIterable(elements)) throw notIterable('LinkedList', elements)
const items: E[] = []
for (const element of elements) items.push(element)
this.engine.appendAll(items, 'LinkedList')
}
/**
* Returns a new list of the elements of `elements`, in order, like `Array.from` but for
* iterables only.
*
* Time: O(m) for `m` elements.
*
* @param elements - The elements.
* @returns A new list.
* @throws TypeError when `elements` is not iterable (`null`, `undefined` and array-likes
* included).
*
* @example
* ```ts
* LinkedList.from(new Set([1, 2])) // LinkedList(2) [1, 2]
* ```
*/
static from<T>(elements: Iterable<T>): LinkedList<T>
/**
* Returns a new list of `mapFn(element, index)` for each element of `elements`, in order, with
* `this` set to `thisArg`, as `Array.from` does. `elements` is read while `mapFn` runs, so
* items `mapFn` appends to an array source are included.
*
* Time: O(m) plus the calls.
*
* @param elements - The elements.
* @param mapFn - Called with each element and its index; its result is stored.
* @param thisArg - The `this` value for `mapFn`.
* @returns A new list.
* @throws TypeError when `mapFn` is not a function (checked first) or `elements` is not
* iterable.
* @throws Error whatever `mapFn` throws; no collection is returned.
*
* @example
* ```ts
* LinkedList.from([1, 2], (n) => n * 10) // LinkedList(2) [10, 20]
* ```
*/
static from<T, U>(
elements: Iterable<T>,
mapFn: (element: T, index: number) => U,
thisArg?: unknown,
): LinkedList<U>
static from<T, U>(
elements: Iterable<T>,
mapFn?: (element: T, index: number) => U,
thisArg?: unknown,
): LinkedList<T | U> {
if (mapFn !== undefined) requireFunction(mapFn, 'LinkedList.from', 'mapFn')
if (!isIterable(elements)) throw notIterable('LinkedList.from', elements)
const items: Array<T | U> = []
let index = 0
for (const element of elements) {
items.push(mapFn === undefined ? element : (apply(mapFn, thisArg, [element, index]) as U))
index++
}
const list = new LinkedList<T | U>()
list.engine.appendAll(items, 'LinkedList.from')
return list
}
/**
* Returns whether `value` is a `LinkedList`, including one created by another copy of the
* library with the same major version. A reversed view is not a `LinkedList`.
*
* Time: O(1).
*
* @param value - The value on the left of `instanceof`.
* @returns `true` when `value` is a `LinkedList`.
*
* @example
* ```ts
* new LinkedList() instanceof LinkedList // true
* ```
*/
static [Symbol.hasInstance](value: unknown): boolean {
// biome-ignore lint/complexity/noThisInStatic: `this` is the class on the right of `instanceof`
return brandedHasInstance(this, LinkedList, LINKED_LIST_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_LIST_BRAND](): true {
return true
}
/**
* Marks deques so `isDeque` recognises them across copies of the package. Its key is not
* exported, so the getter is not called directly. Time: O(1).
*/
get [DEQUE_BRAND](): true {
return true
}
/**
* Marks queues so `isQueue` recognises them across copies of the package. Its key is not
* exported, so the getter is not called directly. Time: O(1).
*/
get [QUEUE_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 elements. Time: O(1). */
get size(): number {
return this.engine.size
}
/**
* A number that changes on every change to the contents and never on a read. Its exact values
* are unspecified. Time: O(1).
*/
get revision(): number {
return this.engine.revision
}
/** `'LinkedList'`, for `Object.prototype.toString`. Time: O(1). */
get [Symbol.toStringTag](): string {
return 'LinkedList'
}
/**
* Returns whether the list has no elements.
*
* Time: O(1).
*
* @returns `size === 0`.
*
* @example
* ```ts
* new LinkedList().isEmpty() // true
* ```
*/
isEmpty(): boolean {
return this.engine.isEmpty()
}
/**
* Adds `element` at the end and returns the list, so calls chain.
*
* Time: O(1).
*
* @param element - The element to add.
* @returns The list.
*
* @example
* ```ts
* new LinkedList<number>().add(1).add(2) // LinkedList(2) [1, 2]
* ```
*/
add(element: E): this {
this.engine.add(element)
return this
}
/**
* Adds `element` at the end, as `add` does, and returns the list. Unlike
* `Array.prototype.push`, it returns the list, not the new length.
*
* Time: O(1).
*
* @param element - The element to add.
* @returns The list.
*
* @example
* ```ts
* new LinkedList([1]).push(2) // LinkedList(2) [1, 2]
* ```
*/
push(element: E): this {
this.engine.push(element)
return this
}
/**
* Adds `element` at the end and returns the list; the same as `add`.
*
* Time: O(1).
*
* @param element - The element to add.
* @returns The list.
*
* @example
* ```ts
* new LinkedList([1]).addLast(2) // LinkedList(2) [1, 2]
* ```
*/
addLast(element: E): this {
this.engine.addLast(element)
return this
}
/**
* Adds `element` at the front and returns the list.
*
* Time: O(1).
*
* @param element - The element to add.
* @returns The list.
*
* @example
* ```ts
* new LinkedList([2]).addFirst(1) // LinkedList(2) [1, 2]
* ```
*/
addFirst(element: E): this {
this.engine.addFirst(element)
return this
}
/**
* Adds every element of `elements` at the end, in order, after reading them all (so a list can
* add itself).
*
* Time: O(m) for `m` elements.
*
* @param elements - The elements to add.
* @returns `true` when any element was added.
* @throws TypeError when `elements` is not iterable.
*
* @example
* ```ts
* new LinkedList([1]).addAll([2, 3]) // true; [1, 2, 3]
* ```
*/
addAll(elements: Iterable<E>): boolean {
return this.engine.addAll(elements)
}
/**
* Removes and returns the first element, or returns `undefined` when the list is empty. A stored
* `undefined` is returned as well: check `isEmpty()` first to tell them apart.
*
* Time: O(1).
*
* @returns The removed element, or `undefined`.
*
* @example
* ```ts
* new LinkedList(['a', 'b']).poll() // 'a'
* ```
*/
poll(): E | undefined {
return this.engine.poll()
}
/**
* Removes and returns the last element (a stored `undefined` included), or returns `undefined`
* when the list is empty, as `Array.prototype.pop` does.
*
* Time: O(1).
*
* @returns The removed element, or `undefined`.
*
* @example
* ```ts
* new LinkedList([1, 2]).pop() // 2
* ```
*/
pop(): E | undefined {
return this.engine.pop()
}
/**
* Removes and returns the first element.
*
* Time: O(1).
*
* @returns The removed element.
* @throws NoSuchElementError when the list is empty.
*
* @example
* ```ts
* new LinkedList([1, 2]).removeFirst() // 1
* ```
*/
removeFirst(): E {
return this.engine.removeFirst()
}
/**
* Removes and returns the last element.
*
* Time: O(1).
*
* @returns The removed element.
* @throws NoSuchElementError when the list is empty.
*
* @example
* ```ts
* new LinkedList([1, 2]).removeLast() // 2
* ```
*/
removeLast(): E {
return this.engine.removeLast()
}
/**
* Returns the first element without removing it, or `undefined` when the list is empty (or the
* first element is `undefined`; `isEmpty()` tells them apart).
*
* Time: O(1).
*
* @returns The first element, or `undefined`.
*
* @example
* ```ts
* new LinkedList([1, 2]).peek() // 1
* ```
*/
peek(): E | undefined {
return this.engine.peek()
}
/**
* Returns the last element without removing it, or `undefined` when the list is empty (or the
* last element is `undefined`; `isEmpty()` tells them apart).
*
* Time: O(1).
*
* @returns The last element, or `undefined`.
*
* @example
* ```ts
* new LinkedList([1, 2]).peekLast() // 2
* ```
*/
peekLast(): E | undefined {
return this.engine.peekLast()
}
/**
* Returns the first element.
*
* Time: O(1).
*
* @returns The first element.
* @throws NoSuchElementError when the list is empty.
*
* @example
* ```ts
* new LinkedList([1, 2]).getFirst() // 1
* ```
*/
getFirst(): E {
return this.engine.getFirst()
}
/**
* Returns the last element.
*
* Time: O(1).
*
* @returns The last element.
* @throws NoSuchElementError when the list is empty.
*
* @example
* ```ts
* new LinkedList([1, 2]).getLast() // 2
* ```
*/
getLast(): E {
return this.engine.getLast()
}
/**
* Returns whether some element is equal to `element` under SameValueZero (`null` and
* `undefined` included).
*
* Time: O(n).
*
* @param element - The value to look for.
* @returns `true` when present.
*
* @example
* ```ts
* new LinkedList([1, 2]).has(2) // true
* ```
*/
has(element: E): boolean {
return this.engine.has(element)
}
/**
* Removes the first element equal to `element` under SameValueZero, searching from the front.
* `reversed().delete(element)` removes the last one instead.
*
* Time: O(n).
*
* @param element - The value to remove.
* @returns `true` when an element was removed.
*
* @example
* ```ts
* new LinkedList([1, 2, 1]).delete(1) // true; [2, 1]
* ```
*/
delete(element: E): boolean {
return this.engine.delete(element)
}
/**
* Removes every element equal to one in `elements` (SameValueZero). Decides first, so an error
* while deciding removes nothing.
*
* Time: O(n + m) for `m` elements.
*
* @param elements - The elements to remove.
* @returns `true` when any element was removed.
* @throws TypeError when `elements` is not iterable.
*
* @example
* ```ts
* new LinkedList([1, 2, 1]).removeAll([1]) // true; [2]
* ```
*/
removeAll(elements: Iterable<E>): boolean {
return this.engine.removeAll(elements)
}
/**
* Removes every element not equal to one in `elements` (SameValueZero). Decides first, so an
* error while deciding removes nothing.
*
* Time: O(n + m) for `m` elements.
*
* @param elements - The elements to keep.
* @returns `true` when any element was removed.
* @throws TypeError when `elements` is not iterable.
*
* @example
* ```ts
* new LinkedList([1, 2, 3]).retainAll([2]) // true; [2]
* ```
*/
retainAll(elements: Iterable<E>): boolean {
return this.engine.retainAll(elements)
}
/**
* Calls `predicate(element)` on every element, then removes those for which it returned a truthy
* value; an error from `predicate` removes nothing.
*
* Time: O(n) plus the calls.
*
* @param predicate - Called with each element.
* @returns `true` when any element was removed.
* @throws TypeError when `predicate` is not a function.
* @throws ConcurrentModificationError when `predicate` adds or removes elements.
*
* @example
* ```ts
* new LinkedList([1, -2, 3]).removeIf((n) => n < 0) // true; [1, 3]
* ```
*/
removeIf(predicate: (element: E) => unknown): boolean {
return this.engine.removeIf(predicate)
}
/**
* Removes every element.
*
* Time: O(n).
*
* @example
* ```ts
* list.clear() // list.size === 0
* ```
*/
clear(): void {
this.engine.clear()
}
/**
* Returns a new array of the elements, from first to last.
*
* Time: O(n).
*
* @returns A new array.
*
* @example
* ```ts
* new LinkedList([1, 2]).toArray() // [1, 2]
* ```
*/
toArray(): E[] {
return this.engine.toArray()
}
/**
* Calls `action(element)` for each element from first to last, with `this` set to `thisArg`.
*
* Time: O(n) plus the calls.
*
* @param action - Called with each element.
* @param thisArg - The `this` value for `action`.
* @throws TypeError when `action` is not a function.
* @throws ConcurrentModificationError when `action` adds or removes elements.
*
* @example
* ```ts
* new LinkedList(['a', 'b']).forEach((s) => console.log(s))
* ```
*/
forEach(action: (element: E) => void, thisArg?: unknown): void {
this.engine.forEach(action, thisArg)
}
/**
* Returns an iterator over the elements from first to last.
*
* Time: O(1) to create; O(1) per step.
*
* @returns A new iterator.
* @throws ConcurrentModificationError (from `next()`) when elements are added or removed after
* the iterator is created, including through a cursor.
*
* @example
* ```ts
* for (const element of new LinkedList([1, 2])) console.log(element)
* ```
*/
[Symbol.iterator](): IterableIterator<E> {
return this.engine[Symbol.iterator]()
}
/**
* Returns a cursor positioned before the first element; `remove()` removes the element `next()`
* returned. `reversed().cursor()` walks from the last element to the first.
*
* Time: O(1) to create; `next` and `remove` O(1).
*
* @returns A new cursor.
*
* @example
* ```ts
* const cursor = list.cursor()
* while (cursor.hasNext()) if (cursor.next() < 0) cursor.remove()
* ```
*/
cursor(): Cursor<E> {
return this.engine.cursor()
}
/**
* Adds `element` at the front, as `addFirst` does, and returns its handle.
*
* Time: O(1).
*
* @param element - The element to add.
* @returns The new element's handle.
*
* @example
* ```ts
* const node = list.addFirstNode('a')
* list.firstNode() === node // true
* ```
*/
addFirstNode(element: E): LinkedListNode<E> {
return this.engine.addFirstNode(element)
}
/**
* Adds `element` at the end, as `addLast` does, and returns its handle.
*
* Time: O(1).
*
* @param element - The element to add.
* @returns The new element's handle.
*
* @example
* ```ts
* const node = list.addLastNode('z')
* list.lastNode() === node // true
* ```
*/
addLastNode(element: E): LinkedListNode<E> {
return this.engine.addLastNode(element)
}
/**
* Inserts `element` immediately before the element of `node` and returns the new handle.
*
* Time: O(1).
*
* @param node - A handle of an element of this list.
* @param element - The element to insert.
* @returns The new element's handle.
* @throws IllegalArgumentError when `node` is not a handle of an element now in this list
* (removed, from another list, or not a handle); nothing changes.
*
* @example
* ```ts
* const c = list.addLastNode('c')
* list.insertBefore(c, 'b') // [..., 'b', 'c']
* ```
*/
insertBefore(node: LinkedListNode<E>, element: E): LinkedListNode<E> {
return this.engine.insertBefore(node, element)
}
/**
* Inserts `element` immediately after the element of `node` and returns the new handle.
*
* Time: O(1).
*
* @param node - A handle of an element of this list.
* @param element - The element to insert.
* @returns The new element's handle.
* @throws IllegalArgumentError when `node` is not a handle of an element now in this list
* (removed, from another list, or not a handle); nothing changes.
*
* @example
* ```ts
* const a = list.addFirstNode('a')
* list.insertAfter(a, 'b') // ['a', 'b', ...]
* ```
*/
insertAfter(node: LinkedListNode<E>, element: E): LinkedListNode<E> {
return this.engine.insertAfter(node, element)
}
/**
* Removes the element of `node` and returns it. Afterwards the handle's `next` and `previous`
* are `undefined` and its `value` can still be read.
*
* Time: O(1).
*
* @param node - A handle of an element of this list.
* @returns The removed element.
* @throws IllegalArgumentError when `node` is not a handle of an element now in this list
* (removed, from another list, or not a handle); nothing changes.
*
* @example
* ```ts
* // Move an entry to the front.
* list.addFirstNode(list.removeNode(node))
* ```
*/
removeNode(node: LinkedListNode<E>): E {
return this.engine.removeNode(node)
}
/**
* Returns the handle of the first element, or `undefined` when the list is empty.
*
* Time: O(1).
*
* @returns The first handle, or `undefined`.
*
* @example
* ```ts
* // Remove while walking: read `next` before removing.
* for (let node = list.firstNode(); node !== undefined; ) {
* const next = node.next
* if (node.value < 0) list.removeNode(node)
* node = next
* }
* ```
*/
firstNode(): LinkedListNode<E> | undefined {
return this.engine.firstNode()
}
/**
* Returns the handle of the last element, or `undefined` when the list is empty.
*
* Time: O(1).
*
* @returns The last handle, or `undefined`.
*
* @example
* ```ts
* for (let node = list.lastNode(); node !== undefined; node = node.previous) console.log(node.value)
* ```
*/
lastNode(): LinkedListNode<E> | undefined {
return this.engine.lastNode()
}
/**
* Returns a live view of this list in reverse order: its first element is this list's last,
* and changes through either show in both (the view's `push` adds at this list's front, its
* `poll` is this list's `pop`). `reversed().reversed()` returns this list.
*
* Time: O(1); each operation on the view costs what its mirror costs.
*
* @returns The reversed view.
*
* @example
* ```ts
* const list = new LinkedList([1, 2, 3])
* list.reversed().peek() // 3
* list.reversed().delete(1) // removes the last 1
* ```
*/
reversed(): Deque<E> {
return this.engine.reversed()
}
/**
* Returns a new `LinkedList` with the same elements in the same order, independent of this one
* (the elements themselves are not copied).
*
* Time: O(n).
*
* @returns The copy.
*
* @example
* ```ts
* const copy = list.copy()
* copy.poll() // list is unchanged
* ```
*/
copy(): LinkedList<E> {
return new LinkedList(this.engine.toArray())
}
/**
* Returns whether `other` is this list. Lists compare by identity; compare `toArray()` to
* compare contents. Called through a `Proxy` of the list, it is `true` for that proxy too; the list itself
* does not know its proxies, so `list.equals(proxy)` is `false`.
*
* Time: O(1).
*
* @param other - Any value.
* @returns `true` when `other` is this list.
*
* @example
* ```ts
* new LinkedList([1]).equals(new LinkedList([1])) // false
* ```
*/
equals(other: unknown): boolean {
return other === this || this.engine.equals(other)
}
/** The value-equality protocol; delegates to `equals` (identity). Time: O(1). */
[equalsSymbol](other: unknown): boolean {
return other === this || this.engine.equals(other)
}
/** The value-equality protocol: an identity hash, unspecified in value. Time: O(1). */
[hashCodeSymbol](hash: HashFunction): number {
return this.engine[hashCodeSymbol](hash)
}
/**
* Returns the class name, size and elements, e.g. `LinkedList(2) [1, 'a']`.
*
* Time: O(length of the output) for flat contents.
*
* @returns The text.
*
* @example
* ```ts
* String(new LinkedList([1, 'a'])) // "LinkedList(2) [1, 'a']"
* ```
*/
toString(): string {
return this.engine.toString()
}
/**
* Returns a new array of the elements, so `JSON.stringify` writes the list as an array.
*
* Time: O(n).
*
* @returns A new array.
*
* @example
* ```ts
* JSON.stringify(new LinkedList([1, 2])) // '[1,2]'
* ```
*/
toJSON(): E[] {
return this.engine.toJSON()
}
/** Node's inspect hook: `LinkedList(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)
}
}
|