24#ifndef PXR_BASE_TF_HASH_H
25#define PXR_BASE_TF_HASH_H
32#include "pxr/base/tf/api.h"
44PXR_NAMESPACE_OPEN_SCOPE
47template <
class HashState,
class T>
48std::enable_if_t<std::is_integral<T>::value>
49TfHashAppend(HashState &h, T integral)
56template <
size_t Size>
struct Tf_SizedUnsignedInt;
57template <>
struct Tf_SizedUnsignedInt<1> {
using type = uint8_t; };
58template <>
struct Tf_SizedUnsignedInt<2> {
using type = uint16_t; };
59template <>
struct Tf_SizedUnsignedInt<4> {
using type = uint32_t; };
60template <>
struct Tf_SizedUnsignedInt<8> {
using type = uint64_t; };
63template <
class HashState,
class Enum>
64std::enable_if_t<std::is_enum<Enum>::value>
65TfHashAppend(HashState &h, Enum e)
67 h.Append(
static_cast<std::underlying_type_t<Enum>
>(e));
71template <
class HashState,
class T>
72std::enable_if_t<std::is_floating_point<T>::value>
73TfHashAppend(HashState &h, T fp)
77 typename Tf_SizedUnsignedInt<
sizeof(T)>::type intbuf = 0;
78 if (fp !=
static_cast<T
>(0)) {
79 memcpy(&intbuf, &fp,
sizeof(T));
85template <
class HashState,
class T,
class U>
87TfHashAppend(HashState &h, std::pair<T, U>
const &p)
94template <
class HashState,
class T>
96TfHashAppend(HashState &h, std::vector<T>
const &vec)
98 static_assert(!std::is_same_v<std::remove_cv_t<T>,
bool>,
99 "Unexpected usage of vector of 'bool'."
100 "Expected explicit overload.");
101 h.AppendContiguous(vec.data(), vec.size());
105template <
class HashState>
107TfHashAppend(HashState &h, std::vector<bool>
const &vec)
109 h.Append(std::hash<std::vector<bool>>{}(vec));
115template <
class HashState,
class T,
class Compare>
117TfHashAppend(HashState& h, std::set<T, Compare>
const &elements)
119 h.AppendRange(std::begin(elements), std::end(elements));
125template <
class HashState,
class Key,
class Value,
class Compare>
127TfHashAppend(HashState& h, std::map<Key, Value, Compare>
const &elements)
129 h.AppendRange(std::begin(elements), std::end(elements));
133template <
class HashState>
135TfHashAppend(HashState &h,
const std::string& s)
137 return h.AppendContiguous(s.c_str(), s.length());
142template <
class HashState,
class T>
144TfHashAppend(HashState &h,
const T* ptr) {
145 return h.Append(
reinterpret_cast<uintptr_t
>(ptr));
154template <
class HashState>
155inline void TfHashAppend(HashState &h,
char const *ptr) =
delete;
156template <
class HashState>
157inline void TfHashAppend(HashState &h,
char *ptr) =
delete;
180template <
class HashState>
183 return h.AppendContiguous(hcstr.cstr, std::strlen(hcstr.cstr));
193template <
class HashState,
class T>
194inline auto Tf_HashImpl(HashState &h, T &&obj,
long)
195 ->
decltype(std::hash<typename std::decay<T>::type>()(
196 std::forward<T>(obj)),
void())
199 h, std::hash<
typename std::decay<T>::type>()(std::forward<T>(obj)));
203template <
class HashState,
class T>
204inline auto Tf_HashImpl(HashState &h, T &&obj, ...)
205 ->
decltype(
hash_value(std::forward<T>(obj)), void())
207 TfHashAppend(h,
hash_value(std::forward<T>(obj)));
211template <
class HashState,
class T>
212inline auto Tf_HashImpl(HashState &h, T &&obj,
int)
213 ->
decltype(TfHashAppend(h, std::forward<T>(obj)), void())
215 TfHashAppend(h, std::forward<T>(obj));
220template <
class Derived>
225 template <
class... Args>
226 void Append(Args &&... args) {
227 _AppendImpl(std::forward<Args>(args)...);
232 void AppendContiguous(T
const *elems,
size_t numElems) {
233 this->_AsDerived()._AppendContiguous(elems, numElems);
237 template <
class Iter>
238 void AppendRange(Iter f, Iter l) {
239 this->_AsDerived()._AppendRange(f, l);
243 size_t GetCode()
const {
244 return this->_AsDerived()._GetCode();
248 template <
class T,
class... Args>
249 void _AppendImpl(T &&obj, Args &&... rest) {
250 this->_AsDerived()._Append(std::forward<T>(obj));
251 _AppendImpl(std::forward<Args>(rest)...);
253 void _AppendImpl()
const {
257 Derived &_AsDerived() {
258 return *
static_cast<Derived *
>(
this);
261 Derived
const &_AsDerived()
const {
262 return *
static_cast<Derived
const *
>(
this);
267class Tf_HashState :
public Tf_HashStateAPI<Tf_HashState>
270 friend class Tf_HashStateAPI<Tf_HashState>;
274 std::enable_if_t<!std::is_integral<std::decay_t<T>>::value>
276 Tf_HashImpl(*
this, std::forward<T>(obj), 0);
281 std::enable_if_t<std::is_integral<T>::value>
288 _state = _Combine(_state, i);
294 std::enable_if_t<std::is_integral<T>::value>
295 _AppendContiguous(T
const *elems,
size_t numElems) {
296 _AppendBytes(
reinterpret_cast<char const *
>(elems),
297 numElems *
sizeof(T));
302 std::enable_if_t<!std::is_integral<T>::value>
303 _AppendContiguous(T
const *elems,
size_t numElems) {
310 template <
class Iter>
311 void _AppendRange(Iter f, Iter l) {
318 TF_API
void _AppendBytes(
char const *bytes,
size_t numBytes);
321 size_t _GetCode()
const {
329 return _SwapByteOrder(_state * 11400714819323198549ULL);
334 _SwapByteOrder(uint64_t val)
const {
336 ((val & 0xFF00000000000000u) >> 56u) |
337 ((val & 0x00FF000000000000u) >> 40u) |
338 ((val & 0x0000FF0000000000u) >> 24u) |
339 ((val & 0x000000FF00000000u) >> 8u) |
340 ((val & 0x00000000FF000000u) << 8u) |
341 ((val & 0x0000000000FF0000u) << 24u) |
342 ((val & 0x000000000000FF00u) << 40u) |
343 ((val & 0x00000000000000FFu) << 56u);
347 size_t _Combine(
size_t x,
size_t y)
const {
379 return y + x * (x + 1) / 2;
383 bool _didOne =
false;
483 decltype(Tf_HashImpl(std::declval<Tf_HashState &>(),
484 std::forward<T>(obj), 0), size_t()) {
486 Tf_HashImpl(h, std::forward<T>(obj), 0);
491 template <
class... Args>
494 _CombineImpl(h, std::forward<Args>(args)...);
499 template <
class HashState,
class T,
class... Args>
500 static void _CombineImpl(HashState &h, T &&obj, Args &&... rest) {
501 Tf_HashImpl(h, std::forward<T>(obj), 0);
502 _CombineImpl(h, std::forward<Args>(rest)...);
505 template <
class HashState>
506 static void _CombineImpl(HashState &h) {
513 size_t operator()(
const char* ptr)
const;
518 size_t operator()(
const char* ptr)
const;
523 bool operator()(
const char* lhs,
const char* rhs)
const;
526PXR_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.