Skip to content

Commit c29bd46

Browse files
author
Julian LALU
committed
Add first Hasher32
1 parent 578d087 commit c29bd46

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

interface/core/hash.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ namespace hud
115115
}
116116

117117
/** Combine two 32 bits value. */
118-
[[nodiscard]] static constexpr u64 combine_32(u64 a, u64 b) noexcept
118+
[[nodiscard]] static constexpr u32 combine_32(u32 a, u32 b) noexcept
119119
{
120120
return hud::hash_algorithm::city_hash::combine_32(a, b);
121121
}
@@ -223,6 +223,19 @@ namespace hud
223223
{
224224
return hud::hash_algorithm::city_hash::combine_64(a, b);
225225
}
226+
227+
struct Hasher32
228+
{
229+
template<typename T>
230+
[[nodiscard]] constexpr u32 operator()(const T &value) noexcept
231+
{
232+
state_ = hud::combine_32(state_, hud::hash_32(value));
233+
return state_;
234+
}
235+
236+
u32 state_ {0}; // Default is 0, but can be a seed
237+
};
238+
226239
} // namespace hud
227240

228241
#endif // HD_INC_CORE_HASH_H

test/hashmap/hashmap_insert.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include <core/containers/hashmap.h>
2+
#include <core/cstring.h>
23

34
GTEST_TEST(hashmap, insert)
45
{
56
hud::hashmap<const char *, i64> map;
6-
u64 hash = hud::hash_64("key", 3);
7+
constexpr const usize len = hud::cstring::length("key");
8+
9+
u64 hash = hud::combine_64(hud::combine_64(0, hud::hash_64("key", len)), hud::hash_64((const char *)&len, sizeof(usize)));
710
auto res = map.insert("key", 1);
811
}

0 commit comments

Comments
 (0)