Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-15 08:03:41

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <csignal>
0012 #include <cstddef>
0013 #include <cstdint>
0014 #include <string_view>
0015 #include <type_traits>
0016 #include <utility>
0017 
0018 namespace Acts {
0019 /// @brief Type alias for hashed string representation
0020 /// @details Represents a string as a compile-time hash value for efficient comparison
0021 using HashedString = std::uint32_t;
0022 
0023 // Adapted from https://gist.github.com/Lee-R/3839813
0024 namespace detail {
0025 // FNV-1a 32bit hashing algorithm.
0026 constexpr HashedString fnv1a_32(char const* s, std::size_t count) {
0027   return count != 0u ? (fnv1a_32(s, count - 1) ^ s[count - 1]) * 16777619u
0028                      : 2166136261u;
0029 }
0030 
0031 constexpr HashedString fnv1a_32(std::string_view s) {
0032   return !s.empty() ? (fnv1a_32(s.substr(0, s.size() - 1)) ^ s[s.size() - 1]) *
0033                           16777619u
0034                     : 2166136261u;
0035 }
0036 
0037 constexpr int length(const char* str) {
0038   return *str != 0 ? 1 + length(str + 1) : 0;
0039 }
0040 }  // namespace detail
0041 
0042 /// Compile-time hash of string literal
0043 /// @param s String view to hash
0044 /// @return Hashed string representation
0045 consteval HashedString hashString(std::string_view s) {
0046   return detail::fnv1a_32(s);
0047 }
0048 
0049 /// Runtime hash of string
0050 /// @param s String view to hash
0051 /// @return Hashed string representation
0052 constexpr HashedString hashStringDynamic(std::string_view s) {
0053   return detail::fnv1a_32(s);
0054 }
0055 
0056 namespace HashedStringLiteral {
0057 constexpr HashedString operator""_hash(char const* s, std::size_t count) {
0058   return detail::fnv1a_32(s, count);
0059 }
0060 
0061 }  // namespace HashedStringLiteral
0062 }  // namespace Acts