X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/b37bf2e156556c589aea3e1f58a377f2b1189665..14957cd040308e3eeec43d26bae5d76da13fcd85:/wtf/HashFunctions.h diff --git a/wtf/HashFunctions.h b/wtf/HashFunctions.h index bf85dc0..2c66a2d 100644 --- a/wtf/HashFunctions.h +++ b/wtf/HashFunctions.h @@ -1,7 +1,5 @@ -// -*- mode: c++; c-basic-offset: 4 -*- /* - * This file is part of the KDE libraries - * Copyright (C) 2005, 2006 Apple Computer, Inc. + * Copyright (C) 2005, 2006, 2008 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 @@ -36,6 +34,32 @@ namespace WTF { // 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) { @@ -69,7 +93,15 @@ namespace WTF { }; template struct FloatHash { - static unsigned hash(T key) { return intHash(*reinterpret_cast::UnsignedType*>(&key)); } + static unsigned hash(T key) + { + union { + T key; + typename IntTypes::UnsignedType bits; + } u; + u.key = key; + return intHash(u.bits); + } static bool equal(T a, T b) { return a == b; } static const bool safeToCompareToEmptyOrDeleted = true; }; @@ -91,18 +123,36 @@ namespace WTF { static bool equal(T a, T b) { return a == b; } static const bool safeToCompareToEmptyOrDeleted = true; }; - template struct PtrHash > { - static unsigned hash(const RefPtr

& key) { return PtrHash::hash(key.get()); } + template struct PtrHash > : PtrHash { + using PtrHash::hash; + static unsigned hash(const RefPtr

& key) { return hash(key.get()); } + using PtrHash::equal; static bool equal(const RefPtr

& a, const RefPtr

& b) { return a == b; } - static const bool safeToCompareToEmptyOrDeleted = true; + static bool equal(P* a, const RefPtr

& b) { return a == b; } + static bool equal(const RefPtr

& a, P* b) { return a == b; } }; // default hash function for each type template struct DefaultHash; + template struct PairHash { + static unsigned hash(const std::pair& p) + { + return intHash((static_cast(DefaultHash::Hash::hash(p.first)) << 32 | DefaultHash::Hash::hash(p.second))); + } + static bool equal(const std::pair& a, const std::pair& b) + { + return DefaultHash::Hash::equal(a.first, b.first) && DefaultHash::Hash::equal(a.second, b.second); + } + static const bool safeToCompareToEmptyOrDeleted = DefaultHash::Hash::safeToCompareToEmptyOrDeleted + && DefaultHash::Hash::safeToCompareToEmptyOrDeleted; + }; + // make IntHash the default hash function for many integer types + template<> struct DefaultHash { typedef IntHash Hash; }; + template<> struct DefaultHash { typedef IntHash Hash; }; template<> struct DefaultHash { typedef IntHash Hash; }; template<> struct DefaultHash { typedef IntHash Hash; }; template<> struct DefaultHash { typedef IntHash Hash; }; @@ -110,6 +160,10 @@ namespace WTF { template<> struct DefaultHash { typedef IntHash Hash; }; template<> struct DefaultHash { typedef IntHash Hash; }; +#if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED) + template<> struct DefaultHash { typedef IntHash Hash; }; +#endif + template<> struct DefaultHash { typedef FloatHash Hash; }; template<> struct DefaultHash { typedef FloatHash Hash; }; @@ -118,6 +172,8 @@ namespace WTF { template struct DefaultHash { typedef PtrHash Hash; }; template struct DefaultHash > { typedef PtrHash > Hash; }; + template struct DefaultHash > { typedef PairHash Hash; }; + } // namespace WTF using WTF::DefaultHash;