Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-14 09:21:07

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 // FNV-1a 64bit hashing algorithm.
0038 constexpr std::uint64_t fnv1a_64(const char* data, std::size_t len) {
0039   constexpr std::uint64_t fnv_offset_basis = 0xcbf29ce484222325ULL;
0040   constexpr std::uint64_t fnv_prime = 0x100000001b3ULL;
0041 
0042   std::uint64_t hash = fnv_offset_basis;
0043   for (std::size_t i = 0; i < len; ++i) {
0044     hash ^= static_cast<std::uint64_t>(static_cast<unsigned char>(data[i]));
0045     hash *= fnv_prime;
0046   }
0047   return hash;
0048 }
0049 
0050 constexpr std::uint64_t fnv1a_64(std::string_view sv) {
0051   return fnv1a_64(sv.data(), sv.size());
0052 }
0053 
0054 constexpr int length(const char* str) {
0055   return *str != 0 ? 1 + length(str + 1) : 0;
0056 }
0057 }  // namespace detail
0058 
0059 /// Compile-time hash of string literal
0060 /// @param s String view to hash
0061 /// @return Hashed string representation
0062 consteval HashedString hashString(std::string_view s) {
0063   return detail::fnv1a_32(s);
0064 }
0065 
0066 /// Runtime hash of string
0067 /// @param s String view to hash
0068 /// @return Hashed string representation
0069 constexpr HashedString hashStringDynamic(std::string_view s) {
0070   return detail::fnv1a_32(s);
0071 }
0072 
0073 namespace HashedStringLiteral {
0074 constexpr HashedString operator""_hash(char const* s, std::size_t count) {
0075   return detail::fnv1a_32(s, count);
0076 }
0077 
0078 }  // namespace HashedStringLiteral
0079 }  // namespace Acts