source: webkit/trunk/JavaScriptCore/wtf/HashFunctions.h@ 34424

Last change on this file since 34424 was 32650, checked in by Darin Adler, 17 years ago

2008-04-28 Darin Adler <Darin Adler>

Reviewed by Adam.

  • make sure RefPtr's default hash doesn't ref/deref when computing the hash
  • remove remnants of the hash table storage type optimization
  • wtf/HashFunctions.h: Used "using" to get the hash and equal functions from PtrHash<P*> into PtrHash<RefPtr<P>>.
  • wtf/HashMap.h: Replaced uses of PairBaseHashTraits with PairHashTraits. Eliminated storage-related typedefs. Removed constructor, destructor, copy constructor, and destructor since the compiler-generated ones are fine. Removed refAll and derefAll. Took out unnnecessary typecasts. Removed use of RefCounter.
  • wtf/HashSet.h: Eliminated storage-related typedefs. Removed constructor, destructor, copy constructor, and destructor since the compiler-generated ones are fine. Removed refAll and derefAll. Removed unneeded template arguents from HashSetTranslatorAdapter. Eliminated unneeded HashSetTranslator template.
  • wtf/HashTable.h: Tweaked formatting. Removed NeedsRef, RefCounterBase, RefCounter, HashTableRefCounterBase, HashTableRefCounter, and Assigner class templates.
  • wtf/HashTraits.h: Removed StorageTraits, needsRef, PairBaseHashTraits, and HashKeyStorageTraits.
  • wtf/RefPtrHashMap.h: Made all the same fixes as in HashMap. Also made the corresponding changes to RefPtrHashMapRawKeyTranslator.
  • Property svn:eol-style set to native
File size: 6.7 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifndef WTF_HashFunctions_h
23#define WTF_HashFunctions_h
24
25#include "RefPtr.h"
26#include <stdint.h>
27
28namespace WTF {
29
30 template<size_t size> struct IntTypes;
31 template<> struct IntTypes<1> { typedef int8_t SignedType; typedef uint8_t UnsignedType; };
32 template<> struct IntTypes<2> { typedef int16_t SignedType; typedef uint16_t UnsignedType; };
33 template<> struct IntTypes<4> { typedef int32_t SignedType; typedef uint32_t UnsignedType; };
34 template<> struct IntTypes<8> { typedef int64_t SignedType; typedef uint64_t UnsignedType; };
35
36 // integer hash function
37
38 // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
39 inline unsigned intHash(uint8_t key8)
40 {
41 unsigned key = key8;
42 key += ~(key << 15);
43 key ^= (key >> 10);
44 key += (key << 3);
45 key ^= (key >> 6);
46 key += ~(key << 11);
47 key ^= (key >> 16);
48 return key;
49 }
50
51 // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
52 inline unsigned intHash(uint16_t key16)
53 {
54 unsigned key = key16;
55 key += ~(key << 15);
56 key ^= (key >> 10);
57 key += (key << 3);
58 key ^= (key >> 6);
59 key += ~(key << 11);
60 key ^= (key >> 16);
61 return key;
62 }
63
64 // Thomas Wang's 32 Bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
65 inline unsigned intHash(uint32_t key)
66 {
67 key += ~(key << 15);
68 key ^= (key >> 10);
69 key += (key << 3);
70 key ^= (key >> 6);
71 key += ~(key << 11);
72 key ^= (key >> 16);
73 return key;
74 }
75
76 // Thomas Wang's 64 bit Mix Function: http://www.cris.com/~Ttwang/tech/inthash.htm
77 inline unsigned intHash(uint64_t key)
78 {
79 key += ~(key << 32);
80 key ^= (key >> 22);
81 key += ~(key << 13);
82 key ^= (key >> 8);
83 key += (key << 3);
84 key ^= (key >> 15);
85 key += ~(key << 27);
86 key ^= (key >> 31);
87 return static_cast<unsigned>(key);
88 }
89
90 template<typename T> struct IntHash {
91 static unsigned hash(T key) { return intHash(static_cast<typename IntTypes<sizeof(T)>::UnsignedType>(key)); }
92 static bool equal(T a, T b) { return a == b; }
93 static const bool safeToCompareToEmptyOrDeleted = true;
94 };
95
96 template<typename T> struct FloatHash {
97 static unsigned hash(T key) { return intHash(*reinterpret_cast<typename IntTypes<sizeof(T)>::UnsignedType*>(&key)); }
98 static bool equal(T a, T b) { return a == b; }
99 static const bool safeToCompareToEmptyOrDeleted = true;
100 };
101
102 // pointer identity hash function
103
104 template<typename T> struct PtrHash {
105 static unsigned hash(T key)
106 {
107#if COMPILER(MSVC)
108#pragma warning(push)
109#pragma warning(disable: 4244) // work around what seems to be a bug in MSVC's conversion warnings
110#endif
111 return IntHash<uintptr_t>::hash(reinterpret_cast<uintptr_t>(key));
112#if COMPILER(MSVC)
113#pragma warning(pop)
114#endif
115 }
116 static bool equal(T a, T b) { return a == b; }
117 static const bool safeToCompareToEmptyOrDeleted = true;
118 };
119 template<typename P> struct PtrHash<RefPtr<P> > : PtrHash<P*> {
120 using PtrHash<P*>::hash;
121 static unsigned hash(const RefPtr<P>& key) { return hash(key.get()); }
122 using PtrHash<P*>::equal;
123 static bool equal(const RefPtr<P>& a, const RefPtr<P>& b) { return a == b; }
124 static bool equal(P* a, const RefPtr<P>& b) { return a == b; }
125 static bool equal(const RefPtr<P>& a, P* b) { return a == b; }
126 };
127
128 // default hash function for each type
129
130 template<typename T> struct DefaultHash;
131
132 template<typename T, typename U> struct PairHash {
133 static unsigned hash(const std::pair<T, U>& p)
134 {
135 return intHash((static_cast<uint64_t>(DefaultHash<T>::Hash::hash(p.first)) << 32 | DefaultHash<U>::Hash::hash(p.second)));
136 }
137 static bool equal(const std::pair<T, U>& a, const std::pair<T, U>& b)
138 {
139 return DefaultHash<T>::Hash::equal(a.first, b.first) && DefaultHash<U>::Hash::equal(a.second, b.second);
140 }
141 static const bool safeToCompareToEmptyOrDeleted = DefaultHash<T>::Hash::safeToCompareToEmptyOrDeleted
142 && DefaultHash<U>::Hash::safeToCompareToEmptyOrDeleted;
143 };
144
145 // make IntHash the default hash function for many integer types
146
147 template<> struct DefaultHash<short> { typedef IntHash<unsigned> Hash; };
148 template<> struct DefaultHash<unsigned short> { typedef IntHash<unsigned> Hash; };
149 template<> struct DefaultHash<int> { typedef IntHash<unsigned> Hash; };
150 template<> struct DefaultHash<unsigned> { typedef IntHash<unsigned> Hash; };
151 template<> struct DefaultHash<long> { typedef IntHash<unsigned long> Hash; };
152 template<> struct DefaultHash<unsigned long> { typedef IntHash<unsigned long> Hash; };
153 template<> struct DefaultHash<long long> { typedef IntHash<unsigned long long> Hash; };
154 template<> struct DefaultHash<unsigned long long> { typedef IntHash<unsigned long long> Hash; };
155
156#if !COMPILER(MSVC) || defined(_NATIVE_WCHAR_T_DEFINED)
157 template<> struct DefaultHash<wchar_t> { typedef IntHash<wchar_t> Hash; };
158#endif
159
160 template<> struct DefaultHash<float> { typedef FloatHash<float> Hash; };
161 template<> struct DefaultHash<double> { typedef FloatHash<double> Hash; };
162
163 // make PtrHash the default hash function for pointer types that don't specialize
164
165 template<typename P> struct DefaultHash<P*> { typedef PtrHash<P*> Hash; };
166 template<typename P> struct DefaultHash<RefPtr<P> > { typedef PtrHash<RefPtr<P> > Hash; };
167
168 template<typename T, typename U> struct DefaultHash<std::pair<T, U> > { typedef PairHash<T, U> Hash; };
169
170} // namespace WTF
171
172using WTF::DefaultHash;
173using WTF::IntHash;
174using WTF::PtrHash;
175
176#endif // WTF_HashFunctions_h
Note: See TracBrowser for help on using the repository browser.