// Copyright 2016 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_WTF_HASH_UTIL_H_ #define MOJO_PUBLIC_CPP_BINDINGS_LIB_WTF_HASH_UTIL_H_ #include #include "mojo/public/cpp/bindings/lib/hash_util.h" #include "mojo/public/cpp/bindings/struct_ptr.h" #include "third_party/blink/renderer/platform/wtf/hash_functions.h" #include "third_party/blink/renderer/platform/wtf/text/string_hash.h" #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" namespace mojo { namespace internal { template size_t WTFHashCombine(size_t seed, const T& value) { // Based on proposal in: // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1756.pdf // // TODO(tibell): We'd like to use WTF::DefaultHash instead of std::hash, but // there is no general template specialization of DefaultHash for enums // and there can't be an instance for bool. return seed ^ (std::hash()(value) + (seed << 6) + (seed >> 2)); } template ::value> struct WTFHashTraits; template size_t WTFHash(size_t seed, const T& value); template struct WTFHashTraits { static size_t Hash(size_t seed, const T& value) { return value.Hash(seed); } }; template struct WTFHashTraits { static size_t Hash(size_t seed, const T& value) { return WTFHashCombine(seed, value); } }; template <> struct WTFHashTraits { static size_t Hash(size_t seed, const WTF::String& value) { return HashCombine(seed, WTF::StringHash::GetHash(value)); } }; template size_t WTFHash(size_t seed, const T& value) { return WTFHashTraits::Hash(seed, value); } template struct StructPtrHashFn { static unsigned GetHash(const StructPtr& value) { return value.Hash(kHashSeed); } static bool Equal(const StructPtr& left, const StructPtr& right) { return left.Equals(right); } static const bool safe_to_compare_to_empty_or_deleted = false; }; template struct InlinedStructPtrHashFn { static unsigned GetHash(const InlinedStructPtr& value) { return value.Hash(kHashSeed); } static bool Equal(const InlinedStructPtr& left, const InlinedStructPtr& right) { return left.Equals(right); } static const bool safe_to_compare_to_empty_or_deleted = false; }; } // namespace internal } // namespace mojo namespace WTF { template struct DefaultHash> { using Hash = mojo::internal::StructPtrHashFn; }; template struct HashTraits> : public GenericHashTraits> { static const bool kHasIsEmptyValueFunction = true; static bool IsEmptyValue(const mojo::StructPtr& value) { return value.is_null(); } static void ConstructDeletedValue(mojo::StructPtr& slot, bool) { mojo::internal::StructPtrWTFHelper::ConstructDeletedValue(slot); } static bool IsDeletedValue(const mojo::StructPtr& value) { return mojo::internal::StructPtrWTFHelper::IsHashTableDeletedValue( value); } }; template struct DefaultHash> { using Hash = mojo::internal::InlinedStructPtrHashFn; }; template struct HashTraits> : public GenericHashTraits> { static const bool kHasIsEmptyValueFunction = true; static bool IsEmptyValue(const mojo::InlinedStructPtr& value) { return value.is_null(); } static void ConstructDeletedValue(mojo::InlinedStructPtr& slot, bool) { mojo::internal::InlinedStructPtrWTFHelper::ConstructDeletedValue(slot); } static bool IsDeletedValue(const mojo::InlinedStructPtr& value) { return mojo::internal::InlinedStructPtrWTFHelper< T>::IsHashTableDeletedValue(value); } }; } // namespace WTF #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_WTF_HASH_UTIL_H_