C++ Essence Library 0.1.0
A Utility Library for Modern C++ Programming
Loading...
Searching...
No Matches
hashing.hpp
1/*
2 * Copyright (c) 2024 The RefValue Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <cstddef>
26#include <cstdint>
27#include <functional>
28#include <utility>
29
30#if UINTPTR_MAX == UINT64_MAX
31#define ES_PTR64
32#elif UINTPTR_MAX == UINT32_MAX
33#define ES_PTR32
34#else
35#define ES_PTR_UNKNOWN
36#endif
37
38namespace essence {
39#ifdef ES_PTR32
40 template <typename T>
41 void hash_combine(std::size_t& result, T&& value) noexcept(
42 noexcept(std::declval<std::hash<std::decay_t<T>>>()(std::forward<T>(value)))) {
43 using pure_type = std::decay_t<T>;
44 constexpr std::size_t magic_factor = 0x9E3779B9;
45 auto hash = std::hash<pure_type>{}(std::forward<T>(value));
46
47 result ^= hash + magic_factor + (result << 6) + (result >> 2);
48 }
49#elif defined(ES_PTR64)
50 template <typename T>
51 void hash_combine(std::size_t& result, T&& value) noexcept(
52 noexcept(std::declval<std::hash<std::decay_t<T>>>()(std::forward<T>(value)))) {
53 using pure_type = std::decay_t<T>;
54 constexpr std::size_t magic_factor = 0xC6A4A7935BD1E995;
55 auto hash = std::hash<pure_type>{}(std::forward<T>(value));
56
57 hash *= magic_factor;
58 hash ^= hash >> 47;
59 hash *= magic_factor;
60
61 result ^= hash;
62 result *= magic_factor;
63
64 // Completely arbitrary number, to prevent 0's from hashing to 0.
65 result += 0xE6546B64;
66 }
67#else
68#error "Unknown pointer length."
69#endif
70
71 template <typename... Args>
72 std::size_t hash_arbitrary(Args&&... args) noexcept(
73 (noexcept(hash_combine(std::declval<std::size_t&>(), std::forward<Args>(args))) && ...)) {
74 std::size_t result{};
75
76 return (hash_combine(result, std::forward<Args>(args)), ..., result);
77 }
78} // namespace essence
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Definition json.hpp:6036