• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2022 The Khronos Group Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SOURCE_UTIL_HASH_COMBINE_H_
16 #define SOURCE_UTIL_HASH_COMBINE_H_
17 
18 #include <cstddef>
19 #include <functional>
20 #include <vector>
21 
22 namespace spvtools {
23 namespace utils {
24 
25 // Helpers for incrementally computing hashes.
26 // For reference, see
27 // http://open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3876.pdf
28 
29 template <typename T>
hash_combine(std::size_t seed,const T & val)30 inline size_t hash_combine(std::size_t seed, const T& val) {
31   return seed ^ (std::hash<T>()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
32 }
33 
34 template <typename T>
hash_combine(std::size_t hash,const std::vector<T> & vals)35 inline size_t hash_combine(std::size_t hash, const std::vector<T>& vals) {
36   for (const T& val : vals) {
37     hash = hash_combine(hash, val);
38   }
39   return hash;
40 }
41 
hash_combine(std::size_t hash)42 inline size_t hash_combine(std::size_t hash) { return hash; }
43 
44 template <typename T, typename... Types>
hash_combine(std::size_t hash,const T & val,const Types &...args)45 inline size_t hash_combine(std::size_t hash, const T& val,
46                            const Types&... args) {
47   return hash_combine(hash_combine(hash, val), args...);
48 }
49 
50 }  // namespace utils
51 }  // namespace spvtools
52 
53 #endif  // SOURCE_UTIL_HASH_COMBINE_H_
54