opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
hash_helpers.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <cstddef>
5#include <functional>
6#include <iterator>
7#include <utility>
8
9namespace osc
10{
11 template<typename T>
12 concept Hashable = requires(T v) {
13 { std::hash<T>{}(v) } -> std::convertible_to<size_t>;
14 };
15
16 // combines hash of `T` into the seed value
17 template<Hashable T>
18 size_t hash_combine(size_t seed, const T& v) noexcept(noexcept(std::hash<T>{}(v)))
19 {
20 return seed ^ (std::hash<T>{}(v) + 0x9e3779b9 + (seed<<6) + (seed>>2));
21 }
22
23 template<Hashable T>
24 size_t hash_of(const T& v) noexcept(noexcept(std::hash<T>{}(v)))
25 {
26 return std::hash<T>{}(v);
27 }
28
29 template<Hashable T, Hashable... Ts>
30 size_t hash_of(const T& v, const Ts&... vs)
31 {
32 return hash_combine(hash_of(v), hash_of(vs...));
33 }
34
35 template<typename Range>
36 size_t hash_range(const Range& range) noexcept(noexcept(hash_combine(0, *std::ranges::begin(range))))
37 {
38 size_t rv = 0;
39 for (const auto& el : range) {
40 rv = hash_combine(rv, el);
41 }
42 return rv;
43 }
44
45 // an osc-specific hashing object
46 //
47 // think of it as a `std::hash` that's used specifically in situations where
48 // specializing `std::hash` might be a bad idea (e.g. on `std` library types
49 // templated on other `std` library types, where there's a nonzero chance the
50 template<typename Key>
51 struct Hasher;
52
53 template<typename T1, typename T2>
54 struct Hasher<std::pair<T1, T2>> final {
55 size_t operator()(const std::pair<T1, T2>& p) const noexcept(noexcept(hash_of(p.first, p.second)))
56 {
57 return hash_of(p.first, p.second);
58 }
59 };
60}
Definition hash_helpers.h:12
Definition custom_decoration_generator.h:5
size_t hash_combine(size_t seed, const T &v) noexcept(noexcept(std::hash< T >{}(v)))
Definition hash_helpers.h:18
size_t hash_of(const T &v) noexcept(noexcept(std::hash< T >{}(v)))
Definition hash_helpers.h:24
constexpr U to(T &&value)
Definition conversion.h:56
size_t hash_range(const Range &range) noexcept(noexcept(hash_combine(0, *std::ranges::begin(range))))
Definition hash_helpers.h:36
size_t operator()(const std::pair< T1, T2 > &p) const noexcept(noexcept(hash_of(p.first, p.second)))
Definition hash_helpers.h:55
Definition hash_helpers.h:51