@thi.ng/bidir-index
Version:
Bi-directional index mapping arbitrary keys to numeric IDs & vice versa
177 lines • 5.77 kB
TypeScript
import type { IClear, ICopy, IEmpty } from "@thi.ng/api";
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> implements IClear, ICopy<BidirIndex<T>>, IEmpty<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]>;
clear(): void;
copy(): BidirIndex<T>;
empty(): BidirIndex<T>;
/**
* 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).
*
* @remarks
* Only used if `fail=false` (default): If `all=true`, any unknown values
* will be mapped to `null` values in the result array. Otherwise, the
* default behavior is for such unknown values to be skipped.
*
* @param keys
* @param fail
* @param all
*/
getAll(keys: Iterable<T>, fail?: boolean): number[];
getAll(keys: Iterable<T>, fail: boolean, all: true): (number | null)[];
/**
* 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>;
/**
* Reverse op of {@link BidirIndex.getAll}. 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).
*
* @remarks
* Only used if `fail=false` (default): If `all=true`, any unknown IDs will
* be mapped to `null` values in the result array. Otherwise, the default
* behavior is for such unknown values to be skipped.
*
* @param ids
* @param fail
* @param all
*/
getAllIDs(ids: Iterable<number>, fail?: boolean): T[];
getAllIDs(ids: Iterable<number>, fail: boolean, all: true): (T | null)[];
/**
* Attempts to rename `currKey` into `newKey` without changing its ID
* mapping. Returns `ok` if successful, otherwise `missing` if `currKey` is
* unknown or `conflict` if `newKey` already exists.
*
* @param currKey
* @param newKey
*/
renameKey(currKey: T, newKey: T): "missing" | "conflict" | "ok";
/**
* 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