Last active
April 17, 2025 23:35
-
-
Save dk949/d96a33574723b17cf705abb5fb04ec50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef UT_CONSTEXPR_HASH_HPP | |
#define UT_CONSTEXPR_HASH_HPP | |
#include <string_view> | |
/* Usage: | |
* | |
* int main(int argc, char const **argv) { | |
* constexpr auto hash = ut::fnv_1a<>; // defaults to uint64_t | |
* | |
* for (int i = 1; i < argc; i++) { | |
* switch (hash(argv[i])) { | |
* case hash("-h"): std::cout << argv[0] << ": Usage: -hv [ARG...]\n"; return 0; | |
* case hash("-v"): std::cout << "1.0.0\n"; return 0; | |
* default: std::cout << argv[i] << '\n'; | |
* } | |
* } | |
* | |
* static_assert(ut::fnv_1a<uint64_t, ut::CHIgnoreCase::Yes>("hello") | |
* == ut::fnv_1a<uint64_t, ut::CHIgnoreCase::Yes>("helLO")); | |
* } | |
* | |
* | |
* | |
*/ | |
/** | |
* constexpr fnv-1a hash for 32 or 64 bit hashes | |
* requires minimum c++17 | |
* tested on gcc 11.1.0 and clang 13.0.0 | |
* --------- | |
* using uint64_t calculates hashes for ~400'000 strings (between 1 and 29 characters) in ~300ms with no collisions | |
* | |
* algorithm from from: https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash | |
* --------- | |
* NOTE on CHIgnoreCase: This only supports ASCII characters and converts everything to lower case | |
*/ | |
namespace ut { | |
enum struct CHIgnoreCase : bool { | |
No = false, | |
Yes = true, | |
}; | |
namespace detail { | |
constexpr char asciiToLower(char c) { | |
switch (c) { | |
// clang-format off | |
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': | |
case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': | |
case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': | |
case 'V': case 'W': case 'X': case 'Y': case 'Z': return c + ('a' - 'A'); | |
// clang-format on | |
default: return c; | |
} | |
} | |
} // namespace detail | |
template<typename Size = std::size_t, CHIgnoreCase IgnoreCase = CHIgnoreCase::No> | |
[[nodiscard]] | |
constexpr Size fnv_1a(std::string_view data) { | |
static_assert(std::is_unsigned_v<Size>, "Cannot use signed integers in hash. Signed integer overflow is UB."); | |
static_assert(sizeof(Size) == 4 || sizeof(Size) == 8, "Size has to be of size 4 or 8"); | |
Size hash = sizeof(Size) == 4 ? 0x811c9dc5 : sizeof(Size) == 8 ? 0xcbf29ce484222325 : 0; | |
constexpr Size prime = sizeof(Size) == 4 ? 0x01000193 : sizeof(Size) == 8 ? 0x00000100000001B3 : 0; | |
for (auto chr : data) { | |
if constexpr (IgnoreCase == CHIgnoreCase::Yes) chr = detail::asciiToLower(chr); | |
hash ^= static_cast<Size>(chr); | |
hash *= prime; | |
} | |
return hash; | |
} | |
} // namespace ut | |
/* | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <https://unlicense.org> | |
*/ | |
#endif // UT_CONSTEXPR_HASH_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a hashing function usable in `constexpr` context |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment