@thi.ng/associative
Version:
ES Map/Set-compatible implementations with customizable equality semantics & supporting operations
71 lines • 2.56 kB
TypeScript
import type { Fn, Fn3, ICopy, IEmpty, IEquiv, IObjectOf, Maybe, Pair, Predicate2 } from "@thi.ng/api";
import type { HashMapOpts } from "./api.js";
/** @internal */
interface HashMapState<K, V> {
hash: Fn<K, number>;
equiv: Predicate2<K>;
load: number;
bins: Pair<K, V>[];
mask: number;
size: number;
}
/**
* 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([...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 {
constructor(pairs: Iterable<Pair<K, V>> | null, opts: HashMapOpts<K>);
get [Symbol.species](): typeof HashMap;
get [Symbol.toStringTag](): string;
get size(): number;
[Symbol.iterator](): IterableIterator<Pair<K, V>>;
entries(): IterableIterator<Pair<K, V>>;
keys(): IterableIterator<K>;
values(): IterableIterator<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, $this: HashMapState<K, V>): number;
/** @internal */
protected resize($this: HashMapState<K, V>): 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>;
export {};
//# sourceMappingURL=hash-map.d.ts.map