24 #ifndef PXR_BASE_TF_HASH_H 25 #define PXR_BASE_TF_HASH_H 32 #include "pxr/base/tf/api.h" 40 #include <type_traits> 44 PXR_NAMESPACE_OPEN_SCOPE
47 template <
class HashState,
class T>
48 std::enable_if_t<std::is_integral<T>::value>
49 TfHashAppend(HashState &h, T integral)
56 template <
size_t Size>
struct Tf_SizedUnsignedInt;
57 template <>
struct Tf_SizedUnsignedInt<1> {
using type = uint8_t; };
58 template <>
struct Tf_SizedUnsignedInt<2> {
using type = uint16_t; };
59 template <>
struct Tf_SizedUnsignedInt<4> {
using type = uint32_t; };
60 template <>
struct Tf_SizedUnsignedInt<8> {
using type = uint64_t; };
63 template <
class HashState,
class Enum>
64 std::enable_if_t<std::is_enum<Enum>::value>
65 TfHashAppend(HashState &h, Enum e)
67 h.Append(
static_cast<std::underlying_type_t<Enum>
>(e));
71 template <
class HashState,
class T>
72 std::enable_if_t<std::is_floating_point<T>::value>
73 TfHashAppend(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));
85 template <
class HashState,
class T,
class U>
87 TfHashAppend(HashState &h, std::pair<T, U>
const &p)
94 template <
class HashState,
class T>
95 inline std::enable_if_t<!std::is_same<std::remove_const_t<T>,
bool>::value>
96 TfHashAppend(HashState &h, std::vector<T>
const &vec)
98 h.AppendContiguous(vec.data(), vec.size());
102 template <
class HashState>
104 TfHashAppend(HashState &h, std::vector<bool>
const &vec)
106 h.Append(std::hash<std::vector<bool>>{}(vec));
112 template <
class HashState,
class T,
class Compare>
114 TfHashAppend(HashState& h, std::set<T, Compare>
const &elements)
116 h.AppendRange(std::begin(elements), std::end(elements));
122 template <
class HashState,
class Key,
class Value,
class Compare>
124 TfHashAppend(HashState& h, std::map<Key, Value, Compare>
const &elements)
126 h.AppendRange(std::begin(elements), std::end(elements));
131 template <
class HashState>
133 TfHashAppend(HashState& h, std::type_index
const &type_index)
135 return h.Append(type_index.hash_code());
139 template <
class HashState>
141 TfHashAppend(HashState &h,
const std::string& s)
143 return h.AppendContiguous(s.c_str(), s.length());
148 template <
class HashState,
class T>
150 TfHashAppend(HashState &h,
const T* ptr) {
151 return h.Append(reinterpret_cast<uintptr_t>(ptr));
156 template <
class HashState,
class T>
158 TfHashAppend(HashState &h,
const std::shared_ptr<T>& ptr) {
159 h.Append(std::hash<std::shared_ptr<T>>{}(ptr));
164 template <
class HashState,
class T>
166 TfHashAppend(HashState &h,
const std::unique_ptr<T>& ptr) {
167 h.Append(std::hash<std::unique_ptr<T>>{}(ptr));
176 template <
class HashState>
177 inline void TfHashAppend(HashState &h,
char const *ptr) =
delete;
178 template <
class HashState>
179 inline void TfHashAppend(HashState &h,
char *ptr) =
delete;
202 template <
class HashState>
205 return h.AppendContiguous(hcstr.cstr, std::strlen(hcstr.cstr));
230 template <
class HashState,
class T>
231 inline 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)));
238 template <
class HashState,
class T>
239 inline auto Tf_HashImpl(HashState &h, T &&obj,
int)
240 -> decltype(TfHashAppend(h, std::forward<T>(obj)),
void())
242 TfHashAppend(h, std::forward<T>(obj));
247 template <
class Derived>
248 class Tf_HashStateAPI
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);
294 class 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;
553 PXR_NAMESPACE_CLOSE_SCOPE
A hash function object that hashes the address of a char pointer.
A structure that wraps a char pointer, indicating intent that it should be hashed as a c-style null t...
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 hash function object that hashes null-terminated c-string content.
A function object that compares two c-strings for equality.
size_t hash_value(const half h)
Overload hash_value for half.
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.