#include "dlist.h"

#include <stdlib.h>

/*
 * Node layout: exactly 16 bytes (8 nodes per 128-byte M3 cache line, never
 * straddling a cache-line boundary when the pool is 16-byte aligned).
 *
 * Links and handles are 32-bit byte offsets into the contiguous node pool
 * (multiples of 16). Offset 0 is the circular sentinel node:
 *   sentinel.next == front handle (or 0 if empty)
 *   sentinel.prev == back handle  (or 0 if empty)
 * Because offset 0 is both the sentinel and the "no node" handle, traversal,
 * insertion, and removal at either end require no boundary branches, and
 * addressing a node is a single unscaled base+offset load on arm64.
 */
typedef struct {
    uint32_t next;
    uint32_t prev;
    int64_t  val;
} dnode;

_Static_assert(sizeof(dnode) == 16, "dnode must be 16 bytes");

struct dlist {
    dnode   *nodes;      /* contiguous node pool; nodes[0] is sentinel */
    uint32_t used;       /* bump frontier in bytes (starts at 16) */
    uint32_t cap;        /* allocated capacity in bytes (multiple of 16) */
    uint32_t free_head;  /* LIFO freelist head (byte offset, 0 = empty) */
    uint32_t len;        /* number of live elements */
};

enum {
    INIT_CAP_BYTES = 64u * (uint32_t)sizeof(dnode), /* 1 KiB (63 usable nodes) */
};

static const size_t MAX_CAP_BYTES = (size_t)0xFFFFFFF0u;
static const size_t QUAD_GROW_LIMIT = 256ULL * 1024ULL * 1024ULL;

static inline dnode *node_at(dnode *base, uint64_t off) {
    return (dnode *)((char *)base + off);
}

static inline const dnode *node_at_const(const dnode *base, uint64_t off) {
    return (const dnode *)((const char *)base + off);
}

static __attribute__((noinline, cold)) void dlist_grow(dlist *l) {
    size_t cap = l->cap;
    size_t new_cap = (cap < QUAD_GROW_LIMIT) ? (cap * 4) : (cap * 2);
    if (new_cap > MAX_CAP_BYTES || new_cap < cap) {
        if (cap >= MAX_CAP_BYTES) {
            abort();
        }
        new_cap = MAX_CAP_BYTES;
    }
    dnode *p = (dnode *)realloc(l->nodes, new_cap);
    if (!p) {
        abort();
    }
    l->nodes = p;
    l->cap = (uint32_t)new_cap;
}

static inline uint32_t dlist_alloc_node(dlist *l) {
    uint32_t h = l->free_head;
    if (h != 0) {
        l->free_head = node_at(l->nodes, h)->next;
        return h;
    }
    uint32_t used = l->used;
    if (__builtin_expect(used == l->cap, 0)) {
        dlist_grow(l);
    }
    l->used = used + (uint32_t)sizeof(dnode);
    return used;
}

dlist *dlist_new(void) {
    dlist *l = (dlist *)malloc(sizeof(dlist));
    if (!l) {
        abort();
    }
    dnode *nodes = (dnode *)malloc(INIT_CAP_BYTES);
    if (!nodes) {
        free(l);
        abort();
    }
    nodes[0].next = 0;
    nodes[0].prev = 0;
    nodes[0].val = 0;

    l->nodes = nodes;
    l->used = (uint32_t)sizeof(dnode);
    l->cap = INIT_CAP_BYTES;
    l->free_head = 0;
    l->len = 0;
    return l;
}

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

uint64_t dlist_push_front(dlist *l, int64_t value) {
    uint32_t h = dlist_alloc_node(l);
    dnode *base = l->nodes;
    uint32_t head_off = base->next;
    dnode *n = node_at(base, h);
    n->next = head_off;
    n->prev = 0;
    n->val = value;
    base->next = h;
    node_at(base, head_off)->prev = h;
    l->len++;
    return h;
}

uint64_t dlist_push_back(dlist *l, int64_t value) {
    uint32_t h = dlist_alloc_node(l);
    dnode *base = l->nodes;
    uint32_t tail_off = base->prev;
    dnode *n = node_at(base, h);
    n->next = 0;
    n->prev = tail_off;
    n->val = value;
    node_at(base, tail_off)->next = h;
    base->prev = h;
    l->len++;
    return h;
}

uint64_t dlist_insert_before(dlist *l, uint64_t node, int64_t value) {
    uint32_t h = dlist_alloc_node(l);
    dnode *base = l->nodes;
    uint32_t next_off = (uint32_t)node;
    dnode *next_node = node_at(base, node);
    uint32_t prev_off = next_node->prev;
    dnode *n = node_at(base, h);
    n->next = next_off;
    n->prev = prev_off;
    n->val = value;
    next_node->prev = h;
    node_at(base, prev_off)->next = h;
    l->len++;
    return h;
}

uint64_t dlist_insert_after(dlist *l, uint64_t node, int64_t value) {
    uint32_t h = dlist_alloc_node(l);
    dnode *base = l->nodes;
    uint32_t prev_off = (uint32_t)node;
    dnode *prev_node = node_at(base, node);
    uint32_t next_off = prev_node->next;
    dnode *n = node_at(base, h);
    n->next = next_off;
    n->prev = prev_off;
    n->val = value;
    prev_node->next = h;
    node_at(base, next_off)->prev = h;
    l->len++;
    return h;
}

int64_t dlist_remove(dlist *l, uint64_t node) {
    dnode *base = l->nodes;
    dnode *n = node_at(base, node);
    uint32_t next_off = n->next;
    uint32_t prev_off = n->prev;
    int64_t val = n->val;
    node_at(base, prev_off)->next = next_off;
    node_at(base, next_off)->prev = prev_off;
    n->next = l->free_head;
    l->free_head = (uint32_t)node;
    l->len--;
    return val;
}

bool dlist_pop_front(dlist *l, int64_t *value) {
    dnode *base = l->nodes;
    uint32_t h = base->next;
    if (__builtin_expect(h == 0, 0)) {
        return false;
    }
    dnode *n = node_at(base, h);
    uint32_t next_off = n->next;
    *value = n->val;
    base->next = next_off;
    node_at(base, next_off)->prev = 0;
    n->next = l->free_head;
    l->free_head = h;
    l->len--;
    return true;
}

bool dlist_pop_back(dlist *l, int64_t *value) {
    dnode *base = l->nodes;
    uint32_t t = base->prev;
    if (__builtin_expect(t == 0, 0)) {
        return false;
    }
    dnode *n = node_at(base, t);
    uint32_t prev_off = n->prev;
    *value = n->val;
    base->prev = prev_off;
    node_at(base, prev_off)->next = 0;
    n->next = l->free_head;
    l->free_head = t;
    l->len--;
    return true;
}

void dlist_move_to_front(dlist *l, uint64_t node) {
    dnode *base = l->nodes;
    uint32_t head_off = base->next;
    uint32_t cur_off = (uint32_t)node;
    if (__builtin_expect(head_off == cur_off, 0)) {
        return;
    }
    dnode *n = node_at(base, node);
    uint32_t next_off = n->next;
    uint32_t prev_off = n->prev;
    node_at(base, prev_off)->next = next_off;
    node_at(base, next_off)->prev = prev_off;
    n->next = head_off;
    n->prev = 0;
    node_at(base, head_off)->prev = cur_off;
    base->next = cur_off;
}

uint64_t dlist_front(const dlist *l) {
    return l->nodes->next;
}

uint64_t dlist_back(const dlist *l) {
    return l->nodes->prev;
}

uint64_t dlist_next(const dlist *l, uint64_t node) {
    return node_at_const(l->nodes, node)->next;
}

uint64_t dlist_prev(const dlist *l, uint64_t node) {
    return node_at_const(l->nodes, node)->prev;
}

int64_t dlist_get(const dlist *l, uint64_t node) {
    return node_at_const(l->nodes, node)->val;
}

size_t dlist_len(const dlist *l) {
    return l->len;
}
