Skip to content

Class: SortedSet<T>

Defined in: sorted-set.ts:17

Like SortedList, but rejects duplicates (compared via the comparator, not reference identity). Adds set-theory operations.

Reuses SortedList's bucket structure entirely: the only behavioral difference is that add is a no-op for values already present.

Example

ts
const tags = new SortedSet<string>(['b', 'a', 'c', 'a']);
[...tags]; // ['a', 'b', 'c'] — the duplicate 'a' was dropped

Extends

Type Parameters

T

T

Constructors

Constructor

new SortedSet<T>(iterable?, ...options): SortedSet<T>

Defined in: sorted-list.ts:40

Parameters

iterable?

Iterable<T, any, any>

options

...ComparatorArg<T>

Returns

SortedSet<T>

Example

ts
new SortedList<number>(); // empty
new SortedList<number>([3, 1, 2]); // [1, 2, 3]
new SortedList<Person>([...], { comparator: (a, b) => a.age - b.age });

Inherited from

SortedList.constructor

Accessors

length

Get Signature

get length(): number

Defined in: sorted-list.ts:246

Example
ts
new SortedList<number>([1, 2, 3]).length; // 3
Returns

number

Inherited from

SortedList.length

Methods

[iterator]()

[iterator](): IterableIterator<T>

Defined in: sorted-list.ts:285

Returns

IterableIterator<T>

Example

ts
const scores = new SortedList<number>([3, 1, 2]);
for (const value of scores) console.log(value); // 1, 2, 3

Inherited from

SortedList.[iterator]


add()

add(value): void

Defined in: sorted-set.ts:55

O(log n) has check + O(√n) amortized insert if the value is new.

Parameters

value

T

Returns

void

Example

ts
const tags = new SortedSet<string>(['a']);
tags.add('a'); // no-op, already present
tags.length; // 1

Overrides

SortedList.add


at()

at(index): T | undefined

Defined in: sorted-list.ts:158

O(√n): walks bucket lengths to find the element at index (negative indices count from the end).

Parameters

index

number

Returns

T | undefined

Example

ts
const scores = new SortedList<number>([10, 20, 30]);
scores.at(0); // 10
scores.at(-1); // 30 — same as Array.prototype.at
scores.at(99); // undefined — out of range

Inherited from

SortedList.at


bisectLeft()

bisectLeft(value): number

Defined in: sorted-list.ts:197

The leftmost index where value could be inserted while keeping the list sorted.

Parameters

value

T

Returns

number

Example

ts
new SortedList<number>([1, 2, 2, 2, 3]).bisectLeft(2); // 1

Inherited from

SortedList.bisectLeft


bisectRight()

bisectRight(value): number

Defined in: sorted-list.ts:209

The rightmost index where value could be inserted while keeping the list sorted.

Parameters

value

T

Returns

number

Example

ts
new SortedList<number>([1, 2, 2, 2, 3]).bisectRight(2); // 4

Inherited from

SortedList.bisectRight


clear()

clear(): void

Defined in: sorted-list.ts:258

Returns

void

Example

ts
const scores = new SortedList<number>([1, 2, 3]);
scores.clear();
scores.length; // 0

Inherited from

SortedList.clear


clone()

clone(): SortedSet<T>

Defined in: sorted-set.ts:70

Returns

SortedSet<T>

Example

ts
const original = new SortedSet<number>([1, 2, 3]);
const copy = original.clone();
copy.add(4);
original.length; // 3

Overrides

SortedList.clone


comparatorArg()

protected comparatorArg(): ComparatorArg<T>

Defined in: sorted-list.ts:70

Safe to call from subclasses/clone: both ComparatorArg<T> branches accept { comparator }.

Returns

ComparatorArg<T>

Inherited from

SortedList.comparatorArg


difference()

difference(other): SortedSet<T>

Defined in: sorted-set.ts:118

Parameters

other

SortedSet<T>

Returns

SortedSet<T>

Example

ts
const a = new SortedSet<number>([1, 2, 3]);
const b = new SortedSet<number>([2, 3]);
[...a.difference(b)]; // [1] — in a, not in b

discard()

discard(value): void

Defined in: sorted-list.ts:128

Like remove, but never throws (or returns anything) if value isn't present.

Parameters

value

T

Returns

void

Example

ts
const scores = new SortedList<number>([1, 2, 3]);
scores.discard(99); // no-op, no error

Inherited from

SortedList.discard


has()

has(value): boolean

Defined in: sorted-list.ts:185

O(log n): binary search for the bucket, then binary search within it.

Parameters

value

T

Returns

boolean

Example

ts
const scores = new SortedList<number>([1, 2, 3]);
scores.has(2); // true
scores.has(99); // false

Inherited from

SortedList.has


indexOf()

indexOf(value): number

Defined in: sorted-list.ts:171

The index of the first occurrence of value, or -1 if absent.

Parameters

value

T

Returns

number

Example

ts
new SortedList<number>([1, 2, 2, 3]).indexOf(2); // 1
new SortedList<number>([1, 2, 3]).indexOf(99); // -1

Inherited from

SortedList.indexOf


intersection()

intersection(other): SortedSet<T>

Defined in: sorted-set.ts:100

Parameters

other

SortedSet<T>

Returns

SortedSet<T>

Example

ts
const a = new SortedSet<number>([1, 2, 3]);
const b = new SortedSet<number>([2, 3, 4]);
[...a.intersection(b)]; // [2, 3]

irange()

irange(min?, max?, options?): IterableIterator<T>

Defined in: sorted-list.ts:223

O(log n) to locate both bounds by value + O(k) to iterate the k results.

Parameters

min?

T

max?

T

options?
inclusive?

[boolean, boolean]

Returns

IterableIterator<T>

Example

ts
const scores = new SortedList<number>([1, 2, 3, 4, 5]);
[...scores.irange(2, 4)]; // [2, 3, 4] — inclusive by default
[...scores.irange(2, 4, { inclusive: [false, true] })]; // [3, 4]

Inherited from

SortedList.irange


islice()

islice(start?, end?): IterableIterator<T>

Defined in: sorted-list.ts:236

O(√n) to locate both positional bounds + O(k) to iterate the k results.

Parameters

start?

number

end?

number

Returns

IterableIterator<T>

Example

ts
const scores = new SortedList<number>([10, 20, 30, 40, 50]);
[...scores.islice(1, 4)]; // [20, 30, 40] — like Array.prototype.slice

Inherited from

SortedList.islice


isSubsetOf()

isSubsetOf(other): boolean

Defined in: sorted-set.ts:135

Parameters

other

SortedSet<T>

Returns

boolean

Example

ts
new SortedSet<number>([2, 3]).isSubsetOf(new SortedSet<number>([1, 2, 3, 4])); // true
new SortedSet<number>([1, 5]).isSubsetOf(new SortedSet<number>([1, 2, 3])); // false

pop()

pop(index?): T

Defined in: sorted-list.ts:143

Removes and returns the element at index (default: the last element). Negative indices count from the end, same as at.

Parameters

index?

number

Returns

T

Example

ts
const scores = new SortedList<number>([10, 20, 30]);
scores.pop(); // 30 — last element
scores.pop(0); // 10 — first element

Inherited from

SortedList.pop


prepareSorted()

protected prepareSorted(iterable, comparator): T[]

Defined in: sorted-set.ts:24

Dedupes adjacent equal elements (by comparator) after the base class's sort, keeping the first occurrence — matching add's no-op-if- present semantics, which is what incremental construction produced before bulk construction existed.

Parameters

iterable

Iterable<T>

comparator

Comparator<T>

Returns

T[]

Overrides

SortedList.prepareSorted


remove()

remove(value): boolean

Defined in: sorted-list.ts:115

Removes the first occurrence of value. Returns false if it wasn't present.

Parameters

value

T

Returns

boolean

Example

ts
const scores = new SortedList<number>([1, 2, 2, 3]);
scores.remove(2); // true — removes one occurrence
scores.remove(99); // false — not present

Inherited from

SortedList.remove


union()

union(other): SortedSet<T>

Defined in: sorted-set.ts:84

O(m log n + m) for a set of size m being merged in.

Parameters

other

SortedSet<T>

Returns

SortedSet<T>

Example

ts
const a = new SortedSet<number>([1, 2, 3]);
const b = new SortedSet<number>([3, 4]);
[...a.union(b)]; // [1, 2, 3, 4]

update()

update(iterable): void

Defined in: sorted-list.ts:99

Bulk-inserts every value from iterable, one add at a time.

Parameters

iterable

Iterable<T>

Returns

void

Example

ts
const scores = new SortedList<number>([3, 1]);
scores.update([2, 5, 4]);
[...scores]; // [1, 2, 3, 4, 5]

Inherited from

SortedList.update


from()

static from<T>(iterable, ...options): SortedSet<T>

Defined in: sorted-set.ts:41

Type Parameters

T

T

Parameters

iterable

Iterable<T>

options

...ComparatorArg<T>

Returns

SortedSet<T>

Example

ts
SortedSet.from([3, 1, 2, 1]); // same as new SortedSet([3, 1, 2, 1])

Overrides

SortedList.from

Released under the MIT License.