// Simple universal hashing family over 32-bit integers using 53-bit JS numbers
// h_{a,b}(x) = ((a*x + b) mod P) mod m, with P large prime, a in [1..P-1], b in [0..P-1]
function makeUniversalHash(m, seedA, seedB, P = 4294967311n) { // 2^32 prime near
const a = BigInt(seedA)||1n, b = BigInt(seedB)||0n, bigM = BigInt(m);
return (x) => Number((a * BigInt(x >>> 0) + b) % P % bigM);
}
- Separate chaining/linear probing: expected O(1) ops with good load α and universal hashing.
- Two-choice hashing: choose min-load of two bins to reduce max load to O(log log n).
Bloom Filter
// Bloom filter with k hash functions over m bits
class Bloom {
constructor(m, k, seeds) { this.m=m; this.k=k; this.bits=new Uint8Array(m); this.h=[]; for(let i=0;iint hash (FNV-1a like)
const s = String(x); let h=2166136261>>>0; for(let i=0;i>>0;
}
- FP rate: ≈ (1 - e^{-kn/m})^k. Optimal k ≈ (m/n) ln 2.
- Notes: no false negatives; deletions require counting Bloom filters.