/**
 * Doubly linked list backed by a single pool of nodes.
 *
 * Every node occupies 4 consecutive ints (16 bytes) of one int[] pool,
 * so a node never straddles a cache line and following a link plus reading
 * the value touches a single line:
 *
 *   a[4*i + 0]  next index
 *   a[4*i + 1]  prev index
 *   a[4*i + 2]  low  32 bits of the value
 *   a[4*i + 3]  high 32 bits of the value
 *
 * Node 0 is a sentinel: its next is the first node, its prev is the last
 * node, and the list is circular through it. Because the sentinel has index
 * 0, "next of the last node" and "prev of the first node" are naturally 0,
 * which is exactly the "no node" handle. A handle is simply the node's pool
 * index (always >= 1).
 *
 * Removed nodes go onto an intrusive free list threaded through their next
 * field and are reused before the pool's bump pointer advances. The pool
 * doubles when full (amortised O(1)); nodes keep their indices on growth.
 */
public final class DList {
    private static final int MAX_NODES = (Integer.MAX_VALUE - 16) >>> 2;

    private int[] a;
    private int top;   // next never-used node index
    private int free;  // head of free list (0 = empty)
    private int size;

    public DList() {
        a = new int[64 << 2];
        top = 1; // node 0 is the sentinel; its links are 0 (empty list)
    }

    private int grow() {
        int[] old = a;
        int cap = old.length >>> 2;
        if (cap >= MAX_NODES) throw new OutOfMemoryError("DList pool full");
        int ncap = cap > MAX_NODES / 2 ? MAX_NODES : cap << 1;
        a = java.util.Arrays.copyOf(old, ncap << 2);
        return top++;
    }

    private int alloc() {
        int f = free;
        if (f != 0) {
            free = a[f << 2];
            return f;
        }
        int t = top;
        if ((t << 2) >= a.length) return grow();
        top = t + 1;
        return t;
    }

    /** Link a new node holding v between p and n (n == next of p). */
    private long link(int p, int n, long v) {
        int idx = alloc();
        int[] a = this.a;
        int o = idx << 2;
        a[o] = n;
        a[o + 1] = p;
        a[o + 2] = (int) v;
        a[o + 3] = (int) (v >>> 32);
        a[p << 2] = idx;
        a[(n << 2) + 1] = idx;
        size++;
        return idx;
    }

    public long pushFront(long value) {
        return link(0, a[0], value);
    }

    public long pushBack(long value) {
        return link(a[1], 0, value);
    }

    public long insertBefore(long node, long value) {
        int x = (int) node;
        return link(a[(x << 2) + 1], x, value);
    }

    public long insertAfter(long node, long value) {
        int x = (int) node;
        return link(x, a[x << 2], value);
    }

    private long unlink(int x) {
        int[] a = this.a;
        int o = x << 2;
        int n = a[o];
        int p = a[o + 1];
        a[p << 2] = n;
        a[(n << 2) + 1] = p;
        long v = (a[o + 2] & 0xFFFFFFFFL) | ((long) a[o + 3] << 32);
        a[o] = free;
        free = x;
        size--;
        return v;
    }

    public long remove(long node) {
        return unlink((int) node);
    }

    public long popFront(long missing) {
        int x = a[0];
        return x == 0 ? missing : unlink(x);
    }

    public long popBack(long missing) {
        int x = a[1];
        return x == 0 ? missing : unlink(x);
    }

    public void moveToFront(long node) {
        int x = (int) node;
        int[] a = this.a;
        int first = a[0];
        if (first == x) return;
        int o = x << 2;
        int n = a[o];
        int p = a[o + 1];
        a[p << 2] = n;
        a[(n << 2) + 1] = p;
        a[o] = first;
        a[o + 1] = 0;
        a[(first << 2) + 1] = x;
        a[0] = x;
    }

    public long front() {
        return a[0];
    }

    public long back() {
        return a[1];
    }

    public long next(long node) {
        return a[((int) node) << 2];
    }

    public long prev(long node) {
        return a[(((int) node) << 2) + 1];
    }

    public long get(long node) {
        int o = ((int) node) << 2;
        int[] a = this.a;
        return (a[o + 2] & 0xFFFFFFFFL) | ((long) a[o + 3] << 32);
    }

    public int size() {
        return size;
    }
}
