Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HashedString.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file HashedString.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
11 #include <cstddef>
12 #include <cstdint>
13 #include <string_view>
14 #include <utility>
15 
16 namespace Acts {
17 using HashedString = std::uint32_t;
18 
19 // Adapted from https://gist.github.com/Lee-R/3839813
20 namespace detail {
21 // FNV-1a 32bit hashing algorithm.
22 constexpr HashedString fnv1a_32(char const* s, std::size_t count) {
23  return count != 0u ? (fnv1a_32(s, count - 1) ^ s[count - 1]) * 16777619u
24  : 2166136261u;
25 }
26 
27 constexpr HashedString fnv1a_32(std::string_view s) {
28  return !s.empty() ? (fnv1a_32(s.substr(0, s.size() - 1)) ^ s[s.size() - 1]) *
29  16777619u
30  : 2166136261u;
31 }
32 
33 constexpr int length(const char* str) {
34  return *str != 0 ? 1 + length(str + 1) : 0;
35 }
36 } // namespace detail
37 
38 constexpr HashedString hashString(std::string_view s) {
39  return detail::fnv1a_32(s);
40 }
41 
42 namespace HashedStringLiteral {
43 constexpr HashedString operator"" _hash(char const* s, std::size_t count) {
44  return detail::fnv1a_32(s, count);
45 }
46 
47 } // namespace HashedStringLiteral
48 } // namespace Acts