#pragma once

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

class HashMap {
public:
    HashMap() : mask_(15), len_(0), max_len_(14), zero_slot_(NO_SLOT), shift_(60) {
        slots_ = static_cast<Slot*>(std::calloc(16, sizeof(Slot)));
        if (!slots_) [[unlikely]] {
            std::abort();
        }
    }

    ~HashMap() {
        std::free(slots_);
    }

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

    bool insert(std::int64_t key, std::int64_t value) {
        if (key == 0) [[unlikely]] {
            return insert_zero(value);
        }
        Slot* slots = slots_;
        const std::size_t mask = mask_;
        std::size_t idx = home(key);
        for (;;) {
            const std::int64_t sk = slots[idx].key;
            if (sk == key || sk == 0) {
                if (sk != 0) {
                    slots[idx].val = value;
                    return false;
                }
                if (idx != zero_slot_) [[likely]] {
                    if (len_ == max_len_) [[unlikely]] {
                        grow_and_insert(key, value);
                        return true;
                    }
                    slots[idx].key = key;
                    slots[idx].val = value;
                    ++len_;
                    return true;
                }
            }
            idx = (idx + 1) & mask;
        }
    }

    std::optional<std::int64_t> get(std::int64_t key) const {
        if (key == 0) [[unlikely]] {
            if (zero_slot_ != NO_SLOT) {
                return slots_[zero_slot_].val;
            }
            return std::nullopt;
        }
        const Slot* slots = slots_;
        const std::size_t mask = mask_;
        std::size_t idx = home(key);
        for (;;) {
            const std::int64_t sk = slots[idx].key;
            if (sk == key || sk == 0) {
                if (sk != 0) {
                    return slots[idx].val;
                }
                if (idx != zero_slot_) [[likely]] {
                    return std::nullopt;
                }
            }
            idx = (idx + 1) & mask;
        }
    }

    bool remove(std::int64_t key) {
        if (key == 0) [[unlikely]] {
            return remove_zero();
        }
        Slot* slots = slots_;
        const std::size_t mask = mask_;
        std::size_t idx = home(key);
        for (;;) {
            const std::int64_t sk = slots[idx].key;
            if (sk == key || sk == 0) {
                if (sk != 0) {
                    --len_;
                    std::size_t hole = idx;
                    std::size_t cur = (hole + 1) & mask;
                    for (;;) {
                        const std::int64_t ck = slots[cur].key;
                        if (ck == 0) {
                            if (cur != zero_slot_) [[likely]] {
                                slots[hole].key = 0;
                                return true;
                            }
                        }
                        const std::size_t h = home(ck);
                        if (((cur - hole) & mask) <= ((cur - h) & mask)) {
                            slots[hole] = slots[cur];
                            if (ck == 0) [[unlikely]] {
                                zero_slot_ = hole;
                            }
                            hole = cur;
                        }
                        cur = (cur + 1) & mask;
                    }
                }
                if (idx != zero_slot_) [[likely]] {
                    return false;
                }
            }
            idx = (idx + 1) & mask;
        }
    }

    std::size_t scan(std::int64_t* out_keys, std::int64_t* out_vals) const {
        const std::size_t target = len_;
        if (target == 0) {
            return 0;
        }
        std::int64_t* __restrict__ ok = out_keys;
        std::int64_t* __restrict__ ov = out_vals;
        std::size_t out = 0;
        if (zero_slot_ != NO_SLOT) [[unlikely]] {
            ok[0] = 0;
            ov[0] = slots_[zero_slot_].val;
            out = 1;
        }
        const Slot* __restrict__ p = slots_;
        while (out + 4 <= target) {
            const std::int64_t k0 = p[0].key, v0 = p[0].val;
            const std::int64_t k1 = p[1].key, v1 = p[1].val;
            const std::int64_t k2 = p[2].key, v2 = p[2].val;
            const std::int64_t k3 = p[3].key, v3 = p[3].val;
            const std::size_t m0 = static_cast<std::size_t>(k0 != 0);
            const std::size_t m1 = static_cast<std::size_t>(k1 != 0);
            const std::size_t m2 = static_cast<std::size_t>(k2 != 0);
            const std::size_t m3 = static_cast<std::size_t>(k3 != 0);
            const std::size_t s01 = m0 + m1;
            const std::size_t s23 = m2 + m3;
            const std::size_t o1 = out + m0;
            const std::size_t o2 = out + s01;
            const std::size_t o3 = o2 + m2;
            ok[out] = k0; ov[out] = v0;
            ok[o1]  = k1; ov[o1]  = v1;
            ok[o2]  = k2; ov[o2]  = v2;
            ok[o3]  = k3; ov[o3]  = v3;
            out = o2 + s23;
            p += 4;
        }
        while (out < target) {
            const std::int64_t k = p->key;
            const std::int64_t v = p->val;
            ok[out] = k;
            ov[out] = v;
            out += static_cast<std::size_t>(k != 0);
            ++p;
        }
        return target;
    }

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

    std::size_t capacity() const {
        return mask_ + 1;
    }

private:
    struct Slot {
        std::int64_t key;
        std::int64_t val;
    };

    static constexpr std::uint64_t HASH_MULT = 0x9E3779B97F4A7C15ULL;
    static constexpr std::size_t NO_SLOT = ~std::size_t{0};

    Slot* slots_;
    std::size_t mask_;
    std::size_t len_;
    std::size_t max_len_;
    std::size_t zero_slot_;
    unsigned int shift_;

    inline std::size_t home(std::int64_t key) const noexcept {
        return static_cast<std::size_t>(
            (static_cast<std::uint64_t>(key) * HASH_MULT) >> shift_);
    }

    __attribute__((noinline)) void grow() {
        const std::size_t old_cap = mask_ + 1;
        const std::size_t new_cap = old_cap << 1;
        const std::size_t new_mask = new_cap - 1;
        const unsigned int new_shift = shift_ - 1;

        Slot* const __restrict__ new_slots =
            static_cast<Slot*>(std::calloc(new_cap, sizeof(Slot)));
        if (!new_slots) [[unlikely]] {
            std::abort();
        }

        const Slot* const __restrict__ old_slots = slots_;
        for (std::size_t i = 0; i < old_cap; ++i) {
            const std::int64_t k = old_slots[i].key;
            if (k != 0) {
                std::size_t idx = static_cast<std::size_t>(
                    (static_cast<std::uint64_t>(k) * HASH_MULT) >> new_shift);
                while (new_slots[idx].key != 0) {
                    idx = (idx + 1) & new_mask;
                }
                new_slots[idx].key = k;
                new_slots[idx].val = old_slots[i].val;
            }
        }

        if (zero_slot_ != NO_SLOT) [[unlikely]] {
            const std::int64_t zv = old_slots[zero_slot_].val;
            std::size_t idx = 0;
            while (new_slots[idx].key != 0) {
                idx = (idx + 1) & new_mask;
            }
            new_slots[idx].key = 0;
            new_slots[idx].val = zv;
            zero_slot_ = idx;
        }

        std::free(slots_);
        slots_ = new_slots;
        mask_ = new_mask;
        max_len_ = new_cap - (new_cap >> 3);
        shift_ = new_shift;
    }

    __attribute__((noinline)) void grow_and_insert(std::int64_t key, std::int64_t value) {
        grow();
        Slot* slots = slots_;
        const std::size_t mask = mask_;
        std::size_t idx = home(key);
        while (slots[idx].key != 0 || idx == zero_slot_) {
            idx = (idx + 1) & mask;
        }
        slots[idx].key = key;
        slots[idx].val = value;
        ++len_;
    }

    __attribute__((noinline)) bool insert_zero(std::int64_t value) {
        if (zero_slot_ != NO_SLOT) {
            slots_[zero_slot_].val = value;
            return false;
        }
        if (len_ == max_len_) [[unlikely]] {
            grow();
        }
        Slot* slots = slots_;
        const std::size_t mask = mask_;
        std::size_t idx = 0;
        while (slots[idx].key != 0) {
            idx = (idx + 1) & mask;
        }
        slots[idx].key = 0;
        slots[idx].val = value;
        zero_slot_ = idx;
        ++len_;
        return true;
    }

    __attribute__((noinline)) bool remove_zero() {
        if (zero_slot_ == NO_SLOT) {
            return false;
        }
        std::size_t hole = zero_slot_;
        zero_slot_ = NO_SLOT;
        --len_;
        Slot* slots = slots_;
        const std::size_t mask = mask_;
        std::size_t cur = (hole + 1) & mask;
        for (;;) {
            const std::int64_t ck = slots[cur].key;
            if (ck == 0) {
                slots[hole].key = 0;
                return true;
            }
            const std::size_t h = home(ck);
            if (((cur - hole) & mask) <= ((cur - h) & mask)) {
                slots[hole] = slots[cur];
                hole = cur;
            }
            cur = (cur + 1) & mask;
        }
    }
};
