hash.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: hash.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. #ifndef ABSL_HASH_INTERNAL_HASH_H_
  20. #define ABSL_HASH_INTERNAL_HASH_H_
  21. #include <algorithm>
  22. #include <array>
  23. #include <cmath>
  24. #include <cstring>
  25. #include <deque>
  26. #include <forward_list>
  27. #include <functional>
  28. #include <iterator>
  29. #include <limits>
  30. #include <list>
  31. #include <map>
  32. #include <memory>
  33. #include <set>
  34. #include <string>
  35. #include <tuple>
  36. #include <type_traits>
  37. #include <utility>
  38. #include <vector>
  39. #include "absl/base/internal/endian.h"
  40. #include "absl/base/port.h"
  41. #include "absl/container/fixed_array.h"
  42. #include "absl/meta/type_traits.h"
  43. #include "absl/numeric/int128.h"
  44. #include "absl/strings/string_view.h"
  45. #include "absl/types/optional.h"
  46. #include "absl/types/variant.h"
  47. #include "absl/utility/utility.h"
  48. #include "absl/hash/internal/city.h"
  49. namespace absl {
  50. ABSL_NAMESPACE_BEGIN
  51. namespace hash_internal {
  52. // Internal detail: Large buffers are hashed in smaller chunks. This function
  53. // returns the size of these chunks.
  54. constexpr size_t PiecewiseChunkSize() { return 1024; }
  55. // PiecewiseCombiner
  56. //
  57. // PiecewiseCombiner is an internal-only helper class for hashing a piecewise
  58. // buffer of `char` or `unsigned char` as though it were contiguous. This class
  59. // provides two methods:
  60. //
  61. // H add_buffer(state, data, size)
  62. // H finalize(state)
  63. //
  64. // `add_buffer` can be called zero or more times, followed by a single call to
  65. // `finalize`. This will produce the same hash expansion as concatenating each
  66. // buffer piece into a single contiguous buffer, and passing this to
  67. // `H::combine_contiguous`.
  68. //
  69. // Example usage:
  70. // PiecewiseCombiner combiner;
  71. // for (const auto& piece : pieces) {
  72. // state = combiner.add_buffer(std::move(state), piece.data, piece.size);
  73. // }
  74. // return combiner.finalize(std::move(state));
  75. class PiecewiseCombiner {
  76. public:
  77. PiecewiseCombiner() : position_(0) {}
  78. PiecewiseCombiner(const PiecewiseCombiner&) = delete;
  79. PiecewiseCombiner& operator=(const PiecewiseCombiner&) = delete;
  80. // PiecewiseCombiner::add_buffer()
  81. //
  82. // Appends the given range of bytes to the sequence to be hashed, which may
  83. // modify the provided hash state.
  84. template <typename H>
  85. H add_buffer(H state, const unsigned char* data, size_t size);
  86. template <typename H>
  87. H add_buffer(H state, const char* data, size_t size) {
  88. return add_buffer(std::move(state),
  89. reinterpret_cast<const unsigned char*>(data), size);
  90. }
  91. // PiecewiseCombiner::finalize()
  92. //
  93. // Finishes combining the hash sequence, which may may modify the provided
  94. // hash state.
  95. //
  96. // Once finalize() is called, add_buffer() may no longer be called. The
  97. // resulting hash state will be the same as if the pieces passed to
  98. // add_buffer() were concatenated into a single flat buffer, and then provided
  99. // to H::combine_contiguous().
  100. template <typename H>
  101. H finalize(H state);
  102. private:
  103. unsigned char buf_[PiecewiseChunkSize()];
  104. size_t position_;
  105. };
  106. // HashStateBase
  107. //
  108. // A hash state object represents an intermediate state in the computation
  109. // of an unspecified hash algorithm. `HashStateBase` provides a CRTP style
  110. // base class for hash state implementations. Developers adding type support
  111. // for `absl::Hash` should not rely on any parts of the state object other than
  112. // the following member functions:
  113. //
  114. // * HashStateBase::combine()
  115. // * HashStateBase::combine_contiguous()
  116. //
  117. // A derived hash state class of type `H` must provide a static member function
  118. // with a signature similar to the following:
  119. //
  120. // `static H combine_contiguous(H state, const unsigned char*, size_t)`.
  121. //
  122. // `HashStateBase` will provide a complete implementation for a hash state
  123. // object in terms of this method.
  124. //
  125. // Example:
  126. //
  127. // // Use CRTP to define your derived class.
  128. // struct MyHashState : HashStateBase<MyHashState> {
  129. // static H combine_contiguous(H state, const unsigned char*, size_t);
  130. // using MyHashState::HashStateBase::combine;
  131. // using MyHashState::HashStateBase::combine_contiguous;
  132. // };
  133. template <typename H>
  134. class HashStateBase {
  135. public:
  136. // HashStateBase::combine()
  137. //
  138. // Combines an arbitrary number of values into a hash state, returning the
  139. // updated state.
  140. //
  141. // Each of the value types `T` must be separately hashable by the Abseil
  142. // hashing framework.
  143. //
  144. // NOTE:
  145. //
  146. // state = H::combine(std::move(state), value1, value2, value3);
  147. //
  148. // is guaranteed to produce the same hash expansion as:
  149. //
  150. // state = H::combine(std::move(state), value1);
  151. // state = H::combine(std::move(state), value2);
  152. // state = H::combine(std::move(state), value3);
  153. template <typename T, typename... Ts>
  154. static H combine(H state, const T& value, const Ts&... values);
  155. static H combine(H state) { return state; }
  156. // HashStateBase::combine_contiguous()
  157. //
  158. // Combines a contiguous array of `size` elements into a hash state, returning
  159. // the updated state.
  160. //
  161. // NOTE:
  162. //
  163. // state = H::combine_contiguous(std::move(state), data, size);
  164. //
  165. // is NOT guaranteed to produce the same hash expansion as a for-loop (it may
  166. // perform internal optimizations). If you need this guarantee, use the
  167. // for-loop instead.
  168. template <typename T>
  169. static H combine_contiguous(H state, const T* data, size_t size);
  170. using AbslInternalPiecewiseCombiner = PiecewiseCombiner;
  171. };
  172. // is_uniquely_represented
  173. //
  174. // `is_uniquely_represented<T>` is a trait class that indicates whether `T`
  175. // is uniquely represented.
  176. //
  177. // A type is "uniquely represented" if two equal values of that type are
  178. // guaranteed to have the same bytes in their underlying storage. In other
  179. // words, if `a == b`, then `memcmp(&a, &b, sizeof(T))` is guaranteed to be
  180. // zero. This property cannot be detected automatically, so this trait is false
  181. // by default, but can be specialized by types that wish to assert that they are
  182. // uniquely represented. This makes them eligible for certain optimizations.
  183. //
  184. // If you have any doubt whatsoever, do not specialize this template.
  185. // The default is completely safe, and merely disables some optimizations
  186. // that will not matter for most types. Specializing this template,
  187. // on the other hand, can be very hazardous.
  188. //
  189. // To be uniquely represented, a type must not have multiple ways of
  190. // representing the same value; for example, float and double are not
  191. // uniquely represented, because they have distinct representations for
  192. // +0 and -0. Furthermore, the type's byte representation must consist
  193. // solely of user-controlled data, with no padding bits and no compiler-
  194. // controlled data such as vptrs or sanitizer metadata. This is usually
  195. // very difficult to guarantee, because in most cases the compiler can
  196. // insert data and padding bits at its own discretion.
  197. //
  198. // If you specialize this template for a type `T`, you must do so in the file
  199. // that defines that type (or in this file). If you define that specialization
  200. // anywhere else, `is_uniquely_represented<T>` could have different meanings
  201. // in different places.
  202. //
  203. // The Enable parameter is meaningless; it is provided as a convenience,
  204. // to support certain SFINAE techniques when defining specializations.
  205. template <typename T, typename Enable = void>
  206. struct is_uniquely_represented : std::false_type {};
  207. // is_uniquely_represented<unsigned char>
  208. //
  209. // unsigned char is a synonym for "byte", so it is guaranteed to be
  210. // uniquely represented.
  211. template <>
  212. struct is_uniquely_represented<unsigned char> : std::true_type {};
  213. // is_uniquely_represented for non-standard integral types
  214. //
  215. // Integral types other than bool should be uniquely represented on any
  216. // platform that this will plausibly be ported to.
  217. template <typename Integral>
  218. struct is_uniquely_represented<
  219. Integral, typename std::enable_if<std::is_integral<Integral>::value>::type>
  220. : std::true_type {};
  221. // is_uniquely_represented<bool>
  222. //
  223. //
  224. template <>
  225. struct is_uniquely_represented<bool> : std::false_type {};
  226. // hash_bytes()
  227. //
  228. // Convenience function that combines `hash_state` with the byte representation
  229. // of `value`.
  230. template <typename H, typename T>
  231. H hash_bytes(H hash_state, const T& value) {
  232. const unsigned char* start = reinterpret_cast<const unsigned char*>(&value);
  233. return H::combine_contiguous(std::move(hash_state), start, sizeof(value));
  234. }
  235. // -----------------------------------------------------------------------------
  236. // AbslHashValue for Basic Types
  237. // -----------------------------------------------------------------------------
  238. // Note: Default `AbslHashValue` implementations live in `hash_internal`. This
  239. // allows us to block lexical scope lookup when doing an unqualified call to
  240. // `AbslHashValue` below. User-defined implementations of `AbslHashValue` can
  241. // only be found via ADL.
  242. // AbslHashValue() for hashing bool values
  243. //
  244. // We use SFINAE to ensure that this overload only accepts bool, not types that
  245. // are convertible to bool.
  246. template <typename H, typename B>
  247. typename std::enable_if<std::is_same<B, bool>::value, H>::type AbslHashValue(
  248. H hash_state, B value) {
  249. return H::combine(std::move(hash_state),
  250. static_cast<unsigned char>(value ? 1 : 0));
  251. }
  252. // AbslHashValue() for hashing enum values
  253. template <typename H, typename Enum>
  254. typename std::enable_if<std::is_enum<Enum>::value, H>::type AbslHashValue(
  255. H hash_state, Enum e) {
  256. // In practice, we could almost certainly just invoke hash_bytes directly,
  257. // but it's possible that a sanitizer might one day want to
  258. // store data in the unused bits of an enum. To avoid that risk, we
  259. // convert to the underlying type before hashing. Hopefully this will get
  260. // optimized away; if not, we can reopen discussion with c-toolchain-team.
  261. return H::combine(std::move(hash_state),
  262. static_cast<typename std::underlying_type<Enum>::type>(e));
  263. }
  264. // AbslHashValue() for hashing floating-point values
  265. template <typename H, typename Float>
  266. typename std::enable_if<std::is_same<Float, float>::value ||
  267. std::is_same<Float, double>::value,
  268. H>::type
  269. AbslHashValue(H hash_state, Float value) {
  270. return hash_internal::hash_bytes(std::move(hash_state),
  271. value == 0 ? 0 : value);
  272. }
  273. // Long double has the property that it might have extra unused bytes in it.
  274. // For example, in x86 sizeof(long double)==16 but it only really uses 80-bits
  275. // of it. This means we can't use hash_bytes on a long double and have to
  276. // convert it to something else first.
  277. template <typename H, typename LongDouble>
  278. typename std::enable_if<std::is_same<LongDouble, long double>::value, H>::type
  279. AbslHashValue(H hash_state, LongDouble value) {
  280. const int category = std::fpclassify(value);
  281. switch (category) {
  282. case FP_INFINITE:
  283. // Add the sign bit to differentiate between +Inf and -Inf
  284. hash_state = H::combine(std::move(hash_state), std::signbit(value));
  285. break;
  286. case FP_NAN:
  287. case FP_ZERO:
  288. default:
  289. // Category is enough for these.
  290. break;
  291. case FP_NORMAL:
  292. case FP_SUBNORMAL:
  293. // We can't convert `value` directly to double because this would have
  294. // undefined behavior if the value is out of range.
  295. // std::frexp gives us a value in the range (-1, -.5] or [.5, 1) that is
  296. // guaranteed to be in range for `double`. The truncation is
  297. // implementation defined, but that works as long as it is deterministic.
  298. int exp;
  299. auto mantissa = static_cast<double>(std::frexp(value, &exp));
  300. hash_state = H::combine(std::move(hash_state), mantissa, exp);
  301. }
  302. return H::combine(std::move(hash_state), category);
  303. }
  304. // AbslHashValue() for hashing pointers
  305. template <typename H, typename T>
  306. H AbslHashValue(H hash_state, T* ptr) {
  307. auto v = reinterpret_cast<uintptr_t>(ptr);
  308. // Due to alignment, pointers tend to have low bits as zero, and the next few
  309. // bits follow a pattern since they are also multiples of some base value.
  310. // Mixing the pointer twice helps prevent stuck low bits for certain alignment
  311. // values.
  312. return H::combine(std::move(hash_state), v, v);
  313. }
  314. // AbslHashValue() for hashing nullptr_t
  315. template <typename H>
  316. H AbslHashValue(H hash_state, std::nullptr_t) {
  317. return H::combine(std::move(hash_state), static_cast<void*>(nullptr));
  318. }
  319. // -----------------------------------------------------------------------------
  320. // AbslHashValue for Composite Types
  321. // -----------------------------------------------------------------------------
  322. // is_hashable()
  323. //
  324. // Trait class which returns true if T is hashable by the absl::Hash framework.
  325. // Used for the AbslHashValue implementations for composite types below.
  326. template <typename T>
  327. struct is_hashable;
  328. // AbslHashValue() for hashing pairs
  329. template <typename H, typename T1, typename T2>
  330. typename std::enable_if<is_hashable<T1>::value && is_hashable<T2>::value,
  331. H>::type
  332. AbslHashValue(H hash_state, const std::pair<T1, T2>& p) {
  333. return H::combine(std::move(hash_state), p.first, p.second);
  334. }
  335. // hash_tuple()
  336. //
  337. // Helper function for hashing a tuple. The third argument should
  338. // be an index_sequence running from 0 to tuple_size<Tuple> - 1.
  339. template <typename H, typename Tuple, size_t... Is>
  340. H hash_tuple(H hash_state, const Tuple& t, absl::index_sequence<Is...>) {
  341. return H::combine(std::move(hash_state), std::get<Is>(t)...);
  342. }
  343. // AbslHashValue for hashing tuples
  344. template <typename H, typename... Ts>
  345. #if defined(_MSC_VER)
  346. // This SFINAE gets MSVC confused under some conditions. Let's just disable it
  347. // for now.
  348. H
  349. #else // _MSC_VER
  350. typename std::enable_if<absl::conjunction<is_hashable<Ts>...>::value, H>::type
  351. #endif // _MSC_VER
  352. AbslHashValue(H hash_state, const std::tuple<Ts...>& t) {
  353. return hash_internal::hash_tuple(std::move(hash_state), t,
  354. absl::make_index_sequence<sizeof...(Ts)>());
  355. }
  356. // -----------------------------------------------------------------------------
  357. // AbslHashValue for Pointers
  358. // -----------------------------------------------------------------------------
  359. // AbslHashValue for hashing unique_ptr
  360. template <typename H, typename T, typename D>
  361. H AbslHashValue(H hash_state, const std::unique_ptr<T, D>& ptr) {
  362. return H::combine(std::move(hash_state), ptr.get());
  363. }
  364. // AbslHashValue for hashing shared_ptr
  365. template <typename H, typename T>
  366. H AbslHashValue(H hash_state, const std::shared_ptr<T>& ptr) {
  367. return H::combine(std::move(hash_state), ptr.get());
  368. }
  369. // -----------------------------------------------------------------------------
  370. // AbslHashValue for String-Like Types
  371. // -----------------------------------------------------------------------------
  372. // AbslHashValue for hashing strings
  373. //
  374. // All the string-like types supported here provide the same hash expansion for
  375. // the same character sequence. These types are:
  376. //
  377. // - `absl::Cord`
  378. // - `std::string` (and std::basic_string<char, std::char_traits<char>, A> for
  379. // any allocator A)
  380. // - `absl::string_view` and `std::string_view`
  381. //
  382. // For simplicity, we currently support only `char` strings. This support may
  383. // be broadened, if necessary, but with some caution - this overload would
  384. // misbehave in cases where the traits' `eq()` member isn't equivalent to `==`
  385. // on the underlying character type.
  386. template <typename H>
  387. H AbslHashValue(H hash_state, absl::string_view str) {
  388. return H::combine(
  389. H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
  390. str.size());
  391. }
  392. // Support std::wstring, std::u16string and std::u32string.
  393. template <typename Char, typename Alloc, typename H,
  394. typename = absl::enable_if_t<std::is_same<Char, wchar_t>::value ||
  395. std::is_same<Char, char16_t>::value ||
  396. std::is_same<Char, char32_t>::value>>
  397. H AbslHashValue(
  398. H hash_state,
  399. const std::basic_string<Char, std::char_traits<Char>, Alloc>& str) {
  400. return H::combine(
  401. H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
  402. str.size());
  403. }
  404. // -----------------------------------------------------------------------------
  405. // AbslHashValue for Sequence Containers
  406. // -----------------------------------------------------------------------------
  407. // AbslHashValue for hashing std::array
  408. template <typename H, typename T, size_t N>
  409. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  410. H hash_state, const std::array<T, N>& array) {
  411. return H::combine_contiguous(std::move(hash_state), array.data(),
  412. array.size());
  413. }
  414. // AbslHashValue for hashing std::deque
  415. template <typename H, typename T, typename Allocator>
  416. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  417. H hash_state, const std::deque<T, Allocator>& deque) {
  418. // TODO(gromer): investigate a more efficient implementation taking
  419. // advantage of the chunk structure.
  420. for (const auto& t : deque) {
  421. hash_state = H::combine(std::move(hash_state), t);
  422. }
  423. return H::combine(std::move(hash_state), deque.size());
  424. }
  425. // AbslHashValue for hashing std::forward_list
  426. template <typename H, typename T, typename Allocator>
  427. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  428. H hash_state, const std::forward_list<T, Allocator>& list) {
  429. size_t size = 0;
  430. for (const T& t : list) {
  431. hash_state = H::combine(std::move(hash_state), t);
  432. ++size;
  433. }
  434. return H::combine(std::move(hash_state), size);
  435. }
  436. // AbslHashValue for hashing std::list
  437. template <typename H, typename T, typename Allocator>
  438. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  439. H hash_state, const std::list<T, Allocator>& list) {
  440. for (const auto& t : list) {
  441. hash_state = H::combine(std::move(hash_state), t);
  442. }
  443. return H::combine(std::move(hash_state), list.size());
  444. }
  445. // AbslHashValue for hashing std::vector
  446. //
  447. // Do not use this for vector<bool>. It does not have a .data(), and a fallback
  448. // for std::hash<> is most likely faster.
  449. template <typename H, typename T, typename Allocator>
  450. typename std::enable_if<is_hashable<T>::value && !std::is_same<T, bool>::value,
  451. H>::type
  452. AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
  453. return H::combine(H::combine_contiguous(std::move(hash_state), vector.data(),
  454. vector.size()),
  455. vector.size());
  456. }
  457. // -----------------------------------------------------------------------------
  458. // AbslHashValue for Ordered Associative Containers
  459. // -----------------------------------------------------------------------------
  460. // AbslHashValue for hashing std::map
  461. template <typename H, typename Key, typename T, typename Compare,
  462. typename Allocator>
  463. typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
  464. H>::type
  465. AbslHashValue(H hash_state, const std::map<Key, T, Compare, Allocator>& map) {
  466. for (const auto& t : map) {
  467. hash_state = H::combine(std::move(hash_state), t);
  468. }
  469. return H::combine(std::move(hash_state), map.size());
  470. }
  471. // AbslHashValue for hashing std::multimap
  472. template <typename H, typename Key, typename T, typename Compare,
  473. typename Allocator>
  474. typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
  475. H>::type
  476. AbslHashValue(H hash_state,
  477. const std::multimap<Key, T, Compare, Allocator>& map) {
  478. for (const auto& t : map) {
  479. hash_state = H::combine(std::move(hash_state), t);
  480. }
  481. return H::combine(std::move(hash_state), map.size());
  482. }
  483. // AbslHashValue for hashing std::set
  484. template <typename H, typename Key, typename Compare, typename Allocator>
  485. typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
  486. H hash_state, const std::set<Key, Compare, Allocator>& set) {
  487. for (const auto& t : set) {
  488. hash_state = H::combine(std::move(hash_state), t);
  489. }
  490. return H::combine(std::move(hash_state), set.size());
  491. }
  492. // AbslHashValue for hashing std::multiset
  493. template <typename H, typename Key, typename Compare, typename Allocator>
  494. typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
  495. H hash_state, const std::multiset<Key, Compare, Allocator>& set) {
  496. for (const auto& t : set) {
  497. hash_state = H::combine(std::move(hash_state), t);
  498. }
  499. return H::combine(std::move(hash_state), set.size());
  500. }
  501. // -----------------------------------------------------------------------------
  502. // AbslHashValue for Wrapper Types
  503. // -----------------------------------------------------------------------------
  504. // AbslHashValue for hashing std::reference_wrapper
  505. template <typename H, typename T>
  506. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  507. H hash_state, std::reference_wrapper<T> opt) {
  508. return H::combine(std::move(hash_state), opt.get());
  509. }
  510. // AbslHashValue for hashing absl::optional
  511. template <typename H, typename T>
  512. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  513. H hash_state, const absl::optional<T>& opt) {
  514. if (opt) hash_state = H::combine(std::move(hash_state), *opt);
  515. return H::combine(std::move(hash_state), opt.has_value());
  516. }
  517. // VariantVisitor
  518. template <typename H>
  519. struct VariantVisitor {
  520. H&& hash_state;
  521. template <typename T>
  522. H operator()(const T& t) const {
  523. return H::combine(std::move(hash_state), t);
  524. }
  525. };
  526. // AbslHashValue for hashing absl::variant
  527. template <typename H, typename... T>
  528. typename std::enable_if<conjunction<is_hashable<T>...>::value, H>::type
  529. AbslHashValue(H hash_state, const absl::variant<T...>& v) {
  530. if (!v.valueless_by_exception()) {
  531. hash_state = absl::visit(VariantVisitor<H>{std::move(hash_state)}, v);
  532. }
  533. return H::combine(std::move(hash_state), v.index());
  534. }
  535. // -----------------------------------------------------------------------------
  536. // AbslHashValue for Other Types
  537. // -----------------------------------------------------------------------------
  538. // AbslHashValue for hashing std::bitset is not defined, for the same reason as
  539. // for vector<bool> (see std::vector above): It does not expose the raw bytes,
  540. // and a fallback to std::hash<> is most likely faster.
  541. // -----------------------------------------------------------------------------
  542. // hash_range_or_bytes()
  543. //
  544. // Mixes all values in the range [data, data+size) into the hash state.
  545. // This overload accepts only uniquely-represented types, and hashes them by
  546. // hashing the entire range of bytes.
  547. template <typename H, typename T>
  548. typename std::enable_if<is_uniquely_represented<T>::value, H>::type
  549. hash_range_or_bytes(H hash_state, const T* data, size_t size) {
  550. const auto* bytes = reinterpret_cast<const unsigned char*>(data);
  551. return H::combine_contiguous(std::move(hash_state), bytes, sizeof(T) * size);
  552. }
  553. // hash_range_or_bytes()
  554. template <typename H, typename T>
  555. typename std::enable_if<!is_uniquely_represented<T>::value, H>::type
  556. hash_range_or_bytes(H hash_state, const T* data, size_t size) {
  557. for (const auto end = data + size; data < end; ++data) {
  558. hash_state = H::combine(std::move(hash_state), *data);
  559. }
  560. return hash_state;
  561. }
  562. #if defined(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE) && \
  563. ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
  564. #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 1
  565. #else
  566. #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 0
  567. #endif
  568. // HashSelect
  569. //
  570. // Type trait to select the appropriate hash implementation to use.
  571. // HashSelect::type<T> will give the proper hash implementation, to be invoked
  572. // as:
  573. // HashSelect::type<T>::Invoke(state, value)
  574. // Also, HashSelect::type<T>::value is a boolean equal to `true` if there is a
  575. // valid `Invoke` function. Types that are not hashable will have a ::value of
  576. // `false`.
  577. struct HashSelect {
  578. private:
  579. struct State : HashStateBase<State> {
  580. static State combine_contiguous(State hash_state, const unsigned char*,
  581. size_t);
  582. using State::HashStateBase::combine_contiguous;
  583. };
  584. struct UniquelyRepresentedProbe {
  585. template <typename H, typename T>
  586. static auto Invoke(H state, const T& value)
  587. -> absl::enable_if_t<is_uniquely_represented<T>::value, H> {
  588. return hash_internal::hash_bytes(std::move(state), value);
  589. }
  590. };
  591. struct HashValueProbe {
  592. template <typename H, typename T>
  593. static auto Invoke(H state, const T& value) -> absl::enable_if_t<
  594. std::is_same<H,
  595. decltype(AbslHashValue(std::move(state), value))>::value,
  596. H> {
  597. return AbslHashValue(std::move(state), value);
  598. }
  599. };
  600. struct LegacyHashProbe {
  601. #if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
  602. template <typename H, typename T>
  603. static auto Invoke(H state, const T& value) -> absl::enable_if_t<
  604. std::is_convertible<
  605. decltype(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>()(value)),
  606. size_t>::value,
  607. H> {
  608. return hash_internal::hash_bytes(
  609. std::move(state),
  610. ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>{}(value));
  611. }
  612. #endif // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
  613. };
  614. struct StdHashProbe {
  615. template <typename H, typename T>
  616. static auto Invoke(H state, const T& value)
  617. -> absl::enable_if_t<type_traits_internal::IsHashable<T>::value, H> {
  618. return hash_internal::hash_bytes(std::move(state), std::hash<T>{}(value));
  619. }
  620. };
  621. template <typename Hash, typename T>
  622. struct Probe : Hash {
  623. private:
  624. template <typename H, typename = decltype(H::Invoke(
  625. std::declval<State>(), std::declval<const T&>()))>
  626. static std::true_type Test(int);
  627. template <typename U>
  628. static std::false_type Test(char);
  629. public:
  630. static constexpr bool value = decltype(Test<Hash>(0))::value;
  631. };
  632. public:
  633. // Probe each implementation in order.
  634. // disjunction provides short circuiting wrt instantiation.
  635. template <typename T>
  636. using Apply = absl::disjunction< //
  637. Probe<UniquelyRepresentedProbe, T>, //
  638. Probe<HashValueProbe, T>, //
  639. Probe<LegacyHashProbe, T>, //
  640. Probe<StdHashProbe, T>, //
  641. std::false_type>;
  642. };
  643. template <typename T>
  644. struct is_hashable
  645. : std::integral_constant<bool, HashSelect::template Apply<T>::value> {};
  646. // CityHashState
  647. class ABSL_DLL CityHashState
  648. : public HashStateBase<CityHashState> {
  649. // absl::uint128 is not an alias or a thin wrapper around the intrinsic.
  650. // We use the intrinsic when available to improve performance.
  651. #ifdef ABSL_HAVE_INTRINSIC_INT128
  652. using uint128 = __uint128_t;
  653. #else // ABSL_HAVE_INTRINSIC_INT128
  654. using uint128 = absl::uint128;
  655. #endif // ABSL_HAVE_INTRINSIC_INT128
  656. static constexpr uint64_t kMul =
  657. sizeof(size_t) == 4 ? uint64_t{0xcc9e2d51}
  658. : uint64_t{0x9ddfea08eb382d69};
  659. template <typename T>
  660. using IntegralFastPath =
  661. conjunction<std::is_integral<T>, is_uniquely_represented<T>>;
  662. public:
  663. // Move only
  664. CityHashState(CityHashState&&) = default;
  665. CityHashState& operator=(CityHashState&&) = default;
  666. // CityHashState::combine_contiguous()
  667. //
  668. // Fundamental base case for hash recursion: mixes the given range of bytes
  669. // into the hash state.
  670. static CityHashState combine_contiguous(CityHashState hash_state,
  671. const unsigned char* first,
  672. size_t size) {
  673. return CityHashState(
  674. CombineContiguousImpl(hash_state.state_, first, size,
  675. std::integral_constant<int, sizeof(size_t)>{}));
  676. }
  677. using CityHashState::HashStateBase::combine_contiguous;
  678. // CityHashState::hash()
  679. //
  680. // For performance reasons in non-opt mode, we specialize this for
  681. // integral types.
  682. // Otherwise we would be instantiating and calling dozens of functions for
  683. // something that is just one multiplication and a couple xor's.
  684. // The result should be the same as running the whole algorithm, but faster.
  685. template <typename T, absl::enable_if_t<IntegralFastPath<T>::value, int> = 0>
  686. static size_t hash(T value) {
  687. return static_cast<size_t>(Mix(Seed(), static_cast<uint64_t>(value)));
  688. }
  689. // Overload of CityHashState::hash()
  690. template <typename T, absl::enable_if_t<!IntegralFastPath<T>::value, int> = 0>
  691. static size_t hash(const T& value) {
  692. return static_cast<size_t>(combine(CityHashState{}, value).state_);
  693. }
  694. private:
  695. // Invoked only once for a given argument; that plus the fact that this is
  696. // move-only ensures that there is only one non-moved-from object.
  697. CityHashState() : state_(Seed()) {}
  698. // Workaround for MSVC bug.
  699. // We make the type copyable to fix the calling convention, even though we
  700. // never actually copy it. Keep it private to not affect the public API of the
  701. // type.
  702. CityHashState(const CityHashState&) = default;
  703. explicit CityHashState(uint64_t state) : state_(state) {}
  704. // Implementation of the base case for combine_contiguous where we actually
  705. // mix the bytes into the state.
  706. // Dispatch to different implementations of the combine_contiguous depending
  707. // on the value of `sizeof(size_t)`.
  708. static uint64_t CombineContiguousImpl(uint64_t state,
  709. const unsigned char* first, size_t len,
  710. std::integral_constant<int, 4>
  711. /* sizeof_size_t */);
  712. static uint64_t CombineContiguousImpl(uint64_t state,
  713. const unsigned char* first, size_t len,
  714. std::integral_constant<int, 8>
  715. /* sizeof_size_t*/);
  716. // Slow dispatch path for calls to CombineContiguousImpl with a size argument
  717. // larger than PiecewiseChunkSize(). Has the same effect as calling
  718. // CombineContiguousImpl() repeatedly with the chunk stride size.
  719. static uint64_t CombineLargeContiguousImpl32(uint64_t state,
  720. const unsigned char* first,
  721. size_t len);
  722. static uint64_t CombineLargeContiguousImpl64(uint64_t state,
  723. const unsigned char* first,
  724. size_t len);
  725. // Reads 9 to 16 bytes from p.
  726. // The first 8 bytes are in .first, the rest (zero padded) bytes are in
  727. // .second.
  728. static std::pair<uint64_t, uint64_t> Read9To16(const unsigned char* p,
  729. size_t len) {
  730. uint64_t high = little_endian::Load64(p + len - 8);
  731. return {little_endian::Load64(p), high >> (128 - len * 8)};
  732. }
  733. // Reads 4 to 8 bytes from p. Zero pads to fill uint64_t.
  734. static uint64_t Read4To8(const unsigned char* p, size_t len) {
  735. return (static_cast<uint64_t>(little_endian::Load32(p + len - 4))
  736. << (len - 4) * 8) |
  737. little_endian::Load32(p);
  738. }
  739. // Reads 1 to 3 bytes from p. Zero pads to fill uint32_t.
  740. static uint32_t Read1To3(const unsigned char* p, size_t len) {
  741. return static_cast<uint32_t>((p[0]) | //
  742. (p[len / 2] << (len / 2 * 8)) | //
  743. (p[len - 1] << ((len - 1) * 8)));
  744. }
  745. ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Mix(uint64_t state, uint64_t v) {
  746. using MultType =
  747. absl::conditional_t<sizeof(size_t) == 4, uint64_t, uint128>;
  748. // We do the addition in 64-bit space to make sure the 128-bit
  749. // multiplication is fast. If we were to do it as MultType the compiler has
  750. // to assume that the high word is non-zero and needs to perform 2
  751. // multiplications instead of one.
  752. MultType m = state + v;
  753. m *= kMul;
  754. return static_cast<uint64_t>(m ^ (m >> (sizeof(m) * 8 / 2)));
  755. }
  756. // Seed()
  757. //
  758. // A non-deterministic seed.
  759. //
  760. // The current purpose of this seed is to generate non-deterministic results
  761. // and prevent having users depend on the particular hash values.
  762. // It is not meant as a security feature right now, but it leaves the door
  763. // open to upgrade it to a true per-process random seed. A true random seed
  764. // costs more and we don't need to pay for that right now.
  765. //
  766. // On platforms with ASLR, we take advantage of it to make a per-process
  767. // random value.
  768. // See https://en.wikipedia.org/wiki/Address_space_layout_randomization
  769. //
  770. // On other platforms this is still going to be non-deterministic but most
  771. // probably per-build and not per-process.
  772. ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Seed() {
  773. return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(kSeed));
  774. }
  775. static const void* const kSeed;
  776. uint64_t state_;
  777. };
  778. // CityHashState::CombineContiguousImpl()
  779. inline uint64_t CityHashState::CombineContiguousImpl(
  780. uint64_t state, const unsigned char* first, size_t len,
  781. std::integral_constant<int, 4> /* sizeof_size_t */) {
  782. // For large values we use CityHash, for small ones we just use a
  783. // multiplicative hash.
  784. uint64_t v;
  785. if (len > 8) {
  786. if (ABSL_PREDICT_FALSE(len > PiecewiseChunkSize())) {
  787. return CombineLargeContiguousImpl32(state, first, len);
  788. }
  789. v = absl::hash_internal::CityHash32(reinterpret_cast<const char*>(first), len);
  790. } else if (len >= 4) {
  791. v = Read4To8(first, len);
  792. } else if (len > 0) {
  793. v = Read1To3(first, len);
  794. } else {
  795. // Empty ranges have no effect.
  796. return state;
  797. }
  798. return Mix(state, v);
  799. }
  800. // Overload of CityHashState::CombineContiguousImpl()
  801. inline uint64_t CityHashState::CombineContiguousImpl(
  802. uint64_t state, const unsigned char* first, size_t len,
  803. std::integral_constant<int, 8> /* sizeof_size_t */) {
  804. // For large values we use CityHash, for small ones we just use a
  805. // multiplicative hash.
  806. uint64_t v;
  807. if (len > 16) {
  808. if (ABSL_PREDICT_FALSE(len > PiecewiseChunkSize())) {
  809. return CombineLargeContiguousImpl64(state, first, len);
  810. }
  811. v = absl::hash_internal::CityHash64(reinterpret_cast<const char*>(first), len);
  812. } else if (len > 8) {
  813. auto p = Read9To16(first, len);
  814. state = Mix(state, p.first);
  815. v = p.second;
  816. } else if (len >= 4) {
  817. v = Read4To8(first, len);
  818. } else if (len > 0) {
  819. v = Read1To3(first, len);
  820. } else {
  821. // Empty ranges have no effect.
  822. return state;
  823. }
  824. return Mix(state, v);
  825. }
  826. struct AggregateBarrier {};
  827. // HashImpl
  828. // Add a private base class to make sure this type is not an aggregate.
  829. // Aggregates can be aggregate initialized even if the default constructor is
  830. // deleted.
  831. struct PoisonedHash : private AggregateBarrier {
  832. PoisonedHash() = delete;
  833. PoisonedHash(const PoisonedHash&) = delete;
  834. PoisonedHash& operator=(const PoisonedHash&) = delete;
  835. };
  836. template <typename T>
  837. struct HashImpl {
  838. size_t operator()(const T& value) const { return CityHashState::hash(value); }
  839. };
  840. template <typename T>
  841. struct Hash
  842. : absl::conditional_t<is_hashable<T>::value, HashImpl<T>, PoisonedHash> {};
  843. template <typename H>
  844. template <typename T, typename... Ts>
  845. H HashStateBase<H>::combine(H state, const T& value, const Ts&... values) {
  846. return H::combine(hash_internal::HashSelect::template Apply<T>::Invoke(
  847. std::move(state), value),
  848. values...);
  849. }
  850. // HashStateBase::combine_contiguous()
  851. template <typename H>
  852. template <typename T>
  853. H HashStateBase<H>::combine_contiguous(H state, const T* data, size_t size) {
  854. return hash_internal::hash_range_or_bytes(std::move(state), data, size);
  855. }
  856. // HashStateBase::PiecewiseCombiner::add_buffer()
  857. template <typename H>
  858. H PiecewiseCombiner::add_buffer(H state, const unsigned char* data,
  859. size_t size) {
  860. if (position_ + size < PiecewiseChunkSize()) {
  861. // This partial chunk does not fill our existing buffer
  862. memcpy(buf_ + position_, data, size);
  863. position_ += size;
  864. return state;
  865. }
  866. // If the buffer is partially filled we need to complete the buffer
  867. // and hash it.
  868. if (position_ != 0) {
  869. const size_t bytes_needed = PiecewiseChunkSize() - position_;
  870. memcpy(buf_ + position_, data, bytes_needed);
  871. state = H::combine_contiguous(std::move(state), buf_, PiecewiseChunkSize());
  872. data += bytes_needed;
  873. size -= bytes_needed;
  874. }
  875. // Hash whatever chunks we can without copying
  876. while (size >= PiecewiseChunkSize()) {
  877. state = H::combine_contiguous(std::move(state), data, PiecewiseChunkSize());
  878. data += PiecewiseChunkSize();
  879. size -= PiecewiseChunkSize();
  880. }
  881. // Fill the buffer with the remainder
  882. memcpy(buf_, data, size);
  883. position_ = size;
  884. return state;
  885. }
  886. // HashStateBase::PiecewiseCombiner::finalize()
  887. template <typename H>
  888. H PiecewiseCombiner::finalize(H state) {
  889. // Hash the remainder left in the buffer, which may be empty
  890. return H::combine_contiguous(std::move(state), buf_, position_);
  891. }
  892. } // namespace hash_internal
  893. ABSL_NAMESPACE_END
  894. } // namespace absl
  895. #endif // ABSL_HASH_INTERNAL_HASH_H_