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>
95inline std::enable_if_t<!std::is_same<std::remove_const_t<T>,
bool>::value>
96TfHashAppend(HashState &h, std::vector<T>
const &vec)
98 h.AppendContiguous(vec.data(), vec.size());
102template <
class HashState>
104TfHashAppend(HashState &h, std::vector<bool>
const &vec)
106 h.Append(std::hash<std::vector<bool>>{}(vec));
112template <
class HashState,
class T,
class Compare>
114TfHashAppend(HashState& h, std::set<T, Compare>
const &elements)
116 h.AppendRange(std::begin(elements), std::end(elements));
122template <
class HashState,
class Key,
class Value,
class Compare>
124TfHashAppend(HashState& h, std::map<Key, Value, Compare>
const &elements)
126 h.AppendRange(std::begin(elements), std::end(elements));
131template <
class HashState>
133TfHashAppend(HashState& h, std::type_index
const &type_index)
135 return h.Append(type_index.hash_code());
139template <
class HashState>
141TfHashAppend(HashState &h,
const std::string& s)
143 return h.AppendContiguous(s.c_str(), s.length());
148template <
class HashState,
class T>
150TfHashAppend(HashState &h,
const T* ptr) {
151 return h.Append(
reinterpret_cast<uintptr_t
>(ptr));
156template <
class HashState,
class T>
158TfHashAppend(HashState &h,
const std::shared_ptr<T>& ptr) {
159 h.Append(std::hash<std::shared_ptr<T>>{}(ptr));
164template <
class HashState,
class T>
166TfHashAppend(HashState &h,
const std::unique_ptr<T>& ptr) {
167 h.Append(std::hash<std::unique_ptr<T>>{}(ptr));
176template <
class HashState>
177inline void TfHashAppend(HashState &h,
char const *ptr) =
delete;
178template <
class HashState>
179inline void TfHashAppend(HashState &h,
char *ptr) =
delete;
202template <
class HashState>
205 return h.AppendContiguous(hcstr.cstr, std::strlen(hcstr.cstr));
230template <
class HashState,
class T>
231inline auto Tf_HashImpl(HashState &h, T &&obj,
long)
232 ->
decltype(
hash_value(std::forward<T>(obj)), void())
234 TfHashAppend(h,
hash_value(std::forward<T>(obj)));
238template <
class HashState,
class T>
239inline auto Tf_HashImpl(HashState &h, T &&obj,
int)
240 ->
decltype(TfHashAppend(h, std::forward<T>(obj)), void())
242 TfHashAppend(h, std::forward<T>(obj));
247template <
class Derived>
252 template <
class... Args>
253 void Append(Args &&... args) {
254 _AppendImpl(std::forward<Args>(args)...);
259 void AppendContiguous(T
const *elems,
size_t numElems) {
260 this->_AsDerived()._AppendContiguous(elems, numElems);
264 template <
class Iter>
265 void AppendRange(Iter f, Iter l) {
266 this->_AsDerived()._AppendRange(f, l);
270 size_t GetCode()
const {
271 return this->_AsDerived()._GetCode();
275 template <
class T,
class... Args>
276 void _AppendImpl(T &&obj, Args &&... rest) {
277 this->_AsDerived()._Append(std::forward<T>(obj));
278 _AppendImpl(std::forward<Args>(rest)...);
280 void _AppendImpl()
const {
284 Derived &_AsDerived() {
285 return *
static_cast<Derived *
>(
this);
288 Derived
const &_AsDerived()
const {
289 return *
static_cast<Derived
const *
>(
this);
294class Tf_HashState :
public Tf_HashStateAPI<Tf_HashState>
297 friend class Tf_HashStateAPI<Tf_HashState>;
301 std::enable_if_t<!std::is_integral<std::decay_t<T>>::value>
303 Tf_HashImpl(*
this, std::forward<T>(obj), 0);
308 std::enable_if_t<std::is_integral<T>::value>
315 _state = _Combine(_state, i);
321 std::enable_if_t<std::is_integral<T>::value>
322 _AppendContiguous(T
const *elems,
size_t numElems) {
323 _AppendBytes(
reinterpret_cast<char const *
>(elems),
324 numElems *
sizeof(T));
329 std::enable_if_t<!std::is_integral<T>::value>
330 _AppendContiguous(T
const *elems,
size_t numElems) {
337 template <
class Iter>
338 void _AppendRange(Iter f, Iter l) {
345 TF_API
void _AppendBytes(
char const *bytes,
size_t numBytes);
348 size_t _GetCode()
const {
356 return _SwapByteOrder(_state * 11400714819323198549ULL);
361 _SwapByteOrder(uint64_t val)
const {
363 ((val & 0xFF00000000000000u) >> 56u) |
364 ((val & 0x00FF000000000000u) >> 40u) |
365 ((val & 0x0000FF0000000000u) >> 24u) |
366 ((val & 0x000000FF00000000u) >> 8u) |
367 ((val & 0x00000000FF000000u) << 8u) |
368 ((val & 0x0000000000FF0000u) << 24u) |
369 ((val & 0x000000000000FF00u) << 40u) |
370 ((val & 0x00000000000000FFu) << 56u);
374 size_t _Combine(
size_t x,
size_t y)
const {
406 return y + x * (x + 1) / 2;
410 bool _didOne =
false;
510 decltype(Tf_HashImpl(std::declval<Tf_HashState &>(),
511 std::forward<T>(obj), 0), size_t()) {
513 Tf_HashImpl(h, std::forward<T>(obj), 0);
518 template <
class... Args>
521 _CombineImpl(h, std::forward<Args>(args)...);
526 template <
class HashState,
class T,
class... Args>
527 static void _CombineImpl(HashState &h, T &&obj, Args &&... rest) {
528 Tf_HashImpl(h, std::forward<T>(obj), 0);
529 _CombineImpl(h, std::forward<Args>(rest)...);
532 template <
class HashState>
533 static void _CombineImpl(HashState &h) {
540 size_t operator()(
const char* ptr)
const;
545 size_t operator()(
const char* ptr)
const;
550 bool operator()(
const char* lhs,
const char* rhs)
const;
553PXR_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.