@thi.ng/associative
Version:
ES Map/Set-compatible implementations with customizable equality semantics & supporting operations
63 lines • 2.32 kB
TypeScript
import type { Fn3, ICopy, IEmpty, IEquiv, IObjectOf, Maybe, Pair } from "@thi.ng/api";
import type { HashMapOpts } from "./api.js";
/**
* Configurable hash map implementation w/ ES6 Map API. Uses open
* addressing / linear probing to resolve key collisions. Supports any
* key types via mandatory user supplied hash function.
*
* See {@link HashMapOpts} for further configuration & behavior details.
*
* @example
* ```ts tangle:../export/hash-map.ts
* import { HashMap } from "@thi.ng/associative";
* import { hash } from "@thi.ng/vectors";
*
* const m = new HashMap([], { hash })
* m.set([1, 2], "a");
* m.set([3, 4], "b");
* m.set([1, 2], "c");
*
* console.log("%s", m);
* // HashMap { [ 1, 2 ] => 'c', [ 3, 4 ] => 'b' }
* ```
*
*/
export declare class HashMap<K, V> extends Map<K, V> implements Iterable<Pair<K, V>>, ICopy<HashMap<K, V>>, IEmpty<HashMap<K, V>>, IEquiv {
#private;
constructor(pairs: Iterable<Pair<K, V>> | null, opts: HashMapOpts<K>);
get [Symbol.species](): typeof HashMap;
get [Symbol.toStringTag](): string;
get size(): number;
[Symbol.iterator](): MapIterator<Pair<K, V>>;
[Symbol.dispose](): void;
entries(): MapIterator<Pair<K, V>>;
keys(): MapIterator<K>;
values(): MapIterator<V>;
/**
* 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;
clear(): void;
empty(): HashMap<K, V>;
copy(): HashMap<K, V>;
equiv(o: any): boolean;
has(key: K): boolean;
get(key: K, notFound?: V): Maybe<V>;
set(key: K, val: V): this;
delete(key: K): boolean;
into(pairs: Iterable<Pair<K, V>>): this;
dissoc(keys: Iterable<K>): this;
opts(overrides?: Partial<HashMapOpts<K>>): HashMapOpts<K>;
/** @internal */
protected find(key: K): number;
/** @internal */
protected resize(): void;
}
export declare function defHashMap<K, V>(pairs: Iterable<Pair<K, V>> | null, opts: HashMapOpts<K>): HashMap<K, V>;
export declare function defHashMap<V>(obj: IObjectOf<V>, opts: HashMapOpts<string>): HashMap<string, V>;
//# sourceMappingURL=hash-map.d.ts.map