diff options
author | George L. Albany <Megacake1234@gmail.com> | 2024-01-01 04:32:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-01 04:32:01 +0100 |
commit | 9988b21278dc1c8df044631bd2935a7e450a7bff (patch) | |
tree | ba081f9f4d74865ab5851a2efd560745900ca81a /src/openvic-simulation/utility/Utility.hpp | |
parent | 0a425fbe05d6138b753c0e4a7c06f06695bde8af (diff) | |
parent | e1496a87178d925277aceed0ebcbab06920e15ee (diff) |
Merge pull request #105 from OpenVicProject/add/ordered-map
Diffstat (limited to 'src/openvic-simulation/utility/Utility.hpp')
-rw-r--r-- | src/openvic-simulation/utility/Utility.hpp | 48 |
1 files changed, 48 insertions, 0 deletions
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 <climits> +#include <functional> +#include <type_traits> + namespace OpenVic::utility { [[noreturn]] inline void unreachable() { // Uses compiler specific extensions if possible. @@ -11,4 +15,48 @@ namespace OpenVic::utility { __assume(false); #endif } + + template<class T> + constexpr inline void hash_combine(std::size_t& s, const T& v) { + std::hash<T> h; + s ^= h(v) + 0x9e3779b9 + (s << 6) + (s >> 2); + } + + template<size_t Shift, class T> + constexpr inline void hash_combine_index(std::size_t& s, const T& v) { + std::hash<T> h; + if constexpr(Shift == 0) { + s = h(v); + } else { + s ^= h(v) << Shift; + } + } + + template<class T, typename ...Args> + 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<T> 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<const void*>(&(std::get<sizeof...(args) - 1>(arg_tuple))) != static_cast<const void*>(&args)) { + s <<= sizeof(Args) * CHAR_BIT; + } + s |= std::hash<Args>{}(args); + }(), ...); + } + } + + template<typename T, template<typename...> class Z> + struct is_specialization_of : std::false_type {}; + + template<typename... Args, template<typename...> class Z> + struct is_specialization_of<Z<Args...>, Z> : std::true_type {}; + + template<typename T, template<typename...> class Z> + inline constexpr bool is_specialization_of_v = is_specialization_of<T, Z>::value; } |