You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

hash.h 60 kB

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