qxLib
hash.h
Go to the documentation of this file.
1 /**
2 
3  @file hash.h
4  @author Khrapov
5  @date 18.08.2025
6  @copyright (c) Nick Khrapov, 2025. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <cstddef>
12 #include <functional>
13 
14 namespace qx
15 {
16 
17 /**
18  @brief Get an object hash
19  @details A simple wrapper over std::hash
20  that removes the need to specify the type and unnecessary parentheses.
21  @tparam T - object type
22  @param value - object value
23  @retval - object hash
24 **/
25 template<class T>
26 constexpr size_t get_hash(const T& value) noexcept
27 {
28  return std::hash<T>()(value);
29 }
30 
31 /**
32  @brief Combine hashes
33  @tparam T - object type
34  @param seed - input/output hash value
35  @param value - object to hash
36 **/
37 template<class T>
38 constexpr void hash_combine(size_t& seed, const T& value) noexcept
39 {
40  seed ^= get_hash(value) + size_t { 0x9e3779b9 } + (seed << 6) + (seed >> 2);
41 }
42 
43 } // namespace qx
constexpr size_t get_hash(const T &value) noexcept
Get an object hash.
Definition: hash.h:26
constexpr void hash_combine(size_t &seed, const T &value) noexcept
Combine hashes.
Definition: hash.h:38