Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
HashUtils.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright © 2014-2024 Allan Leal
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // C++ includes
21 #include <functional>
22 #include <utility>
23 #include <vector>
24 
25 namespace Reaktoro {
26 
27 inline void __hashCombine(std::size_t& seed) {}
28 
29 template<typename T, typename... Rest>
30 inline void __hashCombine(std::size_t& seed, T const& v, Rest... rest)
31 {
32  std::hash<T> hasher;
33  seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
34  __hashCombine(seed, rest...);
35 }
36 
39 template<typename T, typename... Rest>
40 auto hashCombine(std::size_t seed, T const& v, Rest... rest) -> std::size_t
41 {
42  __hashCombine(seed, v, rest...);
43  return seed;
44 }
45 
48 template<typename Vec>
49 auto hashVector(Vec const& vec) -> std::size_t
50 {
51  std::size_t seed = vec.size();
52  for(auto const& val : vec)
53  seed = hashCombine(seed, val);
54  return seed;
55 }
56 
57 } // namespace Reaktoro
58 
60 template<typename T, typename A>
61 struct std::hash<std::vector<T, A>>
62 {
63  std::size_t operator()(std::vector<T, A> const& obj) const
64  {
65  return Reaktoro::hashVector(obj);
66  }
67 };
68 
70 template<typename L, typename R>
71 struct std::hash<std::pair<L, R>>
72 {
73  std::size_t operator()(std::pair<L, R> const& obj) const
74  {
75  return Reaktoro::hashCombine(0, obj.first, obj.second);
76  }
77 };
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
std::vector< T > Vec
Convenient alias for std::vector<T>.
Definition: Types.hpp:66
auto hashCombine(std::size_t seed, T const &v, Rest... rest) -> std::size_t
Return the hash combine of the hash number of given values.
Definition: HashUtils.hpp:40
auto hashVector(Vec const &vec) -> std::size_t
Return the hash of a vector of values.
Definition: HashUtils.hpp:49