/* * Copyright (C) 2005-2019 Apple Inc. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #pragma once #include #include #include #include namespace WTF { template struct IntTypes; template<> struct IntTypes<1> { typedef int8_t SignedType; typedef uint8_t UnsignedType; }; template<> struct IntTypes<2> { typedef int16_t SignedType; typedef uint16_t UnsignedType; }; template<> struct IntTypes<4> { typedef int32_t SignedType; typedef uint32_t UnsignedType; }; template<> struct IntTypes<8> { typedef int64_t SignedType; typedef uint64_t UnsignedType; }; // integer hash function // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm inline unsigned intHash(uint8_t key8) { unsigned key = key8; key += ~(key << 15); key ^= (key >> 10); key += (key << 3); key ^= (key >> 6); key += ~(key << 11); key ^= (key >> 16); return key; } // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm inline unsigned intHash(uint16_t key16) { unsigned key = key16; key += ~(key << 15); key ^= (key >> 10); key += (key << 3); key ^= (key >> 6); key += ~(key << 11); key ^= (key >> 16); return key; } // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm inline unsigned intHash(uint32_t key) { key += ~(key << 15); key ^= (key >> 10); key += (key << 3); key ^= (key >> 6); key += ~(key << 11); key ^= (key >> 16); return key; } // Thomas Wang's 64 bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm inline unsigned intHash(uint64_t key) { key += ~(key << 32); key ^= (key >> 22); key += ~(key << 13); key ^= (key >> 8); key += (key << 3); key ^= (key >> 15); key += ~(key << 27); key ^= (key >> 31); return static_cast(key); } // Compound integer hash method: http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000 inline unsigned pairIntHash(unsigned key1, unsigned key2) { unsigned shortRandom1 = 277951225; // A random 32-bit value. unsigned shortRandom2 = 95187966; // A random 32-bit value. uint64_t longRandom = 19248658165952622LL; // A random 64-bit value. uint64_t product = longRandom * (shortRandom1 * key1 + shortRandom2 * key2); unsigned highBits = static_cast(product >> (sizeof(uint64_t) - sizeof(unsigned))); return highBits; } template struct IntHash { static unsigned hash(T key) { return intHash(static_cast::UnsignedType>(key)); } static bool equal(T a, T b) { return a == b; } static constexpr bool safeToCompareToEmptyOrDeleted = true; }; template struct FloatHash { typedef typename IntTypes::UnsignedType Bits; static unsigned hash(T key) { return intHash(bitwise_cast(key)); } static bool equal(T a, T b) { return bitwise_cast(a) == bitwise_cast(b); } static constexpr bool safeToCompareToEmptyOrDeleted = true; }; // pointer identity hash function template struct PtrHashBase; template struct PtrHashBase { typedef T PtrType; static unsigned hash(PtrType key) { return IntHash::hash(reinterpret_cast(key)); } static bool equal(PtrType a, PtrType b) { return a == b; } static constexpr bool safeToCompareToEmptyOrDeleted = true; }; template struct PtrHashBase { typedef typename GetPtrHelper::PtrType PtrType; static unsigned hash(PtrType key) { return IntHash::hash(reinterpret_cast(key)); } static bool equal(PtrType a, PtrType b) { return a == b; } static constexpr bool safeToCompareToEmptyOrDeleted = true; static unsigned hash(const T& key) { return hash(getPtr(key)); } static bool equal(const T& a, const T& b) { return getPtr(a) == getPtr(b); } static bool equal(PtrType a, const T& b) { return a == getPtr(b); } static bool equal(const T& a, PtrType b) { return getPtr(a) == b; } }; template struct PtrHash : PtrHashBase::value> { }; template struct PtrHash> : PtrHashBase, IsSmartPtr>::value> { static constexpr bool safeToCompareToEmptyOrDeleted = false; }; // default hash function for each type template struct DefaultHash; template struct PairHash { static unsigned hash(const std::pair& p) { return pairIntHash(DefaultHash::hash(p.first), DefaultHash::hash(p.second)); } static bool equal(const std::pair& a, const std::pair& b) { return DefaultHash::equal(a.first, b.first) && DefaultHash::equal(a.second, b.second); } static constexpr bool safeToCompareToEmptyOrDeleted = DefaultHash::safeToCompareToEmptyOrDeleted && DefaultHash::safeToCompareToEmptyOrDeleted; }; template struct IntPairHash { static unsigned hash(const std::pair& p) { return pairIntHash(p.first, p.second); } static bool equal(const std::pair& a, const std::pair& b) { return PairHash::equal(a, b); } static constexpr bool safeToCompareToEmptyOrDeleted = PairHash::safeToCompareToEmptyOrDeleted; }; template struct TupleHash { template static typename std::enable_if::type hash(const std::tuple& t) { using IthTupleElementType = typename std::tuple_element>::type; return pairIntHash(DefaultHash::hash(std::get(t)), hash(t)); } template static typename std::enable_if::type hash(const std::tuple& t) { using IthTupleElementType = typename std::tuple_element>::type; return DefaultHash::hash(std::get(t)); } template static typename std::enable_if::type equal(const std::tuple& a, const std::tuple& b) { using IthTupleElementType = typename std::tuple_element>::type; return DefaultHash::equal(std::get(a), std::get(b)) && equal(a, b); } template static typename std::enable_if::type equal(const std::tuple& a, const std::tuple& b) { using IthTupleElementType = typename std::tuple_element>::type; return DefaultHash::equal(std::get(a), std::get(b)); } // We should use safeToCompareToEmptyOrDeleted = DefaultHash::safeToCompareToEmptyOrDeleted &&... whenever // we switch to C++17. We can't do anything better here right now because GCC can't do C++. template static constexpr bool allTrue(BoolType value) { return value; } template static constexpr bool allTrue(BoolType value, BoolTypes... values) { return value && allTrue(values...); } static constexpr bool safeToCompareToEmptyOrDeleted = allTrue(DefaultHash::safeToCompareToEmptyOrDeleted...); }; // make IntHash the default hash function for many integer types template<> struct DefaultHash : IntHash { }; template<> struct DefaultHash : IntHash { }; template<> struct DefaultHash : IntHash { }; template<> struct DefaultHash : IntHash { }; template<> struct DefaultHash : IntHash { }; template<> struct DefaultHash : IntHash { }; template<> struct DefaultHash : IntHash { }; template<> struct DefaultHash : IntHash { }; template<> struct DefaultHash : IntHash { }; template<> struct DefaultHash : IntHash { }; #if defined(_NATIVE_WCHAR_T_DEFINED) template<> struct DefaultHash : IntHash { }; #endif template<> struct DefaultHash : FloatHash { }; template<> struct DefaultHash : FloatHash { }; // make PtrHash the default hash function for pointer types that don't specialize template struct DefaultHash : PtrHash { }; template struct DefaultHash> : PtrHash> { }; template struct DefaultHash> : PtrHash> { }; template struct DefaultHash> : PtrHash> { }; #ifdef __OBJC__ template<> struct DefaultHash<__unsafe_unretained id> : PtrHash<__unsafe_unretained id> { }; #endif // make IntPairHash the default hash function for pairs of (at most) 32-bit integers. template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; template<> struct DefaultHash> : IntPairHash { }; // make PairHash the default hash function for pairs of arbitrary values. template struct DefaultHash> : PairHash { }; template struct DefaultHash> : TupleHash { }; } // namespace WTF using WTF::DefaultHash; using WTF::IntHash; using WTF::PtrHash; using WTF::intHash; using WTF::pairIntHash;