Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:10:17

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2022 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <cstddef>
0012 #include <cstdint>
0013 #include <string_view>
0014 #include <utility>
0015 
0016 namespace Acts {
0017 using HashedString = std::uint32_t;
0018 
0019 // Adapted from https://gist.github.com/Lee-R/3839813
0020 namespace detail {
0021 // FNV-1a 32bit hashing algorithm.
0022 constexpr HashedString fnv1a_32(char const* s, std::size_t count) {
0023   return count != 0u ? (fnv1a_32(s, count - 1) ^ s[count - 1]) * 16777619u
0024                      : 2166136261u;
0025 }
0026 
0027 constexpr HashedString fnv1a_32(std::string_view s) {
0028   return !s.empty() ? (fnv1a_32(s.substr(0, s.size() - 1)) ^ s[s.size() - 1]) *
0029                           16777619u
0030                     : 2166136261u;
0031 }
0032 
0033 constexpr int length(const char* str) {
0034   return *str != 0 ? 1 + length(str + 1) : 0;
0035 }
0036 }  // namespace detail
0037 
0038 constexpr HashedString hashString(std::string_view s) {
0039   return detail::fnv1a_32(s);
0040 }
0041 
0042 namespace HashedStringLiteral {
0043 constexpr HashedString operator"" _hash(char const* s, std::size_t count) {
0044   return detail::fnv1a_32(s, count);
0045 }
0046 
0047 }  // namespace HashedStringLiteral
0048 }  // namespace Acts