Skip to content

Commit b2a441d

Browse files
committed
Rewrite HashMapHasherDefault based on type traits - it is now possible to declare a default hashing function for any type.
Remove cross-project includes from `hashfuncs.h`. Improve hashing function for `Color` (based on values instead of `String`). Move `Variant` comparison from `hash_map.h` to `dictionary.cpp` (`VariantComparatorDictionary`), where it's used. Remove now unnecessary `HashableHasher`.
1 parent 428a762 commit b2a441d

32 files changed

+234
-227
lines changed

core/math/aabb.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/math/plane.h"
3434
#include "core/math/vector3.h"
35+
#include "core/templates/hashfuncs.h"
3536

3637
/**
3738
* AABB (Axis Aligned Bounding Box)
@@ -131,6 +132,16 @@ struct [[nodiscard]] AABB {
131132
return position + (size * 0.5f);
132133
}
133134

135+
uint32_t hash() const {
136+
uint32_t h = hash_murmur3_one_real(position.x);
137+
h = hash_murmur3_one_real(position.y, h);
138+
h = hash_murmur3_one_real(position.z, h);
139+
h = hash_murmur3_one_real(size.x, h);
140+
h = hash_murmur3_one_real(size.y, h);
141+
h = hash_murmur3_one_real(size.z, h);
142+
return hash_fmix32(h);
143+
}
144+
134145
operator String() const;
135146

136147
AABB() = default;

core/math/color.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#pragma once
3232

3333
#include "core/math/math_funcs.h"
34+
#include "core/templates/hashfuncs.h"
3435

3536
class String;
3637

@@ -237,6 +238,14 @@ struct [[nodiscard]] Color {
237238
_FORCE_INLINE_ void set_ok_hsl_s(float p_s) { set_ok_hsl(get_ok_hsl_h(), p_s, get_ok_hsl_l(), a); }
238239
_FORCE_INLINE_ void set_ok_hsl_l(float p_l) { set_ok_hsl(get_ok_hsl_h(), get_ok_hsl_s(), p_l, a); }
239240

241+
uint32_t hash() const {
242+
uint32_t h = hash_murmur3_one_float(r);
243+
h = hash_murmur3_one_float(r, h);
244+
h = hash_murmur3_one_float(b, h);
245+
h = hash_murmur3_one_float(a, h);
246+
return hash_fmix32(h);
247+
}
248+
240249
constexpr Color() :
241250
r(0), g(0), b(0), a(1) {}
242251

core/math/delaunay_3d.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "core/math/aabb.h"
3434
#include "core/math/projection.h"
3535
#include "core/math/vector3.h"
36+
#include "core/math/vector3i.h"
3637
#include "core/templates/list.h"
3738
#include "core/templates/local_vector.h"
3839
#include "core/templates/oa_hash_map.h"

core/math/geometry_3d.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030

3131
#pragma once
3232

33+
#include "core/math/color.h"
3334
#include "core/math/delaunay_3d.h"
3435
#include "core/math/face3.h"
36+
#include "core/math/vector2.h"
3537
#include "core/templates/local_vector.h"
3638
#include "core/templates/vector.h"
3739

core/math/rect2.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/error/error_macros.h"
3434
#include "core/math/vector2.h"
35+
#include "core/templates/hashfuncs.h"
3536

3637
class String;
3738
struct Rect2i;
@@ -361,6 +362,14 @@ struct [[nodiscard]] Rect2 {
361362
operator String() const;
362363
operator Rect2i() const;
363364

365+
uint32_t hash() const {
366+
uint32_t h = hash_murmur3_one_real(position.x);
367+
h = hash_murmur3_one_real(position.y, h);
368+
h = hash_murmur3_one_real(size.x, h);
369+
h = hash_murmur3_one_real(size.y, h);
370+
return hash_fmix32(h);
371+
}
372+
364373
Rect2() = default;
365374
constexpr Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) :
366375
position(Point2(p_x, p_y)),

core/math/rect2i.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/error/error_macros.h"
3434
#include "core/math/vector2i.h"
35+
#include "core/templates/hashfuncs.h"
3536

3637
class String;
3738
struct Rect2;
@@ -226,6 +227,14 @@ struct [[nodiscard]] Rect2i {
226227
operator String() const;
227228
operator Rect2() const;
228229

230+
uint32_t hash() const {
231+
uint32_t h = hash_murmur3_one_32(uint32_t(position.x));
232+
h = hash_murmur3_one_32(uint32_t(position.y), h);
233+
h = hash_murmur3_one_32(uint32_t(size.x), h);
234+
h = hash_murmur3_one_32(uint32_t(size.y), h);
235+
return hash_fmix32(h);
236+
}
237+
229238
Rect2i() = default;
230239
constexpr Rect2i(int p_x, int p_y, int p_width, int p_height) :
231240
position(Point2i(p_x, p_y)),

core/math/vector2.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/error/error_macros.h"
3434
#include "core/math/math_funcs.h"
35+
#include "core/templates/hashfuncs.h"
3536

3637
class String;
3738
struct Vector2i;
@@ -185,6 +186,12 @@ struct [[nodiscard]] Vector2 {
185186
operator String() const;
186187
operator Vector2i() const;
187188

189+
uint32_t hash() const {
190+
uint32_t h = hash_murmur3_one_real(x);
191+
h = hash_murmur3_one_real(y, h);
192+
return hash_fmix32(h);
193+
}
194+
188195
// NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
189196
constexpr Vector2() :
190197
x(0), y(0) {}

core/math/vector2i.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/error/error_macros.h"
3434
#include "core/math/math_funcs.h"
35+
#include "core/templates/hashfuncs.h"
3536

3637
class String;
3738
struct Vector2;
@@ -142,6 +143,12 @@ struct [[nodiscard]] Vector2i {
142143
operator String() const;
143144
operator Vector2() const;
144145

146+
uint32_t hash() const {
147+
uint32_t h = hash_murmur3_one_32(uint32_t(x));
148+
h = hash_murmur3_one_32(uint32_t(y), h);
149+
return hash_fmix32(h);
150+
}
151+
145152
// NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
146153
constexpr Vector2i() :
147154
x(0), y(0) {}

core/math/vector3.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ struct [[nodiscard]] Vector3 {
189189
operator String() const;
190190
operator Vector3i() const;
191191

192+
uint32_t hash() const {
193+
uint32_t h = hash_murmur3_one_real(x);
194+
h = hash_murmur3_one_real(y, h);
195+
h = hash_murmur3_one_real(z, h);
196+
return hash_fmix32(h);
197+
}
198+
192199
constexpr Vector3() :
193200
x(0), y(0), z(0) {}
194201
constexpr Vector3(real_t p_x, real_t p_y, real_t p_z) :

core/math/vector3i.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "core/error/error_macros.h"
3434
#include "core/math/math_funcs.h"
35+
#include "core/templates/hashfuncs.h"
3536

3637
class String;
3738
struct Vector3;
@@ -133,6 +134,13 @@ struct [[nodiscard]] Vector3i {
133134
operator String() const;
134135
operator Vector3() const;
135136

137+
uint32_t hash() const {
138+
uint32_t h = hash_murmur3_one_32(uint32_t(x));
139+
h = hash_murmur3_one_32(uint32_t(y), h);
140+
h = hash_murmur3_one_32(uint32_t(z), h);
141+
return hash_fmix32(h);
142+
}
143+
136144
constexpr Vector3i() :
137145
x(0), y(0), z(0) {}
138146
constexpr Vector3i(int32_t p_x, int32_t p_y, int32_t p_z) :

0 commit comments

Comments
 (0)