/*
 * Doubly linked list over a single growable node pool.
 *
 * Node: { int64 value; uint32 next; uint32 prev; } = 16 bytes, so eight nodes
 * share a 128-byte cache line.
 *
 * Index 0 of the pool is a sentinel that closes the list into a ring:
 * sentinel.next is the first node, sentinel.prev the last, and the first
 * node's prev / last node's next is 0. A handle is simply the node's pool
 * index, which is never 0 (that is the sentinel), so next()/prev()/front()/
 * back() are a single load with no branches, and "0 = no node" falls out.
 *
 * Removed nodes are pushed onto a LIFO free list threaded through their
 * `next` field and are reused before the pool's bump pointer advances. The
 * pool grows by doubling (realloc), which keeps indices, and therefore
 * handles, valid.
 */
#include "dlist.h"

#include <stdlib.h>

typedef struct node {
    int64_t value;
    uint32_t next;
    uint32_t prev;
} node;

struct dlist {
    node *nodes;     /* pool; nodes[0] is the sentinel */
    uint32_t free;   /* head of free list, 0 = empty */
    uint32_t top;    /* first never-used index */
    uint32_t cap;    /* capacity of the pool in nodes */
    size_t len;
};

#define INITIAL_CAP 64u
#define MAX_CAP 0xFFFFFFFFu

dlist *dlist_new(void) {
    dlist *l = malloc(sizeof *l);
    if (!l) abort();
    l->nodes = malloc((size_t)INITIAL_CAP * sizeof(node));
    if (!l->nodes) abort();
    l->nodes[0].value = 0;
    l->nodes[0].next = 0;
    l->nodes[0].prev = 0;
    l->free = 0;
    l->top = 1;
    l->cap = INITIAL_CAP;
    l->len = 0;
    return l;
}

void dlist_free(dlist *l) {
    if (!l) return;
    free(l->nodes);
    free(l);
}

__attribute__((noinline, cold)) static void grow(dlist *l) {
    if (l->cap == MAX_CAP) abort();
    uint64_t nc = (uint64_t)l->cap * 2;
    if (nc > MAX_CAP) nc = MAX_CAP;
    node *p = realloc(l->nodes, (size_t)nc * sizeof(node));
    if (!p) abort();
    l->nodes = p;
    l->cap = (uint32_t)nc;
}

static inline __attribute__((always_inline)) uint32_t alloc_node(dlist *l) {
    uint32_t i = l->free;
    if (i != 0) {
        l->free = l->nodes[i].next;
        return i;
    }
    if (__builtin_expect(l->top == l->cap, 0)) grow(l);
    return l->top++;
}

/* Link a new node with `value` between p and x (p.next == x). */
static inline __attribute__((always_inline)) uint64_t link_between(dlist *l, uint32_t p, uint32_t x,
                                                                   int64_t value, uint32_t i) {
    node *N = l->nodes;
    N[i].value = value;
    N[i].next = x;
    N[i].prev = p;
    N[p].next = i;
    N[x].prev = i;
    l->len++;
    return i;
}

uint64_t dlist_push_front(dlist *l, int64_t value) {
    uint32_t i = alloc_node(l);
    return link_between(l, 0, l->nodes[0].next, value, i);
}

uint64_t dlist_push_back(dlist *l, int64_t value) {
    uint32_t i = alloc_node(l);
    return link_between(l, l->nodes[0].prev, 0, value, i);
}

uint64_t dlist_insert_before(dlist *l, uint64_t h, int64_t value) {
    uint32_t i = alloc_node(l);
    uint32_t x = (uint32_t)h;
    return link_between(l, l->nodes[x].prev, x, value, i);
}

uint64_t dlist_insert_after(dlist *l, uint64_t h, int64_t value) {
    uint32_t i = alloc_node(l);
    uint32_t p = (uint32_t)h;
    return link_between(l, p, l->nodes[p].next, value, i);
}

static inline __attribute__((always_inline)) int64_t unlink_node(dlist *l, uint32_t i) {
    node *N = l->nodes;
    node *n = &N[i];
    uint32_t p = n->prev, x = n->next;
    int64_t v = n->value;
    N[p].next = x;
    N[x].prev = p;
    n->next = l->free;
    l->free = i;
    l->len--;
    return v;
}

int64_t dlist_remove(dlist *l, uint64_t h) {
    return unlink_node(l, (uint32_t)h);
}

bool dlist_pop_front(dlist *l, int64_t *value) {
    uint32_t i = l->nodes[0].next;
    if (i == 0) return false;
    *value = unlink_node(l, i);
    return true;
}

bool dlist_pop_back(dlist *l, int64_t *value) {
    uint32_t i = l->nodes[0].prev;
    if (i == 0) return false;
    *value = unlink_node(l, i);
    return true;
}

void dlist_move_to_front(dlist *l, uint64_t h) {
    node *N = l->nodes;
    uint32_t i = (uint32_t)h;
    uint32_t f = N[0].next;
    if (f == i) return;
    node *n = &N[i];
    uint32_t p = n->prev, x = n->next;
    N[p].next = x;
    N[x].prev = p;
    n->next = f;
    n->prev = 0;
    N[f].prev = i;
    N[0].next = i;
}

uint64_t dlist_front(const dlist *l) { return l->nodes[0].next; }
uint64_t dlist_back(const dlist *l) { return l->nodes[0].prev; }
uint64_t dlist_next(const dlist *l, uint64_t h) { return l->nodes[(uint32_t)h].next; }
uint64_t dlist_prev(const dlist *l, uint64_t h) { return l->nodes[(uint32_t)h].prev; }
int64_t dlist_get(const dlist *l, uint64_t h) { return l->nodes[(uint32_t)h].value; }
size_t dlist_len(const dlist *l) { return l->len; }
