HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stringHash.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_BASE_TRACE_STRING_HASH_H
9 #define PXR_BASE_TRACE_STRING_HASH_H
10 
11 #include "pxr/pxr.h"
12 
13 #include <cstdint>
14 
16 
17 ///////////////////////////////////////////////////////////////////////////////
18 ///
19 /// \class TraceStringHash
20 ///
21 /// This class provides a function to compute compile time hashes for string
22 /// literals.
23 ///
25  public:
26 
27  /// Computes a compile time hash of \p str.
28  template <int N>
29  static constexpr std::uint32_t Hash(const char (&str)[N]) {
30  return djb2HashStr<N-1>(str);
31  }
32 
33  private:
34  // Recursive function computing the xor variant of the djb2 hash
35  // function.
36  template <int N>
37  static constexpr std::uint32_t djb2HashStr(const char* str) {
38  return (djb2HashStr<N-1>(str) * 33) ^ str[N-1];
39  }
40 };
41 
42 // Template recursion base case.
43 template <>
44 constexpr std::uint32_t TraceStringHash::djb2HashStr<0>(const char* str) {
45  return 5381;
46 }
47 
49 
50 #endif //PXR_BASE_TRACE_STRING_HASH_H
static constexpr std::uint32_t Hash(const char(&str)[N])
Computes a compile time hash of str.
Definition: stringHash.h:29
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GA_API const UT_StringHolder N