@reactodia/hashmap is a TypeScript/JavaScript library that provides HashMap and HashSet collections to use as a compatible replacement for the built-in Map and Set when keys (items) are composite values.
Install with:
npm install --save @reactodia/hashmapimport{HashMap,hashTuple}from'@reactodia/hashmap';typeEdge={from: string;to: string};consthashEdge=(e: Edge)=>hashTuple(e.from,e.to);constequalEdges=(a: Edge,b: Edge)=>(a.from===b.from&&a.to===b.to);// Create a map with required hash and equality functions for keysconstmap=newHashMap<Edge,number>(hashEdge,equalEdges);// Set values for keysmap.set({from: 'A',to: 'B'},10);map.set({from: 'C',to: 'D'},20);// Update value for an equal key with a different object identitymap.set({from: 'A',to: 'B'},100);// Get an existing value by the equal key with a different identityconstvalue=map.get({from: 'A',to: 'B'});// Iterate over entries in the original insertion orderfor(const[key,value]ofmap){console.log('Map entry: ',key,value);}// Clone the map with the same hash and equality functionsconstclonedMap=map.clone();// Delete entry for an equal key with a different object identitymap.delete({from: 'A',to: 'B'});// Clear the mapmap.clear();The library has the following exports:
Fully compatible with built-in Map with a different constructor and an additional method.
The HashMap keeps the entries in the original insertion order the same way as the built-in Map does.
Constructs a new hash map with the specified hash and equality functions for keys.
Parameters:
hash: (k: Key) => numberequals: (k1, k2) => booleanentries?: Iterable<readonly [K, V]>
Returns a copy of the map with the same entries, hash and equality functions for the keys.
Fully compatible with built-in Set with a different constructor and an additional method.
The HashSet keeps the entries in the original insertion order the same way as the built-in Set does.
Constructs a new hash set with the specified hash and equality functions for items.
Parameters:
hash: (v: T) => numberequals: (v1, v2) => booleanitems?: Iterable<T>
Returns a copy of the set with the same items, hash and equality functions for the items.
TypeScript interface for a read-only version of a HashMap which is compatible with ReadonlyMap.
TypeScript interface for a read-only version of a HashSet which is compatible with ReadonlySet.
Utility function to compute a string hash using FNV-32a algorithm.
Default seed is 0x811c9dc5.
Utility function to compute a hash for a JS number:
- for 32-bit integers the hash is the number itself;
- otherwise the hash is
Math.imul(31, H) + LwhereHandLare 32-bit parts of IEEE floating point representation of the number.
Utility function to compute a hash for a bigint value with the following formula: abs(N) % 0x8000_0000 where N is the bigint value.
Utility function to compute a hash for a primitive JS value:
- for
stringthehashString(value)is used; - for
numberthehashNumber(value)is used; - for
bigintthehashBigInt(value)is used; - for
nullthe hash is0; - for
undefinedthe hash is1; - for
booleanthe hash is3forfalseand4fortrue; - otherwise the hash is
0.
Utility function to "chain" hash values when computing a hash of a composite value, for example:
lethash=0;hash=chainHash(hash,hashValue(key.fieldA));hash=chainHash(hash,hashValue(key.fieldB));hash=chainHash(hash,hashValue(key.fieldC));The formula for chained hash is (Math.imul(hash, 31) + added) | 0.
Utility function to compute hash for a tuple of primitive values, equivalent to subsequent calls to chainHash() with hashValue() for each argument with the initial hash value of a hashed tuple length:
consthash=hashTuple(key.fieldA,key.fieldB,key.fieldC);// same aslethash=hashNumber(3);hash=chainHash(hash,hashValue(key.fieldA));hash=chainHash(hash,hashValue(key.fieldB));hash=chainHash(hash,hashValue(key.fieldC));Utility function which coerces a number value into an integer such that "small integer optimization" (Smi) in V8 engine can be applied.
Passing the value from this function to return a computed hash may improve performance in some specific situations.
The library is distributed under MIT license, see LICENSE.