#pragma once

#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <optional>

class SkipList {
public:
    SkipList() noexcept = default;

    ~SkipList() noexcept {
        std::free(arena_);
    }

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

    bool insert(std::int64_t key, std::int64_t value) {
        const std::uint64_t next_rng = rng_ + 0x9E3779B97F4A7C15ULL;
        const std::uint32_t new_h = height_from_seed(next_rng);

        if (cur_height_ == 0) [[unlikely]] {
            rng_ = next_rng;
            const std::uint32_t idx = alloc_node(new_h);
            std::byte* const p = arena_ + (static_cast<std::size_t>(idx) << 3);
            auto* const hdr = reinterpret_cast<NodeHeader*>(p);
            hdr->key = key;
            hdr->val = value;
            auto* const nnext = reinterpret_cast<std::uint32_t*>(p + 16);
            for (std::uint32_t l = 0; l < new_h; ++l) {
                nnext[l] = 0;
                header_next_[l] = idx;
            }
            cur_height_ = new_h;
            size_ = 1;
            return true;
        }

        if (new_h == 1u) {
            return insert_h1(key, value, next_rng);
        }
        return insert_tall(key, value, new_h, next_rng);
    }

    std::optional<std::int64_t> get(std::int64_t key) const noexcept {
        if (cur_height_ == 0) [[unlikely]] {
            return std::nullopt;
        }
        const std::byte* const arena = arena_;
        const std::uint32_t* cur_next = header_next_;
        std::uint32_t stop = 0;

        int lvl = static_cast<int>(cur_height_) - 1;
        for (; lvl > 4; --lvl) {
            std::uint32_t next_idx = cur_next[lvl];
            while (next_idx != stop) {
                const std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < key) {
                    cur_next = reinterpret_cast<const std::uint32_t*>(p + 16);
                    next_idx = cur_next[lvl];
                } else if (nk == key) [[unlikely]] {
                    return reinterpret_cast<const NodeHeader*>(p)->val;
                } else {
                    stop = next_idx;
                    break;
                }
            }
        }
        for (; lvl >= 1; --lvl) {
            std::uint32_t next_idx = cur_next[lvl];
            while (next_idx != stop) {
                __builtin_prefetch(arena + (static_cast<std::size_t>(cur_next[lvl - 1]) << 3), 0, 3);
                const std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < key) {
                    cur_next = reinterpret_cast<const std::uint32_t*>(p + 16);
                    next_idx = cur_next[lvl];
                } else if (nk == key) [[unlikely]] {
                    return reinterpret_cast<const NodeHeader*>(p)->val;
                } else {
                    stop = next_idx;
                    break;
                }
            }
        }
        {
            std::uint32_t next_idx = cur_next[0];
            while (next_idx != stop) {
                const std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < key) {
                    cur_next = reinterpret_cast<const std::uint32_t*>(p + 16);
                    next_idx = cur_next[0];
                } else if (nk == key) {
                    return reinterpret_cast<const NodeHeader*>(p)->val;
                } else {
                    break;
                }
            }
        }
        return std::nullopt;
    }

    bool remove(std::int64_t key) noexcept {
        if (cur_height_ == 0) [[unlikely]] {
            return false;
        }
        std::byte* const arena = arena_;
        std::uint32_t* cur_next = header_next_;
        std::uint32_t stop = 0;
        std::uint32_t target_idx = 0;
        int found_lvl = -1;

        int lvl = static_cast<int>(cur_height_) - 1;
        for (; lvl > 4; --lvl) {
            std::uint32_t next_idx = cur_next[lvl];
            while (next_idx != stop) {
                std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < key) {
                    cur_next = reinterpret_cast<std::uint32_t*>(p + 16);
                    next_idx = cur_next[lvl];
                } else if (nk == key) [[unlikely]] {
                    target_idx = next_idx;
                    found_lvl = lvl;
                    goto found;
                } else {
                    stop = next_idx;
                    break;
                }
            }
        }
        for (; lvl >= 1; --lvl) {
            std::uint32_t next_idx = cur_next[lvl];
            while (next_idx != stop) {
                __builtin_prefetch(arena + (static_cast<std::size_t>(cur_next[lvl - 1]) << 3), 0, 3);
                std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < key) {
                    cur_next = reinterpret_cast<std::uint32_t*>(p + 16);
                    next_idx = cur_next[lvl];
                } else if (nk == key) [[unlikely]] {
                    target_idx = next_idx;
                    found_lvl = lvl;
                    goto found;
                } else {
                    stop = next_idx;
                    break;
                }
            }
        }
        {
            std::uint32_t next_idx = cur_next[0];
            while (next_idx != stop) {
                std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < key) {
                    cur_next = reinterpret_cast<std::uint32_t*>(p + 16);
                    next_idx = cur_next[0];
                } else if (nk == key) {
                    target_idx = next_idx;
                    found_lvl = 0;
                    goto found;
                } else {
                    break;
                }
            }
        }
        return false;

    found:
        const std::uint32_t* const target_next =
            reinterpret_cast<const std::uint32_t*>(arena + (static_cast<std::size_t>(target_idx) << 3) + 16);
        cur_next[found_lvl] = target_next[found_lvl];

        if (found_lvl > 0) {
            for (int l = found_lvl - 1; l >= 0; --l) {
                std::uint32_t next_idx = cur_next[l];
                while (next_idx != target_idx) {
                    cur_next = reinterpret_cast<std::uint32_t*>(arena + (static_cast<std::size_t>(next_idx) << 3) + 16);
                    next_idx = cur_next[l];
                }
                cur_next[l] = target_next[l];
            }
        }

        if (cur_next == header_next_) {
            __builtin_prefetch(arena + (static_cast<std::size_t>(header_next_[0]) << 3), 0, 3);
        }

        free_node(target_idx, static_cast<std::uint32_t>(found_lvl + 1));
        --size_;
        while (cur_height_ > 0 && header_next_[cur_height_ - 1] == 0) {
            --cur_height_;
        }
        return true;
    }

    std::size_t range(std::int64_t lo, std::int64_t hi,
                      std::int64_t* out_keys, std::int64_t* out_vals,
                      std::size_t limit) const noexcept {
        if (limit == 0 || lo > hi || cur_height_ == 0) [[unlikely]] {
            return 0;
        }
        const std::byte* const arena = arena_;
        const std::uint32_t* cur_next = header_next_;
        std::uint32_t stop = 0;
        std::uint32_t start[5] = {0, 0, 0, 0, 0};

        int lvl = static_cast<int>(cur_height_) - 1;
        for (; lvl > 4; --lvl) {
            std::uint32_t next_idx = cur_next[lvl];
            while (next_idx != stop) {
                const std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < lo) {
                    cur_next = reinterpret_cast<const std::uint32_t*>(p + 16);
                    next_idx = cur_next[lvl];
                } else if (nk == lo) [[unlikely]] {
                    start[0] = start[1] = start[2] = start[3] = start[4] = next_idx;
                    goto found_start;
                } else {
                    stop = next_idx;
                    break;
                }
            }
        }
        for (; lvl >= 1; --lvl) {
            std::uint32_t next_idx = cur_next[lvl];
            while (next_idx != stop) {
                __builtin_prefetch(arena + (static_cast<std::size_t>(cur_next[lvl - 1]) << 3), 0, 3);
                const std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < lo) {
                    cur_next = reinterpret_cast<const std::uint32_t*>(p + 16);
                    next_idx = cur_next[lvl];
                } else if (nk == lo) [[unlikely]] {
                    for (int k = lvl; k >= 0; --k) start[k] = next_idx;
                    goto found_start;
                } else {
                    stop = next_idx;
                    break;
                }
            }
            start[lvl] = stop;
        }
        {
            std::uint32_t next_idx = cur_next[0];
            while (next_idx != stop) {
                const std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < lo) {
                    cur_next = reinterpret_cast<const std::uint32_t*>(p + 16);
                    next_idx = cur_next[0];
                } else {
                    stop = next_idx;
                    break;
                }
            }
            start[0] = stop;
        }

    found_start:
        std::uint32_t cur0 = start[0];
        if (cur0 == 0) {
            return 0;
        }

        if (limit < 32u || cur_height_ < 5u) {
            std::size_t count = 0;
            while (cur0 != 0) {
                const std::byte* const p = arena + (static_cast<std::size_t>(cur0) << 3);
                const auto* const hdr = reinterpret_cast<const NodeHeader*>(p);
                const std::int64_t k = hdr->key;
                if (k > hi) break;
                out_keys[count] = k;
                out_vals[count] = hdr->val;
                ++count;
                if (count == limit) break;
                cur0 = reinterpret_cast<const std::uint32_t*>(p + 16)[0];
            }
            return count;
        }

        return range_pipelined(hi, out_keys, out_vals, limit, start);
    }

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

    std::size_t height() const noexcept {
        return cur_height_;
    }

    std::size_t level_count(std::size_t level) const noexcept {
        if (level >= cur_height_) {
            return 0;
        }
        const std::byte* const arena = arena_;
        std::size_t count = 0;
        std::uint32_t idx = header_next_[level];
        while (idx != 0) {
            ++count;
            idx = reinterpret_cast<const std::uint32_t*>(
                arena + (static_cast<std::size_t>(idx) << 3) + 16)[level];
        }
        return count;
    }

private:
    static constexpr std::uint32_t MAX_HEIGHT = 32;

    struct NodeHeader {
        std::int64_t key;
        std::int64_t val;
    };

    struct Pool {
        std::uint32_t free_head = 0;
        std::uint32_t slab_cur = 0;
        std::uint32_t slab_end = 0;
        std::uint32_t slab_words = 0;
    };

    static inline std::uint32_t height_from_seed(std::uint64_t z) noexcept {
        z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
        z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
        z ^= z >> 31;
        return 1u + static_cast<std::uint32_t>(std::countl_zero(z | (1ULL << 32)));
    }

    static constexpr std::uint32_t words_for_height(std::uint32_t h) noexcept {
        return 2u + ((h + 1u) >> 1);
    }

    inline std::uint32_t* node_next(std::uint32_t idx) noexcept {
        return reinterpret_cast<std::uint32_t*>(
            arena_ + (static_cast<std::size_t>(idx) << 3) + 16);
    }

    inline std::uint32_t alloc_node(std::uint32_t h) {
        Pool& p = pools_[h - 1];
        if (p.free_head != 0) {
            const std::uint32_t idx = p.free_head;
            std::uint32_t next_free;
            std::memcpy(&next_free, arena_ + (static_cast<std::size_t>(idx) << 3), sizeof(std::uint32_t));
            p.free_head = next_free;
            return idx;
        }
        const std::uint32_t w = words_for_height(h);
        std::uint32_t idx = p.slab_cur;
        if (w <= 16u && (idx & 15u) + w > 16u) {
            idx = (idx + 15u) & ~15u;
        }
        if (idx + w <= p.slab_end) {
            p.slab_cur = idx + w;
            return idx;
        }
        return alloc_node_slow(h, w);
    }

    [[gnu::noinline]] std::uint32_t alloc_node_slow(std::uint32_t h, std::uint32_t w) {
        Pool& p = pools_[h - 1];
        std::uint32_t sw = p.slab_words;
        if (sw == 0) {
            sw = (w <= 16u) ? 16u : 32u;
        }
        p.slab_words = std::min<std::uint32_t>(sw << 1, 2048u);

        if (arena_used_ + sw > arena_cap_) {
            grow_arena(arena_used_ + sw);
        }
        const std::uint32_t base = arena_used_;
        arena_used_ += sw;
        p.slab_cur = base + w;
        p.slab_end = base + sw;
        return base;
    }

    [[gnu::noinline]] void grow_arena(std::uint32_t min_words) {
        std::size_t new_cap = (arena_cap_ == 0) ? 1024u : static_cast<std::size_t>(arena_cap_);
        if (new_cap < 524288u) {
            while (new_cap < min_words) {
                new_cap <<= 1;
            }
        } else {
            while (new_cap < min_words) {
                new_cap = (new_cap + (new_cap >> 1) + 2047u) & ~std::size_t(2047u);
            }
        }
        const std::size_t bytes = new_cap * sizeof(std::uint64_t);
        void* const mem = std::aligned_alloc(128, bytes);
        if (!mem) [[unlikely]] {
            std::abort();
        }
        if (arena_ != nullptr) {
            std::memcpy(mem, arena_, static_cast<std::size_t>(arena_used_) * sizeof(std::uint64_t));
            std::free(arena_);
        } else {
            std::memset(mem, 0, 128);
        }
        arena_ = static_cast<std::byte*>(mem);
        arena_cap_ = static_cast<std::uint32_t>(new_cap);
    }

    inline void free_node(std::uint32_t idx, std::uint32_t h) noexcept {
        Pool& p = pools_[h - 1];
        std::memcpy(arena_ + (static_cast<std::size_t>(idx) << 3), &p.free_head, sizeof(std::uint32_t));
        p.free_head = idx;
    }

    bool insert_h1(std::int64_t key, std::int64_t value, std::uint64_t next_rng) {
        std::byte* const arena = arena_;
        std::uint32_t cur_idx = 0;
        std::uint32_t* cur_next = header_next_;
        std::uint32_t stop = 0;

        int lvl = static_cast<int>(cur_height_) - 1;
        for (; lvl > 4; --lvl) {
            std::uint32_t next_idx = cur_next[lvl];
            while (next_idx != stop) {
                std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < key) {
                    cur_idx = next_idx;
                    cur_next = reinterpret_cast<std::uint32_t*>(p + 16);
                    next_idx = cur_next[lvl];
                } else if (nk == key) [[unlikely]] {
                    reinterpret_cast<NodeHeader*>(p)->val = value;
                    return false;
                } else {
                    stop = next_idx;
                    break;
                }
            }
        }
        for (; lvl >= 1; --lvl) {
            std::uint32_t next_idx = cur_next[lvl];
            while (next_idx != stop) {
                __builtin_prefetch(arena + (static_cast<std::size_t>(cur_next[lvl - 1]) << 3), 0, 3);
                std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < key) {
                    cur_idx = next_idx;
                    cur_next = reinterpret_cast<std::uint32_t*>(p + 16);
                    next_idx = cur_next[lvl];
                } else if (nk == key) [[unlikely]] {
                    reinterpret_cast<NodeHeader*>(p)->val = value;
                    return false;
                } else {
                    stop = next_idx;
                    break;
                }
            }
        }
        {
            std::uint32_t next_idx = cur_next[0];
            while (next_idx != stop) {
                std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < key) {
                    cur_idx = next_idx;
                    cur_next = reinterpret_cast<std::uint32_t*>(p + 16);
                    next_idx = cur_next[0];
                } else if (nk == key) [[unlikely]] {
                    reinterpret_cast<NodeHeader*>(p)->val = value;
                    return false;
                } else {
                    stop = next_idx;
                    break;
                }
            }
        }

        rng_ = next_rng;
        const std::uint32_t idx = alloc_node(1u);
        std::byte* const np = arena_ + (static_cast<std::size_t>(idx) << 3);
        auto* const hdr = reinterpret_cast<NodeHeader*>(np);
        hdr->key = key;
        hdr->val = value;
        reinterpret_cast<std::uint32_t*>(np + 16)[0] = stop;
        std::uint32_t* const pred_next = (cur_idx == 0) ? header_next_ : node_next(cur_idx);
        pred_next[0] = idx;
        ++size_;
        return true;
    }

    bool insert_tall(std::int64_t key, std::int64_t value,
                     std::uint32_t new_h, std::uint64_t next_rng) {
        std::byte* const arena = arena_;
        std::uint32_t cur_idx = 0;
        std::uint32_t* cur_next = header_next_;
        std::uint32_t stop = 0;
        std::uint32_t update_idx[MAX_HEIGHT];
        const int new_h_i = static_cast<int>(new_h);

        int lvl = static_cast<int>(cur_height_) - 1;
        for (; lvl > 4; --lvl) {
            std::uint32_t next_idx = cur_next[lvl];
            while (next_idx != stop) {
                std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < key) {
                    cur_idx = next_idx;
                    cur_next = reinterpret_cast<std::uint32_t*>(p + 16);
                    next_idx = cur_next[lvl];
                } else if (nk == key) [[unlikely]] {
                    reinterpret_cast<NodeHeader*>(p)->val = value;
                    return false;
                } else {
                    stop = next_idx;
                    break;
                }
            }
            if (lvl < new_h_i) [[unlikely]] {
                update_idx[lvl] = cur_idx;
            }
        }
        for (; lvl >= 1; --lvl) {
            std::uint32_t next_idx = cur_next[lvl];
            while (next_idx != stop) {
                __builtin_prefetch(arena + (static_cast<std::size_t>(cur_next[lvl - 1]) << 3), 0, 3);
                std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < key) {
                    cur_idx = next_idx;
                    cur_next = reinterpret_cast<std::uint32_t*>(p + 16);
                    next_idx = cur_next[lvl];
                } else if (nk == key) [[unlikely]] {
                    reinterpret_cast<NodeHeader*>(p)->val = value;
                    return false;
                } else {
                    stop = next_idx;
                    break;
                }
            }
            update_idx[lvl] = cur_idx;
        }
        {
            std::uint32_t next_idx = cur_next[0];
            while (next_idx != stop) {
                std::byte* const p = arena + (static_cast<std::size_t>(next_idx) << 3);
                const std::int64_t nk = reinterpret_cast<const NodeHeader*>(p)->key;
                if (nk < key) {
                    cur_idx = next_idx;
                    cur_next = reinterpret_cast<std::uint32_t*>(p + 16);
                    next_idx = cur_next[0];
                } else if (nk == key) [[unlikely]] {
                    reinterpret_cast<NodeHeader*>(p)->val = value;
                    return false;
                } else {
                    stop = next_idx;
                    break;
                }
            }
            update_idx[0] = cur_idx;
        }

        rng_ = next_rng;
        const std::uint32_t idx = alloc_node(new_h);
        std::byte* const np = arena_ + (static_cast<std::size_t>(idx) << 3);
        auto* const hdr = reinterpret_cast<NodeHeader*>(np);
        hdr->key = key;
        hdr->val = value;
        auto* const nnext = reinterpret_cast<std::uint32_t*>(np + 16);

        const std::uint32_t link_levels = std::min(cur_height_, new_h);
        for (std::uint32_t l = 0; l < link_levels; ++l) {
            const std::uint32_t u = update_idx[l];
            std::uint32_t* const pred_next = (u == 0) ? header_next_ : node_next(u);
            nnext[l] = pred_next[l];
            pred_next[l] = idx;
        }
        if (new_h > cur_height_) [[unlikely]] {
            for (std::uint32_t l = cur_height_; l < new_h; ++l) {
                nnext[l] = 0;
                header_next_[l] = idx;
            }
            cur_height_ = new_h;
        }
        ++size_;
        return true;
    }

    std::size_t range_pipelined(std::int64_t hi,
                                std::int64_t* out_keys, std::int64_t* out_vals,
                                std::size_t limit,
                                const std::uint32_t start[5]) const noexcept {
        const std::byte* const arena = arena_;
        std::uint32_t cur0 = start[0];
        std::uint32_t ahead1 = start[1];
        std::uint32_t ahead2 = start[2];
        std::uint32_t ahead3 = start[3];
        std::uint32_t ahead4 = start[4];

        std::uint32_t q1[16], q1_n0[16];
        std::uint32_t q2[8], q3[8], q4[8];
        std::uint32_t h1 = 0, t1 = 0;
        std::uint32_t h2 = 0, t2 = 0;
        std::uint32_t h3 = 0, t3 = 0;
        std::uint32_t h4 = 0, t4 = 0;

        std::size_t count = 0;
        while (cur0 != 0) {
            // Level 4 lookahead (L2 resident) -> prefetches level 3 & 4
            if (ahead4 != 0 && (t4 - h4) < 4u) {
                const std::byte* const p4 = arena + (static_cast<std::size_t>(ahead4) << 3);
                if (reinterpret_cast<const NodeHeader*>(p4)->key <= hi) {
                    const auto* const n4 = reinterpret_cast<const std::uint32_t*>(p4 + 16);
                    q4[t4++ & 7u] = ahead4;
                    __builtin_prefetch(arena + (static_cast<std::size_t>(n4[3]) << 3), 0, 3);
                    ahead4 = n4[4];
                    __builtin_prefetch(arena + (static_cast<std::size_t>(ahead4) << 3), 0, 3);
                } else {
                    ahead4 = 0;
                }
            }
            // Level 3 lookahead -> prefetches level 2 & 3
            if (ahead3 != 0 && ahead3 != ahead4 && (t3 - h3) < 4u) {
                const std::byte* const p3 = arena + (static_cast<std::size_t>(ahead3) << 3);
                if (reinterpret_cast<const NodeHeader*>(p3)->key <= hi) {
                    if (h4 != t4 && ahead3 == q4[h4 & 7u]) ++h4;
                    const auto* const n3 = reinterpret_cast<const std::uint32_t*>(p3 + 16);
                    q3[t3++ & 7u] = ahead3;
                    __builtin_prefetch(arena + (static_cast<std::size_t>(n3[2]) << 3), 0, 3);
                    ahead3 = n3[3];
                    __builtin_prefetch(arena + (static_cast<std::size_t>(ahead3) << 3), 0, 3);
                } else {
                    ahead3 = 0;
                }
            }
            // Level 2 lookahead -> prefetches level 1 & 2
            if (ahead2 != 0 && ahead2 != ahead3 && (t2 - h2) < 6u) {
                const std::byte* const p2 = arena + (static_cast<std::size_t>(ahead2) << 3);
                if (reinterpret_cast<const NodeHeader*>(p2)->key <= hi) {
                    if (h3 != t3 && ahead2 == q3[h3 & 7u]) ++h3;
                    const auto* const n2 = reinterpret_cast<const std::uint32_t*>(p2 + 16);
                    q2[t2++ & 7u] = ahead2;
                    __builtin_prefetch(arena + (static_cast<std::size_t>(n2[1]) << 3), 0, 3);
                    ahead2 = n2[2];
                    __builtin_prefetch(arena + (static_cast<std::size_t>(ahead2) << 3), 0, 3);
                } else {
                    ahead2 = 0;
                }
            }
            // Level 1 lookahead -> prefetches level 0 & 1
            if (ahead1 != 0 && ahead1 != ahead2 && (t1 - h1) < 12u) {
                const std::byte* const p1 = arena + (static_cast<std::size_t>(ahead1) << 3);
                if (reinterpret_cast<const NodeHeader*>(p1)->key <= hi) {
                    if (h2 != t2 && ahead1 == q2[h2 & 7u]) ++h2;
                    const auto* const n1 = reinterpret_cast<const std::uint32_t*>(p1 + 16);
                    const std::uint32_t slot = t1++ & 15u;
                    q1[slot] = ahead1;
                    const std::uint32_t n0 = n1[0];
                    q1_n0[slot] = n0;
                    __builtin_prefetch(arena + (static_cast<std::size_t>(n0) << 3), 0, 3);
                    ahead1 = n1[1];
                    __builtin_prefetch(arena + (static_cast<std::size_t>(ahead1) << 3), 0, 3);
                } else {
                    ahead1 = 0;
                }
            }

            // Emit cur0
            const std::byte* const p0 = arena + (static_cast<std::size_t>(cur0) << 3);
            const auto* const hdr = reinterpret_cast<const NodeHeader*>(p0);
            const std::int64_t k = hdr->key;
            if (k > hi) [[unlikely]] break;
            out_keys[count] = k;
            out_vals[count] = hdr->val;
            ++count;
            if (count == limit) [[unlikely]] break;

            // Synchronize q1 and chase 2nd & 3rd consecutive height-1 nodes
            if (h1 != t1 && cur0 == q1[h1 & 15u]) {
                ++h1;
                const std::uint32_t rem = t1 - h1;
                if (rem > 7u) {
                    const std::uint32_t s1 = (h1 + 6u) & 15u;
                    const std::uint32_t px = q1_n0[s1];
                    if (px != 0 && px != q1[(s1 + 1u) & 15u]) {
                        const std::uint32_t nx = reinterpret_cast<const std::uint32_t*>(
                            arena + (static_cast<std::size_t>(px) << 3) + 16)[0];
                        q1_n0[s1] = nx;
                        __builtin_prefetch(arena + (static_cast<std::size_t>(nx) << 3), 0, 3);
                    }
                }
                if (rem > 4u) {
                    const std::uint32_t s2 = (h1 + 3u) & 15u;
                    const std::uint32_t px = q1_n0[s2];
                    if (px != 0 && px != q1[(s2 + 1u) & 15u]) {
                        const std::uint32_t nx = reinterpret_cast<const std::uint32_t*>(
                            arena + (static_cast<std::size_t>(px) << 3) + 16)[0];
                        q1_n0[s2] = nx;
                        __builtin_prefetch(arena + (static_cast<std::size_t>(nx) << 3), 0, 3);
                    }
                }
            }

            cur0 = reinterpret_cast<const std::uint32_t*>(p0 + 16)[0];
        }
        return count;
    }

    // Hot metadata (fits in 1st cache line)
    std::byte* arena_ = nullptr;
    std::size_t size_ = 0;
    std::uint32_t cur_height_ = 0;
    std::uint32_t arena_used_ = 16;
    std::uint32_t arena_cap_ = 0;
    std::uint64_t rng_ = 0x243F6A8885A308D3ULL;

    // Header forward links (exact 128-byte cache line)
    alignas(128) std::uint32_t header_next_[MAX_HEIGHT] = {};

    // Height-segregated slab + freelist pools
    Pool pools_[MAX_HEIGHT] = {};
};
