All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
stringHash.h
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
15PXR_NAMESPACE_OPEN_SCOPE
16
25 public:
26
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.
43template <>
44constexpr std::uint32_t TraceStringHash::djb2HashStr<0>(const char* str) {
45 return 5381;
46}
47
48PXR_NAMESPACE_CLOSE_SCOPE
49
50#endif //PXR_BASE_TRACE_STRING_HASH_H
This class provides a function to compute compile time hashes for string literals.
Definition: stringHash.h:24
static constexpr std::uint32_t Hash(const char(&str)[N])
Computes a compile time hash of str.
Definition: stringHash.h:29