// Randomized skip list (Pugh, 1990) mapping long keys to long values.
//
// Memory layout: every node (and the header) lives in one long[] arena.
// A node of height h starting at offset o occupies h + 2 consecutive slots:
//   a[o]         key
//   a[o + 1]     value
//   a[o + 2 + i] forward link at level i (arena offset of the next node, 0 = null)
// The header sits at offset 0 with MAX_LEVEL links; it is never the target of
// a link, so offset 0 doubles as the null link. Key and links of a node are
// contiguous, so visiting a node during a search usually costs one cache miss.
//
// Removed nodes go onto a free list per height (chained through the key slot)
// and are reused by later inserts of the same height, so the arena stays
// proportional to the largest number of entries the list has held.
public final class SkipList {
    // p = 1/4, maximum height 16: 4^16 = 2^32.
    private static final int MAX_LEVEL = 16;
    private static final int HEADER_SIZE = 2 + MAX_LEVEL;

    private long[] a;
    private int top;          // first unused arena slot
    private int level;        // number of levels in use (height of the tallest node)
    private int size;
    private long seed;
    private final int[] upd = new int[MAX_LEVEL];
    private final int[] freeHead = new int[MAX_LEVEL + 1];

    public SkipList() {
        a = new long[1 << 12];
        top = HEADER_SIZE;
        seed = 0x2545F4914F6CDD1DL;
    }

    // splitmix64 step; height = 1 + floor(trailingZeros / 2), capped.
    private int randomHeight() {
        long z = (seed += 0x9E3779B97F4A7C15L);
        z = (z ^ (z >>> 30)) * 0xBF58476D1CE4E5B9L;
        z = (z ^ (z >>> 27)) * 0x94D049BB133111EBL;
        z ^= z >>> 31;
        int h = (Long.numberOfTrailingZeros(z) >>> 1) + 1;
        return h > MAX_LEVEL ? MAX_LEVEL : h;
    }

    private int allocNode(int h) {
        int o = freeHead[h];
        if (o != 0) {
            freeHead[h] = (int) a[o];
            return o;
        }
        o = top;
        int need = o + 2 + h;
        if (need > a.length) {
            long[] old = a;
            int cap = old.length;
            while (cap < need) cap <<= 1;
            long[] na = new long[cap];
            System.arraycopy(old, 0, na, 0, o);
            a = na;
        }
        top = need;
        return o;
    }

    public boolean insert(long key, long value) {
        long[] a = this.a;
        int[] upd = this.upd;
        int x = 0;
        for (int i = level - 1; i >= 0; i--) {
            int nx;
            while ((nx = (int) a[x + 2 + i]) != 0 && a[nx] < key) x = nx;
            upd[i] = x;
        }
        int nx = (int) a[x + 2];
        if (nx != 0 && a[nx] == key) {
            a[nx + 1] = value;
            return false;
        }
        int h = randomHeight();
        if (h > level) {
            for (int i = level; i < h; i++) upd[i] = 0;
            level = h;
        }
        int o = allocNode(h);
        a = this.a;
        a[o] = key;
        a[o + 1] = value;
        for (int i = 0; i < h; i++) {
            int p = upd[i] + 2 + i;
            a[o + 2 + i] = a[p];
            a[p] = o;
        }
        size++;
        return true;
    }

    public long get(long key, long missing) {
        long[] a = this.a;
        int x = 0;
        for (int i = level - 1; i >= 0; i--) {
            int nx;
            while ((nx = (int) a[x + 2 + i]) != 0 && a[nx] < key) x = nx;
        }
        int nx = (int) a[x + 2];
        if (nx != 0 && a[nx] == key) return a[nx + 1];
        return missing;
    }

    public boolean remove(long key) {
        long[] a = this.a;
        int[] upd = this.upd;
        int x = 0;
        int lvl = level;
        for (int i = lvl - 1; i >= 0; i--) {
            int nx;
            while ((nx = (int) a[x + 2 + i]) != 0 && a[nx] < key) x = nx;
            upd[i] = x;
        }
        int o = (int) a[x + 2];
        if (o == 0 || a[o] != key) return false;
        // The node appears exactly at levels 0..h-1, where its predecessor links to it.
        int h = 0;
        while (h < lvl) {
            int p = upd[h] + 2 + h;
            if ((int) a[p] != o) break;
            a[p] = a[o + 2 + h];
            h++;
        }
        while (lvl > 0 && a[2 + lvl - 1] == 0) lvl--;
        level = lvl;
        a[o] = freeHead[h];
        freeHead[h] = o;
        size--;
        return true;
    }

    public int range(long lo, long hi, long[] outKeys, long[] outVals, int limit) {
        if (lo > hi || limit <= 0) return 0;
        long[] a = this.a;
        int x = 0;
        for (int i = level - 1; i >= 0; i--) {
            int nx;
            while ((nx = (int) a[x + 2 + i]) != 0 && a[nx] < lo) x = nx;
        }
        x = (int) a[x + 2];
        int c = 0;
        while (x != 0 && c < limit) {
            long k = a[x];
            if (k > hi) break;
            outKeys[c] = k;
            outVals[c] = a[x + 1];
            c++;
            x = (int) a[x + 2];
        }
        return c;
    }

    public int size() {
        return size;
    }

    public int height() {
        return level;
    }

    public int levelCount(int lvl) {
        if (lvl < 0 || lvl >= level) return 0;
        long[] a = this.a;
        int c = 0;
        int x = (int) a[2 + lvl];
        while (x != 0) {
            c++;
            x = (int) a[x + 2 + lvl];
        }
        return c;
    }
}
