froebel
Version:
TypeScript utility library
39 lines (38 loc) • 1.63 kB
TypeScript
/**
* Behaves like a regular JavaScript `Map`, but its iteration order is dependant
* on the `compare` function supplied in the constructor.
*
* Note: The item's sort position is only computed automatically on insertion.
* If you update one of the values that the `compare` function depends on, you
* must call the `update(key)` method afterwards to ensure the map stays sorted.
*/
export default class SortedMap<K, V> implements Map<K, V> {
#private;
constructor(compare: Cmp<K, V>, entries?: readonly (readonly [K, V])[] | null);
clear: () => void;
get: (key: K) => V | undefined;
has: (key: K) => boolean;
set(key: K, value: V): this;
delete(key: K): boolean;
/**
* Update the sort position of the element at `key` if necessary.
*
* This method should be called to notify the SortedMap that one of the
* parameters that the `compare` function depends on has been updated and
* consequently the sort order must be verified/updated.
*
* @returns `true` if the sort position of the element with `key` had to be
* updated, `false` if not.
*/
update(key: K): boolean;
forEach(callback: (value: V, key: K, map: SortedMap<K, V>) => void): void;
map<T>(callback: (value: V, key: K, map: SortedMap<K, V>) => T): T[];
get size(): number;
[Symbol.iterator]: () => IterableIterator<[K, V]>;
entries: () => IterableIterator<[K, V]>;
keys: () => IterableIterator<K>;
values: () => IterableIterator<V>;
[Symbol.toStringTag]: string;
}
declare type Cmp<K, V> = (valueA: V, valueB: V, keyA: K, keyB: K) => number;
export {};