UNPKG

@thi.ng/bidir-index

Version:

Bi-directional index mapping arbitrary keys to numeric IDs & vice versa

150 lines 4.51 kB
import type { BidirIndexOpts, SerializedBidirIndex } from "./api.js"; /** * Bi-directional index to map arbitrary keys to numeric IDs and vice versa. */ export declare class BidirIndex<T> { fwd: Map<T, number>; rev: Map<number, T>; nextID: number; constructor(keys?: Iterable<T> | null, opts?: Partial<BidirIndexOpts<T>>); get size(): number; /** * Yields same result as {@link BidirIndex.entries}. */ [Symbol.iterator](): MapIterator<[T, number]>; /** * Returns iterator of `[key,id]` pairs. */ entries(): MapIterator<[T, number]>; /** * Returns iterator of all indexed keys. */ keys(): MapIterator<T>; /** * Returns iterator of all indexed IDs. */ values(): MapIterator<number>; /** * Returns true if given `key` is known/indexed. * * @param key */ has(key: T): boolean; /** * Returns true if given `id` has a corresponding known/indexed key. * * @param id */ hasID(id: number): boolean; /** * Reverse lookup of {@link BidirIndex.getID}. Returns the matching ID for * given `key` or undefined if the key is not known. * * @param key */ get(key: T): number | undefined; /** * Reverse lookup of {@link BidirIndex.get}. Returns the matching key for * given `id` or undefined if the ID is not known. * * @param id */ getID(id: number): T | undefined; /** * Indexes given `key` and assigns & returns a new ID. If `key` is already * known/indexed, returns its existing ID. * * @param key */ add(key: T): number; /** * Batch version of {@link BidirIndex.add}. Indexes all given keys and * returns array of their corresponding IDs. * * @param keys */ addAll(keys: Iterable<T>): number[]; /** * Similar to {@link BidirIndex.addAll}, but returns indexed key IDs as ES6 * Set, thereby removing any duplicates present in `keys`. * * @param keys */ addAllUnique(keys: Iterable<T>): Set<number>; /** * Removes bi-directional mapping for given `key` from the index. Returns * true if successful. * * @param key */ delete(key: T): boolean; /** * Removes bi-directional mapping for given `id` from the index. Returns * true if successful. * * @param id */ deleteID(id: number): boolean; /** * Batch version of {@link BidirIndex.delete}. * * @param keys */ deleteAll(keys: Iterable<T>): void; /** * Batch version of {@link BidirIndex.deleteID}. * * @param ids */ deleteAllIDs(ids: Iterable<number>): void; /** * Returns array of IDs for all given keys. If `fail` is true (default: * false), throws error if any of the given keys is unknown/unindexed (use * {@link BidirIndex.add} or {@link BidirIndex.addAll} first). * * @param keys * @param fail */ getAll(keys: Iterable<T>, fail?: boolean): number[]; /** * Similar to {@link BidirIndex.getAll}, but returns result as ES6 Set, * thereby removing any duplicates in `keys`. * * @param keys * @param fail */ getAllUnique(keys: Iterable<T>, fail?: boolean): Set<number>; /** * Returns array of matching keys for all given IDs. If `fail` is true * (default: false), throws error if any of the given IDs is * unknown/unindexed (use {@link BidirIndex.add} or * {@link BidirIndex.addAll} first). * * @param ids * @param fail */ getAllIDs(ids: Iterable<number>, fail?: boolean): T[]; /** * Returns a compact JSON serializable version of the index. Use * {@link bidirIndexFromJSON} to instantiate an index from such a JSON * serialization. */ toJSON(): SerializedBidirIndex<T>; } /** * Factory function wrapper for {@link BidirIndex}. * * @param keys * @param opts */ export declare const defBidirIndex: <T>(keys?: Iterable<T>, opts?: Partial<BidirIndexOpts<T>>) => BidirIndex<T>; /** * Instantiates a {@link BidirIndex} from given JSON serialization. The optional * `map` arg can be used to provide a customized `key -> id` map implementation * (same use as {@link BidirIndexOpts.map}). * * @param src * @param map */ export declare const bidirIndexFromJSON: <T>(src: string | SerializedBidirIndex<T>, map?: Map<T, number>) => BidirIndex<T>; //# sourceMappingURL=bidir-index.d.ts.map