2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include "AtomicString.h"
25 #include "PlatformString.h"
26 #include <wtf/HashFunctions.h>
27 #include <wtf/HashTraits.h>
28 #include <wtf/unicode/Unicode.h>
32 // FIXME: We should really figure out a way to put the computeHash function that's
33 // currently a member function of StringImpl into this file so we can be a little
34 // closer to having all the nearly-identical hash functions in one place.
37 static unsigned hash(StringImpl
* key
) { return key
->hash(); }
38 static bool equal(StringImpl
* a
, StringImpl
* b
)
45 unsigned aLength
= a
->length();
46 unsigned bLength
= b
->length();
47 if (aLength
!= bLength
)
50 const uint32_t* aChars
= reinterpret_cast<const uint32_t*>(a
->characters());
51 const uint32_t* bChars
= reinterpret_cast<const uint32_t*>(b
->characters());
53 unsigned halfLength
= aLength
>> 1;
54 for (unsigned i
= 0; i
!= halfLength
; ++i
)
55 if (*aChars
++ != *bChars
++)
58 if (aLength
& 1 && *reinterpret_cast<const uint16_t*>(aChars
) != *reinterpret_cast<const uint16_t*>(bChars
))
64 static unsigned hash(const RefPtr
<StringImpl
>& key
) { return key
->hash(); }
65 static bool equal(const RefPtr
<StringImpl
>& a
, const RefPtr
<StringImpl
>& b
)
67 return equal(a
.get(), b
.get());
70 static unsigned hash(const String
& key
) { return key
.impl()->hash(); }
71 static bool equal(const String
& a
, const String
& b
)
73 return equal(a
.impl(), b
.impl());
76 static const bool safeToCompareToEmptyOrDeleted
= false;
79 class CaseFoldingHash
{
81 // Paul Hsieh's SuperFastHash
82 // http://www.azillionmonkeys.com/qed/hash.html
83 static unsigned hash(const UChar
* data
, unsigned length
)
86 const UChar
* s
= data
;
87 uint32_t hash
= WTF::stringHashingStartValue
;
95 hash
+= WTF::Unicode::foldCase(s
[0]);
96 tmp
= (WTF::Unicode::foldCase(s
[1]) << 11) ^ hash
;
97 hash
= (hash
<< 16) ^ tmp
;
104 hash
+= WTF::Unicode::foldCase(s
[0]);
109 // Force "avalanching" of final 127 bits.
116 // This avoids ever returning a hash code of 0, since that is used to
117 // signal "hash not computed yet", using a value that is likely to be
118 // effectively the same as 0 when the low bits are masked.
124 static unsigned hash(StringImpl
* str
)
126 return hash(str
->characters(), str
->length());
129 static unsigned hash(const char* str
, unsigned length
)
131 // This hash is designed to work on 16-bit chunks at a time. But since the normal case
132 // (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
133 // were 16-bit chunks, which will give matching results.
137 uint32_t hash
= WTF::stringHashingStartValue
;
145 hash
+= WTF::Unicode::foldCase(s
[0]);
146 tmp
= (WTF::Unicode::foldCase(s
[1]) << 11) ^ hash
;
147 hash
= (hash
<< 16) ^ tmp
;
154 hash
+= WTF::Unicode::foldCase(s
[0]);
159 // Force "avalanching" of final 127 bits
166 // this avoids ever returning a hash code of 0, since that is used to
167 // signal "hash not computed yet", using a value that is likely to be
168 // effectively the same as 0 when the low bits are masked
174 static bool equal(StringImpl
* a
, StringImpl
* b
)
180 unsigned length
= a
->length();
181 if (length
!= b
->length())
183 return WTF::Unicode::umemcasecmp(a
->characters(), b
->characters(), length
) == 0;
186 static unsigned hash(const RefPtr
<StringImpl
>& key
)
188 return hash(key
.get());
191 static bool equal(const RefPtr
<StringImpl
>& a
, const RefPtr
<StringImpl
>& b
)
193 return equal(a
.get(), b
.get());
196 static unsigned hash(const String
& key
)
198 return hash(key
.impl());
200 static unsigned hash(const AtomicString
& key
)
202 return hash(key
.impl());
204 static bool equal(const String
& a
, const String
& b
)
206 return equal(a
.impl(), b
.impl());
208 static bool equal(const AtomicString
& a
, const AtomicString
& b
)
210 return (a
== b
) || equal(a
.impl(), b
.impl());
213 static const bool safeToCompareToEmptyOrDeleted
= false;
216 // This hash can be used in cases where the key is a hash of a string, but we don't
217 // want to store the string. It's not really specific to string hashing, but all our
218 // current uses of it are for strings.
219 struct AlreadyHashed
: IntHash
<unsigned> {
220 static unsigned hash(unsigned key
) { return key
; }
222 // To use a hash value as a key for a hash table, we need to eliminate the
223 // "deleted" value, which is negative one. That could be done by changing
224 // the string hash function to never generate negative one, but this works
225 // and is still relatively efficient.
226 static unsigned avoidDeletedValue(unsigned hash
)
229 unsigned newHash
= hash
| (!(hash
+ 1) << 31);
231 ASSERT(newHash
!= 0xFFFFFFFF);
240 template<> struct HashTraits
<WebCore::String
> : GenericHashTraits
<WebCore::String
> {
241 static const bool emptyValueIsZero
= true;
242 static void constructDeletedValue(WebCore::String
& slot
) { new (&slot
) WebCore::String(HashTableDeletedValue
); }
244 static bool isDeletedValue(const WebCore::String
& slot
) { return slot
.isHashTableDeletedValue(); }