Universal hash function
Stability: 1 - Experimental
Universal hash function.
npm install universal-hash-function
If a malicious adversary chooses the keys to be hashed by some fixed hash function, then the adversary can choose n keys that all hash to the same slot, yielding an average retrieval time of O(n). Any fixed hash function is vulnerable to such terrible worst-case behavior; the only effective way to improve the situation is to choose the hash function randomly in a way that is independent of the keys that are actually going to be stored. This approach, called universal hashing, can yield provably good performance on average, no matter which keys the adversary chooses. http://mitpress.mit.edu/books/introduction-algorithms
someKey are all Integers.
var uhf = require'universal-hash-function';// prime should be such that the greatest integer value of someKey should be// less than prime// uhf(prime, numberOfHashSlots);var hashFunc1 = uhf17 6;var hashFunc2 = uhf17 6;var hashFunc3 = uhf17 6;// hashFunc(someKey)hashFunc10; // -> 2hashFunc20; // -> 1hashFunc30; // -> 5hashFunc11; // -> 3hashFunc21; // -> 5hashFunc31; // -> 1hashFunc12; // -> 5hashFunc22; // -> 2hashFunc32; // -> 3// etc...