Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:50:47

0001 // Copyright (c) 2023 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _Standard_HashUtils_HeaderFile
0015 #define _Standard_HashUtils_HeaderFile
0016 
0017 #include <Standard_Macro.hxx>
0018 
0019 #include <cstdint>
0020 #include <cstddef>
0021 #include <functional>
0022 #include <type_traits>
0023 
0024 namespace opencascade
0025 {
0026 //! Implementation of Murmur hash with autodetect of sizeof(size_t).
0027 //!
0028 //! The default value for the seed is optimal for general cases at a certain hash size.
0029 namespace MurmurHash
0030 {
0031 uint32_t MurmurHash2A(const void* theKey, int theLen, uint32_t theSeed);
0032 uint64_t MurmurHash64A(const void* theKey, int theLen, uint64_t theSeed);
0033 
0034 template <typename T1, typename T = size_t>
0035 typename std::enable_if<sizeof(T) == 8, uint64_t>::type hash_combine(
0036   const T1& theValue,
0037   const int theLen  = sizeof(T1),
0038   const T   theSeed = 0xA329F1D3A586ULL)
0039 {
0040   return MurmurHash::MurmurHash64A(&theValue, theLen, theSeed);
0041 }
0042 
0043 template <typename T1, typename T = size_t>
0044 typename std::enable_if<sizeof(T) != 8, T>::type hash_combine(const T1& theValue,
0045                                                               const int theLen  = sizeof(T1),
0046                                                               const T   theSeed = 0xc70f6907U)
0047 {
0048   return static_cast<T>(MurmurHash::MurmurHash2A(&theValue, theLen, theSeed));
0049 }
0050 
0051 template <typename T = size_t>
0052 constexpr T optimalSeed()
0053 {
0054   return sizeof(T) == 8 ? static_cast<T>(0xA329F1D3A586ULL) : static_cast<T>(0xc70f6907U);
0055 }
0056 }; // namespace MurmurHash
0057 
0058 //! Implementation of FNV-1a with autodetect of sizeof(size_t).
0059 //! This function should work on unsigned char, otherwise it does not
0060 //! correctly implement the FNV-1a algorithm.
0061 //! The existing behaviour is retained for backwards compatibility.
0062 //!
0063 //! The default value for the seed is optimal for general cases at a certain hash size.
0064 namespace FNVHash
0065 {
0066 uint32_t FNVHash1A(const void* theKey, int theLen, uint32_t theSeed);
0067 uint64_t FNVHash64A(const void* theKey, int theLen, uint64_t theSeed);
0068 
0069 template <typename T1, typename T = size_t>
0070 static typename std::enable_if<sizeof(T) == 8, uint64_t>::type hash_combine(
0071   const T1& theValue,
0072   const int theLen  = sizeof(T1),
0073   const T   theSeed = 14695981039346656037ULL)
0074 {
0075   return FNVHash::FNVHash64A(&theValue, theLen, theSeed);
0076 }
0077 
0078 template <typename T1, typename T = size_t>
0079 static typename std::enable_if<sizeof(T) != 8, T>::type hash_combine(const T1& theValue,
0080                                                                      const int theLen = sizeof(T1),
0081                                                                      const T theSeed  = 2166136261U)
0082 {
0083   return static_cast<T>(FNVHash::FNVHash1A(&theValue, theLen, theSeed));
0084 }
0085 
0086 template <typename T = size_t>
0087 constexpr T optimalSeed()
0088 {
0089   return sizeof(T) == 8 ? static_cast<T>(14695981039346656037ULL) : static_cast<T>(2166136261U);
0090 }
0091 }; // namespace FNVHash
0092 
0093 template <typename T1, typename T = size_t>
0094 T hash(const T1 theValue) noexcept
0095 {
0096   return opencascade::MurmurHash::hash_combine<T1, T>(theValue);
0097 }
0098 
0099 template <typename T1, typename T = size_t>
0100 T hashBytes(const T1* theKey, int theLen)
0101 {
0102   return opencascade::MurmurHash::hash_combine<T1, T>(*theKey, theLen);
0103 }
0104 
0105 template <typename T1, typename T = size_t>
0106 T hash_combine(const T1 theValue, const int theLen, const T theSeed)
0107 {
0108   return opencascade::MurmurHash::hash_combine<T1, T>(theValue, theLen, theSeed);
0109 }
0110 } // namespace opencascade
0111 
0112 #include <Standard_HashUtils.lxx>
0113 
0114 #endif