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
const tags = new SortedSet<string>(['b', 'a', 'c', 'a']);
[...tags]; // ['a', 'b', 'c'] — the duplicate 'a' was droppedExtends
SortedList<T>
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
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
Accessors
length
Get Signature
get length():
number
Defined in: sorted-list.ts:246
Example
new SortedList<number>([1, 2, 3]).length; // 3Returns
number
Inherited from
Methods
[iterator]()
[iterator]():
IterableIterator<T>
Defined in: sorted-list.ts:285
Returns
IterableIterator<T>
Example
const scores = new SortedList<number>([3, 1, 2]);
for (const value of scores) console.log(value); // 1, 2, 3Inherited from
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
const tags = new SortedSet<string>(['a']);
tags.add('a'); // no-op, already present
tags.length; // 1Overrides
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
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 rangeInherited from
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
new SortedList<number>([1, 2, 2, 2, 3]).bisectLeft(2); // 1Inherited from
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
new SortedList<number>([1, 2, 2, 2, 3]).bisectRight(2); // 4Inherited from
clear()
clear():
void
Defined in: sorted-list.ts:258
Returns
void
Example
const scores = new SortedList<number>([1, 2, 3]);
scores.clear();
scores.length; // 0Inherited from
clone()
clone():
SortedSet<T>
Defined in: sorted-set.ts:70
Returns
SortedSet<T>
Example
const original = new SortedSet<number>([1, 2, 3]);
const copy = original.clone();
copy.add(4);
original.length; // 3Overrides
comparatorArg()
protectedcomparatorArg():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
difference()
difference(
other):SortedSet<T>
Defined in: sorted-set.ts:118
Parameters
other
SortedSet<T>
Returns
SortedSet<T>
Example
const a = new SortedSet<number>([1, 2, 3]);
const b = new SortedSet<number>([2, 3]);
[...a.difference(b)]; // [1] — in a, not in bdiscard()
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
const scores = new SortedList<number>([1, 2, 3]);
scores.discard(99); // no-op, no errorInherited from
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
const scores = new SortedList<number>([1, 2, 3]);
scores.has(2); // true
scores.has(99); // falseInherited from
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
new SortedList<number>([1, 2, 2, 3]).indexOf(2); // 1
new SortedList<number>([1, 2, 3]).indexOf(99); // -1Inherited from
intersection()
intersection(
other):SortedSet<T>
Defined in: sorted-set.ts:100
Parameters
other
SortedSet<T>
Returns
SortedSet<T>
Example
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
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
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
const scores = new SortedList<number>([10, 20, 30, 40, 50]);
[...scores.islice(1, 4)]; // [20, 30, 40] — like Array.prototype.sliceInherited from
isSubsetOf()
isSubsetOf(
other):boolean
Defined in: sorted-set.ts:135
Parameters
other
SortedSet<T>
Returns
boolean
Example
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])); // falsepop()
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
const scores = new SortedList<number>([10, 20, 30]);
scores.pop(); // 30 — last element
scores.pop(0); // 10 — first elementInherited from
prepareSorted()
protectedprepareSorted(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
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
const scores = new SortedList<number>([1, 2, 2, 3]);
scores.remove(2); // true — removes one occurrence
scores.remove(99); // false — not presentInherited from
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
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
const scores = new SortedList<number>([3, 1]);
scores.update([2, 5, 4]);
[...scores]; // [1, 2, 3, 4, 5]Inherited from
from()
staticfrom<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
SortedSet.from([3, 1, 2, 1]); // same as new SortedSet([3, 1, 2, 1])