libhatchet
Loading...
Searching...
No Matches
hxkey.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
16
17#include "libhatchet.h"
18
19// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
20#if HX_USE_MACROS_WITH_MODULE
21#error Header does not provide macros alone.
22#endif
23
24#include "hxutility.h"
25
26HX_NS_BEGIN_
27
29// Used for readability.
30using hxcstring_const_ = const char*;
31using hxcstring_ = char*;
33
34#if HX_CPLUSPLUS >= 202002L
40template<typename from_t_, typename to_t_>
41concept hxconvertible_to = requires(from_t_ (&&from_)()) {
42 requires requires { static_cast<to_t_>(from_()); };
43};
44
50template<typename from_t_, typename to_t_>
52#endif
53
59template<typename A_, typename B_>
60#if HX_CPLUSPLUS >= 202002L
61requires requires(const A_& a_, const B_& b_) { { a_ == b_ } -> hxconvertible_to<bool>; }
62#endif
63hxattr_nodiscard hxinline hxattr_flatten constexpr bool hxkey_equal(const A_& a_, const B_& b_) {
64 return a_ == b_;
65}
66
71hxattr_nodiscard hxinline bool hxkey_equal(const hxcstring_const_& a_, const hxcstring_const_& b_) {
72 return ::strcmp(a_, b_) == 0;
73}
74hxattr_nodiscard hxinline bool hxkey_equal(const hxcstring_const_& a_, const hxcstring_& b_) {
75 return ::strcmp(a_, b_) == 0;
76}
77hxattr_nodiscard hxinline bool hxkey_equal(const hxcstring_& a_, const hxcstring_const_& b_) {
78 return ::strcmp(a_, b_) == 0;
79}
80hxattr_nodiscard hxinline bool hxkey_equal(const hxcstring_& a_, const hxcstring_& b_) {
81 return ::strcmp(a_, b_) == 0;
82}
83
86template<typename T_>
88public:
90 const hxremove_cvref_t<T_>& b_) const {
91 return hxkey_equal(a_, b_);
92 }
93};
94
102template<typename A_, typename B_>
103#if HX_CPLUSPLUS >= 202002L
104requires requires(const A_& a_, const B_& b_) { { a_ < b_ } -> hxconvertible_to<bool>; }
105#endif
106hxattr_nodiscard hxinline hxattr_flatten constexpr bool hxkey_less(const A_& a_, const B_& b_) {
107 return a_ < b_;
108}
109
115hxattr_nodiscard hxinline bool hxkey_less(const hxcstring_const_& a_, const hxcstring_const_& b_) {
116 return ::strcmp(a_, b_) < 0;
117}
118hxattr_nodiscard hxinline bool hxkey_less(const hxcstring_const_& a_, const hxcstring_& b_) {
119 return ::strcmp(a_, b_) < 0;
120}
121hxattr_nodiscard hxinline bool hxkey_less(const hxcstring_& a_, const hxcstring_const_& b_) {
122 return ::strcmp(a_, b_) < 0;
123}
124hxattr_nodiscard hxinline bool hxkey_less(const hxcstring_& a_, const hxcstring_& b_) {
125 return ::strcmp(a_, b_) < 0;
126}
127
130template<typename T_>
132public:
134 const hxremove_cvref_t<T_>& b_) const {
135 return hxkey_less(a_, b_);
136 }
137};
138
140// xxhash32 prime constants and avalanche mixing. Useful when hashing sequential
141// data. These are used by hxkey_hash overloads below.
142hxinline_constexpr hxhash_t hxhash_prime1_ = hxhash_t{0x9E3779B1u};
143hxinline_constexpr hxhash_t hxhash_prime2_ = hxhash_t{0x85EBCA77u};
144hxinline_constexpr hxhash_t hxhash_prime3_ = hxhash_t{0xC2B2AE3Du};
145hxinline_constexpr hxhash_t hxhash_prime4_ = hxhash_t{0x27D4EB2Fu};
146hxinline_constexpr hxhash_t hxhash_prime5_ = hxhash_t{0x165667B1u};
147
148// xxhash32 avalanche: x ^= x >> 15, x *= prime2, x ^= x >> 13, x *= prime3, x ^= x >> 16.
150 x_ ^= x_ >> 15u;
151 x_ *= hxhash_prime2_;
152 x_ ^= x_ >> 13u;
153 x_ *= hxhash_prime3_;
154 x_ ^= x_ >> 16u;
155 return x_;
156}
158
164template<typename T_>
166 // xxhash32 short-input path: seed=0, length=4, single 4-byte word.
167 hxhash_t h_ = hxhash_prime5_ + hxhash_t{4u};
168 // Did you write a custom hxkey_hash()?
169 h_ += static_cast<hxhash_t>(x_) * hxhash_prime3_;
170 h_ = ((h_ << 17u) | (h_ >> 15u)) * hxhash_prime4_;
171 return hxhash_avalanche_(h_);
172}
173
179 hxhash_t h_ = hxhash_prime5_;
180 while(*s_ != '\0') {
181 h_ += static_cast<hxhash_t>(static_cast<unsigned char>(*s_++)) * hxhash_prime5_;
182 h_ = ((h_ << 11u) | (h_ >> 21u)) * hxhash_prime1_;
183 }
184 return hxhash_avalanche_(h_);
185}
186
187HX_NS_END_
hxkey_equal_t<T> - A constexpr callable that invokes hxkey_equal.
Definition hxkey.hpp:87
constexpr bool operator()(const hxremove_cvref_t< T > &a_, const hxremove_cvref_t< T > &b_) const
Definition hxkey.hpp:89
hxkey_less_t<T> - A constexpr callable that invokes hxkey_less.
Definition hxkey.hpp:131
constexpr bool operator()(const hxremove_cvref_t< T > &a_, const hxremove_cvref_t< T > &b_) const
Definition hxkey.hpp:133
hxconvertible_to - A concept that requires one type to be convertible to another.
Definition hxkey.hpp:41
hxsame_as - A concept that requires two types to be the same.
Definition hxkey.hpp:51
hxhash_t hxkey_hash(T x_)
hxkey_hash(T) - Returns the xxhash32 of a numeric value cast to 32 bits.
Definition hxkey.hpp:165
constexpr bool hxkey_equal(const A_ &a_, const B_ &b_)
hxkey_equal(const A& a, const B& b) - Returns true if two objects are equivalent.
Definition hxkey.hpp:63
constexpr bool hxkey_less(const A_ &a_, const B_ &b_)
hxkey_less(const T&, const T&) - User-overloadable function for performing comparisons.
Definition hxkey.hpp:106
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:120
#define hxinline_constexpr
hxinline_constexpr - Enables C++17 compatable "inline constexpr" usage for variables so they can be e...
Definition hxsettings.h:272
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:89
#define hxattr_flatten
hxattr_flatten - Inline every call inside a function's body into it, recursively, regardless of the c...
Definition hxsettings.h:82
#define hxconstexpr
hxconstexpr - Enables C++23 compatable constexpr usage for functions as that has support for destruct...
Definition hxsettings.h:263
A few utility functions and most standard C++ meta programming functions.
hxremove_cv_t< hxremove_reference_t< T > > hxremove_cvref_t
hxremove_cvref_t<T> - Returns T with const, volatile, and references removed.
Definition hxutility.h:115
Provides core macros, memory management and feature detection.
uint32_t hxhash_t
hxhash_t - Unsigned 32-bit hash value. Expect collisions.
Definition libhatchet.h:212
static constexpr bool value
Definition hxutility.h:124