sandbox
Loading...
Searching...
No Matches
hash.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2/*
3 * Copyright (c) 2022 Jonas Kabelitz
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * You should have received a copy of the MIT License along with this program.
24 * If not, see <https://opensource.org/licenses/MIT/>.
25 */
26
31#ifndef LIBSBX_UTILITY_HASH_HPP_
32#define LIBSBX_UTILITY_HASH_HPP_
33
38#include <utility>
39#include <string>
40#include <string_view>
41#include <span>
42#include <iostream>
43#include <concepts>
44#include <cinttypes>
45
46namespace sbx::utility {
47
51template<typename Type>
52concept hashable = requires(const Type& instance) {
53 { std::hash<Type>{}(instance) } -> std::same_as<std::size_t>;
54}; // concept hashable
55
59inline constexpr auto hash_combine([[maybe_unused]] std::size_t& seed) -> void { }
60
71template<hashable Type, hashable... Rest>
72inline constexpr auto hash_combine(std::size_t& seed, const Type& value, Rest... rest) -> void {
73 auto hasher = std::hash<Type>{};
74 seed ^= hasher(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
75 (hash_combine(seed, rest), ...);
76}
77
83template<typename Type>
84concept character = std::same_as<Type, char> || std::same_as<Type, wchar_t> || std::same_as<Type, char8_t> || std::same_as<Type, char16_t> || std::same_as<Type, char32_t>;
85
91template<std::unsigned_integral Type>
93
99template<>
100struct fnv1a_traits<std::uint32_t> {
102 inline static constexpr auto basis = std::uint32_t{0x811c9dc5};
104 inline static constexpr auto prime = std::uint32_t{0x01000193};
105}; // struct fnv1a_traits
106
112template<>
113struct fnv1a_traits<std::uint64_t> {
115 inline static constexpr auto basis = std::uint64_t{0xcbf29ce484222325};
117 inline static constexpr auto prime = std::uint64_t{0x00000100000001B3};
118}; // struct fnv1a_traits
119
127template<character Char, std::unsigned_integral Hash = std::uint64_t, typename HashTraits = fnv1a_traits<Hash>>
129 using char_type = Char;
130 using size_type = std::size_t;
131 using hash_type = Hash;
132 using hash_traits = HashTraits;
133
142 inline constexpr auto operator()(std::basic_string_view<Char> string) const noexcept -> hash_type {
143 auto hash = hash_traits::basis;
144
145 for (const auto& character : string) {
146 hash ^= static_cast<hash_type>(character);
147 hash *= hash_traits::prime;
148 }
149
150 return hash;
151 }
152}; // struct fnv1a_hash
153
154template<std::unsigned_integral Hash = std::uint64_t>
155struct djb2_hash {
156
157 using hash_type = Hash;
158
159 inline constexpr auto operator()(std::span<const std::uint8_t> buffer) const noexcept -> hash_type {
160 // Implementation from https://theartincode.stanis.me/008-djb2/
161 auto hash = hash_type{5381};
162
163 for (auto byte : buffer) {
164 hash = ((hash << 5) + hash) + static_cast<std::int32_t>(byte);
165 }
166
167 return hash;
168 }
169
170 template<typename Type>
171 requires (std::is_trivially_copyable_v<Type>)
172 inline constexpr auto operator()(const Type& value) const noexcept -> hash_type {
173 return operator()({reinterpret_cast<std::uint8_t*>(std::addressof(value)), sizeof(Type)});
174 }
175
176}; // struct djb2_hash
177
178} // namespace sbx::utility
179
180#endif // LIBSBX_UTILITY_HASH_HPP_
A concept that represents a character type.
Definition: hash.hpp:84
A concept that represents a type that can be hashed.
Definition: hash.hpp:52
constexpr auto hash_combine(std::size_t &seed) -> void
Combines multiple hashes into a single hash.
Definition: hash.hpp:59
Definition: hash.hpp:155
Functor that implements the fnv1a hash algorithm.
Definition: hash.hpp:128
constexpr auto operator()(std::basic_string_view< Char > string) const noexcept -> hash_type
Hashes the given string.
Definition: hash.hpp:142
Traits for the fnv1a hash function.
Definition: hash.hpp:92