hash.h 41 KB

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