Skip to content

Commit 5538850

Browse files
committed
Core: Convert Pair/KeyValue to constexpr
1 parent 5e27318 commit 5538850

File tree

8 files changed

+50
-64
lines changed

8 files changed

+50
-64
lines changed

core/math/a_star.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class AStar3D : public RefCounted {
8989
unsigned char direction = NONE;
9090

9191
static uint32_t hash(const Segment &p_seg) {
92-
return PairHash<int64_t, int64_t>().hash(p_seg.key);
92+
return HashMapHasherDefault::hash(p_seg.key);
9393
}
9494
bool operator==(const Segment &p_s) const { return key == p_s.key; }
9595

core/os/os.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ class OS {
7777
// For tracking benchmark data
7878
bool use_benchmark = false;
7979
String benchmark_file;
80-
HashMap<Pair<String, String>, uint64_t, PairHash<String, String>> benchmark_marks_from;
81-
HashMap<Pair<String, String>, double, PairHash<String, String>> benchmark_marks_final;
80+
HashMap<Pair<String, String>, uint64_t> benchmark_marks_from;
81+
HashMap<Pair<String, String>, double> benchmark_marks_final;
8282

8383
protected:
8484
void _set_logger(CompositeLogger *p_logger);

core/templates/cowdata.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040

4141
static_assert(std::is_trivially_destructible_v<std::atomic<uint64_t>>);
4242

43-
GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wplacement-new") // Silence a false positive warning (see GH-52119).
43+
GODOT_GCC_WARNING_PUSH
44+
GODOT_GCC_WARNING_IGNORE("-Wplacement-new") // Silence a false positive warning (see GH-52119).
45+
GODOT_GCC_WARNING_IGNORE("-Wmaybe-uninitialized") // False positive raised when using constexpr.
4446

4547
template <typename T>
4648
class CowData {

core/templates/hashfuncs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "core/string/node_path.h"
5353
#include "core/string/string_name.h"
5454
#include "core/string/ustring.h"
55+
#include "core/templates/pair.h"
5556
#include "core/templates/rid.h"
5657
#include "core/typedefs.h"
5758

@@ -324,6 +325,13 @@ struct HashMapHasherDefault {
324325
template <typename T>
325326
static _FORCE_INLINE_ uint32_t hash(const Ref<T> &p_ref) { return hash_one_uint64((uint64_t)p_ref.operator->()); }
326327

328+
template <typename F, typename S>
329+
static _FORCE_INLINE_ uint32_t hash(const Pair<F, S> &p_pair) {
330+
uint64_t h1 = hash(p_pair.first);
331+
uint64_t h2 = hash(p_pair.second);
332+
return hash_one_uint64((h1 << 32) | h2);
333+
}
334+
327335
static _FORCE_INLINE_ uint32_t hash(const String &p_string) { return p_string.hash(); }
328336
static _FORCE_INLINE_ uint32_t hash(const char *p_cstr) { return hash_djb2(p_cstr); }
329337
static _FORCE_INLINE_ uint32_t hash(const wchar_t p_wchar) { return hash_fmix32(uint32_t(p_wchar)); }

core/templates/pair.h

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -30,50 +30,29 @@
3030

3131
#pragma once
3232

33-
#include "core/templates/hashfuncs.h"
3433
#include "core/typedefs.h"
34+
3535
template <typename F, typename S>
3636
struct Pair {
37-
F first;
38-
S second;
37+
F first{};
38+
S second{};
3939

40-
Pair() :
41-
first(),
42-
second() {
43-
}
40+
constexpr Pair() = default;
41+
constexpr Pair(const F &p_first, const S &p_second) :
42+
first(p_first), second(p_second) {}
4443

45-
Pair(F p_first, const S &p_second) :
46-
first(p_first),
47-
second(p_second) {
48-
}
44+
constexpr bool operator==(const Pair &p_other) const { return first == p_other.first && second == p_other.second; }
45+
constexpr bool operator!=(const Pair &p_other) const { return first != p_other.first || second != p_other.second; }
46+
constexpr bool operator<(const Pair &p_other) const { return first == p_other.first ? (second < p_other.second) : (first < p_other.first); }
47+
constexpr bool operator<=(const Pair &p_other) const { return first == p_other.first ? (second <= p_other.second) : (first < p_other.first); }
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); }
4950
};
5051

51-
template <typename F, typename S>
52-
bool operator==(const Pair<F, S> &pair, const Pair<F, S> &other) {
53-
return (pair.first == other.first) && (pair.second == other.second);
54-
}
55-
56-
template <typename F, typename S>
57-
bool operator!=(const Pair<F, S> &pair, const Pair<F, S> &other) {
58-
return (pair.first != other.first) || (pair.second != other.second);
59-
}
60-
6152
template <typename F, typename S>
6253
struct PairSort {
63-
bool operator()(const Pair<F, S> &A, const Pair<F, S> &B) const {
64-
if (A.first != B.first) {
65-
return A.first < B.first;
66-
}
67-
return A.second < B.second;
68-
}
69-
};
70-
71-
template <typename F, typename S>
72-
struct PairHash {
73-
static uint32_t hash(const Pair<F, S> &P) {
74-
uint64_t h1 = HashMapHasherDefault::hash(P.first);
75-
uint64_t h2 = HashMapHasherDefault::hash(P.second);
76-
return hash_one_uint64((h1 << 32) | h2);
54+
constexpr bool operator()(const Pair<F, S> &p_lhs, const Pair<F, S> &p_rhs) const {
55+
return p_lhs < p_rhs;
7756
}
7857
};
7958

@@ -83,34 +62,31 @@ struct is_zero_constructible<Pair<F, S>> : std::conjunction<is_zero_constructibl
8362

8463
template <typename K, typename V>
8564
struct KeyValue {
86-
const K key;
87-
V value;
65+
const K key{};
66+
V value{};
8867

89-
void operator=(const KeyValue &p_kv) = delete;
90-
_FORCE_INLINE_ KeyValue(const KeyValue &p_kv) :
91-
key(p_kv.key),
92-
value(p_kv.value) {
93-
}
94-
_FORCE_INLINE_ KeyValue(const K &p_key, const V &p_value) :
95-
key(p_key),
96-
value(p_value) {
97-
}
98-
};
68+
KeyValue &operator=(const KeyValue &p_kv) = delete;
69+
KeyValue &operator=(KeyValue &&p_kv) = delete;
9970

100-
template <typename K, typename V>
101-
bool operator==(const KeyValue<K, V> &pair, const KeyValue<K, V> &other) {
102-
return (pair.key == other.key) && (pair.value == other.value);
103-
}
71+
constexpr KeyValue(const KeyValue &p_kv) = default;
72+
constexpr KeyValue(KeyValue &&p_kv) = default;
73+
constexpr KeyValue(const K &p_key, const V &p_value) :
74+
key(p_key), value(p_value) {}
75+
constexpr KeyValue(const Pair<K, V> &p_pair) :
76+
key(p_pair.first), value(p_pair.second) {}
10477

105-
template <typename K, typename V>
106-
bool operator!=(const KeyValue<K, V> &pair, const KeyValue<K, V> &other) {
107-
return (pair.key != other.key) || (pair.value != other.value);
108-
}
78+
constexpr bool operator==(const KeyValue &p_other) const { return key == p_other.key && value == p_other.value; }
79+
constexpr bool operator!=(const KeyValue &p_other) const { return key != p_other.key || value != p_other.value; }
80+
constexpr bool operator<(const KeyValue &p_other) const { return key == p_other.key ? (value < p_other.value) : (key < p_other.key); }
81+
constexpr bool operator<=(const KeyValue &p_other) const { return key == p_other.key ? (value <= p_other.value) : (key < p_other.key); }
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+
};
10985

11086
template <typename K, typename V>
11187
struct KeyValueSort {
112-
bool operator()(const KeyValue<K, V> &A, const KeyValue<K, V> &B) const {
113-
return A.key < B.key;
88+
constexpr bool operator()(const KeyValue<K, V> &p_lhs, const KeyValue<K, V> &p_rhs) const {
89+
return p_lhs.key < p_rhs.key;
11490
}
11591
};
11692

modules/fbx/fbx_state.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class FBXState : public GLTFState {
4949
bool allow_geometry_helper_nodes = false;
5050

5151
HashMap<uint64_t, Image::AlphaMode> alpha_mode_cache;
52-
HashMap<Pair<uint64_t, uint64_t>, GLTFTextureIndex, PairHash<uint64_t, uint64_t>> albedo_transparency_textures;
52+
HashMap<Pair<uint64_t, uint64_t>, GLTFTextureIndex> albedo_transparency_textures;
5353

5454
Vector<GLTFSkinIndex> skin_indices;
5555
Vector<GLTFSkinIndex> original_skin_indices;

scene/main/viewport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ void Viewport::_process_picking() {
896896
send_event = false;
897897
}
898898
}
899-
HashMap<Pair<ObjectID, int>, uint64_t, PairHash<ObjectID, int>>::Iterator SF = physics_2d_shape_mouseover.find(Pair(res[i].collider_id, res[i].shape));
899+
HashMap<Pair<ObjectID, int>, uint64_t>::Iterator SF = physics_2d_shape_mouseover.find(Pair(res[i].collider_id, res[i].shape));
900900
if (!SF) {
901901
physics_2d_shape_mouseover.insert(Pair(res[i].collider_id, res[i].shape), frame);
902902
co->_mouse_shape_enter(res[i].shape);

scene/main/viewport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ class Viewport : public Node {
751751
// Collider to frame
752752
HashMap<ObjectID, uint64_t> physics_2d_mouseover;
753753
// Collider & shape to frame
754-
HashMap<Pair<ObjectID, int>, uint64_t, PairHash<ObjectID, int>> physics_2d_shape_mouseover;
754+
HashMap<Pair<ObjectID, int>, uint64_t> physics_2d_shape_mouseover;
755755
// Cleans up colliders corresponding to old frames or all of them.
756756
void _cleanup_mouseover_colliders(bool p_clean_all_frames, bool p_paused_only, uint64_t p_frame_reference = 0);
757757
#endif // PHYSICS_2D_DISABLED

0 commit comments

Comments
 (0)