javascript-collections-framework
    Preparing search index...

    Interface SortedSet<E>

    A set kept in the order of a comparator, with the ends, nearest-match queries, removal from either end and live range views. Two members are the same member when the comparator returns 0 for them. addFirst and addLast throw UnsupportedOperationError: the order decides where a member goes.

    function takeUpTo<E>(set: SortedSet<E>, limit: E): E[] {
    const taken = set.headSet(limit, { inclusive: true }).toArray()
    set.headSet(limit, { inclusive: true }).clear()
    return taken
    }
    interface SortedSet<E> {
        revision: number;
        size: number;
        "[equalsSymbol]"(other: unknown): boolean;
        "[hashCodeSymbol]"(hash: HashFunction): number;
        "[iterator]"(): IterableIterator<E>;
        add(element: E): this;
        addAll(elements: Iterable<E>): boolean;
        addFirst(element: E): this;
        addLast(element: E): this;
        ceiling(element: E): E | undefined;
        clear(): void;
        comparator(): Comparator<E> | undefined;
        cursor(): Cursor<E>;
        delete(element: E): boolean;
        entries(): IterableIterator<[E, E]>;
        equals(other: unknown): boolean;
        floor(element: E): E | undefined;
        forEach(
            action: (element: E, same: E, set: SetCollection<E>) => void,
            thisArg?: unknown,
        ): void;
        getFirst(): E;
        getLast(): E;
        has(element: E): boolean;
        headSet(to: E, options?: BoundOptions | null): SortedSet<E>;
        higher(element: E): E | undefined;
        isEmpty(): boolean;
        keys(): IterableIterator<E>;
        lower(element: E): E | undefined;
        pollFirst(): E | undefined;
        pollLast(): E | undefined;
        removeAll(elements: Iterable<E>): boolean;
        removeFirst(): E;
        removeIf(predicate: (element: E) => unknown): boolean;
        removeLast(): E;
        retainAll(elements: Iterable<E>): boolean;
        reversed(): SortedSet<E>;
        subSet(from: E, to: E, options?: SubSetOptions | null): SortedSet<E>;
        tailSet(from: E, options?: BoundOptions | null): SortedSet<E>;
        toArray(): E[];
        toJSON(): E[];
        toString(): string;
        values(): IterableIterator<E>;
    }

    Type Parameters

    • E

    Hierarchy (View Summary)

    Implemented by

    Index
    revision: number

    A number that changes on every change to the contents — adding, removing or replacing an element — and never on a read. Watch it to detect changes; its exact values are unspecified. Time: O(1).

    size: number

    The number of elements. Time: O(1).

    • The value-equality protocol; delegates to equals. Time: as equals.

      Parameters

      • other: unknown

      Returns boolean

    • Returns an iterator over the elements in iteration order.

      Time: O(1) to create; each step amortised O(1) unless the implementation documents otherwise.

      Returns IterableIterator<E>

      A new iterator.

      ConcurrentModificationError (from next()) when elements of a fail-fast collection are added or removed after the iterator is created, including through a cursor.

    • Adds element and returns the collection, so calls chain. A collection that refuses duplicates is left unchanged. Time: as the implementation documents.

      Parameters

      • element: E

        The element to add.

      Returns this

      The collection.

    • Adds every element of elements, in order, after reading them all. If an error is thrown part-way, the elements added before it stay.

      Time: O(m) plus m additions.

      Parameters

      • elements: Iterable<E>

        The elements to add.

      Returns boolean

      true when any element was added.

      TypeError when elements is not iterable.

    • Inserts element at the front, or moves the member equal to it to the front, and returns the set.

      Time: as the implementation documents.

      Parameters

      • element: E

        The element to insert or move.

      Returns this

      The set.

      UnsupportedOperationError where the set orders its members itself.

    • Inserts element at the end, or moves the member equal to it to the end, and returns the set.

      Time: as the implementation documents.

      Parameters

      • element: E

        The element to insert or move.

      Returns this

      The set.

      UnsupportedOperationError where the set orders its members itself.

    • Returns the least member equal to or after element, or undefined when there is none.

      Time: O(log n).

      Parameters

      • element: E

        The value to search from; it need not be a member.

      Returns E | undefined

      The member, or undefined.

    • Removes every element. Time: as the implementation documents.

      Returns void

    • Returns the comparator that orders the set, or undefined for natural order; a reversed view returns a function that orders in reverse.

      Time: O(1).

      Returns Comparator<E> | undefined

      The comparator, or undefined.

    • Removes one element equal to element (the first, where order is defined). Time: as the implementation documents.

      Parameters

      • element: E

        The value to remove.

      Returns boolean

      true when an element was removed.

    • Returns an iterator over [member, member] pairs, as native Set does.

      Time: O(1) to create; each step amortised O(1) unless the implementation documents otherwise.

      Returns IterableIterator<[E, E]>

      A new iterator.

    • Returns whether other is equal to this collection, as the collection's family defines; otherwise only the collection itself. Time: as the implementation documents.

      Parameters

      • other: unknown

        Any value.

      Returns boolean

      true when equal.

    • Returns the greatest member equal to or before element, or undefined when there is none.

      Time: O(log n).

      Parameters

      • element: E

        The value to search from; it need not be a member.

      Returns E | undefined

      The member, or undefined.

    • Calls action(member, member, set) for each member in order, with this set to thisArg.

      Time: O(n) plus the calls.

      Parameters

      • action: (element: E, same: E, set: SetCollection<E>) => void

        Called with each member twice and the set.

      • OptionalthisArg: unknown

        The this value for action.

      Returns void

      TypeError when action is not a function.

    • Returns the first element.

      Time: as the implementation documents.

      Returns E

      The first element.

      NoSuchElementError when the collection is empty.

    • Returns the last element.

      Time: as the implementation documents.

      Returns E

      The last element.

      NoSuchElementError when the collection is empty.

    • Returns whether some element is equal to element under the collection's equality (SameValueZero unless the collection says otherwise). Time: as the implementation documents.

      Parameters

      • element: E

        The value to look for.

      Returns boolean

      true when an equal element is present.

    • Returns a live view of the members before to (exclusive by default); changes through the view reach this set.

      Time: O(log n).

      Parameters

      • to: E

        The high end, in this set's order.

      • Optionaloptions: BoundOptions | null

        The inclusive flag.

      Returns SortedSet<E>

      The view.

    • Returns the least member after element, or undefined when there is none.

      Time: O(log n).

      Parameters

      • element: E

        The value to search from; it need not be a member.

      Returns E | undefined

      The member, or undefined.

    • Returns whether the collection has no elements.

      Time: O(1).

      Returns boolean

      size === 0.

    • Returns an iterator over the members, as values() does (native Set has both).

      Time: O(1) to create; each step amortised O(1) unless the implementation documents otherwise.

      Returns IterableIterator<E>

      A new iterator.

    • Returns the greatest member before element, or undefined when there is none.

      Time: O(log n).

      Parameters

      • element: E

        The value to search from; it need not be a member.

      Returns E | undefined

      The member, or undefined.

    • Removes and returns the first member, or returns undefined when the set is empty.

      Time: O(log n).

      Returns E | undefined

      The removed member, or undefined.

    • Removes and returns the last member, or returns undefined when the set is empty.

      Time: O(log n).

      Returns E | undefined

      The removed member, or undefined.

    • Removes every element equal to one in elements. Decides first, so an error while deciding removes nothing. Time: as the implementation documents.

      Parameters

      • elements: Iterable<E>

        The elements to remove.

      Returns boolean

      true when any element was removed.

      TypeError when elements is not iterable.

      ConcurrentModificationError when your code adds or removes elements while it decides.

    • Removes and returns the first element.

      Time: as the implementation documents.

      Returns E

      The removed element.

      NoSuchElementError when the collection is empty.

    • Calls predicate on every element, then removes those for which it returned a truthy value; an error from predicate removes nothing. Time: as the implementation documents.

      Parameters

      • predicate: (element: E) => unknown

        Called with each element.

      Returns boolean

      true when any element was removed.

      TypeError when predicate is not a function.

      ConcurrentModificationError when predicate adds or removes elements.

    • Removes and returns the last element.

      Time: as the implementation documents.

      Returns E

      The removed element.

      NoSuchElementError when the collection is empty.

    • Removes every element not equal to one in elements. Decides first, so an error while deciding removes nothing. Time: as the implementation documents.

      Parameters

      • elements: Iterable<E>

        The elements to keep.

      Returns boolean

      true when any element was removed.

      TypeError when elements is not iterable.

      ConcurrentModificationError when your code adds or removes elements while it decides.

    • Returns a live view of this set in the opposite order; changes through the view reach this set.

      Time: O(1).

      Returns SortedSet<E>

      The reversed view.

    • Returns a live view of the members from from (inclusive by default) to to (exclusive by default); changes through the view reach this set.

      Time: O(log n).

      Parameters

      • from: E

        The low end, in this set's order.

      • to: E

        The high end, in this set's order.

      • Optionaloptions: SubSetOptions | null

        The inclusive flags.

      Returns SortedSet<E>

      The view.

    • Returns a live view of the members from from on (inclusive by default); changes through the view reach this set.

      Time: O(log n).

      Parameters

      • from: E

        The low end, in this set's order.

      • Optionaloptions: BoundOptions | null

        The inclusive flag.

      Returns SortedSet<E>

      The view.

    • Returns a new array of the elements in iteration order; changing it does not change the collection.

      Time: O(n).

      Returns E[]

      A new array.

    • Returns a new array of the elements, so JSON.stringify writes the collection as an array.

      Time: O(n).

      Returns E[]

      A new array.

    • Returns the class name, size and elements, e.g. ArrayList(3) [1, 2, 'a'].

      Time: O(length of the output) for flat contents; each level of nested arrays or collections copies its text once more.

      Returns string

      The text.

    • Returns an iterator over the members.

      Time: O(1) to create; each step amortised O(1) unless the implementation documents otherwise.

      Returns IterableIterator<E>

      A new iterator.