Skip to content

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

ts
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

ts
new SortedMap<number, string>(); // empty
new SortedMap<number, string>([[2, 'b'], [1, 'a']]); // ordered by key: 1, 2

Accessors

size

Get Signature

get size(): number

Defined in: sorted-map.ts:207

Example
ts
new SortedMap<number, string>([[1, 'a'], [2, 'b']]).size; // 2
Returns

number

Methods

[iterator]()

[iterator](): IterableIterator<[K, V]>

Defined in: sorted-map.ts:231

Returns

IterableIterator<[K, V]>

Example

ts
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

ts
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

ts
const byPrice = new SortedMap<number, string>([[1, 'a']]);
byPrice.clear();
byPrice.size; // 0

delete()

delete(key): boolean

Defined in: sorted-map.ts:118

Parameters

key

K

Returns

boolean

Example

ts
const byPrice = new SortedMap<number, string>([[101.5, 'order-1']]);
byPrice.delete(101.5); // true
byPrice.delete(101.5); // false — already gone

entries()

entries(): IterableIterator<[K, V]>

Defined in: sorted-map.ts:169

Returns

IterableIterator<[K, V]>

Example

ts
[...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

ts
const byPrice = new SortedMap<number, string>([[101.5, 'order-1']]);
byPrice.get(101.5); // 'order-1'
byPrice.get(1); // undefined

has()

has(key): boolean

Defined in: sorted-map.ts:128

Parameters

key

K

Returns

boolean

Example

ts
new SortedMap<number, string>([[1, 'a']]).has(1); // true

irange()

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

ts
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

ts
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

ts
const byPrice = new SortedMap<number, string>();
byPrice.set(101.5, 'order-1');
byPrice.set(101.5, 'order-1-updated'); // overwrites, doesn't grow size

values()

values(): IterableIterator<V>

Defined in: sorted-map.ts:157

Returns

IterableIterator<V>

Example

ts
[...new SortedMap<number, string>([[2, 'b'], [1, 'a']]).values()]; // ['a', 'b']

from()

static from<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

ts
SortedMap.from([[2, 'b'], [1, 'a']]); // same as new SortedMap([[2, 'b'], [1, 'a']])

Released under the MIT License.