StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
hash.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This is the base for StormByte Suite libraries.
5 *
6 * StormByte original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
43
44#include <cstddef>
45#include <cstdint>
46#include <cstring>
47#include <limits>
48#include <span>
49#include <type_traits>
50
55namespace StormByte {
60 namespace Safe {
66 STORMBYTE_FORCE_INLINE std::size_t HashBytes(std::span<const std::byte> bytes) noexcept {
67 constexpr std::uint64_t offset = 14695981039346656037ull;
68 constexpr std::uint64_t prime = 1099511628211ull;
69 std::uint64_t value = offset;
70 for (const std::byte byte : bytes) {
71 value ^= static_cast<std::uint64_t>(byte);
72 value *= prime;
73 }
74 return static_cast<std::size_t>(value);
75 }
76
83 STORMBYTE_FORCE_INLINE std::size_t HashCombine(std::size_t seed, std::size_t next) noexcept {
84 return seed ^ (next + 0x9e3779b97f4a7c15ull + (seed << 6) + (seed >> 2));
85 }
86
94 template<class T>
95 struct Hash;
96
101 template<class T>
102 requires std::is_integral_v<T> || std::is_enum_v<T>
109 STORMBYTE_FORCE_INLINE std::size_t operator()(T value) const noexcept {
110 std::uint64_t widened = 0;
111 if constexpr (std::is_same_v<T, bool>)
112 widened = value ? 1ull : 0ull;
113 else if constexpr (std::is_enum_v<T>) {
114 using Underlying = std::underlying_type_t<T>;
115 widened = static_cast<std::uint64_t>(static_cast<std::make_unsigned_t<Underlying>>(static_cast<Underlying>(value)));
116 } else
117 widened = static_cast<std::uint64_t>(static_cast<std::make_unsigned_t<T>>(value));
118 return HashBytes(std::as_bytes(std::span<const std::uint64_t>(&widened, 1)));
119 }
120 };
121
126 template<class T>
127 requires std::is_floating_point_v<T>
128 struct STORMBYTE_PUBLIC_TYPE Hash<T> {
134 STORMBYTE_FORCE_INLINE std::size_t operator()(T value) const noexcept {
135 unsigned char bytes[sizeof(T)] = {};
136 std::size_t used = sizeof(T);
137 if constexpr (std::is_same_v<T, long double> && sizeof(long double) > 10 && std::numeric_limits<long double>::digits == 64)
138 used = 10;
139 std::memcpy(bytes, &value, used);
140 return HashBytes(std::span<const std::byte>(reinterpret_cast<const std::byte*>(bytes), used));
141 }
142 };
143
148 template<class T>
155 STORMBYTE_FORCE_INLINE std::size_t operator()(T* value) const noexcept {
156 return Hash<std::uintptr_t>{}(reinterpret_cast<std::uintptr_t>(value));
157 }
158 };
159 }
160}
Owned values that cross a DLL without the caller's CRT.
STORMBYTE_FORCE_INLINE std::size_t HashCombine(std::size_t seed, std::size_t next) noexcept
Mix an existing hash with another.
Definition hash.hxx:83
STORMBYTE_FORCE_INLINE std::size_t HashBytes(std::span< const std::byte > bytes) noexcept
Mix a byte range with FNV-1a.
Definition hash.hxx:66
Root namespace of the StormByte suite.
#define STORMBYTE_FORCE_INLINE
Definition platform.h:134
STORMBYTE_FORCE_INLINE std::size_t operator()(T value) const noexcept
Hash value.
Definition hash.hxx:109
STORMBYTE_FORCE_INLINE std::size_t operator()(T *value) const noexcept
Hash value.
Definition hash.hxx:155
Cross-module hash.
Definition hash.hxx:95
#define STORMBYTE_PUBLIC_TYPE
Definition visibility.h:57