Last active
July 14, 2025 07:21
-
-
Save mntone/6c62df120d858eb7acd5435d36724da5 to your computer and use it in GitHub Desktop.
constexpr hash
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
| /* | |
| * hash_djb2a | |
| * | |
| * Copyright (C) 2025 mntone <[email protected]> | |
| * | |
| * This program is free software: you can redistribute it and/or modify | |
| * it under the terms of the GNU General Public License as published by | |
| * the Free Software Foundation, either version 3 of the License, or | |
| * (at your option) any later version. | |
| * | |
| * This program is distributed in the hope that it will be useful, | |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| * GNU General Public License for more details. | |
| * | |
| * You should have received a copy of the GNU General Public License | |
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | |
| */ | |
| #include <string_view> | |
| namespace mntone::literals { | |
| namespace details { | |
| inline constexpr auto hash_djb2a(std::string_view const sv) { | |
| hash_t hash {5381}; | |
| for (hash_t c : sv) { | |
| hash = ((hash << 5) + hash) ^ c; | |
| } | |
| return hash; | |
| } | |
| } // namespace details | |
| inline constexpr auto operator"" _hash(char const *str, std::size_t len) { | |
| return details::hash_djb2a(std::string_view {str, len}); | |
| } | |
| } // namespace mntone::literals |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment