Class: SortedMap<K, V>
Defined in: sorted-map.ts:52
A dictionary ordered by key.
Uses the internal BucketEngine directly (on [K, V] tuples, with a comparator that only looks at the key) rather than composing over SortedList's public API. Two reasons: set needs the engine's O(log n) in-place replace-or-insert (SortedList#bisectLeft + #at would pay for a positional index it doesn't need), and get/has/delete use the *By lookup methods with a closure over the bare key — that compares the key only once per candidate (not twice, since the search side needs no entry[0] unwrapping) and avoids allocating a throwaway [key, undefined] search tuple per call.
Example
const byPrice = new SortedMap<number, string>([[101.5, 'order-1'], [99.75, 'order-2']]);
[...byPrice.keys()]; // [99.75, 101.5]Type Parameters
K
K
V
V
Implements
Iterable<[K,V]>
Constructors
Constructor
new SortedMap<
K,V>(entries?, ...options):SortedMap<K,V>
Defined in: sorted-map.ts:63
Parameters
entries?
Iterable<[K, V], any, any>
options
...ComparatorArg<K>
Returns
SortedMap<K, V>
Example
new SortedMap<number, string>(); // empty
new SortedMap<number, string>([[2, 'b'], [1, 'a']]); // ordered by key: 1, 2Accessors
size
Get Signature
get size():
number
Defined in: sorted-map.ts:207
Example
new SortedMap<number, string>([[1, 'a'], [2, 'b']]).size; // 2Returns
number
Methods
[iterator]()
[iterator]():
IterableIterator<[K,V]>
Defined in: sorted-map.ts:231
Returns
IterableIterator<[K, V]>
Example
for (const [key, value] of new SortedMap([[2, 'b'], [1, 'a']])) {
console.log(key, value); // 1 'a', then 2 'b'
}Implementation of
Iterable.[iterator]
at()
at(
index): [K,V] |undefined
Defined in: sorted-map.ts:197
O(√n). Entry at ordinal position index in key order.
Parameters
index
number
Returns
[K, V] | undefined
Example
const byPrice = new SortedMap<number, string>([[2, 'b'], [1, 'a']]);
byPrice.at(0); // [1, 'a']clear()
clear():
void
Defined in: sorted-map.ts:219
Returns
void
Example
const byPrice = new SortedMap<number, string>([[1, 'a']]);
byPrice.clear();
byPrice.size; // 0delete()
delete(
key):boolean
Defined in: sorted-map.ts:118
Parameters
key
K
Returns
boolean
Example
const byPrice = new SortedMap<number, string>([[101.5, 'order-1']]);
byPrice.delete(101.5); // true
byPrice.delete(101.5); // false — already goneentries()
entries():
IterableIterator<[K,V]>
Defined in: sorted-map.ts:169
Returns
IterableIterator<[K, V]>
Example
[...new SortedMap<number, string>([[2, 'b'], [1, 'a']]).entries()]; // [[1, 'a'], [2, 'b']]get()
get(
key):V|undefined
Defined in: sorted-map.ts:106
O(log n): binary search for the bucket, then binary search within it.
Parameters
key
K
Returns
V | undefined
Example
const byPrice = new SortedMap<number, string>([[101.5, 'order-1']]);
byPrice.get(101.5); // 'order-1'
byPrice.get(1); // undefinedhas()
has(
key):boolean
Defined in: sorted-map.ts:128
Parameters
key
K
Returns
boolean
Example
new SortedMap<number, string>([[1, 'a']]).has(1); // trueirange()
irange(
minKey?,maxKey?):IterableIterator<[K,V]>
Defined in: sorted-map.ts:182
Iterates [key, value] pairs with keys in [minKey, maxKey].
Parameters
minKey?
K
maxKey?
K
Returns
IterableIterator<[K, V]>
Example
const byPrice = new SortedMap<number, string>([[95, 'a'], [100, 'b'], [105, 'c']]);
[...byPrice.irange(95, 100)]; // [[95, 'a'], [100, 'b']]keys()
keys():
SortedSet<K>
Defined in: sorted-map.ts:141
A snapshot SortedSet of the current keys — not a live view.
Returns
SortedSet<K>
Example
const byPrice = new SortedMap<number, string>([[2, 'b'], [1, 'a']]);
[...byPrice.keys()]; // [1, 2]set()
set(
key,value):void
Defined in: sorted-map.ts:92
O(log n) to locate + O(1) to replace in place, or O(√n) amortized to insert.
Parameters
key
K
value
V
Returns
void
Example
const byPrice = new SortedMap<number, string>();
byPrice.set(101.5, 'order-1');
byPrice.set(101.5, 'order-1-updated'); // overwrites, doesn't grow sizevalues()
values():
IterableIterator<V>
Defined in: sorted-map.ts:157
Returns
IterableIterator<V>
Example
[...new SortedMap<number, string>([[2, 'b'], [1, 'a']]).values()]; // ['a', 'b']from()
staticfrom<K,V>(entries, ...options):SortedMap<K,V>
Defined in: sorted-map.ts:78
Type Parameters
K
K
V
V
Parameters
entries
Iterable<[K, V]>
options
...ComparatorArg<K>
Returns
SortedMap<K, V>
Example
SortedMap.from([[2, 'b'], [1, 'a']]); // same as new SortedMap([[2, 'b'], [1, 'a']])