From e1496a87178d925277aceed0ebcbab06920e15ee Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Mon, 25 Dec 2023 02:42:11 -0500 Subject: Add `https://github.com/Tessil/ordered-map` Add is_specialization_of to Utility.hpp Add OpenVic::ordered_map and OpenVic::ordered_set Change `std::map` to `ordered_map` Change `std::set to use `ordered_set` Add `set_callback_pointer(tsl::ordered_set& set)` Add mutable_iterator to enable mutable value iterator for `tsl::ordered_map` Add std::hash implementation Enable deps/SCsub to expose dependency includes neccessary for inclusion --- src/openvic-simulation/utility/Utility.hpp | 48 ++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'src/openvic-simulation/utility/Utility.hpp') diff --git a/src/openvic-simulation/utility/Utility.hpp b/src/openvic-simulation/utility/Utility.hpp index e8d7205..0387e7f 100644 --- a/src/openvic-simulation/utility/Utility.hpp +++ b/src/openvic-simulation/utility/Utility.hpp @@ -1,5 +1,9 @@ #pragma once +#include +#include +#include + namespace OpenVic::utility { [[noreturn]] inline void unreachable() { // Uses compiler specific extensions if possible. @@ -11,4 +15,48 @@ namespace OpenVic::utility { __assume(false); #endif } + + template + constexpr inline void hash_combine(std::size_t& s, const T& v) { + std::hash h; + s ^= h(v) + 0x9e3779b9 + (s << 6) + (s >> 2); + } + + template + constexpr inline void hash_combine_index(std::size_t& s, const T& v) { + std::hash h; + if constexpr(Shift == 0) { + s = h(v); + } else { + s ^= h(v) << Shift; + } + } + + template + constexpr void perfect_hash(std::size_t& s, T&& v, Args&&... args) { + static_assert(sizeof(T) + (sizeof(Args) + ...) <= sizeof(std::size_t), "Perfect hashes must be able to fit into size_t"); + std::hash h; + if constexpr(sizeof...(args) == 0) { + s = h(v); + } else { + const std::tuple arg_tuple { args... }; + s = h(v) << (sizeof(T) * CHAR_BIT); + ([&]{ + // If args is not last pointer of args + if (static_cast(&(std::get(arg_tuple))) != static_cast(&args)) { + s <<= sizeof(Args) * CHAR_BIT; + } + s |= std::hash{}(args); + }(), ...); + } + } + + template class Z> + struct is_specialization_of : std::false_type {}; + + template class Z> + struct is_specialization_of, Z> : std::true_type {}; + + template class Z> + inline constexpr bool is_specialization_of_v = is_specialization_of::value; } -- cgit v1.2.3-56-ga3b1