#pragma once

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

class DList {
    struct Node {
        std::uint32_t next;
        std::uint32_t prev;
        std::int64_t val;
    };

    static_assert(sizeof(Node) == 16);
    static_assert(std::is_trivially_copyable_v<Node>);

    static constexpr std::uint32_t kNodeSize = sizeof(Node);
    static constexpr std::size_t kInitialBytes = 64 * kNodeSize;
    static constexpr std::size_t kMaxBytes = 0xFFFFFFF0ULL;

    char* base_ = nullptr;
    std::uint32_t cap_ = kNodeSize;
    std::uint32_t bump_ = kNodeSize;
    std::uint32_t free_head_ = 0;
    std::uint32_t head_ = 0;
    std::uint32_t tail_ = 0;
    std::size_t len_ = 0;

    inline Node* node_at(std::uint32_t off) noexcept {
        return reinterpret_cast<Node*>(base_ + off);
    }

    inline const Node* node_at(std::uint32_t off) const noexcept {
        return reinterpret_cast<const Node*>(base_ + off);
    }

    [[gnu::noinline, gnu::cold]] void grow() {
        std::size_t new_cap;
        if (base_ == nullptr) {
            new_cap = kInitialBytes;
        } else {
            if (cap_ >= kMaxBytes) {
                throw std::bad_alloc();
            }
            std::size_t grown = static_cast<std::size_t>(cap_) * 4;
            new_cap = grown > kMaxBytes ? kMaxBytes : grown;
        }
        void* mem = std::realloc(base_, new_cap);
        if (!mem) [[unlikely]] {
            throw std::bad_alloc();
        }
        base_ = static_cast<char*>(mem);
        cap_ = static_cast<std::uint32_t>(new_cap);
    }

    inline std::uint32_t alloc_node() {
        std::uint32_t off = free_head_;
        if (off != 0) {
            free_head_ = node_at(off)->next;
            return off;
        }
        off = bump_;
        if (off >= cap_) [[unlikely]] {
            grow();
        }
        bump_ = off + kNodeSize;
        return off;
    }

public:
    DList() noexcept = default;

    ~DList() noexcept {
        std::free(base_);
    }

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

    inline std::uint64_t push_front(std::int64_t value) {
        std::uint32_t off = alloc_node();
        std::uint32_t h = head_;
        Node* nd = node_at(off);
        nd->next = h;
        nd->prev = 0;
        nd->val = value;
        if (h != 0) [[likely]] {
            node_at(h)->prev = off;
        } else {
            tail_ = off;
        }
        head_ = off;
        len_++;
        return off;
    }

    inline std::uint64_t push_back(std::int64_t value) {
        std::uint32_t off = alloc_node();
        std::uint32_t t = tail_;
        Node* nd = node_at(off);
        nd->next = 0;
        nd->prev = t;
        nd->val = value;
        if (t != 0) [[likely]] {
            node_at(t)->next = off;
        } else {
            head_ = off;
        }
        tail_ = off;
        len_++;
        return off;
    }

    inline std::uint64_t insert_before(std::uint64_t node, std::int64_t value) {
        std::uint32_t h = static_cast<std::uint32_t>(node);
        std::uint32_t off = alloc_node();
        Node* target = node_at(h);
        std::uint32_t p = target->prev;
        Node* nd = node_at(off);
        nd->next = h;
        nd->prev = p;
        nd->val = value;
        target->prev = off;
        if (p != 0) [[likely]] {
            node_at(p)->next = off;
        } else {
            head_ = off;
        }
        len_++;
        return off;
    }

    inline std::uint64_t insert_after(std::uint64_t node, std::int64_t value) {
        std::uint32_t h = static_cast<std::uint32_t>(node);
        std::uint32_t off = alloc_node();
        Node* target = node_at(h);
        std::uint32_t n = target->next;
        Node* nd = node_at(off);
        nd->next = n;
        nd->prev = h;
        nd->val = value;
        target->next = off;
        if (n != 0) [[likely]] {
            node_at(n)->prev = off;
        } else {
            tail_ = off;
        }
        len_++;
        return off;
    }

    inline std::int64_t remove(std::uint64_t node) noexcept {
        std::uint32_t h = static_cast<std::uint32_t>(node);
        Node* nd = node_at(h);
        std::uint32_t n = nd->next;
        std::uint32_t p = nd->prev;
        std::int64_t v = nd->val;
        if (p != 0) [[likely]] {
            node_at(p)->next = n;
        } else {
            head_ = n;
        }
        if (n != 0) [[likely]] {
            node_at(n)->prev = p;
        } else {
            tail_ = p;
        }
        if (--len_ != 0) [[likely]] {
            nd->next = free_head_;
            free_head_ = h;
        } else {
            free_head_ = 0;
            bump_ = kNodeSize;
        }
        return v;
    }

    inline std::optional<std::int64_t> pop_front() noexcept {
        std::uint32_t h = head_;
        if (h == 0) [[unlikely]] {
            return std::nullopt;
        }
        Node* nd = node_at(h);
        std::int64_t v = nd->val;
        std::uint32_t nx = nd->next;
        head_ = nx;
        len_--;
        if (nx != 0) [[likely]] {
            node_at(nx)->prev = 0;
            nd->next = free_head_;
            free_head_ = h;
        } else {
            tail_ = 0;
            free_head_ = 0;
            bump_ = kNodeSize;
        }
        return v;
    }

    inline std::optional<std::int64_t> pop_back() noexcept {
        std::uint32_t h = tail_;
        if (h == 0) [[unlikely]] {
            return std::nullopt;
        }
        Node* nd = node_at(h);
        std::int64_t v = nd->val;
        std::uint32_t pr = nd->prev;
        tail_ = pr;
        len_--;
        if (pr != 0) [[likely]] {
            node_at(pr)->next = 0;
            nd->next = free_head_;
            free_head_ = h;
        } else {
            head_ = 0;
            free_head_ = 0;
            bump_ = kNodeSize;
        }
        return v;
    }

    inline void move_to_front(std::uint64_t node) noexcept {
        std::uint32_t h = static_cast<std::uint32_t>(node);
        std::uint32_t old_head = head_;
        if (h == old_head) [[unlikely]] {
            return;
        }
        Node* nd = node_at(h);
        std::uint32_t n = nd->next;
        std::uint32_t p = nd->prev;
        node_at(p)->next = n;
        if (n != 0) [[likely]] {
            node_at(n)->prev = p;
        } else {
            tail_ = p;
        }
        nd->next = old_head;
        nd->prev = 0;
        node_at(old_head)->prev = h;
        head_ = h;
    }

    inline std::uint64_t front() const noexcept {
        return head_;
    }

    inline std::uint64_t back() const noexcept {
        return tail_;
    }

    inline std::uint64_t next(std::uint64_t node) const noexcept {
        return node_at(static_cast<std::uint32_t>(node))->next;
    }

    inline std::uint64_t prev(std::uint64_t node) const noexcept {
        return node_at(static_cast<std::uint32_t>(node))->prev;
    }

    inline std::int64_t get(std::uint64_t node) const noexcept {
        return node_at(static_cast<std::uint32_t>(node))->val;
    }

    inline std::size_t len() const noexcept {
        return len_;
    }
};
