/*
 * Copyright 2015 - 2025 gary@drinkingtea.net
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#pragma once

#include "algorithm.hpp"
#include "hash.hpp"
#include "ignore.hpp"
#include "optional.hpp"
#include "stringview.hpp"
#include "strops.hpp"
#include "vector.hpp"

namespace ox {

template<typename K, typename T>
class HashMap {

	public:
		using key_t = K;
		using value_t = T;

	private:
		struct Pair {
			UPtr<Pair> next;
			K key = {};
			T value{};
		};
		Vector<K> m_keys;
		Vector<UPtr<Pair>> m_pairs;

	public:
		explicit constexpr HashMap(std::size_t size = 127);

		constexpr HashMap(HashMap const&other);

		constexpr HashMap(HashMap &&other) noexcept;

		constexpr ~HashMap();

		constexpr bool operator==(HashMap const&other) const;

		constexpr HashMap &operator=(HashMap const&other);

		constexpr HashMap &operator=(HashMap &&other) noexcept;

		constexpr T &operator[](MaybeView_t<K> const&key);

		constexpr Result<T*> at(MaybeView_t<K> const&key) noexcept;

		constexpr Result<const T*> at(MaybeView_t<K> const&key) const noexcept;

		constexpr void erase(MaybeView_t<K> const&key);

		[[nodiscard]]
		constexpr bool contains(MaybeView_t<K> const&key) const noexcept;

		[[nodiscard]]
		constexpr std::size_t size() const noexcept;

		[[nodiscard]]
		constexpr Vector<K> const&keys() const noexcept;

		[[nodiscard]]
		constexpr Vector<T> values() const noexcept;

		constexpr void clear();

	private:
		constexpr void expand();

		template<typename KK>
		constexpr UPtr<Pair> const &access(Vector<UPtr<Pair>> const &pairs, KK const &key) const;

		template<typename KK>
		constexpr UPtr<Pair> &access(Vector<UPtr<Pair>> &pairs, KK const &key);

};

template<typename K, typename T>
constexpr HashMap<K, T>::HashMap(std::size_t size): m_pairs(size) {
}

template<typename K, typename T>
constexpr HashMap<K, T>::HashMap(HashMap const &other) {
	operator=(other);
}

template<typename K, typename T>
constexpr HashMap<K, T>::HashMap(HashMap &&other) noexcept {
	operator=(std::move(other));
}

template<typename K, typename T>
constexpr HashMap<K, T>::~HashMap() {
	clear();
}

template<typename K, typename T>
constexpr bool HashMap<K, T>::operator==(HashMap const &other) const {
	if (m_keys != other.m_keys) {
		return false;
	}
	for (auto &k : m_keys) {
		auto const a = at(k).value;
		auto const b = other.at(k).value;
		if (
			a != b && // handle one being null and other not
			(a && b && *a != *b)) {
			return false;
		}
	}
	return true;
}

template<typename K, typename T>
constexpr HashMap<K, T> &HashMap<K, T>::operator=(HashMap const &other) {
	if (this != &other) {
		clear();
		m_keys = other.m_keys;
		m_pairs.resize(other.m_pairs.size());
		for (auto const&k : m_keys) {
			auto const &src = access(other.m_pairs, k);
			auto &dst = access(m_pairs, k);
			dst = ox::make_unique<Pair>();
			dst->key = src->key;
			dst->value = src->value;
		}
	}
	return *this;
}

template<typename K, typename T>
constexpr HashMap<K, T> &HashMap<K, T>::operator=(HashMap &&other) noexcept {
	if (this != &other) {
		m_keys = std::move(other.m_keys);
		m_pairs = std::move(other.m_pairs);
	}
	return *this;
}

template<typename K, typename T>
constexpr T &HashMap<K, T>::operator[](MaybeView_t<K> const &key) {
	auto p = &access(m_pairs, key);
	if (*p == nullptr) {
		if (static_cast<double>(m_pairs.size()) * 0.7 <
			 static_cast<double>(m_keys.size())) {
			expand();
			p = &access(m_pairs, key);
		}
		*p = ox::make_unique<Pair>();
		(*p)->key = key;
		m_keys.emplace_back(key);
	}
	return (*p)->value;
}

template<typename K, typename T>
constexpr Result<T*> HashMap<K, T>::at(MaybeView_t<K> const &key) noexcept {
	auto &p = access(m_pairs, key);
	if (!p) {
		return ox::Error{1, "value not found for given key"};
	}
	return &p->value;
}

template<typename K, typename T>
constexpr Result<const T*> HashMap<K, T>::at(MaybeView_t<K> const &key) const noexcept {
	auto &p = access(m_pairs, key);
	if (!p) {
		return ox::Error{1, "value not found for given key"};
	}
	return &p->value;
}

template<typename K, typename T>
constexpr void HashMap<K, T>::erase(MaybeView_t<K> const &key) {
	if (!contains(key)) {
		return;
	}
	auto &c = access(m_pairs, key);
	c = std::move(c->next);
	std::ignore = m_keys.erase(ox::find(m_keys.begin(), m_keys.end(), key));
}

template<typename K, typename T>
constexpr bool HashMap<K, T>::contains(MaybeView_t<K> const &key) const noexcept {
	return access(m_pairs, key).get() != nullptr;
}

template<typename K, typename T>
constexpr std::size_t HashMap<K, T>::size() const noexcept {
	return m_keys.size();
}

template<typename K, typename T>
constexpr Vector<K> const &HashMap<K, T>::keys() const noexcept {
	return m_keys;
}

template<typename K, typename T>
constexpr Vector<T> HashMap<K, T>::values() const noexcept {
	Vector<T> out;
	out.reserve(m_keys.size());
	for (auto const &p : m_pairs) {
		if (p) {
			out.emplace_back(*p->value);
		}
	}
	return out;
}

template<typename K, typename T>
constexpr void HashMap<K, T>::clear() {
	m_pairs.clear();
	m_pairs.resize(127);
}

template<typename K, typename T>
constexpr void HashMap<K, T>::expand() {
	Vector<UPtr<Pair>> r{m_pairs.size() * 2};
	for (std::size_t i = 0; i < m_keys.size(); ++i) {
		auto const &k = m_keys[i];
		access(r, k) = std::move(access(m_pairs, k));
	}
	m_pairs = std::move(r);
}

template<typename K, typename T>
template<typename KK>
constexpr UPtr<typename HashMap<K, T>::Pair> const &HashMap<K, T>::access(
		Vector<UPtr<Pair>> const& pairs,
		KK const &key) const {
	auto const h = static_cast<std::size_t>(ox::hash<KK>{}(key) % pairs.size());
	auto const &p = *pairs.at(h).unwrap();
	if (p == nullptr || p->key == key) {
		return p;
	}
	auto c = &p->next;
	while (true) {
		if (*c == nullptr || (*c)->key == key) {
			return *c;
		}
		c = &(*c)->next;
	}
}

template<typename K, typename T>
template<typename KK>
constexpr UPtr<typename HashMap<K, T>::Pair> &HashMap<K, T>::access(
		Vector<UPtr<Pair>> &pairs,
		KK const &key) {
	auto const h = static_cast<std::size_t>(ox::hash<KK>{}(key) % pairs.size());
	auto &p = *pairs.at(h).unwrap();
	if (p == nullptr || p->key == key) {
		return p;
	}
	auto c = &p->next;
	while (true) {
		if (*c == nullptr || (*c)->key == key) {
			return *c;
		}
		c = &(*c)->next;
	}
}

}
