From 40736c5763bf61337c8c14e16d8587db021a87d4 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Fri, 6 Jan 2012 14:44:00 +0100 Subject: Imported WebKit commit 2ea9d364d0f6efa8fa64acf19f451504c59be0e4 (http://svn.webkit.org/repository/webkit/trunk@104285) --- Source/JavaScriptCore/wtf/HashFunctions.h | 183 ++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 Source/JavaScriptCore/wtf/HashFunctions.h (limited to 'Source/JavaScriptCore/wtf/HashFunctions.h') diff --git a/Source/JavaScriptCore/wtf/HashFunctions.h b/Source/JavaScriptCore/wtf/HashFunctions.h new file mode 100644 index 000000000..2c66a2d9f --- /dev/null +++ b/Source/JavaScriptCore/wtf/HashFunctions.h @@ -0,0 +1,183 @@ +/* + * 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 + * 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. + * + */ + +#ifndef WTF_HashFunctions_h +#define WTF_HashFunctions_h + +#include "RefPtr.h" +#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); + } + + 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 const bool safeToCompareToEmptyOrDeleted = true; + }; + + template struct FloatHash { + 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; + }; + + // pointer identity hash function + + template struct PtrHash { + static unsigned hash(T key) + { +#if COMPILER(MSVC) +#pragma warning(push) +#pragma warning(disable: 4244) // work around what seems to be a bug in MSVC's conversion warnings +#endif + return IntHash::hash(reinterpret_cast(key)); +#if COMPILER(MSVC) +#pragma warning(pop) +#endif + } + static bool equal(T a, T b) { return a == b; } + static const bool safeToCompareToEmptyOrDeleted = true; + }; + 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 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; }; + template<> struct DefaultHash { typedef IntHash Hash; }; + 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; }; + + // make PtrHash the default hash function for pointer types that don't specialize + + template struct DefaultHash { typedef PtrHash Hash; }; + template struct DefaultHash > { typedef PtrHash > Hash; }; + + template struct DefaultHash > { typedef PairHash Hash; }; + +} // namespace WTF + +using WTF::DefaultHash; +using WTF::IntHash; +using WTF::PtrHash; + +#endif // WTF_HashFunctions_h -- cgit v1.2.1