public final class HashMap {
    private static final long MULT = 0x9E3779B97F4A7C15L;
    private static final int INITIAL_CAPACITY = 16;

    // Flat interleaved table of (key, value) pairs: slot i lives at
    // table[2 * i] (key) and table[2 * i + 1] (value).
    // Empty slots have key == 0L, except when key 0L itself is present,
    // in which case its slot offset (2 * slotIndex) is stored in zeroPos
    // and its value lives at table[zeroPos + 1].
    private long[] table;
    private int shift;
    private int size;
    private int maxSize;
    private int zeroPos;

    public HashMap() {
        this.table = new long[INITIAL_CAPACITY << 1];
        this.shift = 60; // 64 - 4, for capacity = 16 = 2^4
        this.size = 0;
        this.maxSize = 14; // 16 * 7 / 8
        this.zeroPos = -1;
    }

    public boolean insert(long key, long value) {
        if (key == 0L) {
            return insertZero(value);
        }
        long[] tab = this.table;
        int pos = ((int) ((key * MULT) >>> this.shift)) << 1;
        long k = tab[pos];
        if (k == key) {
            tab[pos + 1] = value;
            return false;
        }
        if (k != 0L || pos == this.zeroPos) {
            int mask = tab.length - 1;
            for (;;) {
                pos = (pos + 2) & mask;
                k = tab[pos];
                if (k == key) {
                    tab[pos + 1] = value;
                    return false;
                }
                if (k == 0L && pos != this.zeroPos) {
                    break;
                }
            }
        }
        tab[pos] = key;
        tab[pos + 1] = value;
        if (++this.size > this.maxSize) {
            grow();
        }
        return true;
    }

    private boolean insertZero(long value) {
        long[] tab = this.table;
        int zp = this.zeroPos;
        if (zp >= 0) {
            tab[zp + 1] = value;
            return false;
        }
        // home(0L) == 0, and at least capacity/8 >= 2 slots are empty.
        int pos = 0;
        while (tab[pos] != 0L) {
            pos += 2;
        }
        tab[pos + 1] = value;
        this.zeroPos = pos;
        if (++this.size > this.maxSize) {
            grow();
        }
        return true;
    }

    public long get(long key, long missing) {
        if (key == 0L) {
            int zp = this.zeroPos;
            return zp >= 0 ? this.table[zp + 1] : missing;
        }
        long[] tab = this.table;
        int pos = ((int) ((key * MULT) >>> this.shift)) << 1;
        long k = tab[pos];
        if (k == key) {
            return tab[pos + 1];
        }
        if (k == 0L && pos != this.zeroPos) {
            return missing;
        }
        int mask = tab.length - 1;
        for (;;) {
            pos = (pos + 2) & mask;
            k = tab[pos];
            if (k == key) {
                return tab[pos + 1];
            }
            if (k == 0L && pos != this.zeroPos) {
                return missing;
            }
        }
    }

    public boolean remove(long key) {
        if (key == 0L) {
            return removeZero();
        }
        long[] tab = this.table;
        int sh = this.shift;
        int pos = ((int) ((key * MULT) >>> sh)) << 1;
        long k = tab[pos];
        if (k != key) {
            if (k == 0L && pos != this.zeroPos) {
                return false;
            }
            int mask = tab.length - 1;
            for (;;) {
                pos = (pos + 2) & mask;
                k = tab[pos];
                if (k == key) {
                    break;
                }
                if (k == 0L && pos != this.zeroPos) {
                    return false;
                }
            }
        }
        this.size--;
        int mask = tab.length - 1;
        int next = (pos + 2) & mask;
        long nk = tab[next];
        if (nk == 0L && next != this.zeroPos) {
            tab[pos] = 0L;
            return true;
        }
        shiftDelete(tab, pos, next, nk, mask, sh);
        return true;
    }

    private boolean removeZero() {
        int hole = this.zeroPos;
        if (hole < 0) {
            return false;
        }
        this.zeroPos = -1;
        this.size--;
        long[] tab = this.table;
        int mask = tab.length - 1;
        int next = (hole + 2) & mask;
        long nk = tab[next];
        if (nk == 0L) {
            return true;
        }
        shiftDelete(tab, hole, next, nk, mask, this.shift);
        return true;
    }

    private void shiftDelete(long[] tab, int hole, int curr, long k, int mask, int sh) {
        int zp = this.zeroPos;
        for (;;) {
            if (k == 0L) {
                if (curr != zp) {
                    tab[hole] = 0L;
                    return;
                }
                // Key 0L has homePos == 0; hole lies in [0, curr) iff hole < curr.
                if (hole < curr) {
                    tab[hole] = 0L;
                    tab[hole + 1] = tab[curr + 1];
                    zp = hole;
                    this.zeroPos = hole;
                    hole = curr;
                }
            } else {
                int home = ((int) ((k * MULT) >>> sh)) << 1;
                if (((hole - home) & mask) < ((curr - home) & mask)) {
                    tab[hole] = k;
                    tab[hole + 1] = tab[curr + 1];
                    hole = curr;
                }
            }
            curr = (curr + 2) & mask;
            k = tab[curr];
        }
    }

    private void grow() {
        long[] oldTab = this.table;
        int oldLen = oldTab.length;
        int newLen = oldLen << 1;
        long[] newTab = new long[newLen];
        int newShift = this.shift - 1;
        int newMask = newLen - 1;

        for (int i = 0; i < oldLen; i += 2) {
            long k = oldTab[i];
            if (k != 0L) {
                int pos = ((int) ((k * MULT) >>> newShift)) << 1;
                while (newTab[pos] != 0L) {
                    pos = (pos + 2) & newMask;
                }
                newTab[pos] = k;
                newTab[pos + 1] = oldTab[i + 1];
            }
        }

        int oldZp = this.zeroPos;
        if (oldZp >= 0) {
            int pos = 0;
            while (newTab[pos] != 0L) {
                pos += 2;
            }
            newTab[pos + 1] = oldTab[oldZp + 1];
            this.zeroPos = pos;
        }

        this.table = newTab;
        this.shift = newShift;
        int newCap = newLen >>> 1;
        this.maxSize = newCap - (newCap >>> 3);
    }

    public int scan(long[] outKeys, long[] outVals) {
        int sz = this.size;
        if (sz == 0) {
            return 0;
        }
        if (outKeys.length < sz || outVals.length < sz) {
            throw new IndexOutOfBoundsException();
        }
        long[] tab = this.table;
        int out = 0;
        int zp = this.zeroPos;
        if (zp >= 0) {
            outKeys[0] = 0L;
            outVals[0] = tab[zp + 1];
            out = 1;
            if (out == sz) {
                return sz;
            }
        }
        int len = tab.length;
        int limit = len - 8;
        int outLimit = sz - 4;
        int i = 0;
        for (; i <= limit && out <= outLimit; i += 8) {
            long k0 = tab[i];
            long k1 = tab[i + 2];
            long k2 = tab[i + 4];
            long k3 = tab[i + 6];
            int b0 = (int) ((k0 | -k0) >>> 63);
            int b1 = (int) ((k1 | -k1) >>> 63);
            int b2 = (int) ((k2 | -k2) >>> 63);
            int b3 = (int) ((k3 | -k3) >>> 63);
            int o1 = out + b0;
            int o2 = o1 + b1;
            int o3 = o2 + b2;
            outKeys[out] = k0;
            outVals[out] = tab[i + 1];
            outKeys[o1] = k1;
            outVals[o1] = tab[i + 3];
            outKeys[o2] = k2;
            outVals[o2] = tab[i + 5];
            outKeys[o3] = k3;
            outVals[o3] = tab[i + 7];
            out = (out + (b0 + b1)) + (b2 + b3);
        }
        for (; i < len && out < sz; i += 2) {
            long k = tab[i];
            outKeys[out] = k;
            outVals[out] = tab[i + 1];
            out += (int) ((k | -k) >>> 63);
        }
        return out;
    }

    public int size() {
        return this.size;
    }

    public int capacity() {
        return this.table.length >>> 1;
    }
}
