"""Open-addressing hash map with linear probing (pure Python).

Layout: one flat Python list `_t` of 2*capacity references. Slot s occupies
_t[2*s] (key) and _t[2*s+1] (value), so a key and its value share a cache
line. An empty slot holds None in both positions; since the list stores
Python objects, None is not an i64, so no key value has to be reserved.

Hashing: home(k) = ((k * 0x9E3779B97F4A7C15) mod 2^64) >> (64 - b). The list
index of the home slot, 2*home(k), is computed as
    (k * C >> (63 - b)) & ((cap - 1) << 1)
which extracts the same top b bits of the low 64 bits of the product (Python's
>> and & behave as infinite two's complement, so negative keys work too).

Deletion uses backward shifting, so there are no tombstones.
"""

from itertools import compress

__all__ = ["HashMap"]


class HashMap:
    __slots__ = ("_t", "_mask", "_sh", "_n", "_cap", "_limit")

    def __init__(self) -> None:
        self._cap = 16
        self._t = [None] * 32
        self._mask = 30          # (cap - 1) << 1
        self._sh = 59            # 63 - b, b = 4
        self._n = 0
        self._limit = 14         # cap * 7 // 8

    def get(self, key):
        t = self._t
        m = self._mask
        i = (key * 0x9E3779B97F4A7C15 >> self._sh) & m
        k = t[i]
        while k is not None:
            if k == key:
                return t[i + 1]
            i = (i + 2) & m
            k = t[i]
        return None

    def insert(self, key, value):
        t = self._t
        m = self._mask
        i = (key * 0x9E3779B97F4A7C15 >> self._sh) & m
        k = t[i]
        while k is not None:
            if k == key:
                t[i + 1] = value
                return False
            i = (i + 2) & m
            k = t[i]
        t[i] = key
        t[i + 1] = value
        n = self._n + 1
        self._n = n
        if n > self._limit:
            self._grow()
        return True

    def remove(self, key):
        t = self._t
        m = self._mask
        sh = self._sh
        i = (key * 0x9E3779B97F4A7C15 >> sh) & m
        k = t[i]
        while k is not None:
            if k == key:
                break
            i = (i + 2) & m
            k = t[i]
        else:
            return False
        # Backward-shift deletion: i is the hole. Walk the rest of the
        # cluster; move back any entry whose home is not in (i, j].
        j = (i + 2) & m
        k = t[j]
        while k is not None:
            h = (k * 0x9E3779B97F4A7C15 >> sh) & m
            if ((j - h) & m) >= ((j - i) & m):
                t[i] = k
                t[i + 1] = t[j + 1]
                i = j
            j = (j + 2) & m
            k = t[j]
        t[i] = None
        t[i + 1] = None
        self._n -= 1
        return True

    def _grow(self):
        old = self._t
        cap = self._cap << 1
        b = cap.bit_length() - 1
        m = (cap - 1) << 1
        sh = 63 - b
        t = [None] * (cap << 1)
        for k, v in zip(old[0::2], old[1::2]):
            if k is None:
                continue
            i = (k * 0x9E3779B97F4A7C15 >> sh) & m
            while t[i] is not None:
                i = (i + 2) & m
            t[i] = k
            t[i + 1] = v
        self._t = t
        self._cap = cap
        self._mask = m
        self._sh = sh
        self._limit = cap * 7 // 8

    def scan(self):
        t = self._t
        ks = t[0::2]
        # Occupied slots have an int key; empty slots have None. Both None
        # and the key 0 are falsy, so key 0 is filtered out here and added
        # back below from its own slot.
        vs = list(compress(t[1::2], ks))
        ks = list(filter(None, ks))
        v0 = self.get(0)
        if v0 is not None:
            ks.append(0)
            vs.append(v0)
        return ks, vs

    def __len__(self):
        return self._n

    def capacity(self):
        return self._cap
