Skip to content

Commit fc70347

Browse files
committed
Update missing hashfuncs
1 parent 28e8b1a commit fc70347

File tree

2 files changed

+59
-59
lines changed

2 files changed

+59
-59
lines changed

include/godot_cpp/templates/hashfuncs.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
#include <godot_cpp/core/math.hpp>
3939
#include <godot_cpp/core/object.hpp>
40+
#include <godot_cpp/templates/pair.hpp>
4041
#include <godot_cpp/variant/aabb.hpp>
4142
#include <godot_cpp/variant/node_path.hpp>
4243
#include <godot_cpp/variant/rect2.hpp>
@@ -319,6 +320,13 @@ struct HashMapHasherDefault {
319320
template <typename T>
320321
static _FORCE_INLINE_ uint32_t hash(const Ref<T> &p_ref) { return hash_one_uint64((uint64_t)p_ref.operator->()); }
321322

323+
template <typename F, typename S>
324+
static _FORCE_INLINE_ uint32_t hash(const Pair<F, S> &p_pair) {
325+
uint64_t h1 = hash(p_pair.first);
326+
uint64_t h2 = hash(p_pair.second);
327+
return hash_one_uint64((h1 << 32) | h2);
328+
}
329+
322330
static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); }
323331
static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); }
324332
static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return hash_fmix32(uint32_t(p_wchar)); }
@@ -329,6 +337,7 @@ struct HashMapHasherDefault {
329337
static _FORCE_INLINE_ uint32_t hash(const StringName &p_string_name) { return p_string_name.hash(); }
330338
static _FORCE_INLINE_ uint32_t hash(const NodePath &p_path) { return p_path.hash(); }
331339
static _FORCE_INLINE_ uint32_t hash(const ObjectID &p_id) { return hash_one_uint64(p_id); }
340+
static _FORCE_INLINE_ uint32_t hash(const Callable &p_callable) { return p_callable.hash(); }
332341

333342
static _FORCE_INLINE_ uint32_t hash(const uint64_t p_int) { return hash_one_uint64(p_int); }
334343
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 {
376385
h = hash_murmur3_one_real(p_vec.w, h);
377386
return hash_fmix32(h);
378387
}
388+
static _FORCE_INLINE_ uint32_t hash(const Color &p_vec) {
389+
uint32_t h = hash_murmur3_one_float(p_vec.r);
390+
h = hash_murmur3_one_float(p_vec.g, h);
391+
h = hash_murmur3_one_float(p_vec.b, h);
392+
h = hash_murmur3_one_float(p_vec.a, h);
393+
return hash_fmix32(h);
394+
}
379395
static _FORCE_INLINE_ uint32_t hash(const Rect2i &p_rect) {
380396
uint32_t h = hash_murmur3_one_32(uint32_t(p_rect.position.x));
381397
h = hash_murmur3_one_32(uint32_t(p_rect.position.y), h);

include/godot_cpp/templates/pair.hpp

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -30,86 +30,70 @@
3030

3131
#pragma once
3232

33-
#include <godot_cpp/templates/hashfuncs.hpp>
33+
#include <godot_cpp/core/defs.hpp>
3434

3535
namespace godot {
3636

3737
template <typename F, typename S>
3838
struct Pair {
39-
F first;
40-
S second;
41-
42-
Pair() :
43-
first(),
44-
second() {
45-
}
46-
47-
Pair(F p_first, const S &p_second) :
48-
first(p_first),
49-
second(p_second) {
50-
}
39+
F first{};
40+
S second{};
41+
42+
constexpr Pair() = default;
43+
constexpr Pair(const F &p_first, const S &p_second) :
44+
first(p_first), second(p_second) {}
45+
46+
constexpr bool operator==(const Pair &p_other) const { return first == p_other.first && second == p_other.second; }
47+
constexpr bool operator!=(const Pair &p_other) const { return first != p_other.first || second != p_other.second; }
48+
constexpr bool operator<(const Pair &p_other) const { return first == p_other.first ? (second < p_other.second) : (first < p_other.first); }
49+
constexpr bool operator<=(const Pair &p_other) const { return first == p_other.first ? (second <= p_other.second) : (first < p_other.first); }
50+
constexpr bool operator>(const Pair &p_other) const { return first == p_other.first ? (second > p_other.second) : (first > p_other.first); }
51+
constexpr bool operator>=(const Pair &p_other) const { return first == p_other.first ? (second >= p_other.second) : (first > p_other.first); }
5152
};
5253

53-
template <typename F, typename S>
54-
bool operator==(const Pair<F, S> &pair, const Pair<F, S> &other) {
55-
return (pair.first == other.first) && (pair.second == other.second);
56-
}
57-
58-
template <typename F, typename S>
59-
bool operator!=(const Pair<F, S> &pair, const Pair<F, S> &other) {
60-
return (pair.first != other.first) || (pair.second != other.second);
61-
}
62-
6354
template <typename F, typename S>
6455
struct PairSort {
65-
bool operator()(const Pair<F, S> &A, const Pair<F, S> &B) const {
66-
if (A.first != B.first) {
67-
return A.first < B.first;
68-
}
69-
return A.second < B.second;
56+
constexpr bool operator()(const Pair<F, S> &p_lhs, const Pair<F, S> &p_rhs) const {
57+
return p_lhs < p_rhs;
7058
}
7159
};
7260

61+
// Pair is zero-constructible if and only if both constrained types are zero-constructible.
7362
template <typename F, typename S>
74-
struct PairHash {
75-
static uint32_t hash(const Pair<F, S> &P) {
76-
uint64_t h1 = HashMapHasherDefault::hash(P.first);
77-
uint64_t h2 = HashMapHasherDefault::hash(P.second);
78-
return hash_one_uint64((h1 << 32) | h2);
79-
}
80-
};
63+
struct is_zero_constructible<Pair<F, S>> : std::conjunction<is_zero_constructible<F>, is_zero_constructible<S>> {};
8164

8265
template <typename K, typename V>
8366
struct KeyValue {
84-
const K key;
85-
V value;
86-
87-
void operator=(const KeyValue &p_kv) = delete;
88-
_FORCE_INLINE_ KeyValue(const KeyValue &p_kv) :
89-
key(p_kv.key),
90-
value(p_kv.value) {
91-
}
92-
_FORCE_INLINE_ KeyValue(const K &p_key, const V &p_value) :
93-
key(p_key),
94-
value(p_value) {
95-
}
67+
const K key{};
68+
V value{};
69+
70+
KeyValue &operator=(const KeyValue &p_kv) = delete;
71+
KeyValue &operator=(KeyValue &&p_kv) = delete;
72+
73+
constexpr KeyValue(const KeyValue &p_kv) = default;
74+
constexpr KeyValue(KeyValue &&p_kv) = default;
75+
constexpr KeyValue(const K &p_key, const V &p_value) :
76+
key(p_key), value(p_value) {}
77+
constexpr KeyValue(const Pair<K, V> &p_pair) :
78+
key(p_pair.first), value(p_pair.second) {}
79+
80+
constexpr bool operator==(const KeyValue &p_other) const { return key == p_other.key && value == p_other.value; }
81+
constexpr bool operator!=(const KeyValue &p_other) const { return key != p_other.key || value != p_other.value; }
82+
constexpr bool operator<(const KeyValue &p_other) const { return key == p_other.key ? (value < p_other.value) : (key < p_other.key); }
83+
constexpr bool operator<=(const KeyValue &p_other) const { return key == p_other.key ? (value <= p_other.value) : (key < p_other.key); }
84+
constexpr bool operator>(const KeyValue &p_other) const { return key == p_other.key ? (value > p_other.value) : (key > p_other.key); }
85+
constexpr bool operator>=(const KeyValue &p_other) const { return key == p_other.key ? (value >= p_other.value) : (key > p_other.key); }
9686
};
9787

98-
template <typename K, typename V>
99-
bool operator==(const KeyValue<K, V> &pair, const KeyValue<K, V> &other) {
100-
return (pair.key == other.key) && (pair.value == other.value);
101-
}
102-
103-
template <typename K, typename V>
104-
bool operator!=(const KeyValue<K, V> &pair, const KeyValue<K, V> &other) {
105-
return (pair.key != other.key) || (pair.value != other.value);
106-
}
107-
10888
template <typename K, typename V>
10989
struct KeyValueSort {
110-
bool operator()(const KeyValue<K, V> &A, const KeyValue<K, V> &B) const {
111-
return A.key < B.key;
90+
constexpr bool operator()(const KeyValue<K, V> &p_lhs, const KeyValue<K, V> &p_rhs) const {
91+
return p_lhs.key < p_rhs.key;
11292
}
11393
};
11494

95+
// KeyValue is zero-constructible if and only if both constrained types are zero-constructible.
96+
template <typename K, typename V>
97+
struct is_zero_constructible<KeyValue<K, V>> : std::conjunction<is_zero_constructible<K>, is_zero_constructible<V>> {};
98+
11599
} // namespace godot

0 commit comments

Comments
 (0)