Skip to content

Commit dbd5b44

Browse files
committed
add rapidhash from conan
1 parent a389f46 commit dbd5b44

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ if (WITH_SODIUM)
4545
find_package(libsodium REQUIRED)
4646
find_package(highway REQUIRED)
4747
endif ()
48+
find_package(rapidhash)
4849

4950
add_subdirectory(include/dice/hash/blake/internal/blake3)
5051

@@ -62,6 +63,7 @@ if (WITH_SODIUM)
6263
PUBLIC
6364
libsodium::libsodium
6465
blake3
66+
rapidhash::rapidhash
6567
PRIVATE
6668
highway::highway
6769
)
@@ -76,6 +78,7 @@ else()
7678
target_link_libraries(${PROJECT_NAME}
7779
INTERFACE
7880
blake3
81+
rapidhash::rapidhash
7982
)
8083
endif()
8184

conanfile.py

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class DiceHashConan(ConanFile):
3232
generators = ("CMakeDeps", "CMakeToolchain")
3333

3434
def requirements(self):
35+
self.requires("rapidhash/1.0")
3536
if self.options.with_sodium:
3637
self.requires("libsodium/cci.20220430", transitive_headers=True)
3738
self.requires("highway/1.2.0")

include/dice/hash/internal/DiceHashPolicies.hpp

+44
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "xxhash.hpp"
88
#endif
99
#include <type_traits>
10+
#include "rapidhash.h"
1011

1112
namespace dice::hash::Policies {
1213
template<typename T>
@@ -153,5 +154,48 @@ namespace dice::hash::Policies {
153154
}
154155
};
155156
};
157+
158+
struct rapidhash {
159+
inline static constexpr uint64_t kSeed = RAPID_SEED;
160+
inline static constexpr std::size_t ErrorValue = kSeed;
161+
162+
template<typename T>
163+
static std::size_t hash_fundamental(T x) noexcept {
164+
return static_cast<std::size_t>(rapidhash_withSeed(&x, sizeof(T), kSeed));
165+
}
166+
167+
static std::size_t hash_bytes(void const *ptr, std::size_t len) noexcept {
168+
return static_cast<std::size_t>(rapidhash_withSeed(ptr, len, kSeed));
169+
}
170+
171+
static std::size_t hash_combine(std::initializer_list<size_t> hashes) noexcept {
172+
uint64_t state = kSeed;
173+
for (auto hash : hashes) {
174+
state = rapid_mix(state, hash);
175+
}
176+
return static_cast<std::size_t>(state);
177+
}
178+
179+
static std::size_t hash_invertible_combine(std::initializer_list<size_t> hashes) noexcept {
180+
std::size_t result = 0;
181+
for (auto hash : hashes) {
182+
result = result ^ hash;
183+
}
184+
return result;
185+
}
186+
187+
class HashState {
188+
private:
189+
uint64_t state = kSeed;
190+
public:
191+
explicit HashState(std::size_t) noexcept {}
192+
void add (std::size_t hash) noexcept {
193+
state = rapid_mix(state, static_cast<uint64_t>(hash));
194+
}
195+
[[nodiscard]] std::size_t digest() noexcept {
196+
return static_cast<std::size_t>(state);
197+
}
198+
};
199+
};
156200
}// namespace dice::hash::Policies
157201
#endif//DICE_HASH_DICEHASHPOLICIES_HPP

tests/TestDiceHash.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
#ifdef __x86_64__
66
#define AllPoliciesToTestForDiceHash dice::hash::Policies::Martinus, dice::hash::Policies::xxh3, \
7-
dice::hash::Policies::wyhash
7+
dice::hash::Policies::wyhash, dice::hash::Policies::rapidhash
88
#else
99
#define AllPoliciesToTestForDiceHash dice::hash::Policies::Martinus, \
10-
dice::hash::Policies::wyhash
10+
dice::hash::Policies::wyhash, dice::hash::Policies::rapidhash
1111
#endif
1212
#define AllTypesToTestForDiceHash int, long, std::size_t, std::byte, std::string, std::string_view, int *, long *, \
1313
std::string *, std::unique_ptr<int>, std::shared_ptr<int>, std::vector<int>, \

0 commit comments

Comments
 (0)