/**
 * Open-addressing hash map (long -> long) with linear probing and
 * backward-shift deletion.
 *
 * Layout: one interleaved long[] of 2*capacity elements; slot s holds its key
 * at tab[2s] and its value at tab[2s+1], so a probe touches one cache line.
 * Key 0 marks an empty slot. The real key 0, when present, occupies a slot
 * like any other entry; its position is remembered in zeroPos so that slot
 * is not mistaken for an empty one.
 */
public final class HashMap {
    private static final long MUL = 0x9E3779B97F4A7C15L;
    private static final int INITIAL_CAPACITY = 16;

    private long[] tab;   // interleaved key/value pairs
    private int mask2;    // 2*capacity - 1 (positions are even indices)
    private int shift;    // 64 - log2(capacity)
    private int size;
    private int maxSize;  // capacity * 7 / 8
    private int zeroPos;  // index in tab of the key-0 entry, or -1

    public HashMap() {
        tab = new long[INITIAL_CAPACITY * 2];
        mask2 = INITIAL_CAPACITY * 2 - 1;
        shift = 64 - Integer.numberOfTrailingZeros(INITIAL_CAPACITY);
        maxSize = INITIAL_CAPACITY / 8 * 7;
        zeroPos = -1;
    }

    private static int homePos(long key, int shift) {
        return ((int) ((key * MUL) >>> shift)) << 1;
    }

    public boolean insert(long key, long value) {
        if (key == 0) return insertZero(value);
        long[] t = tab;
        int m = mask2;
        int p = homePos(key, shift);
        long k;
        while ((k = t[p]) != key) {
            if (k == 0 && p != zeroPos) {
                // empty slot: key is absent
                if (size >= maxSize) {
                    grow();
                    t = tab;
                    m = mask2;
                    p = homePos(key, shift);
                    int z = zeroPos;
                    while (t[p] != 0 || p == z) p = (p + 2) & m;
                }
                t[p] = key;
                t[p + 1] = value;
                size++;
                return true;
            }
            p = (p + 2) & m;
        }
        t[p + 1] = value;
        return false;
    }

    private boolean insertZero(long value) {
        if (zeroPos >= 0) {
            tab[zeroPos + 1] = value;
            return false;
        }
        if (size >= maxSize) grow();
        long[] t = tab;
        int m = mask2;
        int p = 0; // home slot of key 0 is always 0
        while (t[p] != 0) p = (p + 2) & m;
        t[p + 1] = value;
        zeroPos = p;
        size++;
        return true;
    }

    public long get(long key, long missing) {
        if (key == 0) {
            int z = zeroPos;
            return z >= 0 ? tab[z + 1] : missing;
        }
        long[] t = tab;
        int m = mask2;
        int p = homePos(key, shift);
        long k;
        while ((k = t[p]) != key) {
            if (k == 0 && p != zeroPos) return missing;
            p = (p + 2) & m;
        }
        return t[p + 1];
    }

    public boolean remove(long key) {
        long[] t = tab;
        int m = mask2;
        int p;
        if (key == 0) {
            p = zeroPos;
            if (p < 0) return false;
            zeroPos = -1;
        } else {
            p = homePos(key, shift);
            long k;
            while ((k = t[p]) != key) {
                if (k == 0 && p != zeroPos) return false;
                p = (p + 2) & m;
            }
        }
        size--;
        // Backward-shift deletion: p is the hole.
        int sh = shift;
        int j = p;
        for (;;) {
            j = (j + 2) & m;
            long k = t[j];
            if (k == 0) {
                if (j != zeroPos) break;
                // key 0 entry: home slot is 0
                if (((j - 0) & m) >= ((j - p) & m)) {
                    t[p] = 0;
                    t[p + 1] = t[j + 1];
                    zeroPos = p;
                    p = j;
                }
                continue;
            }
            int h = homePos(k, sh);
            if (((j - h) & m) >= ((j - p) & m)) {
                t[p] = k;
                t[p + 1] = t[j + 1];
                p = j;
            }
        }
        t[p] = 0;
        t[p + 1] = 0;
        return true;
    }

    private void grow() {
        long[] old = tab;
        int oldLen = old.length;
        int newCap = oldLen; // old capacity * 2 == oldLen
        long[] t = new long[newCap * 2];
        int m = newCap * 2 - 1;
        int sh = shift - 1;
        int oldZero = zeroPos;
        int newZero = -1;
        for (int i = 0; i < oldLen; i += 2) {
            long k = old[i];
            if (k != 0) {
                int p = homePos(k, sh);
                while (t[p] != 0 || p == newZero) p = (p + 2) & m;
                t[p] = k;
                t[p + 1] = old[i + 1];
            } else if (i == oldZero) {
                int p = 0;
                while (t[p] != 0) p = (p + 2) & m;
                t[p + 1] = old[i + 1];
                newZero = p;
            }
        }
        tab = t;
        mask2 = m;
        shift = sh;
        maxSize = newCap / 8 * 7;
        zeroPos = newZero;
    }

    public int scan(long[] outKeys, long[] outVals) {
        long[] t = tab;
        int z = zeroPos;
        int target = size - (z >= 0 ? 1 : 0);
        int c = 0;
        if (target > 0) {
            int len = t.length;
            for (int i = 0; i < len; i += 2) {
                long k = t[i];
                outKeys[c] = k;
                outVals[c] = t[i + 1];
                c += (k != 0) ? 1 : 0;
                if (c == target) break;
            }
        }
        if (z >= 0) {
            outKeys[c] = 0;
            outVals[c] = t[z + 1];
            c++;
        }
        return c;
    }

    public int size() {
        return size;
    }

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