From 4090cb34d702eab0dc4698e1b138a9ce7e0a87a9 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 8 Aug 2022 00:05:12 +0200 Subject: [PATCH] Backported helper classes to pair.h from Godot4. --- core/pair.h | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/core/pair.h b/core/pair.h index dad6fdb8a..31a95c2f5 100644 --- a/core/pair.h +++ b/core/pair.h @@ -30,6 +30,9 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#include "core/hashfuncs.h" +#include "core/typedefs.h" + template struct Pair { F first; @@ -59,7 +62,52 @@ bool operator!=(const Pair &pair, const Pair &other) { template struct PairSort { bool operator()(const Pair &A, const Pair &B) const { - return A.first < B.first; + if (A.first != B.first) { + return A.first < B.first; + } + return A.second < B.second; + } +}; + +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); + } +}; + +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) { + } +}; + +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; } };