PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Hash.h
Go to the documentation of this file.
1
9
10#ifndef PBAT_COMMON_H
11#define PBAT_COMMON_H
12
13#include "pbat/Aliases.h"
14
15#include <cstddef>
16#include <tuple>
17#include <utility>
18
19namespace pbat {
20namespace common {
21
29template <typename T>
30void HashCombineAccumulate(std::size_t& seed, T const& val)
31{
32 seed ^= std::hash<T>()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
33}
34
42template <typename... Types>
43std::size_t HashCombine(Types const&... args)
44{
45 std::size_t seed = 0;
46 (HashCombineAccumulate(seed, args), ...); // create hash value with seed over all args
47 return seed;
48}
49
50} // namespace common
51} // namespace pbat
52
53namespace std {
54
58template <>
59struct hash<pair<pbat::Index, pbat::Index>>
60{
66 [[maybe_unused]] std::size_t operator()(pair<pbat::Index, pbat::Index> const& inds) const
67 {
68 return pbat::common::HashCombine(inds.first, inds.second);
69 }
70};
71
75template <>
76struct hash<tuple<pbat::Index, pbat::Index>>
77{
83 [[maybe_unused]] std::size_t operator()(tuple<pbat::Index, pbat::Index> const& inds) const
84 {
85 return pbat::common::HashCombine(get<0>(inds), get<1>(inds));
86 }
87};
88
92template <>
93struct hash<tuple<pbat::Index, pbat::Index, pbat::Index>>
94{
100 [[maybe_unused]] std::size_t
101 operator()(tuple<pbat::Index, pbat::Index, pbat::Index> const& inds) const
102 {
103 return pbat::common::HashCombine(get<0>(inds), get<1>(inds), get<2>(inds));
104 }
105};
106
110template <>
111struct hash<pbat::IndexVector<2>>
112{
118 [[maybe_unused]] std::size_t operator()(pbat::IndexVector<2> const& inds) const
119 {
120 return pbat::common::HashCombine(inds(0), inds(1));
121 }
122};
123
127template <>
128struct hash<pbat::IndexVector<3>>
129{
135 [[maybe_unused]] std::size_t operator()(pbat::IndexVector<3> const& inds) const
136 {
137 return pbat::common::HashCombine(inds(0), inds(1), inds(2));
138 }
139};
140
141} // namespace std
142
143#endif // PBAT_COMMON_H
Common functionality.
Definition ArgSort.h:20
std::size_t HashCombine(Types const &... args)
Combine hash values of multiple arguments.
Definition Hash.h:43
void HashCombineAccumulate(std::size_t &seed, T const &val)
Incrementally combine hash values of multiple arguments.
Definition Hash.h:30
The main namespace of the library.
Definition Aliases.h:15
Eigen::Vector< Index, N > IndexVector
Fixed-size index vector type.
Definition Aliases.h:40
std::ptrdiff_t Index
Index type.
Definition Aliases.h:17
std::size_t operator()(pair< pbat::Index, pbat::Index > const &inds) const
Hash function for pair of Index.
Definition Hash.h:66
std::size_t operator()(pbat::IndexVector< 2 > const &inds) const
Hash function for pbat::IndexVector<2>
Definition Hash.h:118
std::size_t operator()(pbat::IndexVector< 3 > const &inds) const
Hash function for pbat::IndexVector<3>
Definition Hash.h:135
std::size_t operator()(tuple< pbat::Index, pbat::Index, pbat::Index > const &inds) const
Hash function for 3-tuple of Index.
Definition Hash.h:101
std::size_t operator()(tuple< pbat::Index, pbat::Index > const &inds) const
Hash function for 2-tuple of Index.
Definition Hash.h:83