Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-10 08:18:32

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 <typeinfo>
0016 
0017 namespace Acts {
0018 /// @brief Type alias for hashed string representation
0019 /// @details Represents a string as a compile-time hash value for efficient comparison
0020 using HashedString = std::uint32_t;
0021 
0022 // Adapted from https://gist.github.com/Lee-R/3839813
0023 namespace detail {
0024 // FNV-1a 32bit hashing algorithm.
0025 constexpr HashedString fnv1a_32(char const* s, std::size_t count) {
0026   return count != 0u ? (fnv1a_32(s, count - 1) ^ s[count - 1]) * 16777619u
0027                      : 2166136261u;
0028 }
0029 
0030 constexpr HashedString fnv1a_32(std::string_view s) {
0031   return !s.empty() ? (fnv1a_32(s.substr(0, s.size() - 1)) ^ s[s.size() - 1]) *
0032                           16777619u
0033                     : 2166136261u;
0034 }
0035 
0036 // FNV-1a 64bit hashing algorithm.
0037 constexpr std::uint64_t fnv1a_64(const char* data, std::size_t len) {
0038   constexpr std::uint64_t fnv_offset_basis = 0xcbf29ce484222325ULL;
0039   constexpr std::uint64_t fnv_prime = 0x100000001b3ULL;
0040 
0041   std::uint64_t hash = fnv_offset_basis;
0042   for (std::size_t i = 0; i < len; ++i) {
0043     hash ^= static_cast<std::uint64_t>(static_cast<unsigned char>(data[i]));
0044     hash *= fnv_prime;
0045   }
0046   return hash;
0047 }
0048 
0049 constexpr std::uint64_t fnv1a_64(std::string_view sv) {
0050   return fnv1a_64(sv.data(), sv.size());
0051 }
0052 
0053 constexpr int length(const char* str) {
0054   return *str != 0 ? 1 + length(str + 1) : 0;
0055 }
0056 }  // namespace detail
0057 
0058 /// Compile-time hash of string literal
0059 /// @param s String view to hash
0060 /// @return Hashed string representation
0061 consteval HashedString hashString(std::string_view s) {
0062   return detail::fnv1a_32(s);
0063 }
0064 
0065 /// Runtime hash of string
0066 /// @param s String view to hash
0067 /// @return Hashed string representation
0068 constexpr HashedString hashStringDynamic(std::string_view s) {
0069   return detail::fnv1a_32(s);
0070 }
0071 
0072 namespace HashedStringLiteral {
0073 constexpr HashedString operator""_hash(char const* s, std::size_t count) {
0074   return detail::fnv1a_32(s, count);
0075 }
0076 
0077 }  // namespace HashedStringLiteral
0078 
0079 /// Hash for a type. Since it's not possible to hash a type at compile-time,
0080 /// this function returns a runtime hash but caches it in a static variable.
0081 /// @tparam T Type to hash
0082 /// @return Hashed string representation
0083 template <typename T>
0084 std::uint64_t typeHash() {
0085   const static std::uint64_t value = detail::fnv1a_64(typeid(T).name());
0086   return value;
0087 }
0088 
0089 }  // namespace Acts