hash.h
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 //
00015 // -----------------------------------------------------------------------------
00016 // File: hash.h
00017 // -----------------------------------------------------------------------------
00018 //
00019 #ifndef ABSL_HASH_INTERNAL_HASH_H_
00020 #define ABSL_HASH_INTERNAL_HASH_H_
00021 
00022 #include <algorithm>
00023 #include <array>
00024 #include <cmath>
00025 #include <cstring>
00026 #include <deque>
00027 #include <forward_list>
00028 #include <functional>
00029 #include <iterator>
00030 #include <limits>
00031 #include <list>
00032 #include <map>
00033 #include <memory>
00034 #include <set>
00035 #include <string>
00036 #include <tuple>
00037 #include <type_traits>
00038 #include <utility>
00039 #include <vector>
00040 
00041 #include "absl/base/internal/endian.h"
00042 #include "absl/base/port.h"
00043 #include "absl/container/fixed_array.h"
00044 #include "absl/meta/type_traits.h"
00045 #include "absl/numeric/int128.h"
00046 #include "absl/strings/string_view.h"
00047 #include "absl/types/optional.h"
00048 #include "absl/types/variant.h"
00049 #include "absl/utility/utility.h"
00050 #include "absl/hash/internal/city.h"
00051 
00052 namespace absl {
00053 namespace hash_internal {
00054 
00055 // HashStateBase
00056 //
00057 // A hash state object represents an intermediate state in the computation
00058 // of an unspecified hash algorithm. `HashStateBase` provides a CRTP style
00059 // base class for hash state implementations. Developers adding type support
00060 // for `absl::Hash` should not rely on any parts of the state object other than
00061 // the following member functions:
00062 //
00063 //   * HashStateBase::combine()
00064 //   * HashStateBase::combine_contiguous()
00065 //
00066 // A derived hash state class of type `H` must provide a static member function
00067 // with a signature similar to the following:
00068 //
00069 //    `static H combine_contiguous(H state, const unsigned char*, size_t)`.
00070 //
00071 // `HashStateBase` will provide a complete implementations for a hash state
00072 // object in terms of this method.
00073 //
00074 // Example:
00075 //
00076 //   // Use CRTP to define your derived class.
00077 //   struct MyHashState : HashStateBase<MyHashState> {
00078 //       static H combine_contiguous(H state, const unsigned char*, size_t);
00079 //       using MyHashState::HashStateBase::combine;
00080 //       using MyHashState::HashStateBase::combine_contiguous;
00081 //   };
00082 template <typename H>
00083 class HashStateBase {
00084  public:
00085   // HashStateBase::combine()
00086   //
00087   // Combines an arbitrary number of values into a hash state, returning the
00088   // updated state.
00089   //
00090   // Each of the value types `T` must be separately hashable by the Abseil
00091   // hashing framework.
00092   //
00093   // NOTE:
00094   //
00095   //   state = H::combine(std::move(state), value1, value2, value3);
00096   //
00097   // is guaranteed to produce the same hash expansion as:
00098   //
00099   //   state = H::combine(std::move(state), value1);
00100   //   state = H::combine(std::move(state), value2);
00101   //   state = H::combine(std::move(state), value3);
00102   template <typename T, typename... Ts>
00103   static H combine(H state, const T& value, const Ts&... values);
00104   static H combine(H state) { return state; }
00105 
00106   // HashStateBase::combine_contiguous()
00107   //
00108   // Combines a contiguous array of `size` elements into a hash state, returning
00109   // the updated state.
00110   //
00111   // NOTE:
00112   //
00113   //   state = H::combine_contiguous(std::move(state), data, size);
00114   //
00115   // is NOT guaranteed to produce the same hash expansion as a for-loop (it may
00116   // perform internal optimizations).  If you need this guarantee, use the
00117   // for-loop instead.
00118   template <typename T>
00119   static H combine_contiguous(H state, const T* data, size_t size);
00120 };
00121 
00122 // is_uniquely_represented
00123 //
00124 // `is_uniquely_represented<T>` is a trait class that indicates whether `T`
00125 // is uniquely represented.
00126 //
00127 // A type is "uniquely represented" if two equal values of that type are
00128 // guaranteed to have the same bytes in their underlying storage. In other
00129 // words, if `a == b`, then `memcmp(&a, &b, sizeof(T))` is guaranteed to be
00130 // zero. This property cannot be detected automatically, so this trait is false
00131 // by default, but can be specialized by types that wish to assert that they are
00132 // uniquely represented. This makes them eligible for certain optimizations.
00133 //
00134 // If you have any doubt whatsoever, do not specialize this template.
00135 // The default is completely safe, and merely disables some optimizations
00136 // that will not matter for most types. Specializing this template,
00137 // on the other hand, can be very hazardous.
00138 //
00139 // To be uniquely represented, a type must not have multiple ways of
00140 // representing the same value; for example, float and double are not
00141 // uniquely represented, because they have distinct representations for
00142 // +0 and -0. Furthermore, the type's byte representation must consist
00143 // solely of user-controlled data, with no padding bits and no compiler-
00144 // controlled data such as vptrs or sanitizer metadata. This is usually
00145 // very difficult to guarantee, because in most cases the compiler can
00146 // insert data and padding bits at its own discretion.
00147 //
00148 // If you specialize this template for a type `T`, you must do so in the file
00149 // that defines that type (or in this file). If you define that specialization
00150 // anywhere else, `is_uniquely_represented<T>` could have different meanings
00151 // in different places.
00152 //
00153 // The Enable parameter is meaningless; it is provided as a convenience,
00154 // to support certain SFINAE techniques when defining specializations.
00155 template <typename T, typename Enable = void>
00156 struct is_uniquely_represented : std::false_type {};
00157 
00158 // is_uniquely_represented<unsigned char>
00159 //
00160 // unsigned char is a synonym for "byte", so it is guaranteed to be
00161 // uniquely represented.
00162 template <>
00163 struct is_uniquely_represented<unsigned char> : std::true_type {};
00164 
00165 // is_uniquely_represented for non-standard integral types
00166 //
00167 // Integral types other than bool should be uniquely represented on any
00168 // platform that this will plausibly be ported to.
00169 template <typename Integral>
00170 struct is_uniquely_represented<
00171     Integral, typename std::enable_if<std::is_integral<Integral>::value>::type>
00172     : std::true_type {};
00173 
00174 // is_uniquely_represented<bool>
00175 //
00176 //
00177 template <>
00178 struct is_uniquely_represented<bool> : std::false_type {};
00179 
00180 // hash_bytes()
00181 //
00182 // Convenience function that combines `hash_state` with the byte representation
00183 // of `value`.
00184 template <typename H, typename T>
00185 H hash_bytes(H hash_state, const T& value) {
00186   const unsigned char* start = reinterpret_cast<const unsigned char*>(&value);
00187   return H::combine_contiguous(std::move(hash_state), start, sizeof(value));
00188 }
00189 
00190 // -----------------------------------------------------------------------------
00191 // AbslHashValue for Basic Types
00192 // -----------------------------------------------------------------------------
00193 
00194 // Note: Default `AbslHashValue` implementations live in `hash_internal`. This
00195 // allows us to block lexical scope lookup when doing an unqualified call to
00196 // `AbslHashValue` below. User-defined implementations of `AbslHashValue` can
00197 // only be found via ADL.
00198 
00199 // AbslHashValue() for hashing bool values
00200 //
00201 // We use SFINAE to ensure that this overload only accepts bool, not types that
00202 // are convertible to bool.
00203 template <typename H, typename B>
00204 typename std::enable_if<std::is_same<B, bool>::value, H>::type AbslHashValue(
00205     H hash_state, B value) {
00206   return H::combine(std::move(hash_state),
00207                     static_cast<unsigned char>(value ? 1 : 0));
00208 }
00209 
00210 // AbslHashValue() for hashing enum values
00211 template <typename H, typename Enum>
00212 typename std::enable_if<std::is_enum<Enum>::value, H>::type AbslHashValue(
00213     H hash_state, Enum e) {
00214   // In practice, we could almost certainly just invoke hash_bytes directly,
00215   // but it's possible that a sanitizer might one day want to
00216   // store data in the unused bits of an enum. To avoid that risk, we
00217   // convert to the underlying type before hashing. Hopefully this will get
00218   // optimized away; if not, we can reopen discussion with c-toolchain-team.
00219   return H::combine(std::move(hash_state),
00220                     static_cast<typename std::underlying_type<Enum>::type>(e));
00221 }
00222 // AbslHashValue() for hashing floating-point values
00223 template <typename H, typename Float>
00224 typename std::enable_if<std::is_same<Float, float>::value ||
00225                             std::is_same<Float, double>::value,
00226                         H>::type
00227 AbslHashValue(H hash_state, Float value) {
00228   return hash_internal::hash_bytes(std::move(hash_state),
00229                                    value == 0 ? 0 : value);
00230 }
00231 
00232 // Long double has the property that it might have extra unused bytes in it.
00233 // For example, in x86 sizeof(long double)==16 but it only really uses 80-bits
00234 // of it. This means we can't use hash_bytes on a long double and have to
00235 // convert it to something else first.
00236 template <typename H, typename LongDouble>
00237 typename std::enable_if<std::is_same<LongDouble, long double>::value, H>::type
00238 AbslHashValue(H hash_state, LongDouble value) {
00239   const int category = std::fpclassify(value);
00240   switch (category) {
00241     case FP_INFINITE:
00242       // Add the sign bit to differentiate between +Inf and -Inf
00243       hash_state = H::combine(std::move(hash_state), std::signbit(value));
00244       break;
00245 
00246     case FP_NAN:
00247     case FP_ZERO:
00248     default:
00249       // Category is enough for these.
00250       break;
00251 
00252     case FP_NORMAL:
00253     case FP_SUBNORMAL:
00254       // We can't convert `value` directly to double because this would have
00255       // undefined behavior if the value is out of range.
00256       // std::frexp gives us a value in the range (-1, -.5] or [.5, 1) that is
00257       // guaranteed to be in range for `double`. The truncation is
00258       // implementation defined, but that works as long as it is deterministic.
00259       int exp;
00260       auto mantissa = static_cast<double>(std::frexp(value, &exp));
00261       hash_state = H::combine(std::move(hash_state), mantissa, exp);
00262   }
00263 
00264   return H::combine(std::move(hash_state), category);
00265 }
00266 
00267 // AbslHashValue() for hashing pointers
00268 template <typename H, typename T>
00269 H AbslHashValue(H hash_state, T* ptr) {
00270   auto v = reinterpret_cast<uintptr_t>(ptr);
00271   // Due to alignment, pointers tend to have low bits as zero, and the next few
00272   // bits follow a pattern since they are also multiples of some base value.
00273   // Mixing the pointer twice helps prevent stuck low bits for certain alignment
00274   // values.
00275   return H::combine(std::move(hash_state), v, v);
00276 }
00277 
00278 // AbslHashValue() for hashing nullptr_t
00279 template <typename H>
00280 H AbslHashValue(H hash_state, std::nullptr_t) {
00281   return H::combine(std::move(hash_state), static_cast<void*>(nullptr));
00282 }
00283 
00284 // -----------------------------------------------------------------------------
00285 // AbslHashValue for Composite Types
00286 // -----------------------------------------------------------------------------
00287 
00288 // is_hashable()
00289 //
00290 // Trait class which returns true if T is hashable by the absl::Hash framework.
00291 // Used for the AbslHashValue implementations for composite types below.
00292 template <typename T>
00293 struct is_hashable;
00294 
00295 // AbslHashValue() for hashing pairs
00296 template <typename H, typename T1, typename T2>
00297 typename std::enable_if<is_hashable<T1>::value && is_hashable<T2>::value,
00298                         H>::type
00299 AbslHashValue(H hash_state, const std::pair<T1, T2>& p) {
00300   return H::combine(std::move(hash_state), p.first, p.second);
00301 }
00302 
00303 // hash_tuple()
00304 //
00305 // Helper function for hashing a tuple. The third argument should
00306 // be an index_sequence running from 0 to tuple_size<Tuple> - 1.
00307 template <typename H, typename Tuple, size_t... Is>
00308 H hash_tuple(H hash_state, const Tuple& t, absl::index_sequence<Is...>) {
00309   return H::combine(std::move(hash_state), std::get<Is>(t)...);
00310 }
00311 
00312 // AbslHashValue for hashing tuples
00313 template <typename H, typename... Ts>
00314 #if defined(_MSC_VER)
00315 // This SFINAE gets MSVC confused under some conditions. Let's just disable it
00316 // for now.
00317 H
00318 #else  // _MSC_VER
00319 typename std::enable_if<absl::conjunction<is_hashable<Ts>...>::value, H>::type
00320 #endif  // _MSC_VER
00321 AbslHashValue(H hash_state, const std::tuple<Ts...>& t) {
00322   return hash_internal::hash_tuple(std::move(hash_state), t,
00323                                    absl::make_index_sequence<sizeof...(Ts)>());
00324 }
00325 
00326 // -----------------------------------------------------------------------------
00327 // AbslHashValue for Pointers
00328 // -----------------------------------------------------------------------------
00329 
00330 // AbslHashValue for hashing unique_ptr
00331 template <typename H, typename T, typename D>
00332 H AbslHashValue(H hash_state, const std::unique_ptr<T, D>& ptr) {
00333   return H::combine(std::move(hash_state), ptr.get());
00334 }
00335 
00336 // AbslHashValue for hashing shared_ptr
00337 template <typename H, typename T>
00338 H AbslHashValue(H hash_state, const std::shared_ptr<T>& ptr) {
00339   return H::combine(std::move(hash_state), ptr.get());
00340 }
00341 
00342 // -----------------------------------------------------------------------------
00343 // AbslHashValue for String-Like Types
00344 // -----------------------------------------------------------------------------
00345 
00346 // AbslHashValue for hashing strings
00347 //
00348 // All the string-like types supported here provide the same hash expansion for
00349 // the same character sequence. These types are:
00350 //
00351 //  - `std::string` (and std::basic_string<char, std::char_traits<char>, A> for
00352 //      any allocator A)
00353 //  - `absl::string_view` and `std::string_view`
00354 //
00355 // For simplicity, we currently support only `char` strings. This support may
00356 // be broadened, if necessary, but with some caution - this overload would
00357 // misbehave in cases where the traits' `eq()` member isn't equivalent to `==`
00358 // on the underlying character type.
00359 template <typename H>
00360 H AbslHashValue(H hash_state, absl::string_view str) {
00361   return H::combine(
00362       H::combine_contiguous(std::move(hash_state), str.data(), str.size()),
00363       str.size());
00364 }
00365 
00366 // -----------------------------------------------------------------------------
00367 // AbslHashValue for Sequence Containers
00368 // -----------------------------------------------------------------------------
00369 
00370 // AbslHashValue for hashing std::array
00371 template <typename H, typename T, size_t N>
00372 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
00373     H hash_state, const std::array<T, N>& array) {
00374   return H::combine_contiguous(std::move(hash_state), array.data(),
00375                                array.size());
00376 }
00377 
00378 // AbslHashValue for hashing std::deque
00379 template <typename H, typename T, typename Allocator>
00380 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
00381     H hash_state, const std::deque<T, Allocator>& deque) {
00382   // TODO(gromer): investigate a more efficient implementation taking
00383   // advantage of the chunk structure.
00384   for (const auto& t : deque) {
00385     hash_state = H::combine(std::move(hash_state), t);
00386   }
00387   return H::combine(std::move(hash_state), deque.size());
00388 }
00389 
00390 // AbslHashValue for hashing std::forward_list
00391 template <typename H, typename T, typename Allocator>
00392 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
00393     H hash_state, const std::forward_list<T, Allocator>& list) {
00394   size_t size = 0;
00395   for (const T& t : list) {
00396     hash_state = H::combine(std::move(hash_state), t);
00397     ++size;
00398   }
00399   return H::combine(std::move(hash_state), size);
00400 }
00401 
00402 // AbslHashValue for hashing std::list
00403 template <typename H, typename T, typename Allocator>
00404 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
00405     H hash_state, const std::list<T, Allocator>& list) {
00406   for (const auto& t : list) {
00407     hash_state = H::combine(std::move(hash_state), t);
00408   }
00409   return H::combine(std::move(hash_state), list.size());
00410 }
00411 
00412 // AbslHashValue for hashing std::vector
00413 //
00414 // Do not use this for vector<bool>. It does not have a .data(), and a fallback
00415 // for std::hash<> is most likely faster.
00416 template <typename H, typename T, typename Allocator>
00417 typename std::enable_if<is_hashable<T>::value && !std::is_same<T, bool>::value,
00418                         H>::type
00419 AbslHashValue(H hash_state, const std::vector<T, Allocator>& vector) {
00420   return H::combine(H::combine_contiguous(std::move(hash_state), vector.data(),
00421                                           vector.size()),
00422                     vector.size());
00423 }
00424 
00425 // -----------------------------------------------------------------------------
00426 // AbslHashValue for Ordered Associative Containers
00427 // -----------------------------------------------------------------------------
00428 
00429 // AbslHashValue for hashing std::map
00430 template <typename H, typename Key, typename T, typename Compare,
00431           typename Allocator>
00432 typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
00433                         H>::type
00434 AbslHashValue(H hash_state, const std::map<Key, T, Compare, Allocator>& map) {
00435   for (const auto& t : map) {
00436     hash_state = H::combine(std::move(hash_state), t);
00437   }
00438   return H::combine(std::move(hash_state), map.size());
00439 }
00440 
00441 // AbslHashValue for hashing std::multimap
00442 template <typename H, typename Key, typename T, typename Compare,
00443           typename Allocator>
00444 typename std::enable_if<is_hashable<Key>::value && is_hashable<T>::value,
00445                         H>::type
00446 AbslHashValue(H hash_state,
00447               const std::multimap<Key, T, Compare, Allocator>& map) {
00448   for (const auto& t : map) {
00449     hash_state = H::combine(std::move(hash_state), t);
00450   }
00451   return H::combine(std::move(hash_state), map.size());
00452 }
00453 
00454 // AbslHashValue for hashing std::set
00455 template <typename H, typename Key, typename Compare, typename Allocator>
00456 typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
00457     H hash_state, const std::set<Key, Compare, Allocator>& set) {
00458   for (const auto& t : set) {
00459     hash_state = H::combine(std::move(hash_state), t);
00460   }
00461   return H::combine(std::move(hash_state), set.size());
00462 }
00463 
00464 // AbslHashValue for hashing std::multiset
00465 template <typename H, typename Key, typename Compare, typename Allocator>
00466 typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue(
00467     H hash_state, const std::multiset<Key, Compare, Allocator>& set) {
00468   for (const auto& t : set) {
00469     hash_state = H::combine(std::move(hash_state), t);
00470   }
00471   return H::combine(std::move(hash_state), set.size());
00472 }
00473 
00474 // -----------------------------------------------------------------------------
00475 // AbslHashValue for Wrapper Types
00476 // -----------------------------------------------------------------------------
00477 
00478 // AbslHashValue for hashing absl::optional
00479 template <typename H, typename T>
00480 typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue(
00481     H hash_state, const absl::optional<T>& opt) {
00482   if (opt) hash_state = H::combine(std::move(hash_state), *opt);
00483   return H::combine(std::move(hash_state), opt.has_value());
00484 }
00485 
00486 // VariantVisitor
00487 template <typename H>
00488 struct VariantVisitor {
00489   H&& hash_state;
00490   template <typename T>
00491   H operator()(const T& t) const {
00492     return H::combine(std::move(hash_state), t);
00493   }
00494 };
00495 
00496 // AbslHashValue for hashing absl::variant
00497 template <typename H, typename... T>
00498 typename std::enable_if<conjunction<is_hashable<T>...>::value, H>::type
00499 AbslHashValue(H hash_state, const absl::variant<T...>& v) {
00500   if (!v.valueless_by_exception()) {
00501     hash_state = absl::visit(VariantVisitor<H>{std::move(hash_state)}, v);
00502   }
00503   return H::combine(std::move(hash_state), v.index());
00504 }
00505 
00506 // -----------------------------------------------------------------------------
00507 // AbslHashValue for Other Types
00508 // -----------------------------------------------------------------------------
00509 
00510 // AbslHashValue for hashing std::bitset is not defined, for the same reason as
00511 // for vector<bool> (see std::vector above): It does not expose the raw bytes,
00512 // and a fallback to std::hash<> is most likely faster.
00513 
00514 // -----------------------------------------------------------------------------
00515 
00516 // hash_range_or_bytes()
00517 //
00518 // Mixes all values in the range [data, data+size) into the hash state.
00519 // This overload accepts only uniquely-represented types, and hashes them by
00520 // hashing the entire range of bytes.
00521 template <typename H, typename T>
00522 typename std::enable_if<is_uniquely_represented<T>::value, H>::type
00523 hash_range_or_bytes(H hash_state, const T* data, size_t size) {
00524   const auto* bytes = reinterpret_cast<const unsigned char*>(data);
00525   return H::combine_contiguous(std::move(hash_state), bytes, sizeof(T) * size);
00526 }
00527 
00528 // hash_range_or_bytes()
00529 template <typename H, typename T>
00530 typename std::enable_if<!is_uniquely_represented<T>::value, H>::type
00531 hash_range_or_bytes(H hash_state, const T* data, size_t size) {
00532   for (const auto end = data + size; data < end; ++data) {
00533     hash_state = H::combine(std::move(hash_state), *data);
00534   }
00535   return hash_state;
00536 }
00537 
00538 #if defined(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE) && \
00539     ABSL_META_INTERNAL_STD_HASH_SFINAE_FRIENDLY_
00540 #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 1
00541 #else
00542 #define ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_ 0
00543 #endif
00544 
00545 // HashSelect
00546 //
00547 // Type trait to select the appropriate hash implementation to use.
00548 // HashSelect::type<T> will give the proper hash implementation, to be invoked
00549 // as:
00550 //   HashSelect::type<T>::Invoke(state, value)
00551 // Also, HashSelect::type<T>::value is a boolean equal to `true` if there is a
00552 // valid `Invoke` function. Types that are not hashable will have a ::value of
00553 // `false`.
00554 struct HashSelect {
00555  private:
00556   struct State : HashStateBase<State> {
00557     static State combine_contiguous(State hash_state, const unsigned char*,
00558                                     size_t);
00559     using State::HashStateBase::combine_contiguous;
00560   };
00561 
00562   struct UniquelyRepresentedProbe {
00563     template <typename H, typename T>
00564     static auto Invoke(H state, const T& value)
00565         -> absl::enable_if_t<is_uniquely_represented<T>::value, H> {
00566       return hash_internal::hash_bytes(std::move(state), value);
00567     }
00568   };
00569 
00570   struct HashValueProbe {
00571     template <typename H, typename T>
00572     static auto Invoke(H state, const T& value) -> absl::enable_if_t<
00573         std::is_same<H,
00574                      decltype(AbslHashValue(std::move(state), value))>::value,
00575         H> {
00576       return AbslHashValue(std::move(state), value);
00577     }
00578   };
00579 
00580   struct LegacyHashProbe {
00581 #if ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
00582     template <typename H, typename T>
00583     static auto Invoke(H state, const T& value) -> absl::enable_if_t<
00584         std::is_convertible<
00585             decltype(ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>()(value)),
00586             size_t>::value,
00587         H> {
00588       return hash_internal::hash_bytes(
00589           std::move(state),
00590           ABSL_INTERNAL_LEGACY_HASH_NAMESPACE::hash<T>{}(value));
00591     }
00592 #endif  // ABSL_HASH_INTERNAL_SUPPORT_LEGACY_HASH_
00593   };
00594 
00595   struct StdHashProbe {
00596     template <typename H, typename T>
00597     static auto Invoke(H state, const T& value)
00598         -> absl::enable_if_t<type_traits_internal::IsHashable<T>::value, H> {
00599       return hash_internal::hash_bytes(std::move(state), std::hash<T>{}(value));
00600     }
00601   };
00602 
00603   template <typename Hash, typename T>
00604   struct Probe : Hash {
00605    private:
00606     template <typename H, typename = decltype(H::Invoke(
00607                               std::declval<State>(), std::declval<const T&>()))>
00608     static std::true_type Test(int);
00609     template <typename U>
00610     static std::false_type Test(char);
00611 
00612    public:
00613     static constexpr bool value = decltype(Test<Hash>(0))::value;
00614   };
00615 
00616  public:
00617   // Probe each implementation in order.
00618   // disjunction provides short circuiting wrt instantiation.
00619   template <typename T>
00620   using Apply = absl::disjunction<         //
00621       Probe<UniquelyRepresentedProbe, T>,  //
00622       Probe<HashValueProbe, T>,            //
00623       Probe<LegacyHashProbe, T>,           //
00624       Probe<StdHashProbe, T>,              //
00625       std::false_type>;
00626 };
00627 
00628 template <typename T>
00629 struct is_hashable
00630     : std::integral_constant<bool, HashSelect::template Apply<T>::value> {};
00631 
00632 // CityHashState
00633 class CityHashState : public HashStateBase<CityHashState> {
00634   // absl::uint128 is not an alias or a thin wrapper around the intrinsic.
00635   // We use the intrinsic when available to improve performance.
00636 #ifdef ABSL_HAVE_INTRINSIC_INT128
00637   using uint128 = __uint128_t;
00638 #else   // ABSL_HAVE_INTRINSIC_INT128
00639   using uint128 = absl::uint128;
00640 #endif  // ABSL_HAVE_INTRINSIC_INT128
00641 
00642   static constexpr uint64_t kMul =
00643       sizeof(size_t) == 4 ? uint64_t{0xcc9e2d51} : uint64_t{0x9ddfea08eb382d69};
00644 
00645   template <typename T>
00646   using IntegralFastPath =
00647       conjunction<std::is_integral<T>, is_uniquely_represented<T>>;
00648 
00649  public:
00650   // Move only
00651   CityHashState(CityHashState&&) = default;
00652   CityHashState& operator=(CityHashState&&) = default;
00653 
00654   // CityHashState::combine_contiguous()
00655   //
00656   // Fundamental base case for hash recursion: mixes the given range of bytes
00657   // into the hash state.
00658   static CityHashState combine_contiguous(CityHashState hash_state,
00659                                           const unsigned char* first,
00660                                           size_t size) {
00661     return CityHashState(
00662         CombineContiguousImpl(hash_state.state_, first, size,
00663                               std::integral_constant<int, sizeof(size_t)>{}));
00664   }
00665   using CityHashState::HashStateBase::combine_contiguous;
00666 
00667   // CityHashState::hash()
00668   //
00669   // For performance reasons in non-opt mode, we specialize this for
00670   // integral types.
00671   // Otherwise we would be instantiating and calling dozens of functions for
00672   // something that is just one multiplication and a couple xor's.
00673   // The result should be the same as running the whole algorithm, but faster.
00674   template <typename T, absl::enable_if_t<IntegralFastPath<T>::value, int> = 0>
00675   static size_t hash(T value) {
00676     return static_cast<size_t>(Mix(Seed(), static_cast<uint64_t>(value)));
00677   }
00678 
00679   // Overload of CityHashState::hash()
00680   template <typename T, absl::enable_if_t<!IntegralFastPath<T>::value, int> = 0>
00681   static size_t hash(const T& value) {
00682     return static_cast<size_t>(combine(CityHashState{}, value).state_);
00683   }
00684 
00685  private:
00686   // Invoked only once for a given argument; that plus the fact that this is
00687   // move-only ensures that there is only one non-moved-from object.
00688   CityHashState() : state_(Seed()) {}
00689 
00690   // Workaround for MSVC bug.
00691   // We make the type copyable to fix the calling convention, even though we
00692   // never actually copy it. Keep it private to not affect the public API of the
00693   // type.
00694   CityHashState(const CityHashState&) = default;
00695 
00696   explicit CityHashState(uint64_t state) : state_(state) {}
00697 
00698   // Implementation of the base case for combine_contiguous where we actually
00699   // mix the bytes into the state.
00700   // Dispatch to different implementations of the combine_contiguous depending
00701   // on the value of `sizeof(size_t)`.
00702   static uint64_t CombineContiguousImpl(uint64_t state,
00703                                         const unsigned char* first, size_t len,
00704                                         std::integral_constant<int, 4>
00705                                         /* sizeof_size_t */);
00706   static uint64_t CombineContiguousImpl(uint64_t state,
00707                                         const unsigned char* first, size_t len,
00708                                         std::integral_constant<int, 8>
00709                                         /* sizeof_size_t*/);
00710 
00711   // Reads 9 to 16 bytes from p.
00712   // The first 8 bytes are in .first, the rest (zero padded) bytes are in
00713   // .second.
00714   static std::pair<uint64_t, uint64_t> Read9To16(const unsigned char* p,
00715                                                  size_t len) {
00716     uint64_t high = little_endian::Load64(p + len - 8);
00717     return {little_endian::Load64(p), high >> (128 - len * 8)};
00718   }
00719 
00720   // Reads 4 to 8 bytes from p. Zero pads to fill uint64_t.
00721   static uint64_t Read4To8(const unsigned char* p, size_t len) {
00722     return (static_cast<uint64_t>(little_endian::Load32(p + len - 4))
00723             << (len - 4) * 8) |
00724            little_endian::Load32(p);
00725   }
00726 
00727   // Reads 1 to 3 bytes from p. Zero pads to fill uint32_t.
00728   static uint32_t Read1To3(const unsigned char* p, size_t len) {
00729     return static_cast<uint32_t>((p[0]) |                         //
00730                                  (p[len / 2] << (len / 2 * 8)) |  //
00731                                  (p[len - 1] << ((len - 1) * 8)));
00732   }
00733 
00734   ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Mix(uint64_t state, uint64_t v) {
00735     using MultType =
00736         absl::conditional_t<sizeof(size_t) == 4, uint64_t, uint128>;
00737     // We do the addition in 64-bit space to make sure the 128-bit
00738     // multiplication is fast. If we were to do it as MultType the compiler has
00739     // to assume that the high word is non-zero and needs to perform 2
00740     // multiplications instead of one.
00741     MultType m = state + v;
00742     m *= kMul;
00743     return static_cast<uint64_t>(m ^ (m >> (sizeof(m) * 8 / 2)));
00744   }
00745 
00746   // Seed()
00747   //
00748   // A non-deterministic seed.
00749   //
00750   // The current purpose of this seed is to generate non-deterministic results
00751   // and prevent having users depend on the particular hash values.
00752   // It is not meant as a security feature right now, but it leaves the door
00753   // open to upgrade it to a true per-process random seed. A true random seed
00754   // costs more and we don't need to pay for that right now.
00755   //
00756   // On platforms with ASLR, we take advantage of it to make a per-process
00757   // random value.
00758   // See https://en.wikipedia.org/wiki/Address_space_layout_randomization
00759   //
00760   // On other platforms this is still going to be non-deterministic but most
00761   // probably per-build and not per-process.
00762   ABSL_ATTRIBUTE_ALWAYS_INLINE static uint64_t Seed() {
00763     return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(kSeed));
00764   }
00765   static const void* const kSeed;
00766 
00767   uint64_t state_;
00768 };
00769 
00770 // CityHashState::CombineContiguousImpl()
00771 inline uint64_t CityHashState::CombineContiguousImpl(
00772     uint64_t state, const unsigned char* first, size_t len,
00773     std::integral_constant<int, 4> /* sizeof_size_t */) {
00774   // For large values we use CityHash, for small ones we just use a
00775   // multiplicative hash.
00776   uint64_t v;
00777   if (len > 8) {
00778     v = absl::hash_internal::CityHash32(reinterpret_cast<const char*>(first), len);
00779   } else if (len >= 4) {
00780     v = Read4To8(first, len);
00781   } else if (len > 0) {
00782     v = Read1To3(first, len);
00783   } else {
00784     // Empty ranges have no effect.
00785     return state;
00786   }
00787   return Mix(state, v);
00788 }
00789 
00790 // Overload of CityHashState::CombineContiguousImpl()
00791 inline uint64_t CityHashState::CombineContiguousImpl(
00792     uint64_t state, const unsigned char* first, size_t len,
00793     std::integral_constant<int, 8> /* sizeof_size_t */) {
00794   // For large values we use CityHash, for small ones we just use a
00795   // multiplicative hash.
00796   uint64_t v;
00797   if (len > 16) {
00798     v = absl::hash_internal::CityHash64(reinterpret_cast<const char*>(first), len);
00799   } else if (len > 8) {
00800     auto p = Read9To16(first, len);
00801     state = Mix(state, p.first);
00802     v = p.second;
00803   } else if (len >= 4) {
00804     v = Read4To8(first, len);
00805   } else if (len > 0) {
00806     v = Read1To3(first, len);
00807   } else {
00808     // Empty ranges have no effect.
00809     return state;
00810   }
00811   return Mix(state, v);
00812 }
00813 
00814 
00815 struct AggregateBarrier {};
00816 
00817 // HashImpl
00818 
00819 // Add a private base class to make sure this type is not an aggregate.
00820 // Aggregates can be aggregate initialized even if the default constructor is
00821 // deleted.
00822 struct PoisonedHash : private AggregateBarrier {
00823   PoisonedHash() = delete;
00824   PoisonedHash(const PoisonedHash&) = delete;
00825   PoisonedHash& operator=(const PoisonedHash&) = delete;
00826 };
00827 
00828 template <typename T>
00829 struct HashImpl {
00830   size_t operator()(const T& value) const { return CityHashState::hash(value); }
00831 };
00832 
00833 template <typename T>
00834 struct Hash
00835     : absl::conditional_t<is_hashable<T>::value, HashImpl<T>, PoisonedHash> {};
00836 
00837 template <typename H>
00838 template <typename T, typename... Ts>
00839 H HashStateBase<H>::combine(H state, const T& value, const Ts&... values) {
00840   return H::combine(hash_internal::HashSelect::template Apply<T>::Invoke(
00841                         std::move(state), value),
00842                     values...);
00843 }
00844 
00845 // HashStateBase::combine_contiguous()
00846 template <typename H>
00847 template <typename T>
00848 H HashStateBase<H>::combine_contiguous(H state, const T* data, size_t size) {
00849   return hash_internal::hash_range_or_bytes(std::move(state), data, size);
00850 }
00851 }  // namespace hash_internal
00852 }  // namespace absl
00853 
00854 #endif  // ABSL_HASH_INTERNAL_HASH_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14