// Doubly linked list backed by a single growable pool of 16-byte nodes.
//
// Node i lives at pool[i]; its handle is simply i. Index 0 is a sentinel
// node that closes the list into a ring: sentinel.next is the first node,
// sentinel.prev is the last, and the first/last nodes link back to 0. Since
// handle 0 means "no node", front/back/next/prev need no branches at all.
//
// Links are 32-bit indices, so a node (value + next + prev) is 16 bytes and
// eight of them share a 128-byte cache line. Removed nodes go on a LIFO free
// list threaded through their `next` field and are reused before the pool's
// bump pointer advances. The pool grows by doubling via realloc.
#pragma once

#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <optional>

class DList {
public:
    DList() {
        cap_ = kInitialCap;
        pool_ = static_cast<Node*>(std::malloc(sizeof(Node) * cap_));
        if (!pool_) std::abort();
        pool_[0].value = 0;
        pool_[0].next = 0;
        pool_[0].prev = 0;
        used_ = 1;
        free_ = 0;
        len_ = 0;
    }
    ~DList() { std::free(pool_); }

    DList(const DList&) = delete;
    DList& operator=(const DList&) = delete;

    std::uint64_t push_front(std::int64_t value) { return link_after(0, value); }
    std::uint64_t push_back(std::int64_t value) { return link_after(pool_[0].prev, value); }

    std::uint64_t insert_before(std::uint64_t node, std::int64_t value) {
        return link_after(pool_[node].prev, value);
    }
    std::uint64_t insert_after(std::uint64_t node, std::int64_t value) {
        return link_after(static_cast<std::uint32_t>(node), value);
    }

    std::int64_t remove(std::uint64_t node) {
        Node* p = pool_;
        std::uint32_t x = static_cast<std::uint32_t>(node);
        Node& nx = p[x];
        std::uint32_t a = nx.prev, b = nx.next;
        std::int64_t v = nx.value;
        p[a].next = b;
        p[b].prev = a;
        nx.next = free_;
        free_ = x;
        --len_;
        return v;
    }

    std::optional<std::int64_t> pop_front() {
        if (__builtin_expect(len_ == 0, 0)) return std::nullopt;
        return remove(pool_[0].next);
    }
    std::optional<std::int64_t> pop_back() {
        if (__builtin_expect(len_ == 0, 0)) return std::nullopt;
        return remove(pool_[0].prev);
    }

    void move_to_front(std::uint64_t node) {
        Node* p = pool_;
        std::uint32_t x = static_cast<std::uint32_t>(node);
        Node& nx = p[x];
        std::uint32_t a = nx.prev, b = nx.next;
        if (a == 0) return;  // already the first
        p[a].next = b;
        p[b].prev = a;
        std::uint32_t f = p[0].next;
        nx.next = f;
        nx.prev = 0;
        p[f].prev = x;
        p[0].next = x;
    }

    std::uint64_t front() const { return pool_[0].next; }
    std::uint64_t back() const { return pool_[0].prev; }
    std::uint64_t next(std::uint64_t node) const { return pool_[node].next; }
    std::uint64_t prev(std::uint64_t node) const { return pool_[node].prev; }
    std::int64_t get(std::uint64_t node) const { return pool_[node].value; }
    std::size_t len() const { return len_; }

private:
    struct Node {
        std::int64_t value;
        std::uint32_t next;
        std::uint32_t prev;
    };
    static_assert(sizeof(Node) == 16, "node must be 16 bytes");

    static constexpr std::uint32_t kInitialCap = 64;

    Node* pool_;
    std::uint32_t used_;  // nodes ever handed out by the bump pointer (incl. sentinel)
    std::uint32_t cap_;
    std::uint32_t free_;  // head of the free list, 0 if empty
    std::size_t len_;

    __attribute__((noinline, cold)) void grow() {
        std::uint64_t nc = static_cast<std::uint64_t>(cap_) * 2;
        if (nc > 0xFFFFFFFFull) {
            if (cap_ == 0xFFFFFFFFu) std::abort();
            nc = 0xFFFFFFFFull;
        }
        Node* np = static_cast<Node*>(std::realloc(pool_, sizeof(Node) * nc));
        if (!np) std::abort();
        pool_ = np;
        cap_ = static_cast<std::uint32_t>(nc);
    }

    inline std::uint32_t alloc() {
        std::uint32_t i = free_;
        if (i != 0) {
            free_ = pool_[i].next;
            return i;
        }
        if (__builtin_expect(used_ == cap_, 0)) grow();
        return used_++;
    }

    // Insert a new node holding `value` right after node `a` (0 = sentinel).
    inline std::uint64_t link_after(std::uint32_t a, std::int64_t value) {
        std::uint32_t x = alloc();
        Node* p = pool_;
        std::uint32_t b = p[a].next;
        Node& nx = p[x];
        nx.value = value;
        nx.next = b;
        nx.prev = a;
        p[a].next = x;
        p[b].prev = x;
        ++len_;
        return x;
    }
};
