00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef ABSL_HASH_INTERNAL_HASH_H_
00020 #define ABSL_HASH_INTERNAL_HASH_H_
00021
00022 #include <algorithm>
00023 #include <array>
00024 #include <cmath>
00025 #include <cstring>
00026 #include <deque>
00027 #include <forward_list>
00028 #include <functional>
00029 #include <iterator>
00030 #include <limits>
00031 #include <list>
00032 #include <map>
00033 #include <memory>
00034 #include <set>
00035 #include <string>
00036 #include <tuple>
00037 #include <type_traits>
00038 #include <utility>
00039 #include <vector>
00040
00041 #include "absl/base/internal/endian.h"
00042 #include "absl/base/port.h"
00043 #include "absl/container/fixed_array.h"
00044 #include "absl/meta/type_traits.h"
00045 #include "absl/numeric/int128.h"
00046 #include "absl/strings/string_view.h"
00047 #include "absl/types/optional.h"
00048 #include "absl/types/variant.h"
00049 #include "absl/utility/utility.h"
00050 #include "absl/hash/internal/city.h"
00051
00052 namespace absl {
00053 namespace hash_internal {
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 template <typename H>
00083 class HashStateBase {
00084 public:
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 template <typename T, typename... Ts>
00103 static H combine(H state, const T& value, const Ts&... values);
00104 static H combine(H state) { return state; }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 template <typename T>
00119 static H combine_contiguous(H state, const T* data, size_t size);
00120 };
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 template <typename T, typename Enable = void>
00156 struct is_uniquely_represented : std::false_type {};
00157
00158
00159
00160
00161
00162 template <>
00163 struct is_uniquely_represented<unsigned char> : std::true_type {};
00164
00165
00166
00167
00168
00169 template <typename Integral>
00170 struct is_uniquely_represented<
00171 Integral, typename std::enable_if<std::is_integral<Integral>::value>::type>
00172 : std::true_type {};
00173
00174
00175
00176
00177 template <>
00178 struct is_uniquely_represented<bool> : std::false_type {};
00179
00180
00181
00182
00183
00184 template <typename H, typename T>
00185 H hash_bytes(H hash_state, const T& value) {
00186 const unsigned char* start = reinterpret_cast<const unsigned char*>(&value);
00187 return H::combine_contiguous(std::move(hash_state), start, sizeof(value));
00188 }
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 template <typename H, typename B>
00204 typename std::enable_if<std::is_same<B, bool>::value, H>::type AbslHashValue(
00205 H hash_state, B value) {
00206 return H::combine(std::move(hash_state),
00207 static_cast<unsigned char>(value ? 1 : 0));
00208 }
00209
00210
00211 template <typename H, typename Enum>
00212 typename std::enable_if<std::is_enum<Enum>::value, H>::type AbslHashValue(
00213 H hash_state, Enum e) {
00214
00215
00216
00217
00218
00219 return H::combine(std::move(hash_state),
00220 static_cast<typename std::underlying_type<Enum>::type>(e));
00221 }
00222
00223 template <typename H, typename Float>
00224 typename std::enable_if<std::is_same<Float, float>::value ||
00225 std::is_same<Float, double>::value,
00226 H>::type
00227 AbslHashValue(H hash_state, Float value) {
00228 return hash_internal::hash_bytes(std::move(hash_state),
00229 value == 0 ? 0 : value);
00230 }
00231
00232
00233
00234
00235
00236 template <typename H, typename LongDouble>
00237 typename std::enable_if<std::is_same<LongDouble, long double>::value, H>::type
00238 AbslHashValue(H hash_state, LongDouble value) {
00239 const int category = std::fpclassify(value);
00240 switch (category) {
00241 case FP_INFINITE:
00242
00243 hash_state = H::combine(std::move(hash_state), std::signbit(value));
00244 break;
00245
00246 case FP_NAN:
00247 case FP_ZERO:
00248 default:
00249
00250 break;
00251
00252 case FP_NORMAL:
00253 case FP_SUBNORMAL:
00254
00255
00256
00257
00258
00259 int exp;
00260 auto mantissa = static_cast<double>(std::frexp(value, &exp));
00261 hash_state = H::combine(std::move(hash_state), mantissa, exp);
00262 }
00263
00264 return H::combine(std::move(hash_state), category);
00265 }
00266
00267
00268 template <typename H, typename T>
00269 H AbslHashValue(H hash_state, T* ptr) {
00270 auto v = reinterpret_cast<uintptr_t>(ptr);
00271
00272
00273
00274
00275 return H::combine(std::move(hash_state), v, v);
00276 }
00277
00278
00279 template <typename H>
00280 H AbslHashValue(H hash_state, std::nullptr_t) {
00281 return H::combine(std::move(hash_state), static_cast<void*>(nullptr));
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 template <typename T>
00293 struct is_hashable;
00294
00295
00296 template <typename H, typename T1, typename T2>
00297 typename std::enable_if<is_hashable<T1>::value && is_hashable<T2>::value,
00298 H>::type
00299 AbslHashValue(H hash_state, const std::pair<T1, T2>& p) {
00300 return H::combine(std::move(hash_state), p.first, p.second);
00301 }
00302
00303
00304
00305
00306
00307 template <typename H, typename Tuple, size_t... Is>
00308 H hash_tuple(H hash_state, const Tuple& t, absl::index_sequence<Is...>) {
00309 return H::combine(std::move(hash_state), std::get<Is>(t)...);
00310 }
00311
00312
00313 template <typename H, typename... Ts>
00314 #if defined(_MSC_VER)
00315
00316
00317 H
00318 #else // _MSC_VER
00319 typename std::enable_if<absl::conjunction<is_hashable<Ts>...>::value, H>::type
00320 #endif // _MSC_VER
00321 AbslHashValue(H hash_state, const std::tuple<Ts...>& t) {
00322 return hash_internal::hash_tuple(std::move(hash_state), t,
00323 absl::make_index_sequence<sizeof...(Ts)>());
00324 }
00325
00326
00327
00328
00329
00330
00331 template <typename H, typename T, typename D>
00332 H AbslHashValue(H hash_state, const std::unique_ptr<T, D>& ptr) {
00333 return H::combine(std::move(hash_state), ptr.get());
00334 }
00335
00336
00337 template <typename H, typename T>
00338 H AbslHashValue(H hash_state, const std::shared_ptr<T>& ptr) {
00339 return H::combine(std::move(hash_state), ptr.get());
00340 }
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 template <typename H>
00360 H AbslHashValue(H hash_state, absl::string_view str) {
00361 return H::combine(
00362 H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
00363 str.size());
00364 }
00365
00366
00367
00368
00369
00370
00371 template <typename H, typename T, size_t N>
00372 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
00373 H hash_state, const std::array<T, N>& array) {
00374 return H::combine_contiguous(std::move(hash_state), array.data(),
00375 array.size());
00376 }
00377
00378
00379 template <typename H, typename T, typename Allocator>
00380 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
00381 H hash_state, const std::deque<T, Allocator>& deque) {
00382
00383
00384 for (const auto& t : deque) {
00385 hash_state = H::combine(std::move(hash_state), t);
00386 }
00387 return H::combine(std::move(hash_state), deque.size());
00388 }
00389
00390
00391 template <typename H, typename T, typename Allocator>
00392 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
00393 H hash_state, const std::forward_list<T, Allocator>& list) {
00394 size_t size = 0;
00395 for (const T& t : list) {
00396 hash_state = H::combine(std::move(hash_state), t);
00397 ++size;
00398 }
00399 return H::combine(std::move(hash_state), size);
00400 }
00401
00402
00403 template <typename H, typename T, typename Allocator>
00404 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
00405 H hash_state, const std::list<T, Allocator>& list) {
00406 for (const auto& t : list) {
00407 hash_state = H::combine(std::move(hash_state), t);
00408 }
00409 return H::combine(std::move(hash_state), list.size());
00410 }
00411
00412
00413
00414
00415
00416 template <typename H, typename T, typename Allocator>
00417 typename std::enable_if<is_hashable<T>::value && !std::is_same<T, bool>::value,
00418 H>::type
00419 AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
00420 return H::combine(H::combine_contiguous(std::move(hash_state), vector.data(),
00421 vector.size()),
00422 vector.size());
00423 }
00424
00425
00426
00427
00428
00429
00430 template <typename H, typename Key, typename T, typename Compare,
00431 typename Allocator>
00432 typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
00433 H>::type
00434 AbslHashValue(H hash_state, const std::map<Key, T, Compare, Allocator>& map) {
00435 for (const auto& t : map) {
00436 hash_state = H::combine(std::move(hash_state), t);
00437 }
00438 return H::combine(std::move(hash_state), map.size());
00439 }
00440
00441
00442 template <typename H, typename Key, typename T, typename Compare,
00443 typename Allocator>
00444 typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
00445 H>::type
00446 AbslHashValue(H hash_state,
00447 const std::multimap<Key, T, Compare, Allocator>& map) {
00448 for (const auto& t : map) {
00449 hash_state = H::combine(std::move(hash_state), t);
00450 }
00451 return H::combine(std::move(hash_state), map.size());
00452 }
00453
00454
00455 template <typename H, typename Key, typename Compare, typename Allocator>
00456 typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
00457 H hash_state, const std::set<Key, Compare, Allocator>& set) {
00458 for (const auto& t : set) {
00459 hash_state = H::combine(std::move(hash_state), t);
00460 }
00461 return H::combine(std::move(hash_state), set.size());
00462 }
00463
00464
00465 template <typename H, typename Key, typename Compare, typename Allocator>
00466 typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
00467 H hash_state, const std::multiset<Key, Compare, Allocator>& set) {
00468 for (const auto& t : set) {
00469 hash_state = H::combine(std::move(hash_state), t);
00470 }
00471 return H::combine(std::move(hash_state), set.size());
00472 }
00473
00474
00475
00476
00477
00478
00479 template <typename H, typename T>
00480 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
00481 H hash_state, const absl::optional<T>& opt) {
00482 if (opt) hash_state = H::combine(std::move(hash_state), *opt);
00483 return H::combine(std::move(hash_state), opt.has_value());
00484 }
00485
00486
00487 template <typename H>
00488 struct VariantVisitor {
00489 H&& hash_state;
00490 template <typename T>
00491 H operator()(const T& t) const {
00492 return H::combine(std::move(hash_state), t);
00493 }
00494 };
00495
00496
00497 template <typename H, typename... T>
00498 typename std::enable_if<conjunction<is_hashable<T>...>::value, H>::type
00499 AbslHashValue(H hash_state, const absl::variant<T...>& v) {
00500 if (!v.valueless_by_exception()) {
00501 hash_state = absl::visit(VariantVisitor<H>{std::move(hash_state)}, v);
00502 }
00503 return H::combine(std::move(hash_state), v.index());
00504 }
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521 template <typename H, typename T>
00522 typename std::enable_if<is_uniquely_represented<T>::value, H>::type
00523 hash_range_or_bytes(H hash_state, const T* data, size_t size) {
00524 const auto* bytes = reinterpret_cast<const unsigned char*>(data);
00525 return H::combine_contiguous(std::move(hash_state), bytes, sizeof(T) * size);
00526 }
00527
00528
00529 template <typename H, typename T>
00530 typename std::enable_if<!is_uniquely_represented<T>::value, H>::type
00531 hash_range_or_bytes(H hash_state, const T* data, size_t size) {
00532 for (const auto end = data + size; data < end; ++data) {
00533 hash_state = H::combine(std::move(hash_state), *data);
00534 }
00535 return hash_state;
00536 }
00537
00538 #if defined(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE) && \
00539 ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
00540 #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 1
00541 #else
00542 #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 0
00543 #endif
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 struct HashSelect {
00555 private:
00556 struct State : HashStateBase<State> {
00557 static State combine_contiguous(State hash_state, const unsigned char*,
00558 size_t);
00559 using State::HashStateBase::combine_contiguous;
00560 };
00561
00562 struct UniquelyRepresentedProbe {
00563 template <typename H, typename T>
00564 static auto Invoke(H state, const T& value)
00565 -> absl::enable_if_t<is_uniquely_represented<T>::value, H> {
00566 return hash_internal::hash_bytes(std::move(state), value);
00567 }
00568 };
00569
00570 struct HashValueProbe {
00571 template <typename H, typename T>
00572 static auto Invoke(H state, const T& value) -> absl::enable_if_t<
00573 std::is_same<H,
00574 decltype(AbslHashValue(std::move(state), value))>::value,
00575 H> {
00576 return AbslHashValue(std::move(state), value);
00577 }
00578 };
00579
00580 struct LegacyHashProbe {
00581 #if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
00582 template <typename H, typename T>
00583 static auto Invoke(H state, const T& value) -> absl::enable_if_t<
00584 std::is_convertible<
00585 decltype(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>()(value)),
00586 size_t>::value,
00587 H> {
00588 return hash_internal::hash_bytes(
00589 std::move(state),
00590 ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>{}(value));
00591 }
00592 #endif // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
00593 };
00594
00595 struct StdHashProbe {
00596 template <typename H, typename T>
00597 static auto Invoke(H state, const T& value)
00598 -> absl::enable_if_t<type_traits_internal::IsHashable<T>::value, H> {
00599 return hash_internal::hash_bytes(std::move(state), std::hash<T>{}(value));
00600 }
00601 };
00602
00603 template <typename Hash, typename T>
00604 struct Probe : Hash {
00605 private:
00606 template <typename H, typename = decltype(H::Invoke(
00607 std::declval<State>(), std::declval<const T&>()))>
00608 static std::true_type Test(int);
00609 template <typename U>
00610 static std::false_type Test(char);
00611
00612 public:
00613 static constexpr bool value = decltype(Test<Hash>(0))::value;
00614 };
00615
00616 public:
00617
00618
00619 template <typename T>
00620 using Apply = absl::disjunction<
00621 Probe<UniquelyRepresentedProbe, T>,
00622 Probe<HashValueProbe, T>,
00623 Probe<LegacyHashProbe, T>,
00624 Probe<StdHashProbe, T>,
00625 std::false_type>;
00626 };
00627
00628 template <typename T>
00629 struct is_hashable
00630 : std::integral_constant<bool, HashSelect::template Apply<T>::value> {};
00631
00632
00633 class CityHashState : public HashStateBase<CityHashState> {
00634
00635
00636 #ifdef ABSL_HAVE_INTRINSIC_INT128
00637 using uint128 = __uint128_t;
00638 #else // ABSL_HAVE_INTRINSIC_INT128
00639 using uint128 = absl::uint128;
00640 #endif // ABSL_HAVE_INTRINSIC_INT128
00641
00642 static constexpr uint64_t kMul =
00643 sizeof(size_t) == 4 ? uint64_t{0xcc9e2d51} : uint64_t{0x9ddfea08eb382d69};
00644
00645 template <typename T>
00646 using IntegralFastPath =
00647 conjunction<std::is_integral<T>, is_uniquely_represented<T>>;
00648
00649 public:
00650
00651 CityHashState(CityHashState&&) = default;
00652 CityHashState& operator=(CityHashState&&) = default;
00653
00654
00655
00656
00657
00658 static CityHashState combine_contiguous(CityHashState hash_state,
00659 const unsigned char* first,
00660 size_t size) {
00661 return CityHashState(
00662 CombineContiguousImpl(hash_state.state_, first, size,
00663 std::integral_constant<int, sizeof(size_t)>{}));
00664 }
00665 using CityHashState::HashStateBase::combine_contiguous;
00666
00667
00668
00669
00670
00671
00672
00673
00674 template <typename T, absl::enable_if_t<IntegralFastPath<T>::value, int> = 0>
00675 static size_t hash(T value) {
00676 return static_cast<size_t>(Mix(Seed(), static_cast<uint64_t>(value)));
00677 }
00678
00679
00680 template <typename T, absl::enable_if_t<!IntegralFastPath<T>::value, int> = 0>
00681 static size_t hash(const T& value) {
00682 return static_cast<size_t>(combine(CityHashState{}, value).state_);
00683 }
00684
00685 private:
00686
00687
00688 CityHashState() : state_(Seed()) {}
00689
00690
00691
00692
00693
00694 CityHashState(const CityHashState&) = default;
00695
00696 explicit CityHashState(uint64_t state) : state_(state) {}
00697
00698
00699
00700
00701
00702 static uint64_t CombineContiguousImpl(uint64_t state,
00703 const unsigned char* first, size_t len,
00704 std::integral_constant<int, 4>
00705 );
00706 static uint64_t CombineContiguousImpl(uint64_t state,
00707 const unsigned char* first, size_t len,
00708 std::integral_constant<int, 8>
00709 );
00710
00711
00712
00713
00714 static std::pair<uint64_t, uint64_t> Read9To16(const unsigned char* p,
00715 size_t len) {
00716 uint64_t high = little_endian::Load64(p + len - 8);
00717 return {little_endian::Load64(p), high >> (128 - len * 8)};
00718 }
00719
00720
00721 static uint64_t Read4To8(const unsigned char* p, size_t len) {
00722 return (static_cast<uint64_t>(little_endian::Load32(p + len - 4))
00723 << (len - 4) * 8) |
00724 little_endian::Load32(p);
00725 }
00726
00727
00728 static uint32_t Read1To3(const unsigned char* p, size_t len) {
00729 return static_cast<uint32_t>((p[0]) |
00730 (p[len / 2] << (len / 2 * 8)) |
00731 (p[len - 1] << ((len - 1) * 8)));
00732 }
00733
00734 ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Mix(uint64_t state, uint64_t v) {
00735 using MultType =
00736 absl::conditional_t<sizeof(size_t) == 4, uint64_t, uint128>;
00737
00738
00739
00740
00741 MultType m = state + v;
00742 m *= kMul;
00743 return static_cast<uint64_t>(m ^ (m >> (sizeof(m) * 8 / 2)));
00744 }
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762 ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Seed() {
00763 return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(kSeed));
00764 }
00765 static const void* const kSeed;
00766
00767 uint64_t state_;
00768 };
00769
00770
00771 inline uint64_t CityHashState::CombineContiguousImpl(
00772 uint64_t state, const unsigned char* first, size_t len,
00773 std::integral_constant<int, 4> ) {
00774
00775
00776 uint64_t v;
00777 if (len > 8) {
00778 v = absl::hash_internal::CityHash32(reinterpret_cast<const char*>(first), len);
00779 } else if (len >= 4) {
00780 v = Read4To8(first, len);
00781 } else if (len > 0) {
00782 v = Read1To3(first, len);
00783 } else {
00784
00785 return state;
00786 }
00787 return Mix(state, v);
00788 }
00789
00790
00791 inline uint64_t CityHashState::CombineContiguousImpl(
00792 uint64_t state, const unsigned char* first, size_t len,
00793 std::integral_constant<int, 8> ) {
00794
00795
00796 uint64_t v;
00797 if (len > 16) {
00798 v = absl::hash_internal::CityHash64(reinterpret_cast<const char*>(first), len);
00799 } else if (len > 8) {
00800 auto p = Read9To16(first, len);
00801 state = Mix(state, p.first);
00802 v = p.second;
00803 } else if (len >= 4) {
00804 v = Read4To8(first, len);
00805 } else if (len > 0) {
00806 v = Read1To3(first, len);
00807 } else {
00808
00809 return state;
00810 }
00811 return Mix(state, v);
00812 }
00813
00814
00815 struct AggregateBarrier {};
00816
00817
00818
00819
00820
00821
00822 struct PoisonedHash : private AggregateBarrier {
00823 PoisonedHash() = delete;
00824 PoisonedHash(const PoisonedHash&) = delete;
00825 PoisonedHash& operator=(const PoisonedHash&) = delete;
00826 };
00827
00828 template <typename T>
00829 struct HashImpl {
00830 size_t operator()(const T& value) const { return CityHashState::hash(value); }
00831 };
00832
00833 template <typename T>
00834 struct Hash
00835 : absl::conditional_t<is_hashable<T>::value, HashImpl<T>, PoisonedHash> {};
00836
00837 template <typename H>
00838 template <typename T, typename... Ts>
00839 H HashStateBase<H>::combine(H state, const T& value, const Ts&... values) {
00840 return H::combine(hash_internal::HashSelect::template Apply<T>::Invoke(
00841 std::move(state), value),
00842 values...);
00843 }
00844
00845
00846 template <typename H>
00847 template <typename T>
00848 H HashStateBase<H>::combine_contiguous(H state, const T* data, size_t size) {
00849 return hash_internal::hash_range_or_bytes(std::move(state), data, size);
00850 }
00851 }
00852 }
00853
00854 #endif // ABSL_HASH_INTERNAL_HASH_H_