AutoPas  3.0.0
Loading...
Searching...
No Matches
HashCombine.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <cstddef>
9#include <cstdint>
10#include <functional>
11
12namespace autopas::utils {
13
25template <class T>
26std::size_t hashValue(const T &value) {
27 if constexpr (requires { std::hash<T>{}(value); }) {
28 return std::hash<T>{}(value);
29 } else {
30 return static_cast<std::size_t>(value);
31 }
32}
33
50template <class... Values_T>
51std::size_t hashCombine(const Values_T &...values) {
52 static_assert(sizeof(std::size_t) == 8,
53 "hashCombine() only supports a 64-bit std::size_t, as the mixing constants are chosen for that width.");
54
55 constexpr auto mix = [](std::size_t x) {
56 constexpr std::uint64_t m = 0xe9846af9b1a615dULL;
57 x ^= x >> 32;
58 x *= m;
59 x ^= x >> 32;
60 x *= m;
61 x ^= x >> 28;
62 return x;
63 };
64
65 std::size_t seed = 0;
66 ((seed = mix(seed + 0x9e3779b9 + hashValue(values))), ...);
67 return seed;
68}
69
70} // namespace autopas::utils
In this namespace some helper classes and functions can be found used inside of AutoPas.
Definition: namespaces.h:44
std::size_t hashValue(const T &value)
Hash of a single value.
Definition: HashCombine.h:26
std::size_t hashCombine(const Values_T &...values)
Combines the hashes of an arbitrary number of values into one hash.
Definition: HashCombine.h:51