7#ifndef PXR_BASE_TF_HASH_H
8#define PXR_BASE_TF_HASH_H
15#include "pxr/base/tf/api.h"
27PXR_NAMESPACE_OPEN_SCOPE
30template <
class HashState,
class T>
31std::enable_if_t<std::is_integral<T>::value>
32TfHashAppend(HashState &h, T integral)
39template <
size_t Size>
struct Tf_SizedUnsignedInt;
40template <>
struct Tf_SizedUnsignedInt<1> {
using type = uint8_t; };
41template <>
struct Tf_SizedUnsignedInt<2> {
using type = uint16_t; };
42template <>
struct Tf_SizedUnsignedInt<4> {
using type = uint32_t; };
43template <>
struct Tf_SizedUnsignedInt<8> {
using type = uint64_t; };
46template <
class HashState,
class Enum>
47std::enable_if_t<std::is_enum<Enum>::value>
48TfHashAppend(HashState &h, Enum e)
50 h.Append(
static_cast<std::underlying_type_t<Enum>
>(e));
54template <
class HashState,
class T>
55std::enable_if_t<std::is_floating_point<T>::value>
56TfHashAppend(HashState &h, T fp)
60 typename Tf_SizedUnsignedInt<
sizeof(T)>::type intbuf = 0;
61 if (fp !=
static_cast<T
>(0)) {
62 memcpy(&intbuf, &fp,
sizeof(T));
68template <
class HashState,
class T,
class U>
70TfHashAppend(HashState &h, std::pair<T, U>
const &p)
77template <
class HashState,
class T>
79TfHashAppend(HashState &h, std::vector<T>
const &vec)
81 static_assert(!std::is_same_v<std::remove_cv_t<T>,
bool>,
82 "Unexpected usage of vector of 'bool'."
83 "Expected explicit overload.");
84 h.AppendContiguous(vec.data(), vec.size());
88template <
class HashState>
90TfHashAppend(HashState &h, std::vector<bool>
const &vec)
92 h.Append(std::hash<std::vector<bool>>{}(vec));
98template <
class HashState,
class T,
class Compare>
100TfHashAppend(HashState& h, std::set<T, Compare>
const &elements)
102 h.AppendRange(std::begin(elements), std::end(elements));
108template <
class HashState,
class Key,
class Value,
class Compare>
110TfHashAppend(HashState& h, std::map<Key, Value, Compare>
const &elements)
112 h.AppendRange(std::begin(elements), std::end(elements));
116template <
class HashState>
118TfHashAppend(HashState &h,
const std::string& s)
120 return h.AppendContiguous(s.c_str(), s.length());
125template <
class HashState,
class T>
127TfHashAppend(HashState &h,
const T* ptr) {
128 return h.Append(
reinterpret_cast<uintptr_t
>(ptr));
137template <
class HashState>
138inline void TfHashAppend(HashState &h,
char const *ptr) =
delete;
139template <
class HashState>
140inline void TfHashAppend(HashState &h,
char *ptr) =
delete;
163template <
class HashState>
166 return h.AppendContiguous(hcstr.cstr, std::strlen(hcstr.cstr));
176template <
class HashState,
class T>
177inline auto Tf_HashImpl(HashState &h, T &&obj,
long)
178 ->
decltype(std::hash<typename std::decay<T>::type>()(
179 std::forward<T>(obj)),
void())
182 h, std::hash<
typename std::decay<T>::type>()(std::forward<T>(obj)));
186template <
class HashState,
class T>
187inline auto Tf_HashImpl(HashState &h, T &&obj, ...)
188 ->
decltype(
hash_value(std::forward<T>(obj)), void())
190 TfHashAppend(h,
hash_value(std::forward<T>(obj)));
194template <
class HashState,
class T>
195inline auto Tf_HashImpl(HashState &h, T &&obj,
int)
196 ->
decltype(TfHashAppend(h, std::forward<T>(obj)), void())
198 TfHashAppend(h, std::forward<T>(obj));
203template <
class Derived>
208 template <
class... Args>
209 void Append(Args &&... args) {
210 _AppendImpl(std::forward<Args>(args)...);
215 void AppendContiguous(T
const *elems,
size_t numElems) {
216 this->_AsDerived()._AppendContiguous(elems, numElems);
220 template <
class Iter>
221 void AppendRange(Iter f, Iter l) {
222 this->_AsDerived()._AppendRange(f, l);
226 size_t GetCode()
const {
227 return this->_AsDerived()._GetCode();
231 template <
class T,
class... Args>
232 void _AppendImpl(T &&obj, Args &&... rest) {
233 this->_AsDerived()._Append(std::forward<T>(obj));
234 _AppendImpl(std::forward<Args>(rest)...);
236 void _AppendImpl()
const {
240 Derived &_AsDerived() {
241 return *
static_cast<Derived *
>(
this);
244 Derived
const &_AsDerived()
const {
245 return *
static_cast<Derived
const *
>(
this);
250class Tf_HashState :
public Tf_HashStateAPI<Tf_HashState>
253 friend class Tf_HashStateAPI<Tf_HashState>;
257 std::enable_if_t<!std::is_integral<std::decay_t<T>>::value>
259 Tf_HashImpl(*
this, std::forward<T>(obj), 0);
264 std::enable_if_t<std::is_integral<T>::value>
271 _state = _Combine(_state, i);
277 std::enable_if_t<std::is_integral<T>::value>
278 _AppendContiguous(T
const *elems,
size_t numElems) {
279 _AppendBytes(
reinterpret_cast<char const *
>(elems),
280 numElems *
sizeof(T));
285 std::enable_if_t<!std::is_integral<T>::value>
286 _AppendContiguous(T
const *elems,
size_t numElems) {
293 template <
class Iter>
294 void _AppendRange(Iter f, Iter l) {
301 TF_API
void _AppendBytes(
char const *bytes,
size_t numBytes);
304 size_t _GetCode()
const {
312 return _SwapByteOrder(_state * 11400714819323198549ULL);
317 _SwapByteOrder(uint64_t val)
const {
319 ((val & 0xFF00000000000000u) >> 56u) |
320 ((val & 0x00FF000000000000u) >> 40u) |
321 ((val & 0x0000FF0000000000u) >> 24u) |
322 ((val & 0x000000FF00000000u) >> 8u) |
323 ((val & 0x00000000FF000000u) << 8u) |
324 ((val & 0x0000000000FF0000u) << 24u) |
325 ((val & 0x000000000000FF00u) << 40u) |
326 ((val & 0x00000000000000FFu) << 56u);
330 size_t _Combine(
size_t x,
size_t y)
const {
362 return y + x * (x + 1) / 2;
366 bool _didOne =
false;
466 decltype(Tf_HashImpl(std::declval<Tf_HashState &>(),
467 std::forward<T>(obj), 0), size_t()) {
469 Tf_HashImpl(h, std::forward<T>(obj), 0);
474 template <
class... Args>
477 _CombineImpl(h, std::forward<Args>(args)...);
482 template <
class HashState,
class T,
class... Args>
483 static void _CombineImpl(HashState &h, T &&obj, Args &&... rest) {
484 Tf_HashImpl(h, std::forward<T>(obj), 0);
485 _CombineImpl(h, std::forward<Args>(rest)...);
488 template <
class HashState>
489 static void _CombineImpl(HashState &h) {
496 size_t operator()(
const char* ptr)
const;
501 size_t operator()(
const char* ptr)
const;
506 bool operator()(
const char* lhs,
const char* rhs)
const;
509PXR_NAMESPACE_CLOSE_SCOPE
A user-extensible hashing mechanism for use with runtime hash tables.
static size_t Combine(Args &&... args)
Produce a hash code by combining the hash codes of several objects.
auto operator()(T &&obj) const -> decltype(Tf_HashImpl(std::declval< Tf_HashState & >(), std::forward< T >(obj), 0), size_t())
Produce a hash code for obj.
A structure that wraps a char pointer, indicating intent that it should be hashed as a c-style null t...
A function object that compares two c-strings for equality.
A hash function object that hashes null-terminated c-string content.
A hash function object that hashes the address of a char pointer.
TfCStrHashWrapper TfHashAsCStr(char const *cstr)
Indicate that a char pointer is intended to be hashed as a C-style null terminated string.
A file containing basic constants and definitions.
size_t hash_value(const TfToken &x)
Overload hash_value for TfToken.