author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 14 May 2010 16:40:13 +0300 | |
changeset 22 | 79de32ba3296 |
parent 0 | 1918ee327afb |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved |
|
22
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
* Copyright (C) Research In Motion Limited 2009. All rights reserved. |
0 | 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 StringHash_h |
|
23 |
#define StringHash_h |
|
24 |
||
25 |
#include "AtomicString.h" |
|
26 |
#include "PlatformString.h" |
|
27 |
#include <wtf/HashFunctions.h> |
|
28 |
#include <wtf/HashTraits.h> |
|
29 |
#include <wtf/unicode/Unicode.h> |
|
30 |
||
31 |
namespace WebCore { |
|
32 |
||
33 |
// FIXME: We should really figure out a way to put the computeHash function that's |
|
34 |
// currently a member function of StringImpl into this file so we can be a little |
|
35 |
// closer to having all the nearly-identical hash functions in one place. |
|
36 |
||
37 |
struct StringHash { |
|
38 |
static unsigned hash(StringImpl* key) { return key->hash(); } |
|
39 |
static bool equal(StringImpl* a, StringImpl* b) |
|
40 |
{ |
|
41 |
if (a == b) |
|
42 |
return true; |
|
43 |
if (!a || !b) |
|
44 |
return false; |
|
45 |
||
46 |
unsigned aLength = a->length(); |
|
47 |
unsigned bLength = b->length(); |
|
48 |
if (aLength != bLength) |
|
49 |
return false; |
|
50 |
||
22
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
51 |
#if PLATFORM(ARM) || PLATFORM(SH4) |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
52 |
const UChar* aChars = a->characters(); |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
53 |
const UChar* bChars = b->characters(); |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
54 |
for (unsigned i = 0; i != aLength; ++i) { |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
55 |
if (*aChars++ != *bChars++) |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
56 |
return false; |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
57 |
} |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
58 |
return true; |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
59 |
#else |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
60 |
/* Do it 4-bytes-at-a-time on architectures where it's safe */ |
0 | 61 |
const uint32_t* aChars = reinterpret_cast<const uint32_t*>(a->characters()); |
62 |
const uint32_t* bChars = reinterpret_cast<const uint32_t*>(b->characters()); |
|
63 |
||
64 |
unsigned halfLength = aLength >> 1; |
|
65 |
for (unsigned i = 0; i != halfLength; ++i) |
|
66 |
if (*aChars++ != *bChars++) |
|
67 |
return false; |
|
68 |
||
69 |
if (aLength & 1 && *reinterpret_cast<const uint16_t*>(aChars) != *reinterpret_cast<const uint16_t*>(bChars)) |
|
70 |
return false; |
|
71 |
||
72 |
return true; |
|
22
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
73 |
#endif |
0 | 74 |
} |
75 |
||
76 |
static unsigned hash(const RefPtr<StringImpl>& key) { return key->hash(); } |
|
77 |
static bool equal(const RefPtr<StringImpl>& a, const RefPtr<StringImpl>& b) |
|
78 |
{ |
|
79 |
return equal(a.get(), b.get()); |
|
80 |
} |
|
81 |
||
82 |
static unsigned hash(const String& key) { return key.impl()->hash(); } |
|
83 |
static bool equal(const String& a, const String& b) |
|
84 |
{ |
|
85 |
return equal(a.impl(), b.impl()); |
|
86 |
} |
|
87 |
||
88 |
static const bool safeToCompareToEmptyOrDeleted = false; |
|
89 |
}; |
|
90 |
||
91 |
class CaseFoldingHash { |
|
92 |
public: |
|
93 |
// Paul Hsieh's SuperFastHash |
|
94 |
// http://www.azillionmonkeys.com/qed/hash.html |
|
95 |
static unsigned hash(const UChar* data, unsigned length) |
|
96 |
{ |
|
97 |
unsigned l = length; |
|
98 |
const UChar* s = data; |
|
99 |
uint32_t hash = WTF::stringHashingStartValue; |
|
100 |
uint32_t tmp; |
|
101 |
||
102 |
int rem = l & 1; |
|
103 |
l >>= 1; |
|
104 |
||
105 |
// Main loop. |
|
106 |
for (; l > 0; l--) { |
|
107 |
hash += WTF::Unicode::foldCase(s[0]); |
|
108 |
tmp = (WTF::Unicode::foldCase(s[1]) << 11) ^ hash; |
|
109 |
hash = (hash << 16) ^ tmp; |
|
110 |
s += 2; |
|
111 |
hash += hash >> 11; |
|
112 |
} |
|
113 |
||
114 |
// Handle end case. |
|
115 |
if (rem) { |
|
116 |
hash += WTF::Unicode::foldCase(s[0]); |
|
117 |
hash ^= hash << 11; |
|
118 |
hash += hash >> 17; |
|
119 |
} |
|
120 |
||
121 |
// Force "avalanching" of final 127 bits. |
|
122 |
hash ^= hash << 3; |
|
123 |
hash += hash >> 5; |
|
124 |
hash ^= hash << 2; |
|
125 |
hash += hash >> 15; |
|
126 |
hash ^= hash << 10; |
|
127 |
||
128 |
// This avoids ever returning a hash code of 0, since that is used to |
|
129 |
// signal "hash not computed yet", using a value that is likely to be |
|
130 |
// effectively the same as 0 when the low bits are masked. |
|
131 |
hash |= !hash << 31; |
|
132 |
||
133 |
return hash; |
|
134 |
} |
|
135 |
||
136 |
static unsigned hash(StringImpl* str) |
|
137 |
{ |
|
138 |
return hash(str->characters(), str->length()); |
|
139 |
} |
|
140 |
||
141 |
static unsigned hash(const char* str, unsigned length) |
|
142 |
{ |
|
143 |
// This hash is designed to work on 16-bit chunks at a time. But since the normal case |
|
144 |
// (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they |
|
145 |
// were 16-bit chunks, which will give matching results. |
|
146 |
||
147 |
unsigned l = length; |
|
148 |
const char* s = str; |
|
149 |
uint32_t hash = WTF::stringHashingStartValue; |
|
150 |
uint32_t tmp; |
|
151 |
||
152 |
int rem = l & 1; |
|
153 |
l >>= 1; |
|
154 |
||
155 |
// Main loop |
|
156 |
for (; l > 0; l--) { |
|
157 |
hash += WTF::Unicode::foldCase(s[0]); |
|
158 |
tmp = (WTF::Unicode::foldCase(s[1]) << 11) ^ hash; |
|
159 |
hash = (hash << 16) ^ tmp; |
|
160 |
s += 2; |
|
161 |
hash += hash >> 11; |
|
162 |
} |
|
163 |
||
164 |
// Handle end case |
|
165 |
if (rem) { |
|
166 |
hash += WTF::Unicode::foldCase(s[0]); |
|
167 |
hash ^= hash << 11; |
|
168 |
hash += hash >> 17; |
|
169 |
} |
|
170 |
||
171 |
// Force "avalanching" of final 127 bits |
|
172 |
hash ^= hash << 3; |
|
173 |
hash += hash >> 5; |
|
174 |
hash ^= hash << 2; |
|
175 |
hash += hash >> 15; |
|
176 |
hash ^= hash << 10; |
|
177 |
||
178 |
// this avoids ever returning a hash code of 0, since that is used to |
|
179 |
// signal "hash not computed yet", using a value that is likely to be |
|
180 |
// effectively the same as 0 when the low bits are masked |
|
181 |
hash |= !hash << 31; |
|
182 |
||
183 |
return hash; |
|
184 |
} |
|
185 |
||
186 |
static bool equal(StringImpl* a, StringImpl* b) |
|
187 |
{ |
|
188 |
if (a == b) |
|
189 |
return true; |
|
190 |
if (!a || !b) |
|
191 |
return false; |
|
192 |
unsigned length = a->length(); |
|
193 |
if (length != b->length()) |
|
194 |
return false; |
|
195 |
return WTF::Unicode::umemcasecmp(a->characters(), b->characters(), length) == 0; |
|
196 |
} |
|
197 |
||
198 |
static unsigned hash(const RefPtr<StringImpl>& key) |
|
199 |
{ |
|
200 |
return hash(key.get()); |
|
201 |
} |
|
202 |
||
203 |
static bool equal(const RefPtr<StringImpl>& a, const RefPtr<StringImpl>& b) |
|
204 |
{ |
|
205 |
return equal(a.get(), b.get()); |
|
206 |
} |
|
207 |
||
208 |
static unsigned hash(const String& key) |
|
209 |
{ |
|
210 |
return hash(key.impl()); |
|
211 |
} |
|
212 |
static unsigned hash(const AtomicString& key) |
|
213 |
{ |
|
214 |
return hash(key.impl()); |
|
215 |
} |
|
216 |
static bool equal(const String& a, const String& b) |
|
217 |
{ |
|
218 |
return equal(a.impl(), b.impl()); |
|
219 |
} |
|
220 |
static bool equal(const AtomicString& a, const AtomicString& b) |
|
221 |
{ |
|
222 |
return (a == b) || equal(a.impl(), b.impl()); |
|
223 |
} |
|
224 |
||
225 |
static const bool safeToCompareToEmptyOrDeleted = false; |
|
226 |
}; |
|
227 |
||
228 |
// This hash can be used in cases where the key is a hash of a string, but we don't |
|
229 |
// want to store the string. It's not really specific to string hashing, but all our |
|
230 |
// current uses of it are for strings. |
|
231 |
struct AlreadyHashed : IntHash<unsigned> { |
|
232 |
static unsigned hash(unsigned key) { return key; } |
|
233 |
||
234 |
// To use a hash value as a key for a hash table, we need to eliminate the |
|
235 |
// "deleted" value, which is negative one. That could be done by changing |
|
236 |
// the string hash function to never generate negative one, but this works |
|
237 |
// and is still relatively efficient. |
|
238 |
static unsigned avoidDeletedValue(unsigned hash) |
|
239 |
{ |
|
240 |
ASSERT(hash); |
|
241 |
unsigned newHash = hash | (!(hash + 1) << 31); |
|
242 |
ASSERT(newHash); |
|
243 |
ASSERT(newHash != 0xFFFFFFFF); |
|
244 |
return newHash; |
|
245 |
} |
|
246 |
}; |
|
247 |
||
248 |
} |
|
249 |
||
250 |
namespace WTF { |
|
251 |
||
252 |
template<> struct HashTraits<WebCore::String> : GenericHashTraits<WebCore::String> { |
|
253 |
static const bool emptyValueIsZero = true; |
|
254 |
static void constructDeletedValue(WebCore::String& slot) { new (&slot) WebCore::String(HashTableDeletedValue); } |
|
255 |
static bool isDeletedValue(const WebCore::String& slot) { return slot.isHashTableDeletedValue(); } |
|
256 |
}; |
|
257 |
||
258 |
} |
|
259 |
||
260 |
#endif |