diff --git a/include/godot_cpp/templates/hashfuncs.hpp b/include/godot_cpp/templates/hashfuncs.hpp index 566b8c71a..0413e57c9 100644 --- a/include/godot_cpp/templates/hashfuncs.hpp +++ b/include/godot_cpp/templates/hashfuncs.hpp @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -319,6 +320,13 @@ struct HashMapHasherDefault { template static _FORCE_INLINE_ uint32_t hash(const Ref &p_ref) { return hash_one_uint64((uint64_t)p_ref.operator->()); } + template + static _FORCE_INLINE_ uint32_t hash(const Pair &p_pair) { + uint64_t h1 = hash(p_pair.first); + uint64_t h2 = hash(p_pair.second); + return hash_one_uint64((h1 << 32) | h2); + } + static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); } static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); } static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return hash_fmix32(uint32_t(p_wchar)); } @@ -329,6 +337,7 @@ struct HashMapHasherDefault { static _FORCE_INLINE_ uint32_t hash(const StringName &p_string_name) { return p_string_name.hash(); } static _FORCE_INLINE_ uint32_t hash(const NodePath &p_path) { return p_path.hash(); } static _FORCE_INLINE_ uint32_t hash(const ObjectID &p_id) { return hash_one_uint64(p_id); } + static _FORCE_INLINE_ uint32_t hash(const Callable &p_callable) { return p_callable.hash(); } static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); } static _FORCE_INLINE_ uint32_t hash(const int64_t p_int) { return hash_one_uint64(uint64_t(p_int)); } @@ -376,6 +385,13 @@ struct HashMapHasherDefault { h = hash_murmur3_one_real(p_vec.w, h); return hash_fmix32(h); } + static _FORCE_INLINE_ uint32_t hash(const Color &p_vec) { + uint32_t h = hash_murmur3_one_float(p_vec.r); + h = hash_murmur3_one_float(p_vec.g, h); + h = hash_murmur3_one_float(p_vec.b, h); + h = hash_murmur3_one_float(p_vec.a, h); + return hash_fmix32(h); + } static _FORCE_INLINE_ uint32_t hash(const Rect2i &p_rect) { uint32_t h = hash_murmur3_one_32(uint32_t(p_rect.position.x)); h = hash_murmur3_one_32(uint32_t(p_rect.position.y), h); diff --git a/include/godot_cpp/templates/pair.hpp b/include/godot_cpp/templates/pair.hpp index 15c7c3567..06d73e9fc 100644 --- a/include/godot_cpp/templates/pair.hpp +++ b/include/godot_cpp/templates/pair.hpp @@ -30,86 +30,70 @@ #pragma once -#include +#include namespace godot { template struct Pair { - F first; - S second; - - Pair() : - first(), - second() { - } - - Pair(F p_first, const S &p_second) : - first(p_first), - second(p_second) { - } + F first{}; + S second{}; + + constexpr Pair() = default; + constexpr Pair(const F &p_first, const S &p_second) : + first(p_first), second(p_second) {} + + constexpr bool operator==(const Pair &p_other) const { return first == p_other.first && second == p_other.second; } + constexpr bool operator!=(const Pair &p_other) const { return first != p_other.first || second != p_other.second; } + constexpr bool operator<(const Pair &p_other) const { return first == p_other.first ? (second < p_other.second) : (first < p_other.first); } + constexpr bool operator<=(const Pair &p_other) const { return first == p_other.first ? (second <= p_other.second) : (first < p_other.first); } + constexpr bool operator>(const Pair &p_other) const { return first == p_other.first ? (second > p_other.second) : (first > p_other.first); } + constexpr bool operator>=(const Pair &p_other) const { return first == p_other.first ? (second >= p_other.second) : (first > p_other.first); } }; -template -bool operator==(const Pair &pair, const Pair &other) { - return (pair.first == other.first) && (pair.second == other.second); -} - -template -bool operator!=(const Pair &pair, const Pair &other) { - return (pair.first != other.first) || (pair.second != other.second); -} - template struct PairSort { - bool operator()(const Pair &A, const Pair &B) const { - if (A.first != B.first) { - return A.first < B.first; - } - return A.second < B.second; + constexpr bool operator()(const Pair &p_lhs, const Pair &p_rhs) const { + return p_lhs < p_rhs; } }; +// Pair is zero-constructible if and only if both constrained types are zero-constructible. template -struct PairHash { - static uint32_t hash(const Pair &P) { - uint64_t h1 = HashMapHasherDefault::hash(P.first); - uint64_t h2 = HashMapHasherDefault::hash(P.second); - return hash_one_uint64((h1 << 32) | h2); - } -}; +struct is_zero_constructible> : std::conjunction, is_zero_constructible> {}; template struct KeyValue { - const K key; - V value; - - void operator=(const KeyValue &p_kv) = delete; - _FORCE_INLINE_ KeyValue(const KeyValue &p_kv) : - key(p_kv.key), - value(p_kv.value) { - } - _FORCE_INLINE_ KeyValue(const K &p_key, const V &p_value) : - key(p_key), - value(p_value) { - } + const K key{}; + V value{}; + + KeyValue &operator=(const KeyValue &p_kv) = delete; + KeyValue &operator=(KeyValue &&p_kv) = delete; + + constexpr KeyValue(const KeyValue &p_kv) = default; + constexpr KeyValue(KeyValue &&p_kv) = default; + constexpr KeyValue(const K &p_key, const V &p_value) : + key(p_key), value(p_value) {} + constexpr KeyValue(const Pair &p_pair) : + key(p_pair.first), value(p_pair.second) {} + + constexpr bool operator==(const KeyValue &p_other) const { return key == p_other.key && value == p_other.value; } + constexpr bool operator!=(const KeyValue &p_other) const { return key != p_other.key || value != p_other.value; } + constexpr bool operator<(const KeyValue &p_other) const { return key == p_other.key ? (value < p_other.value) : (key < p_other.key); } + constexpr bool operator<=(const KeyValue &p_other) const { return key == p_other.key ? (value <= p_other.value) : (key < p_other.key); } + constexpr bool operator>(const KeyValue &p_other) const { return key == p_other.key ? (value > p_other.value) : (key > p_other.key); } + constexpr bool operator>=(const KeyValue &p_other) const { return key == p_other.key ? (value >= p_other.value) : (key > p_other.key); } }; -template -bool operator==(const KeyValue &pair, const KeyValue &other) { - return (pair.key == other.key) && (pair.value == other.value); -} - -template -bool operator!=(const KeyValue &pair, const KeyValue &other) { - return (pair.key != other.key) || (pair.value != other.value); -} - template struct KeyValueSort { - bool operator()(const KeyValue &A, const KeyValue &B) const { - return A.key < B.key; + constexpr bool operator()(const KeyValue &p_lhs, const KeyValue &p_rhs) const { + return p_lhs.key < p_rhs.key; } }; +// KeyValue is zero-constructible if and only if both constrained types are zero-constructible. +template +struct is_zero_constructible> : std::conjunction, is_zero_constructible> {}; + } // namespace godot