MinHash sketch — same API as datasketch.MinHash for the subset used here.
| 34 | |
| 35 | |
| 36 | class MinHash: |
| 37 | """MinHash sketch — same API as datasketch.MinHash for the subset used here.""" |
| 38 | |
| 39 | __slots__ = ("num_perm", "hashvalues", "_a", "_b") |
| 40 | |
| 41 | def __init__(self, num_perm: int = 128) -> None: |
| 42 | self.num_perm = num_perm |
| 43 | self.hashvalues = np.full(num_perm, int(_MH), dtype=np.uint64) |
| 44 | self._a, self._b = _mh_coeffs(num_perm) |
| 45 | |
| 46 | def update(self, v: bytes) -> None: |
| 47 | hv = np.uint64(struct.unpack("<I", hashlib.sha1(v).digest()[:4])[0]) |
| 48 | phv = np.bitwise_and((self._a * hv + self._b) % _MP, _MH) |
| 49 | self.hashvalues = np.minimum(self.hashvalues, phv) |
| 50 | |
| 51 | |
| 52 | def _lsh_integrate(f, lo: float, hi: float, n: int = 128) -> float: |
no outgoing calls