delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
hash.hpp
1
7#pragma once
8
9#include <cstddef>
10#include <functional>
11#include <map>
12#include <set>
13#include <utility>
14#include <vector>
15
17namespace delpi::hash {
18
20template <class T>
21size_t hash_combine(size_t seed, const T &v);
22
23template <class T, class... Rest>
24size_t hash_combine(size_t seed, const T &v, Rest... rest) {
25 return hash_combine(hash_combine(seed, v), rest...);
26}
27
29template <typename It>
30size_t hash_range(It first, It last) {
31 size_t seed{};
32 for (; first != last; ++first) {
33 seed = hash_combine(seed, *first);
34 }
35 return seed;
36}
37
39template <class T>
40struct hash_value {
41 size_t operator()(const T &v) const { return std::hash<T>{}(v); }
42};
43
45template <class T1, class T2>
46struct hash_value<std::pair<T1, T2>> {
47 size_t operator()(const std::pair<T1, T2> &p) const { return hash_combine(0, p.first, p.second); }
48};
49
51template <class T>
52struct hash_value<std::vector<T>> {
53 size_t operator()(const std::vector<T> &vec) const { return hash_range(vec.begin(), vec.end()); }
54};
55
57template <class T>
58struct hash_value<std::set<T>> {
59 size_t operator()(const std::set<T> &s) const { return hash_range(s.begin(), s.end()); }
60};
61
63template <class T1, class T2>
64struct hash_value<std::map<T1, T2>> {
65 size_t operator()(const std::map<T1, T2> &map) const { return hash_range(map.begin(), map.end()); }
66};
67
70template <class T>
71inline size_t hash_combine(size_t seed, const T &v) {
72 seed ^= hash_value<T>{}(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
73 return seed;
74}
75
76} // namespace delpi::hash
Namespace containing all the hash functions and utilities.
size_t hash_combine(size_t seed, const T &v)
Combines a given hash value seed and a hash of parameter v.
Definition hash.hpp:71
size_t hash_range(It first, It last)
Computes the combined hash value of the elements of an iterator range.
Definition hash.hpp:30
Computes the hash value of v using std::hash.
Definition hash.hpp:40