hash.h 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  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. #ifdef __APPLE__
  22. #include <Availability.h>
  23. #include <TargetConditionals.h>
  24. #endif
  25. #include "absl/base/config.h"
  26. // For feature testing and determining which headers can be included.
  27. #if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L
  28. #include <version>
  29. #else
  30. #include <ciso646>
  31. #endif
  32. #include <algorithm>
  33. #include <array>
  34. #include <bitset>
  35. #include <cmath>
  36. #include <cstddef>
  37. #include <cstring>
  38. #include <deque>
  39. #include <forward_list>
  40. #include <functional>
  41. #include <iterator>
  42. #include <limits>
  43. #include <list>
  44. #include <map>
  45. #include <memory>
  46. #include <set>
  47. #include <string>
  48. #include <tuple>
  49. #include <type_traits>
  50. #include <unordered_map>
  51. #include <unordered_set>
  52. #include <utility>
  53. #include <vector>
  54. #include "absl/base/internal/unaligned_access.h"
  55. #include "absl/base/port.h"
  56. #include "absl/container/fixed_array.h"
  57. #include "absl/hash/internal/city.h"
  58. #include "absl/hash/internal/low_level_hash.h"
  59. #include "absl/meta/type_traits.h"
  60. #include "absl/numeric/bits.h"
  61. #include "absl/numeric/int128.h"
  62. #include "absl/strings/string_view.h"
  63. #include "absl/types/optional.h"
  64. #include "absl/types/variant.h"
  65. #include "absl/utility/utility.h"
  66. #if defined(__cpp_lib_filesystem) && __cpp_lib_filesystem >= 201703L && \
  67. !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
  68. #include <filesystem> // NOLINT
  69. #endif
  70. #ifdef ABSL_HAVE_STD_STRING_VIEW
  71. #include <string_view>
  72. #endif
  73. namespace absl {
  74. ABSL_NAMESPACE_BEGIN
  75. class HashState;
  76. namespace hash_internal {
  77. // Internal detail: Large buffers are hashed in smaller chunks. This function
  78. // returns the size of these chunks.
  79. constexpr size_t PiecewiseChunkSize() { return 1024; }
  80. // PiecewiseCombiner
  81. //
  82. // PiecewiseCombiner is an internal-only helper class for hashing a piecewise
  83. // buffer of `char` or `unsigned char` as though it were contiguous. This class
  84. // provides two methods:
  85. //
  86. // H add_buffer(state, data, size)
  87. // H finalize(state)
  88. //
  89. // `add_buffer` can be called zero or more times, followed by a single call to
  90. // `finalize`. This will produce the same hash expansion as concatenating each
  91. // buffer piece into a single contiguous buffer, and passing this to
  92. // `H::combine_contiguous`.
  93. //
  94. // Example usage:
  95. // PiecewiseCombiner combiner;
  96. // for (const auto& piece : pieces) {
  97. // state = combiner.add_buffer(std::move(state), piece.data, piece.size);
  98. // }
  99. // return combiner.finalize(std::move(state));
  100. class PiecewiseCombiner {
  101. public:
  102. PiecewiseCombiner() : position_(0) {}
  103. PiecewiseCombiner(const PiecewiseCombiner&) = delete;
  104. PiecewiseCombiner& operator=(const PiecewiseCombiner&) = delete;
  105. // PiecewiseCombiner::add_buffer()
  106. //
  107. // Appends the given range of bytes to the sequence to be hashed, which may
  108. // modify the provided hash state.
  109. template <typename H>
  110. H add_buffer(H state, const unsigned char* data, size_t size);
  111. template <typename H>
  112. H add_buffer(H state, const char* data, size_t size) {
  113. return add_buffer(std::move(state),
  114. reinterpret_cast<const unsigned char*>(data), size);
  115. }
  116. // PiecewiseCombiner::finalize()
  117. //
  118. // Finishes combining the hash sequence, which may may modify the provided
  119. // hash state.
  120. //
  121. // Once finalize() is called, add_buffer() may no longer be called. The
  122. // resulting hash state will be the same as if the pieces passed to
  123. // add_buffer() were concatenated into a single flat buffer, and then provided
  124. // to H::combine_contiguous().
  125. template <typename H>
  126. H finalize(H state);
  127. private:
  128. unsigned char buf_[PiecewiseChunkSize()];
  129. size_t position_;
  130. };
  131. // is_hashable()
  132. //
  133. // Trait class which returns true if T is hashable by the absl::Hash framework.
  134. // Used for the AbslHashValue implementations for composite types below.
  135. template <typename T>
  136. struct is_hashable;
  137. // HashStateBase
  138. //
  139. // An internal implementation detail that contains common implementation details
  140. // for all of the "hash state objects" objects generated by Abseil. This is not
  141. // a public API; users should not create classes that inherit from this.
  142. //
  143. // A hash state object is the template argument `H` passed to `AbslHashValue`.
  144. // It represents an intermediate state in the computation of an unspecified hash
  145. // algorithm. `HashStateBase` provides a CRTP style base class for hash state
  146. // implementations. Developers adding type support for `absl::Hash` should not
  147. // rely on any parts of the state object other than the following member
  148. // functions:
  149. //
  150. // * HashStateBase::combine()
  151. // * HashStateBase::combine_contiguous()
  152. // * HashStateBase::combine_unordered()
  153. //
  154. // A derived hash state class of type `H` must provide a public member function
  155. // with a signature similar to the following:
  156. //
  157. // `static H combine_contiguous(H state, const unsigned char*, size_t)`.
  158. //
  159. // It must also provide a private template method named RunCombineUnordered.
  160. //
  161. // A "consumer" is a 1-arg functor returning void. Its argument is a reference
  162. // to an inner hash state object, and it may be called multiple times. When
  163. // called, the functor consumes the entropy from the provided state object,
  164. // and resets that object to its empty state.
  165. //
  166. // A "combiner" is a stateless 2-arg functor returning void. Its arguments are
  167. // an inner hash state object and an ElementStateConsumer functor. A combiner
  168. // uses the provided inner hash state object to hash each element of the
  169. // container, passing the inner hash state object to the consumer after hashing
  170. // each element.
  171. //
  172. // Given these definitions, a derived hash state class of type H
  173. // must provide a private template method with a signature similar to the
  174. // following:
  175. //
  176. // `template <typename CombinerT>`
  177. // `static H RunCombineUnordered(H outer_state, CombinerT combiner)`
  178. //
  179. // This function is responsible for constructing the inner state object and
  180. // providing a consumer to the combiner. It uses side effects of the consumer
  181. // and combiner to mix the state of each element in an order-independent manner,
  182. // and uses this to return an updated value of `outer_state`.
  183. //
  184. // This inside-out approach generates efficient object code in the normal case,
  185. // but allows us to use stack storage to implement the absl::HashState type
  186. // erasure mechanism (avoiding heap allocations while hashing).
  187. //
  188. // `HashStateBase` will provide a complete implementation for a hash state
  189. // object in terms of these two methods.
  190. //
  191. // Example:
  192. //
  193. // // Use CRTP to define your derived class.
  194. // struct MyHashState : HashStateBase<MyHashState> {
  195. // static H combine_contiguous(H state, const unsigned char*, size_t);
  196. // using MyHashState::HashStateBase::combine;
  197. // using MyHashState::HashStateBase::combine_contiguous;
  198. // using MyHashState::HashStateBase::combine_unordered;
  199. // private:
  200. // template <typename CombinerT>
  201. // static H RunCombineUnordered(H state, CombinerT combiner);
  202. // };
  203. template <typename H>
  204. class HashStateBase {
  205. public:
  206. // HashStateBase::combine()
  207. //
  208. // Combines an arbitrary number of values into a hash state, returning the
  209. // updated state.
  210. //
  211. // Each of the value types `T` must be separately hashable by the Abseil
  212. // hashing framework.
  213. //
  214. // NOTE:
  215. //
  216. // state = H::combine(std::move(state), value1, value2, value3);
  217. //
  218. // is guaranteed to produce the same hash expansion as:
  219. //
  220. // state = H::combine(std::move(state), value1);
  221. // state = H::combine(std::move(state), value2);
  222. // state = H::combine(std::move(state), value3);
  223. template <typename T, typename... Ts>
  224. static H combine(H state, const T& value, const Ts&... values);
  225. static H combine(H state) { return state; }
  226. // HashStateBase::combine_contiguous()
  227. //
  228. // Combines a contiguous array of `size` elements into a hash state, returning
  229. // the updated state.
  230. //
  231. // NOTE:
  232. //
  233. // state = H::combine_contiguous(std::move(state), data, size);
  234. //
  235. // is NOT guaranteed to produce the same hash expansion as a for-loop (it may
  236. // perform internal optimizations). If you need this guarantee, use the
  237. // for-loop instead.
  238. template <typename T>
  239. static H combine_contiguous(H state, const T* data, size_t size);
  240. template <typename I>
  241. static H combine_unordered(H state, I begin, I end);
  242. using AbslInternalPiecewiseCombiner = PiecewiseCombiner;
  243. template <typename T>
  244. using is_hashable = absl::hash_internal::is_hashable<T>;
  245. private:
  246. // Common implementation of the iteration step of a "combiner", as described
  247. // above.
  248. template <typename I>
  249. struct CombineUnorderedCallback {
  250. I begin;
  251. I end;
  252. template <typename InnerH, typename ElementStateConsumer>
  253. void operator()(InnerH inner_state, ElementStateConsumer cb) {
  254. for (; begin != end; ++begin) {
  255. inner_state = H::combine(std::move(inner_state), *begin);
  256. cb(inner_state);
  257. }
  258. }
  259. };
  260. };
  261. // is_uniquely_represented
  262. //
  263. // `is_uniquely_represented<T>` is a trait class that indicates whether `T`
  264. // is uniquely represented.
  265. //
  266. // A type is "uniquely represented" if two equal values of that type are
  267. // guaranteed to have the same bytes in their underlying storage. In other
  268. // words, if `a == b`, then `memcmp(&a, &b, sizeof(T))` is guaranteed to be
  269. // zero. This property cannot be detected automatically, so this trait is false
  270. // by default, but can be specialized by types that wish to assert that they are
  271. // uniquely represented. This makes them eligible for certain optimizations.
  272. //
  273. // If you have any doubt whatsoever, do not specialize this template.
  274. // The default is completely safe, and merely disables some optimizations
  275. // that will not matter for most types. Specializing this template,
  276. // on the other hand, can be very hazardous.
  277. //
  278. // To be uniquely represented, a type must not have multiple ways of
  279. // representing the same value; for example, float and double are not
  280. // uniquely represented, because they have distinct representations for
  281. // +0 and -0. Furthermore, the type's byte representation must consist
  282. // solely of user-controlled data, with no padding bits and no compiler-
  283. // controlled data such as vptrs or sanitizer metadata. This is usually
  284. // very difficult to guarantee, because in most cases the compiler can
  285. // insert data and padding bits at its own discretion.
  286. //
  287. // If you specialize this template for a type `T`, you must do so in the file
  288. // that defines that type (or in this file). If you define that specialization
  289. // anywhere else, `is_uniquely_represented<T>` could have different meanings
  290. // in different places.
  291. //
  292. // The Enable parameter is meaningless; it is provided as a convenience,
  293. // to support certain SFINAE techniques when defining specializations.
  294. template <typename T, typename Enable = void>
  295. struct is_uniquely_represented : std::false_type {};
  296. // is_uniquely_represented<unsigned char>
  297. //
  298. // unsigned char is a synonym for "byte", so it is guaranteed to be
  299. // uniquely represented.
  300. template <>
  301. struct is_uniquely_represented<unsigned char> : std::true_type {};
  302. // is_uniquely_represented for non-standard integral types
  303. //
  304. // Integral types other than bool should be uniquely represented on any
  305. // platform that this will plausibly be ported to.
  306. template <typename Integral>
  307. struct is_uniquely_represented<
  308. Integral, typename std::enable_if<std::is_integral<Integral>::value>::type>
  309. : std::true_type {};
  310. // is_uniquely_represented<bool>
  311. //
  312. //
  313. template <>
  314. struct is_uniquely_represented<bool> : std::false_type {};
  315. // hash_bytes()
  316. //
  317. // Convenience function that combines `hash_state` with the byte representation
  318. // of `value`.
  319. template <typename H, typename T>
  320. H hash_bytes(H hash_state, const T& value) {
  321. const unsigned char* start = reinterpret_cast<const unsigned char*>(&value);
  322. return H::combine_contiguous(std::move(hash_state), start, sizeof(value));
  323. }
  324. // -----------------------------------------------------------------------------
  325. // AbslHashValue for Basic Types
  326. // -----------------------------------------------------------------------------
  327. // Note: Default `AbslHashValue` implementations live in `hash_internal`. This
  328. // allows us to block lexical scope lookup when doing an unqualified call to
  329. // `AbslHashValue` below. User-defined implementations of `AbslHashValue` can
  330. // only be found via ADL.
  331. // AbslHashValue() for hashing bool values
  332. //
  333. // We use SFINAE to ensure that this overload only accepts bool, not types that
  334. // are convertible to bool.
  335. template <typename H, typename B>
  336. typename std::enable_if<std::is_same<B, bool>::value, H>::type AbslHashValue(
  337. H hash_state, B value) {
  338. return H::combine(std::move(hash_state),
  339. static_cast<unsigned char>(value ? 1 : 0));
  340. }
  341. // AbslHashValue() for hashing enum values
  342. template <typename H, typename Enum>
  343. typename std::enable_if<std::is_enum<Enum>::value, H>::type AbslHashValue(
  344. H hash_state, Enum e) {
  345. // In practice, we could almost certainly just invoke hash_bytes directly,
  346. // but it's possible that a sanitizer might one day want to
  347. // store data in the unused bits of an enum. To avoid that risk, we
  348. // convert to the underlying type before hashing. Hopefully this will get
  349. // optimized away; if not, we can reopen discussion with c-toolchain-team.
  350. return H::combine(std::move(hash_state),
  351. static_cast<typename std::underlying_type<Enum>::type>(e));
  352. }
  353. // AbslHashValue() for hashing floating-point values
  354. template <typename H, typename Float>
  355. typename std::enable_if<std::is_same<Float, float>::value ||
  356. std::is_same<Float, double>::value,
  357. H>::type
  358. AbslHashValue(H hash_state, Float value) {
  359. return hash_internal::hash_bytes(std::move(hash_state),
  360. value == 0 ? 0 : value);
  361. }
  362. // Long double has the property that it might have extra unused bytes in it.
  363. // For example, in x86 sizeof(long double)==16 but it only really uses 80-bits
  364. // of it. This means we can't use hash_bytes on a long double and have to
  365. // convert it to something else first.
  366. template <typename H, typename LongDouble>
  367. typename std::enable_if<std::is_same<LongDouble, long double>::value, H>::type
  368. AbslHashValue(H hash_state, LongDouble value) {
  369. const int category = std::fpclassify(value);
  370. switch (category) {
  371. case FP_INFINITE:
  372. // Add the sign bit to differentiate between +Inf and -Inf
  373. hash_state = H::combine(std::move(hash_state), std::signbit(value));
  374. break;
  375. case FP_NAN:
  376. case FP_ZERO:
  377. default:
  378. // Category is enough for these.
  379. break;
  380. case FP_NORMAL:
  381. case FP_SUBNORMAL:
  382. // We can't convert `value` directly to double because this would have
  383. // undefined behavior if the value is out of range.
  384. // std::frexp gives us a value in the range (-1, -.5] or [.5, 1) that is
  385. // guaranteed to be in range for `double`. The truncation is
  386. // implementation defined, but that works as long as it is deterministic.
  387. int exp;
  388. auto mantissa = static_cast<double>(std::frexp(value, &exp));
  389. hash_state = H::combine(std::move(hash_state), mantissa, exp);
  390. }
  391. return H::combine(std::move(hash_state), category);
  392. }
  393. // Without this overload, an array decays to a pointer and we hash that, which
  394. // is not likely to be what the caller intended.
  395. template <typename H, typename T, size_t N>
  396. H AbslHashValue(H hash_state, T (&)[N]) {
  397. static_assert(
  398. sizeof(T) == -1,
  399. "Hashing C arrays is not allowed. For string literals, wrap the literal "
  400. "in absl::string_view(). To hash the array contents, use "
  401. "absl::MakeSpan() or make the array an std::array. To hash the array "
  402. "address, use &array[0].");
  403. return hash_state;
  404. }
  405. // AbslHashValue() for hashing pointers
  406. template <typename H, typename T>
  407. std::enable_if_t<std::is_pointer<T>::value, H> AbslHashValue(H hash_state,
  408. T ptr) {
  409. auto v = reinterpret_cast<uintptr_t>(ptr);
  410. // Due to alignment, pointers tend to have low bits as zero, and the next few
  411. // bits follow a pattern since they are also multiples of some base value.
  412. // Mixing the pointer twice helps prevent stuck low bits for certain alignment
  413. // values.
  414. return H::combine(std::move(hash_state), v, v);
  415. }
  416. // AbslHashValue() for hashing nullptr_t
  417. template <typename H>
  418. H AbslHashValue(H hash_state, std::nullptr_t) {
  419. return H::combine(std::move(hash_state), static_cast<void*>(nullptr));
  420. }
  421. // AbslHashValue() for hashing pointers-to-member
  422. template <typename H, typename T, typename C>
  423. H AbslHashValue(H hash_state, T C::*ptr) {
  424. auto salient_ptm_size = [](std::size_t n) -> std::size_t {
  425. #if defined(_MSC_VER)
  426. // Pointers-to-member-function on MSVC consist of one pointer plus 0, 1, 2,
  427. // or 3 ints. In 64-bit mode, they are 8-byte aligned and thus can contain
  428. // padding (namely when they have 1 or 3 ints). The value below is a lower
  429. // bound on the number of salient, non-padding bytes that we use for
  430. // hashing.
  431. if (alignof(T C::*) == alignof(int)) {
  432. // No padding when all subobjects have the same size as the total
  433. // alignment. This happens in 32-bit mode.
  434. return n;
  435. } else {
  436. // Padding for 1 int (size 16) or 3 ints (size 24).
  437. // With 2 ints, the size is 16 with no padding, which we pessimize.
  438. return n == 24 ? 20 : n == 16 ? 12 : n;
  439. }
  440. #else
  441. // On other platforms, we assume that pointers-to-members do not have
  442. // padding.
  443. #ifdef __cpp_lib_has_unique_object_representations
  444. static_assert(std::has_unique_object_representations<T C::*>::value);
  445. #endif // __cpp_lib_has_unique_object_representations
  446. return n;
  447. #endif
  448. };
  449. return H::combine_contiguous(std::move(hash_state),
  450. reinterpret_cast<unsigned char*>(&ptr),
  451. salient_ptm_size(sizeof ptr));
  452. }
  453. // -----------------------------------------------------------------------------
  454. // AbslHashValue for Composite Types
  455. // -----------------------------------------------------------------------------
  456. // AbslHashValue() for hashing pairs
  457. template <typename H, typename T1, typename T2>
  458. typename std::enable_if<is_hashable<T1>::value && is_hashable<T2>::value,
  459. H>::type
  460. AbslHashValue(H hash_state, const std::pair<T1, T2>& p) {
  461. return H::combine(std::move(hash_state), p.first, p.second);
  462. }
  463. // hash_tuple()
  464. //
  465. // Helper function for hashing a tuple. The third argument should
  466. // be an index_sequence running from 0 to tuple_size<Tuple> - 1.
  467. template <typename H, typename Tuple, size_t... Is>
  468. H hash_tuple(H hash_state, const Tuple& t, absl::index_sequence<Is...>) {
  469. return H::combine(std::move(hash_state), std::get<Is>(t)...);
  470. }
  471. // AbslHashValue for hashing tuples
  472. template <typename H, typename... Ts>
  473. #if defined(_MSC_VER)
  474. // This SFINAE gets MSVC confused under some conditions. Let's just disable it
  475. // for now.
  476. H
  477. #else // _MSC_VER
  478. typename std::enable_if<absl::conjunction<is_hashable<Ts>...>::value, H>::type
  479. #endif // _MSC_VER
  480. AbslHashValue(H hash_state, const std::tuple<Ts...>& t) {
  481. return hash_internal::hash_tuple(std::move(hash_state), t,
  482. absl::make_index_sequence<sizeof...(Ts)>());
  483. }
  484. // -----------------------------------------------------------------------------
  485. // AbslHashValue for Pointers
  486. // -----------------------------------------------------------------------------
  487. // AbslHashValue for hashing unique_ptr
  488. template <typename H, typename T, typename D>
  489. H AbslHashValue(H hash_state, const std::unique_ptr<T, D>& ptr) {
  490. return H::combine(std::move(hash_state), ptr.get());
  491. }
  492. // AbslHashValue for hashing shared_ptr
  493. template <typename H, typename T>
  494. H AbslHashValue(H hash_state, const std::shared_ptr<T>& ptr) {
  495. return H::combine(std::move(hash_state), ptr.get());
  496. }
  497. // -----------------------------------------------------------------------------
  498. // AbslHashValue for String-Like Types
  499. // -----------------------------------------------------------------------------
  500. // AbslHashValue for hashing strings
  501. //
  502. // All the string-like types supported here provide the same hash expansion for
  503. // the same character sequence. These types are:
  504. //
  505. // - `absl::Cord`
  506. // - `std::string` (and std::basic_string<T, std::char_traits<T>, A> for
  507. // any allocator A and any T in {char, wchar_t, char16_t, char32_t})
  508. // - `absl::string_view`, `std::string_view`, `std::wstring_view`,
  509. // `std::u16string_view`, and `std::u32_string_view`.
  510. //
  511. // For simplicity, we currently support only strings built on `char`, `wchar_t`,
  512. // `char16_t`, or `char32_t`. This support may be broadened, if necessary, but
  513. // with some caution - this overload would misbehave in cases where the traits'
  514. // `eq()` member isn't equivalent to `==` on the underlying character type.
  515. template <typename H>
  516. H AbslHashValue(H hash_state, absl::string_view str) {
  517. return H::combine(
  518. H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
  519. str.size());
  520. }
  521. // Support std::wstring, std::u16string and std::u32string.
  522. template <typename Char, typename Alloc, typename H,
  523. typename = absl::enable_if_t<std::is_same<Char, wchar_t>::value ||
  524. std::is_same<Char, char16_t>::value ||
  525. std::is_same<Char, char32_t>::value>>
  526. H AbslHashValue(
  527. H hash_state,
  528. const std::basic_string<Char, std::char_traits<Char>, Alloc>& str) {
  529. return H::combine(
  530. H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
  531. str.size());
  532. }
  533. #ifdef ABSL_HAVE_STD_STRING_VIEW
  534. // Support std::wstring_view, std::u16string_view and std::u32string_view.
  535. template <typename Char, typename H,
  536. typename = absl::enable_if_t<std::is_same<Char, wchar_t>::value ||
  537. std::is_same<Char, char16_t>::value ||
  538. std::is_same<Char, char32_t>::value>>
  539. H AbslHashValue(H hash_state, std::basic_string_view<Char> str) {
  540. return H::combine(
  541. H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
  542. str.size());
  543. }
  544. #endif // ABSL_HAVE_STD_STRING_VIEW
  545. #if defined(__cpp_lib_filesystem) && __cpp_lib_filesystem >= 201703L && \
  546. !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY) && \
  547. (!defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__) || \
  548. __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 130000) && \
  549. (!defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || \
  550. __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101500)
  551. #define ABSL_INTERNAL_STD_FILESYSTEM_PATH_HASH_AVAILABLE 1
  552. // Support std::filesystem::path. The SFINAE is required because some string
  553. // types are implicitly convertible to std::filesystem::path.
  554. template <typename Path, typename H,
  555. typename = absl::enable_if_t<
  556. std::is_same_v<Path, std::filesystem::path>>>
  557. H AbslHashValue(H hash_state, const Path& path) {
  558. // This is implemented by deferring to the standard library to compute the
  559. // hash. The standard library requires that for two paths, `p1 == p2`, then
  560. // `hash_value(p1) == hash_value(p2)`. `AbslHashValue` has the same
  561. // requirement. Since `operator==` does platform specific matching, deferring
  562. // to the standard library is the simplest approach.
  563. return H::combine(std::move(hash_state), std::filesystem::hash_value(path));
  564. }
  565. #endif // ABSL_INTERNAL_STD_FILESYSTEM_PATH_HASH_AVAILABLE
  566. // -----------------------------------------------------------------------------
  567. // AbslHashValue for Sequence Containers
  568. // -----------------------------------------------------------------------------
  569. // AbslHashValue for hashing std::array
  570. template <typename H, typename T, size_t N>
  571. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  572. H hash_state, const std::array<T, N>& array) {
  573. return H::combine_contiguous(std::move(hash_state), array.data(),
  574. array.size());
  575. }
  576. // AbslHashValue for hashing std::deque
  577. template <typename H, typename T, typename Allocator>
  578. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  579. H hash_state, const std::deque<T, Allocator>& deque) {
  580. // TODO(gromer): investigate a more efficient implementation taking
  581. // advantage of the chunk structure.
  582. for (const auto& t : deque) {
  583. hash_state = H::combine(std::move(hash_state), t);
  584. }
  585. return H::combine(std::move(hash_state), deque.size());
  586. }
  587. // AbslHashValue for hashing std::forward_list
  588. template <typename H, typename T, typename Allocator>
  589. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  590. H hash_state, const std::forward_list<T, Allocator>& list) {
  591. size_t size = 0;
  592. for (const T& t : list) {
  593. hash_state = H::combine(std::move(hash_state), t);
  594. ++size;
  595. }
  596. return H::combine(std::move(hash_state), size);
  597. }
  598. // AbslHashValue for hashing std::list
  599. template <typename H, typename T, typename Allocator>
  600. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  601. H hash_state, const std::list<T, Allocator>& list) {
  602. for (const auto& t : list) {
  603. hash_state = H::combine(std::move(hash_state), t);
  604. }
  605. return H::combine(std::move(hash_state), list.size());
  606. }
  607. // AbslHashValue for hashing std::vector
  608. //
  609. // Do not use this for vector<bool> on platforms that have a working
  610. // implementation of std::hash. It does not have a .data(), and a fallback for
  611. // std::hash<> is most likely faster.
  612. template <typename H, typename T, typename Allocator>
  613. typename std::enable_if<is_hashable<T>::value && !std::is_same<T, bool>::value,
  614. H>::type
  615. AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
  616. return H::combine(H::combine_contiguous(std::move(hash_state), vector.data(),
  617. vector.size()),
  618. vector.size());
  619. }
  620. // AbslHashValue special cases for hashing std::vector<bool>
  621. #if defined(ABSL_IS_BIG_ENDIAN) && \
  622. (defined(__GLIBCXX__) || defined(__GLIBCPP__))
  623. // std::hash in libstdc++ does not work correctly with vector<bool> on Big
  624. // Endian platforms therefore we need to implement a custom AbslHashValue for
  625. // it. More details on the bug:
  626. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102531
  627. template <typename H, typename T, typename Allocator>
  628. typename std::enable_if<is_hashable<T>::value && std::is_same<T, bool>::value,
  629. H>::type
  630. AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
  631. typename H::AbslInternalPiecewiseCombiner combiner;
  632. for (const auto& i : vector) {
  633. unsigned char c = static_cast<unsigned char>(i);
  634. hash_state = combiner.add_buffer(std::move(hash_state), &c, sizeof(c));
  635. }
  636. return H::combine(combiner.finalize(std::move(hash_state)), vector.size());
  637. }
  638. #else
  639. // When not working around the libstdc++ bug above, we still have to contend
  640. // with the fact that std::hash<vector<bool>> is often poor quality, hashing
  641. // directly on the internal words and on no other state. On these platforms,
  642. // vector<bool>{1, 1} and vector<bool>{1, 1, 0} hash to the same value.
  643. //
  644. // Mixing in the size (as we do in our other vector<> implementations) on top
  645. // of the library-provided hash implementation avoids this QOI issue.
  646. template <typename H, typename T, typename Allocator>
  647. typename std::enable_if<is_hashable<T>::value && std::is_same<T, bool>::value,
  648. H>::type
  649. AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
  650. return H::combine(std::move(hash_state),
  651. std::hash<std::vector<T, Allocator>>{}(vector),
  652. vector.size());
  653. }
  654. #endif
  655. // -----------------------------------------------------------------------------
  656. // AbslHashValue for Ordered Associative Containers
  657. // -----------------------------------------------------------------------------
  658. // AbslHashValue for hashing std::map
  659. template <typename H, typename Key, typename T, typename Compare,
  660. typename Allocator>
  661. typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
  662. H>::type
  663. AbslHashValue(H hash_state, const std::map<Key, T, Compare, Allocator>& map) {
  664. for (const auto& t : map) {
  665. hash_state = H::combine(std::move(hash_state), t);
  666. }
  667. return H::combine(std::move(hash_state), map.size());
  668. }
  669. // AbslHashValue for hashing std::multimap
  670. template <typename H, typename Key, typename T, typename Compare,
  671. typename Allocator>
  672. typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
  673. H>::type
  674. AbslHashValue(H hash_state,
  675. const std::multimap<Key, T, Compare, Allocator>& map) {
  676. for (const auto& t : map) {
  677. hash_state = H::combine(std::move(hash_state), t);
  678. }
  679. return H::combine(std::move(hash_state), map.size());
  680. }
  681. // AbslHashValue for hashing std::set
  682. template <typename H, typename Key, typename Compare, typename Allocator>
  683. typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
  684. H hash_state, const std::set<Key, Compare, Allocator>& set) {
  685. for (const auto& t : set) {
  686. hash_state = H::combine(std::move(hash_state), t);
  687. }
  688. return H::combine(std::move(hash_state), set.size());
  689. }
  690. // AbslHashValue for hashing std::multiset
  691. template <typename H, typename Key, typename Compare, typename Allocator>
  692. typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
  693. H hash_state, const std::multiset<Key, Compare, Allocator>& set) {
  694. for (const auto& t : set) {
  695. hash_state = H::combine(std::move(hash_state), t);
  696. }
  697. return H::combine(std::move(hash_state), set.size());
  698. }
  699. // -----------------------------------------------------------------------------
  700. // AbslHashValue for Unordered Associative Containers
  701. // -----------------------------------------------------------------------------
  702. // AbslHashValue for hashing std::unordered_set
  703. template <typename H, typename Key, typename Hash, typename KeyEqual,
  704. typename Alloc>
  705. typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
  706. H hash_state, const std::unordered_set<Key, Hash, KeyEqual, Alloc>& s) {
  707. return H::combine(
  708. H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
  709. s.size());
  710. }
  711. // AbslHashValue for hashing std::unordered_multiset
  712. template <typename H, typename Key, typename Hash, typename KeyEqual,
  713. typename Alloc>
  714. typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
  715. H hash_state,
  716. const std::unordered_multiset<Key, Hash, KeyEqual, Alloc>& s) {
  717. return H::combine(
  718. H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
  719. s.size());
  720. }
  721. // AbslHashValue for hashing std::unordered_set
  722. template <typename H, typename Key, typename T, typename Hash,
  723. typename KeyEqual, typename Alloc>
  724. typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
  725. H>::type
  726. AbslHashValue(H hash_state,
  727. const std::unordered_map<Key, T, Hash, KeyEqual, Alloc>& s) {
  728. return H::combine(
  729. H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
  730. s.size());
  731. }
  732. // AbslHashValue for hashing std::unordered_multiset
  733. template <typename H, typename Key, typename T, typename Hash,
  734. typename KeyEqual, typename Alloc>
  735. typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
  736. H>::type
  737. AbslHashValue(H hash_state,
  738. const std::unordered_multimap<Key, T, Hash, KeyEqual, Alloc>& s) {
  739. return H::combine(
  740. H::combine_unordered(std::move(hash_state), s.begin(), s.end()),
  741. s.size());
  742. }
  743. // -----------------------------------------------------------------------------
  744. // AbslHashValue for Wrapper Types
  745. // -----------------------------------------------------------------------------
  746. // AbslHashValue for hashing std::reference_wrapper
  747. template <typename H, typename T>
  748. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  749. H hash_state, std::reference_wrapper<T> opt) {
  750. return H::combine(std::move(hash_state), opt.get());
  751. }
  752. // AbslHashValue for hashing absl::optional
  753. template <typename H, typename T>
  754. typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
  755. H hash_state, const absl::optional<T>& opt) {
  756. if (opt) hash_state = H::combine(std::move(hash_state), *opt);
  757. return H::combine(std::move(hash_state), opt.has_value());
  758. }
  759. // VariantVisitor
  760. template <typename H>
  761. struct VariantVisitor {
  762. H&& hash_state;
  763. template <typename T>
  764. H operator()(const T& t) const {
  765. return H::combine(std::move(hash_state), t);
  766. }
  767. };
  768. // AbslHashValue for hashing absl::variant
  769. template <typename H, typename... T>
  770. typename std::enable_if<conjunction<is_hashable<T>...>::value, H>::type
  771. AbslHashValue(H hash_state, const absl::variant<T...>& v) {
  772. if (!v.valueless_by_exception()) {
  773. hash_state = absl::visit(VariantVisitor<H>{std::move(hash_state)}, v);
  774. }
  775. return H::combine(std::move(hash_state), v.index());
  776. }
  777. // -----------------------------------------------------------------------------
  778. // AbslHashValue for Other Types
  779. // -----------------------------------------------------------------------------
  780. // AbslHashValue for hashing std::bitset is not defined on Little Endian
  781. // platforms, for the same reason as for vector<bool> (see std::vector above):
  782. // It does not expose the raw bytes, and a fallback to std::hash<> is most
  783. // likely faster.
  784. #if defined(ABSL_IS_BIG_ENDIAN) && \
  785. (defined(__GLIBCXX__) || defined(__GLIBCPP__))
  786. // AbslHashValue for hashing std::bitset
  787. //
  788. // std::hash in libstdc++ does not work correctly with std::bitset on Big Endian
  789. // platforms therefore we need to implement a custom AbslHashValue for it. More
  790. // details on the bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102531
  791. template <typename H, size_t N>
  792. H AbslHashValue(H hash_state, const std::bitset<N>& set) {
  793. typename H::AbslInternalPiecewiseCombiner combiner;
  794. for (size_t i = 0; i < N; i++) {
  795. unsigned char c = static_cast<unsigned char>(set[i]);
  796. hash_state = combiner.add_buffer(std::move(hash_state), &c, sizeof(c));
  797. }
  798. return H::combine(combiner.finalize(std::move(hash_state)), N);
  799. }
  800. #endif
  801. // -----------------------------------------------------------------------------
  802. // hash_range_or_bytes()
  803. //
  804. // Mixes all values in the range [data, data+size) into the hash state.
  805. // This overload accepts only uniquely-represented types, and hashes them by
  806. // hashing the entire range of bytes.
  807. template <typename H, typename T>
  808. typename std::enable_if<is_uniquely_represented<T>::value, H>::type
  809. hash_range_or_bytes(H hash_state, const T* data, size_t size) {
  810. const auto* bytes = reinterpret_cast<const unsigned char*>(data);
  811. return H::combine_contiguous(std::move(hash_state), bytes, sizeof(T) * size);
  812. }
  813. // hash_range_or_bytes()
  814. template <typename H, typename T>
  815. typename std::enable_if<!is_uniquely_represented<T>::value, H>::type
  816. hash_range_or_bytes(H hash_state, const T* data, size_t size) {
  817. for (const auto end = data + size; data < end; ++data) {
  818. hash_state = H::combine(std::move(hash_state), *data);
  819. }
  820. return hash_state;
  821. }
  822. #if defined(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE) && \
  823. ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
  824. #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 1
  825. #else
  826. #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 0
  827. #endif
  828. // HashSelect
  829. //
  830. // Type trait to select the appropriate hash implementation to use.
  831. // HashSelect::type<T> will give the proper hash implementation, to be invoked
  832. // as:
  833. // HashSelect::type<T>::Invoke(state, value)
  834. // Also, HashSelect::type<T>::value is a boolean equal to `true` if there is a
  835. // valid `Invoke` function. Types that are not hashable will have a ::value of
  836. // `false`.
  837. struct HashSelect {
  838. private:
  839. struct State : HashStateBase<State> {
  840. static State combine_contiguous(State hash_state, const unsigned char*,
  841. size_t);
  842. using State::HashStateBase::combine_contiguous;
  843. };
  844. struct UniquelyRepresentedProbe {
  845. template <typename H, typename T>
  846. static auto Invoke(H state, const T& value)
  847. -> absl::enable_if_t<is_uniquely_represented<T>::value, H> {
  848. return hash_internal::hash_bytes(std::move(state), value);
  849. }
  850. };
  851. struct HashValueProbe {
  852. template <typename H, typename T>
  853. static auto Invoke(H state, const T& value) -> absl::enable_if_t<
  854. std::is_same<H,
  855. decltype(AbslHashValue(std::move(state), value))>::value,
  856. H> {
  857. return AbslHashValue(std::move(state), value);
  858. }
  859. };
  860. struct LegacyHashProbe {
  861. #if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
  862. template <typename H, typename T>
  863. static auto Invoke(H state, const T& value) -> absl::enable_if_t<
  864. std::is_convertible<
  865. decltype(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>()(value)),
  866. size_t>::value,
  867. H> {
  868. return hash_internal::hash_bytes(
  869. std::move(state),
  870. ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>{}(value));
  871. }
  872. #endif // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
  873. };
  874. struct StdHashProbe {
  875. template <typename H, typename T>
  876. static auto Invoke(H state, const T& value)
  877. -> absl::enable_if_t<type_traits_internal::IsHashable<T>::value, H> {
  878. return hash_internal::hash_bytes(std::move(state), std::hash<T>{}(value));
  879. }
  880. };
  881. template <typename Hash, typename T>
  882. struct Probe : Hash {
  883. private:
  884. template <typename H, typename = decltype(H::Invoke(
  885. std::declval<State>(), std::declval<const T&>()))>
  886. static std::true_type Test(int);
  887. template <typename U>
  888. static std::false_type Test(char);
  889. public:
  890. static constexpr bool value = decltype(Test<Hash>(0))::value;
  891. };
  892. public:
  893. // Probe each implementation in order.
  894. // disjunction provides short circuiting wrt instantiation.
  895. template <typename T>
  896. using Apply = absl::disjunction< //
  897. Probe<UniquelyRepresentedProbe, T>, //
  898. Probe<HashValueProbe, T>, //
  899. Probe<LegacyHashProbe, T>, //
  900. Probe<StdHashProbe, T>, //
  901. std::false_type>;
  902. };
  903. template <typename T>
  904. struct is_hashable
  905. : std::integral_constant<bool, HashSelect::template Apply<T>::value> {};
  906. // MixingHashState
  907. class ABSL_DLL MixingHashState : public HashStateBase<MixingHashState> {
  908. // absl::uint128 is not an alias or a thin wrapper around the intrinsic.
  909. // We use the intrinsic when available to improve performance.
  910. #ifdef ABSL_HAVE_INTRINSIC_INT128
  911. using uint128 = __uint128_t;
  912. #else // ABSL_HAVE_INTRINSIC_INT128
  913. using uint128 = absl::uint128;
  914. #endif // ABSL_HAVE_INTRINSIC_INT128
  915. static constexpr uint64_t kMul =
  916. sizeof(size_t) == 4 ? uint64_t{0xcc9e2d51}
  917. : uint64_t{0x9ddfea08eb382d69};
  918. template <typename T>
  919. using IntegralFastPath =
  920. conjunction<std::is_integral<T>, is_uniquely_represented<T>>;
  921. public:
  922. // Move only
  923. MixingHashState(MixingHashState&&) = default;
  924. MixingHashState& operator=(MixingHashState&&) = default;
  925. // MixingHashState::combine_contiguous()
  926. //
  927. // Fundamental base case for hash recursion: mixes the given range of bytes
  928. // into the hash state.
  929. static MixingHashState combine_contiguous(MixingHashState hash_state,
  930. const unsigned char* first,
  931. size_t size) {
  932. return MixingHashState(
  933. CombineContiguousImpl(hash_state.state_, first, size,
  934. std::integral_constant<int, sizeof(size_t)>{}));
  935. }
  936. using MixingHashState::HashStateBase::combine_contiguous;
  937. // MixingHashState::hash()
  938. //
  939. // For performance reasons in non-opt mode, we specialize this for
  940. // integral types.
  941. // Otherwise we would be instantiating and calling dozens of functions for
  942. // something that is just one multiplication and a couple xor's.
  943. // The result should be the same as running the whole algorithm, but faster.
  944. template <typename T, absl::enable_if_t<IntegralFastPath<T>::value, int> = 0>
  945. static size_t hash(T value) {
  946. return static_cast<size_t>(
  947. Mix(Seed(), static_cast<std::make_unsigned_t<T>>(value)));
  948. }
  949. // Overload of MixingHashState::hash()
  950. template <typename T, absl::enable_if_t<!IntegralFastPath<T>::value, int> = 0>
  951. static size_t hash(const T& value) {
  952. return static_cast<size_t>(combine(MixingHashState{}, value).state_);
  953. }
  954. private:
  955. // Invoked only once for a given argument; that plus the fact that this is
  956. // move-only ensures that there is only one non-moved-from object.
  957. MixingHashState() : state_(Seed()) {}
  958. friend class MixingHashState::HashStateBase;
  959. template <typename CombinerT>
  960. static MixingHashState RunCombineUnordered(MixingHashState state,
  961. CombinerT combiner) {
  962. uint64_t unordered_state = 0;
  963. combiner(MixingHashState{}, [&](MixingHashState& inner_state) {
  964. // Add the hash state of the element to the running total, but mix the
  965. // carry bit back into the low bit. This in intended to avoid losing
  966. // entropy to overflow, especially when unordered_multisets contain
  967. // multiple copies of the same value.
  968. auto element_state = inner_state.state_;
  969. unordered_state += element_state;
  970. if (unordered_state < element_state) {
  971. ++unordered_state;
  972. }
  973. inner_state = MixingHashState{};
  974. });
  975. return MixingHashState::combine(std::move(state), unordered_state);
  976. }
  977. // Allow the HashState type-erasure implementation to invoke
  978. // RunCombinedUnordered() directly.
  979. friend class absl::HashState;
  980. // Workaround for MSVC bug.
  981. // We make the type copyable to fix the calling convention, even though we
  982. // never actually copy it. Keep it private to not affect the public API of the
  983. // type.
  984. MixingHashState(const MixingHashState&) = default;
  985. explicit MixingHashState(uint64_t state) : state_(state) {}
  986. // Implementation of the base case for combine_contiguous where we actually
  987. // mix the bytes into the state.
  988. // Dispatch to different implementations of the combine_contiguous depending
  989. // on the value of `sizeof(size_t)`.
  990. static uint64_t CombineContiguousImpl(uint64_t state,
  991. const unsigned char* first, size_t len,
  992. std::integral_constant<int, 4>
  993. /* sizeof_size_t */);
  994. static uint64_t CombineContiguousImpl(uint64_t state,
  995. const unsigned char* first, size_t len,
  996. std::integral_constant<int, 8>
  997. /* sizeof_size_t */);
  998. // Slow dispatch path for calls to CombineContiguousImpl with a size argument
  999. // larger than PiecewiseChunkSize(). Has the same effect as calling
  1000. // CombineContiguousImpl() repeatedly with the chunk stride size.
  1001. static uint64_t CombineLargeContiguousImpl32(uint64_t state,
  1002. const unsigned char* first,
  1003. size_t len);
  1004. static uint64_t CombineLargeContiguousImpl64(uint64_t state,
  1005. const unsigned char* first,
  1006. size_t len);
  1007. // Reads 9 to 16 bytes from p.
  1008. // The least significant 8 bytes are in .first, the rest (zero padded) bytes
  1009. // are in .second.
  1010. static std::pair<uint64_t, uint64_t> Read9To16(const unsigned char* p,
  1011. size_t len) {
  1012. uint64_t low_mem = absl::base_internal::UnalignedLoad64(p);
  1013. uint64_t high_mem = absl::base_internal::UnalignedLoad64(p + len - 8);
  1014. #ifdef ABSL_IS_LITTLE_ENDIAN
  1015. uint64_t most_significant = high_mem;
  1016. uint64_t least_significant = low_mem;
  1017. #else
  1018. uint64_t most_significant = low_mem;
  1019. uint64_t least_significant = high_mem;
  1020. #endif
  1021. return {least_significant, most_significant};
  1022. }
  1023. // Reads 4 to 8 bytes from p. Zero pads to fill uint64_t.
  1024. static uint64_t Read4To8(const unsigned char* p, size_t len) {
  1025. uint32_t low_mem = absl::base_internal::UnalignedLoad32(p);
  1026. uint32_t high_mem = absl::base_internal::UnalignedLoad32(p + len - 4);
  1027. #ifdef ABSL_IS_LITTLE_ENDIAN
  1028. uint32_t most_significant = high_mem;
  1029. uint32_t least_significant = low_mem;
  1030. #else
  1031. uint32_t most_significant = low_mem;
  1032. uint32_t least_significant = high_mem;
  1033. #endif
  1034. return (static_cast<uint64_t>(most_significant) << (len - 4) * 8) |
  1035. least_significant;
  1036. }
  1037. // Reads 1 to 3 bytes from p. Zero pads to fill uint32_t.
  1038. static uint32_t Read1To3(const unsigned char* p, size_t len) {
  1039. // The trick used by this implementation is to avoid branches if possible.
  1040. unsigned char mem0 = p[0];
  1041. unsigned char mem1 = p[len / 2];
  1042. unsigned char mem2 = p[len - 1];
  1043. #ifdef ABSL_IS_LITTLE_ENDIAN
  1044. unsigned char significant2 = mem2;
  1045. unsigned char significant1 = mem1;
  1046. unsigned char significant0 = mem0;
  1047. #else
  1048. unsigned char significant2 = mem0;
  1049. unsigned char significant1 = len == 2 ? mem0 : mem1;
  1050. unsigned char significant0 = mem2;
  1051. #endif
  1052. return static_cast<uint32_t>(significant0 | //
  1053. (significant1 << (len / 2 * 8)) | //
  1054. (significant2 << ((len - 1) * 8)));
  1055. }
  1056. ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Mix(uint64_t state, uint64_t v) {
  1057. // Though the 128-bit product on AArch64 needs two instructions, it is
  1058. // still a good balance between speed and hash quality.
  1059. using MultType =
  1060. absl::conditional_t<sizeof(size_t) == 4, uint64_t, uint128>;
  1061. // We do the addition in 64-bit space to make sure the 128-bit
  1062. // multiplication is fast. If we were to do it as MultType the compiler has
  1063. // to assume that the high word is non-zero and needs to perform 2
  1064. // multiplications instead of one.
  1065. MultType m = state + v;
  1066. m *= kMul;
  1067. return static_cast<uint64_t>(m ^ (m >> (sizeof(m) * 8 / 2)));
  1068. }
  1069. // An extern to avoid bloat on a direct call to LowLevelHash() with fixed
  1070. // values for both the seed and salt parameters.
  1071. static uint64_t LowLevelHashImpl(const unsigned char* data, size_t len);
  1072. ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Hash64(const unsigned char* data,
  1073. size_t len) {
  1074. #ifdef ABSL_HAVE_INTRINSIC_INT128
  1075. return LowLevelHashImpl(data, len);
  1076. #else
  1077. return hash_internal::CityHash64(reinterpret_cast<const char*>(data), len);
  1078. #endif
  1079. }
  1080. // Seed()
  1081. //
  1082. // A non-deterministic seed.
  1083. //
  1084. // The current purpose of this seed is to generate non-deterministic results
  1085. // and prevent having users depend on the particular hash values.
  1086. // It is not meant as a security feature right now, but it leaves the door
  1087. // open to upgrade it to a true per-process random seed. A true random seed
  1088. // costs more and we don't need to pay for that right now.
  1089. //
  1090. // On platforms with ASLR, we take advantage of it to make a per-process
  1091. // random value.
  1092. // See https://en.wikipedia.org/wiki/Address_space_layout_randomization
  1093. //
  1094. // On other platforms this is still going to be non-deterministic but most
  1095. // probably per-build and not per-process.
  1096. ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Seed() {
  1097. #if (!defined(__clang__) || __clang_major__ > 11) && \
  1098. (!defined(__apple_build_version__) || \
  1099. __apple_build_version__ >= 19558921) // Xcode 12
  1100. return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(&kSeed));
  1101. #else
  1102. // Workaround the absence of
  1103. // https://github.com/llvm/llvm-project/commit/bc15bf66dcca76cc06fe71fca35b74dc4d521021.
  1104. return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(kSeed));
  1105. #endif
  1106. }
  1107. static const void* const kSeed;
  1108. uint64_t state_;
  1109. };
  1110. // MixingHashState::CombineContiguousImpl()
  1111. inline uint64_t MixingHashState::CombineContiguousImpl(
  1112. uint64_t state, const unsigned char* first, size_t len,
  1113. std::integral_constant<int, 4> /* sizeof_size_t */) {
  1114. // For large values we use CityHash, for small ones we just use a
  1115. // multiplicative hash.
  1116. uint64_t v;
  1117. if (len > 8) {
  1118. if (ABSL_PREDICT_FALSE(len > PiecewiseChunkSize())) {
  1119. return CombineLargeContiguousImpl32(state, first, len);
  1120. }
  1121. v = hash_internal::CityHash32(reinterpret_cast<const char*>(first), len);
  1122. } else if (len >= 4) {
  1123. v = Read4To8(first, len);
  1124. } else if (len > 0) {
  1125. v = Read1To3(first, len);
  1126. } else {
  1127. // Empty ranges have no effect.
  1128. return state;
  1129. }
  1130. return Mix(state, v);
  1131. }
  1132. // Overload of MixingHashState::CombineContiguousImpl()
  1133. inline uint64_t MixingHashState::CombineContiguousImpl(
  1134. uint64_t state, const unsigned char* first, size_t len,
  1135. std::integral_constant<int, 8> /* sizeof_size_t */) {
  1136. // For large values we use LowLevelHash or CityHash depending on the platform,
  1137. // for small ones we just use a multiplicative hash.
  1138. uint64_t v;
  1139. if (len > 16) {
  1140. if (ABSL_PREDICT_FALSE(len > PiecewiseChunkSize())) {
  1141. return CombineLargeContiguousImpl64(state, first, len);
  1142. }
  1143. v = Hash64(first, len);
  1144. } else if (len > 8) {
  1145. // This hash function was constructed by the ML-driven algorithm discovery
  1146. // using reinforcement learning. We fed the agent lots of inputs from
  1147. // microbenchmarks, SMHasher, low hamming distance from generated inputs and
  1148. // picked up the one that was good on micro and macrobenchmarks.
  1149. auto p = Read9To16(first, len);
  1150. uint64_t lo = p.first;
  1151. uint64_t hi = p.second;
  1152. // Rotation by 53 was found to be most often useful when discovering these
  1153. // hashing algorithms with ML techniques.
  1154. lo = absl::rotr(lo, 53);
  1155. state += kMul;
  1156. lo += state;
  1157. state ^= hi;
  1158. uint128 m = state;
  1159. m *= lo;
  1160. return static_cast<uint64_t>(m ^ (m >> 64));
  1161. } else if (len >= 4) {
  1162. v = Read4To8(first, len);
  1163. } else if (len > 0) {
  1164. v = Read1To3(first, len);
  1165. } else {
  1166. // Empty ranges have no effect.
  1167. return state;
  1168. }
  1169. return Mix(state, v);
  1170. }
  1171. struct AggregateBarrier {};
  1172. // HashImpl
  1173. // Add a private base class to make sure this type is not an aggregate.
  1174. // Aggregates can be aggregate initialized even if the default constructor is
  1175. // deleted.
  1176. struct PoisonedHash : private AggregateBarrier {
  1177. PoisonedHash() = delete;
  1178. PoisonedHash(const PoisonedHash&) = delete;
  1179. PoisonedHash& operator=(const PoisonedHash&) = delete;
  1180. };
  1181. template <typename T>
  1182. struct HashImpl {
  1183. size_t operator()(const T& value) const {
  1184. return MixingHashState::hash(value);
  1185. }
  1186. };
  1187. template <typename T>
  1188. struct Hash
  1189. : absl::conditional_t<is_hashable<T>::value, HashImpl<T>, PoisonedHash> {};
  1190. template <typename H>
  1191. template <typename T, typename... Ts>
  1192. H HashStateBase<H>::combine(H state, const T& value, const Ts&... values) {
  1193. return H::combine(hash_internal::HashSelect::template Apply<T>::Invoke(
  1194. std::move(state), value),
  1195. values...);
  1196. }
  1197. // HashStateBase::combine_contiguous()
  1198. template <typename H>
  1199. template <typename T>
  1200. H HashStateBase<H>::combine_contiguous(H state, const T* data, size_t size) {
  1201. return hash_internal::hash_range_or_bytes(std::move(state), data, size);
  1202. }
  1203. // HashStateBase::combine_unordered()
  1204. template <typename H>
  1205. template <typename I>
  1206. H HashStateBase<H>::combine_unordered(H state, I begin, I end) {
  1207. return H::RunCombineUnordered(std::move(state),
  1208. CombineUnorderedCallback<I>{begin, end});
  1209. }
  1210. // HashStateBase::PiecewiseCombiner::add_buffer()
  1211. template <typename H>
  1212. H PiecewiseCombiner::add_buffer(H state, const unsigned char* data,
  1213. size_t size) {
  1214. if (position_ + size < PiecewiseChunkSize()) {
  1215. // This partial chunk does not fill our existing buffer
  1216. memcpy(buf_ + position_, data, size);
  1217. position_ += size;
  1218. return state;
  1219. }
  1220. // If the buffer is partially filled we need to complete the buffer
  1221. // and hash it.
  1222. if (position_ != 0) {
  1223. const size_t bytes_needed = PiecewiseChunkSize() - position_;
  1224. memcpy(buf_ + position_, data, bytes_needed);
  1225. state = H::combine_contiguous(std::move(state), buf_, PiecewiseChunkSize());
  1226. data += bytes_needed;
  1227. size -= bytes_needed;
  1228. }
  1229. // Hash whatever chunks we can without copying
  1230. while (size >= PiecewiseChunkSize()) {
  1231. state = H::combine_contiguous(std::move(state), data, PiecewiseChunkSize());
  1232. data += PiecewiseChunkSize();
  1233. size -= PiecewiseChunkSize();
  1234. }
  1235. // Fill the buffer with the remainder
  1236. memcpy(buf_, data, size);
  1237. position_ = size;
  1238. return state;
  1239. }
  1240. // HashStateBase::PiecewiseCombiner::finalize()
  1241. template <typename H>
  1242. H PiecewiseCombiner::finalize(H state) {
  1243. // Hash the remainder left in the buffer, which may be empty
  1244. return H::combine_contiguous(std::move(state), buf_, position_);
  1245. }
  1246. } // namespace hash_internal
  1247. ABSL_NAMESPACE_END
  1248. } // namespace absl
  1249. #endif // ABSL_HASH_INTERNAL_HASH_H_