UNPKG

@thi.ng/sorted-map

Version:

Skiplist-based sorted map & set implementation

118 lines 4.13 kB
import type { Fn3, IObjectOf, Maybe, Pair } from "@thi.ng/api"; import type { Reduced, ReductionFn } from "@thi.ng/transducers"; import type { SortedMapOpts } from "./api.js"; /** @internal */ declare class Node<K, V> { k?: K | undefined; v?: V | undefined; level: number; next: Maybe<Node<K, V>>; prev: Maybe<Node<K, V>>; up: Maybe<Node<K, V>>; down: Maybe<Node<K, V>>; constructor(k?: K | undefined, v?: V | undefined, level?: number); } /** * Sorted map implementation based on Skip List data structure. Supports any * keys (other than `undefined`) which can be sorted via user-defined * comparator, given as ctor option. * * @remarks * v6.3.0 .set() & .delete() implementations rewritten, based on: * * - https://en.wikipedia.org/wiki/Skip_list * - https://www.youtube.com/watch?v=kBwUoWpeH_Q (MIT courseware) * - https://www.educba.com/skip-list-java/ */ export declare class SortedMap<K, V> extends Map<K, V> { #private; constructor(pairs?: Iterable<Pair<K, V>> | null, opts?: Partial<SortedMapOpts<K>>); get size(): number; get [Symbol.species](): typeof SortedMap; get [Symbol.toStringTag](): string; [Symbol.iterator](): MapIterator<Pair<K, V>>; [Symbol.dispose](): void; /** * Yields iterator of sorted `[key, value]` pairs, optionally taking given * `key` and `max` flag into account. * * @remarks * If `key` is given and `max=false`, the key is used as minimum search key * and the iterator will only yield pairs for which keys are `>=` given key. * If `max=true`, the given is used as maximum and only yields pairs for * which keys are `<=` given key. * * If **no** key is given, yields **all** pairs. * * @param key * @param max */ entries(key?: K, max?: boolean): MapIterator<Pair<K, V>>; /** * Similar to {@link SortedMap.entries}, but only yield sequence of keys. * * @param key * @param max */ keys(key?: K, max?: boolean): MapIterator<K>; /** * Similar to {@link SortedMap.entries}, but only yield sequence of values. * * @param key * @param max */ values(key?: K, max?: boolean): MapIterator<V>; clear(): void; empty(): SortedMap<K, V>; copy(): SortedMap<K, V>; compare(o: Map<K, V>): number; equiv(o: any): boolean; first(): (V | (K & ({} | null)) | undefined)[] | undefined; get(key: K, notFound?: V): Maybe<V>; has(key: K): boolean; set(key: K, val: V): this; delete(key: K): boolean; into(pairs: Iterable<Pair<K, V>>): this; dissoc(keys: Iterable<K>): this; /** * The key & value args given the callback `fn` MUST be treated as * readonly/immutable. This could be enforced via TS, but would * break ES6 Map interface contract. * * @param fn - * @param thisArg - */ forEach(fn: Fn3<V, K, Map<K, V>, void>, thisArg?: any): void; $reduce<R>(rfn: ReductionFn<Pair<K, V>, R>, acc: R | Reduced<R>): R | Reduced<R>; opts(): SortedMapOpts<K>; /** * Inserts `b` as successor of `a` (in the same lane as `a`). * * @param a * @param b */ protected insertInLane(a: Node<K, V>, b: Node<K, V>): void; /** * Links lanes by connecting `a` and `b` vertically. * * @param a * @param b */ protected linkLanes(a: Node<K, V>, b: Node<K, V>): void; /** * Returns first node on lowest level. Unless the map is empty, this node * will be the first data node (with the smallest key). */ protected firstNode(): Node<K, V>; /** * Returns the first matching (or predecessor) node for given key (NOT * necessarily at the lowest level). * * @param key */ protected findNode(key: K): Node<K, V>; } export declare function defSortedMap<K, V>(pairs?: Iterable<Pair<K, V>> | null, opts?: Partial<SortedMapOpts<K>>): SortedMap<K, V>; export declare function defSortedMap<V>(obj: IObjectOf<V>, opts?: Partial<SortedMapOpts<string>>): SortedMap<string, V>; export {}; //# sourceMappingURL=sorted-map.d.ts.map