Skip to main content

interface SortedSet<T>

A type-invariant immutable Set of value type T. In the Set, there are no duplicate values. See the Set documentation and the SortedSet API documentation

Companion namespace: SortedSet

Extends: RSetBase<T,Tp>

Implemented by: SortedSetEmpty<T>, SortedSet.NonEmpty<T>

Type parameters

NameDescription
Tthe value type
note
  • The SortedSet keeps the inserted values in sorted order according to the context's comp Comp instance.
example
const s1 = SortedSet.empty<string>()
const s2 = SortedSet.of('a', 'b', 'c')

Properties

context

Returns the context associated to this collection instance.

Definition

readonly context: WithElem<Tp, T>['context'];

Overrides

RSetBase.context

isEmpty

Returns true if the collection is empty.

Definition

readonly isEmpty: boolean;

Overrides

VariantSetBase.isEmpty

size

Returns the number of values in the collection.

Definition

readonly size: number;

Overrides

VariantSetBase.size

Methods

[Symbol.iterator]

Returns a FastIterator instance used to iterate over the values of this Iterable.

Definition

[Symbol.iterator](): FastIterator<T>;

Overrides

FastIterable.[Symbol.iterator]

add

Returns the collection with given value added.

Definition

add(value: T): WithElem<Tp, T>['nonEmpty'];

Parameters

NameTypeDescription
valueTthe value to add
example
HashSet.of(1, 2, 3).add(10).toArray()   // => [1, 2, 3, 10]

Overrides

RSetBase.add

addAll

Returns the collection with the values in given values StreamSource added.

Definitions

addAll(values: StreamSource.NonEmpty<T>): WithElem<Tp, T>['nonEmpty'];

addAll(values: StreamSource<T>): WithElem<Tp, T>['normal'];

Parameters

NameTypeDescription
valuesStreamSource.NonEmpty<T>a StreamSource containing values to add
example
HashSet.of(1, 2, 3).addAll(10, 11).toArray()   // => [1, 2, 3, 10, 11]

Overrides

RSetBase.addAll

assumeNonEmpty

Returns the same collection typed as non-empty.

Definition

assumeNonEmpty(): WithElem<Tp, T>['nonEmpty'];

throws

RimbuError.EmptyCollectionAssumedNonEmptyError if the collection is empty

example
HashSet.empty().assumeNonEmpty()          // => throws RimbuError.EmptyCollectionAssumedNonEmptyError
HashSet.from([[0, 1]]).assumeNonEmpty() // => List.NonEmpty(0, 1, 2)

Overrides

VariantSetBase.assumeNonEmpty

difference

Returns a collection where each value of given other StreamSource is removed from this collection.

Definition

difference<U = T>(other: StreamSource<RelatedTo<T, U>>): WithElem<Tp, T>['normal'];

Type parameters

NameDefaultDescription
UT

Parameters

NameTypeDescription
otherStreamSource<RelatedTo<T, U>>a StreamSource containing values
example
HashSet.of(1, 2, 3).difference(HashSet.of(1, 3)).toArray()  // => [2]

Overrides

VariantSetBase.difference

drop

Returns a SortedSet containing all but the the first amount of value of this SortedSet.

Definition

drop(amount: number): SortedSet<T>;

Parameters

NameTypeDescription
amountnumberthe amount of elements to keep
note

a negative amount drops the last values instead of the first, e.g. -2 is the last 2 elements

example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.drop(2).toArray())
// => ['c', 'd']
console.log(m.drop(-2).toArray())
// => ['a', 'b']

filter

Returns a collection containing only those entries that satisfy given pred predicate.

Definition

filter(pred: (value: T, index: number, halt: () => void) => boolean): WithElem<Tp, T>['normal'];

Parameters

NameTypeDescription
pred(value: T, index: number, halt: () => void) => booleana predicate function receiving:
- value: the next value
- index: the entry index
- halt: a function that, when called, ensures no next elements are passed
example
HashSet.of(1, 2, 3).filter(value < 3).toArray()
// => [1, 2]

Overrides

VariantSetBase.filter

forEach

Performs given function f for each value of the collection, using given state as initial traversal state.

Definition

forEach(f: (value: T, index: number, halt: () => void) => void, state?: TraverseState): void;

Parameters

NameTypeDescription
f(value: T, index: number, halt: () => void) => voidthe function to perform for each element, receiving: - value: the next element
- index: the index of the element
- halt: a function that, if called, ensures that no new elements are passed
stateTraverseState(optional) the traverse state
example
HashSet.of(1, 2, 3).forEach((value, i, halt) => {
console.log([value, i]);
if (i >= 1) halt();
})
// => logs [1, 0] [2, 1]
note

O(N)

Overrides

VariantSetBase.forEach

getAtIndex

Returns the value at the given index of the value sort order of the SortedSet, or a fallback value (default: undefined) if the index is out of bounds.

Definitions

getAtIndex(index: number): T | undefined;

getAtIndex<O>(index: number, otherwise: OptLazy<O>): T | O;

Parameters

NameTypeDescription
indexnumberthe index in the key sort order
note

negative index values will retrieve the values from the end of the sort order, e.g. -1 is the last value

example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.getAtIndex(1))
// => 'b'
console.log(m.getAtIndex(-1))
// => 'd'
console.log(m.getAtIndex(10))
// => undefined
console.log(m.getAtIndex(10, 'q'))
// => 'q'

has

Returns true if given value is in the collection.

Definition

has<U = T>(value: RelatedTo<T, U>): boolean;

Type parameters

NameDefaultDescription
UT

Parameters

NameTypeDescription
valueRelatedTo<T, U>the value to look for
example
HashSet.of(1, 2, 3).has(2)  // => true
HashSet.of(1, 2, 3).has(10) // => false

Overrides

VariantSetBase.has

intersect

Returns a collection containing values that are both in this collection, and in the given other StreamSource.

Definition

intersect<U = T>(other: StreamSource<RelatedTo<T, U>>): WithElem<Tp, T>['normal'];

Type parameters

NameDefaultDescription
UT

Parameters

NameTypeDescription
otherStreamSource<RelatedTo<T, U>>a StreamSource containing values
example
HashSet.of(1, 2, 3).interface(HashSet.of(1, 3)).toArray()   // => [1, 3]

Overrides

VariantSetBase.intersect

max

Returns the maximum value of the SortedSet, or a fallback value (default: undefined) if the SortedSet is empty.

Definitions

max(): T | undefined;

max<O>(otherwise: OptLazy<O>): T | O;

example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.max())
// => 'a'
console.log(m.max('q'))
// => 'a'
console.log(SortedSet.empty().max())
// => undefined
console.log(SortedSet.empty().max('q'))
// => 'q'

min

Returns the minimum value of the SortedSet, or a fallback value (default: undefined) if the SortedSet is empty.

Definitions

min(): T | undefined;

min<O>(otherwise: OptLazy<O>): T | O;

example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.min())
// => 'a'
console.log(m.min('q'))
// => 'a'
console.log(SortedSet.empty().min())
// => undefined
console.log(SortedSet.empty().min('q'))
// => 'q'

nonEmpty

Returns true if there is at least one entry in the collection, and instructs the compiler to treat the collection as a .NonEmpty type.

Definition

nonEmpty(): this is WithElem<Tp, T>['nonEmpty'];

example
const m: HashSet<number> = HashSet.of(1, 2, 2)
m.stream().first(0) // compiler allows fallback value since the Stream may be empty
if (m.nonEmpty()) {
m.stream().first(0) // compiler error: fallback value not allowed since Stream is not empty
}

Overrides

VariantSetBase.nonEmpty

remove

Returns the collection with given value removed.

Definition

remove<U = T>(value: RelatedTo<T, U>): WithElem<Tp, T>['normal'];

Type parameters

NameDefaultDescription
UT

Parameters

NameTypeDescription
valueRelatedTo<T, U>the value to remove
example
const s = HashSet.of(1, 2, 3)
s.remove(2).toArray() // => [1, 3]
s.remove(10).toArray() // => [1, 2, 3]

Overrides

VariantSetBase.remove

removeAll

Returns the collection with all values in the given values StreamSource removed.

Definition

removeAll<U = T>(values: StreamSource<RelatedTo<T, U>>): WithElem<Tp, T>['normal'];

Type parameters

NameDefaultDescription
UT

Parameters

NameTypeDescription
valuesStreamSource<RelatedTo<T, U>>a StreamSource containing values to remove
example
HashSet.of(1, 2, 3).removeAll([1, 3]).toArray()
// => [2]

Overrides

VariantSetBase.removeAll

slice

Returns a SortedSet containing only those values that are within the given keyRange.

Definition

slice(range: Range<T>): SortedSet<T>;

Parameters

NameTypeDescription
rangeRange<T>a Range defining the values to include
example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.slice({ start: 'b', end: 'c' }).toArray())
// => ['b', 'c']

sliceIndex

Returns a SortedSet containing only those values that are within the given range index range of the value sort order.

Definition

sliceIndex(range: IndexRange): SortedSet<T>;

Parameters

NameTypeDescription
rangeIndexRangean IndexRange defining the sort order indices to include.
example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.sliceIndex({ start: 1, amount: 2 }).toArray())
// => ['b', 'c']

stream

undocumented

Definition

stream(reversed?: boolean): Stream<T>;

Parameters

NameTypeDescription
reversedboolean

Overrides

VariantSetBase.stream

streamRange

Returns a Stream of sorted values of this collection within the given keyRange.

Definition

streamRange(range: Range<T>, reversed?: boolean): Stream<T>;

Parameters

NameTypeDescription
rangeRange<T>
reversedboolean
example
const m = SortedSet.of('b', 'd', 'a', 'c');
console.log(m.streamRange({ start: 'b', end: 'c' }).toArray())
// => ['b', 'c']

streamSliceIndex

Returns a Stream of sorted values of this collection within the given range index range.

Definition

streamSliceIndex(range: IndexRange, reversed?: boolean): Stream<T>;

Parameters

NameTypeDescription
rangeIndexRangethe range of values to include in the stream
reversedboolean
example
const m = SortedSet.of('b', 'd', 'a', 'c');
console.log(m.streamSliceIndex({ start: 1, amount: 2 }).toArray())
// => ['b', 'c']

symDifference

Returns a collection of the values that are either in this collection or in the other StreamSource, but not in both.

Definition

symDifference(other: StreamSource<T>): WithElem<Tp, T>['normal'];

Parameters

NameTypeDescription
otherStreamSource<T>a StreamSource containing values
example
HashSet.of(1, 2, 3).symDifference([2, 4]).toArray()
// => [1, 3, 4]

Overrides

RSetBase.symDifference

take

Returns a SortedSet containing the the first amount of value of this SortedSet.

Definition

take(amount: number): SortedSet<T>;

Parameters

NameTypeDescription
amountnumberthe amount of elements to keep
note

a negative amount takes the last values instead of the first, e.g. -2 is the last 2 elements

example
const m = SortedSet.of('b', 'd', 'a', 'c').asNormal();
console.log(m.take(2).toArray())
// => ['a', 'b']
console.log(m.take(-2).toArray())
// => ['c', 'd']

toArray

Returns an array containing all values in this collection.

Definition

toArray(): T[];

example
HashSet.of(1, 2, 3).toArray()   // => [1, 2, 3]
note

O(log(N)) @note it is safe to mutate the returned array, however, the array elements are not copied, thus should be treated as read-only

Overrides

VariantSetBase.toArray

toBuilder

Returns a builder object containing the values of this collection.

Definition

toBuilder(): WithElem<Tp, T>['builder'];

example
const builder: HashSet.Builder<number> = HashSet.of(1, 2, 3).toBuilder()

Overrides

RSetBase.toBuilder

toJSON

Returns a JSON representation of this collection.

Definition

toJSON(): ToJSON<T[]>;

example
HashSet.of(1, 2, 3).toJSON()   // => { dataType: 'HashSet', value: [1, 2, 3] }

Overrides

VariantSetBase.toJSON

toString

Returns a string representation of this collection.

Definition

toString(): string;

example
HashSet.of(1, 2, 3).toString()   // => HashSet(1, 2, 3)

Overrides

VariantSetBase.toString

union

Returns a collection containing all values from this collection and all values of given other StreamSource.

Definitions

union(other: StreamSource.NonEmpty<T>): WithElem<Tp, T>['nonEmpty'];

union(other: StreamSource<T>): WithElem<Tp, T>['normal'];

Parameters

NameTypeDescription
otherStreamSource.NonEmpty<T>a StreamSource containing values
example
HashSet.of(1, 2, 3).union(HashSet.of(2, 4, 6)).toArray()
// => [1, 2, 3, 4, 6]

Overrides

RSetBase.union