A list is a positional collection which can contain multiple elements of the same kind.

A list is characterized by low-cost insertion of elements, while referencing an element at a given position is generally slower.

const myList = new DoubleLinkedList();

myList.append(1);
myList.append(2);
myList.append(3);

for (const num of myList.toRange().reverse()) {
console.log(num)
}

Result:

3
2
1
interface List<T> {
    size: number;
    "[iterator]"(): IterableIterator<T, any, any>;
    add(element: T, hint?: any): AddResult<T>;
    append(el: T): Cursor<T>;
    at(position: number): Cursor<T>;
    clear(): void;
    clone(): Collection<T>;
    delete(element: T): boolean;
    deleteAll(element: T): number;
    deleteAt(position: Cursor<T>): void;
    first(): T;
    getAddHint(element: T): any;
    getAt(position: number): T;
    has(element: T): boolean;
    insertAfter(position: Cursor<T>, el: T): Cursor<T>;
    insertBefore(position: Cursor<T>, el: T): Cursor<T>;
    iterator(): Iterator<T, any, any>;
    last(): T;
    prepend(el: T): Cursor<T>;
    rest(): List<T>;
    toRange(): Range<T>;
}

Type Parameters

  • T

    The type of element in the list.

Hierarchy (View Summary)

Implemented by

Properties

size: number

Count the amount of elements in the collection.

⚠️ In most cases, this should be an O(1) operation. However, there are cases where this can be an O(n) operation. Therefore, it is recommended to always cache the result in a local variable.

Methods

  • Returns an object which is able to sift through the values in this collection.

    The order by which the elements are traversed depends on the kind of collection. For unordered collections, the iteration order is unspecified and may even differ between two iterations on the same collection.

    Returns IterableIterator<T, any, any>

  • Add an element to the collection. If the element already exists, update its value.

    The location where the element is placed depends on the collection type, and in the generic case there is no guarantee on the location where it is inserted.

    This method returns a pair with the first element indicating whether the element was added, while the second element refers to the actual location of the element.

    Parameters

    • element: T
    • Optionalhint: any

      A transparent object obtained by getAddHint.

    Returns AddResult<T>

  • Remove all elements from this collection, effectively setting the collection to the empty collection.

    Returns void

  • Remove an element from the collection. If multiple elements are matched, the collection picks one of them.

    Parameters

    • element: T

    Returns boolean

    true if the element was found, false otherwise.

  • Remove an element from the collection. If multiple elements are matched, the collection removes all of them.

    Parameters

    • element: T

    Returns number

    The amount of elements that was removed.

  • Returns a transparent object that can be used as an argument to add to speed up things. Generally, you don't have to use this method.

    Parameters

    • element: T

    Returns any

  • Allows taking an element directly out of the collection at a given position.

    This method might be faster than at because it is not forced to construct a cursor object.

    Parameters

    • position: number

    Returns T

  • Checks if the collection holds the given element.

    Parameters

    • element: T

      The element to check membership of.

    Returns boolean

    True if the collections holds the given element, false otherwise.

  • Get the last element in the collection.

    Returns T

    An error object if the collection is empty.