]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/StringHashFunctions.h
2 * Copyright (C) 2005, 2006, 2008, 2010 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.
20 #ifndef WTF_StringHashFunctions_h
21 #define WTF_StringHashFunctions_h
23 #include <wtf/unicode/Unicode.h>
27 // Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
28 static const unsigned stringHashingStartValue
= 0x9e3779b9U
;
30 // stringHash methods based on Paul Hsieh's SuperFastHash.
31 // http://www.azillionmonkeys.com/qed/hash.html
32 // char* data is interpreted as latin-encoded (zero extended to 16 bits).
34 inline unsigned stringHash(const UChar
* data
, unsigned length
)
36 unsigned hash
= WTF::stringHashingStartValue
;
37 unsigned rem
= length
& 1;
41 for (; length
> 0; length
--) {
43 unsigned tmp
= (data
[1] << 11) ^ hash
;
44 hash
= (hash
<< 16) ^ tmp
;
56 // Force "avalanching" of final 127 bits
65 // this avoids ever returning a hash code of 0, since that is used to
66 // signal "hash not computed yet", using a value that is likely to be
67 // effectively the same as 0 when the low bits are masked
74 inline unsigned stringHash(const char* data
, unsigned length
)
76 unsigned hash
= WTF::stringHashingStartValue
;
77 unsigned rem
= length
& 1;
81 for (; length
> 0; length
--) {
82 hash
+= static_cast<unsigned char>(data
[0]);
83 unsigned tmp
= (static_cast<unsigned char>(data
[1]) << 11) ^ hash
;
84 hash
= (hash
<< 16) ^ tmp
;
91 hash
+= static_cast<unsigned char>(data
[0]);
96 // Force "avalanching" of final 127 bits
105 // this avoids ever returning a hash code of 0, since that is used to
106 // signal "hash not computed yet", using a value that is likely to be
107 // effectively the same as 0 when the low bits are masked
114 inline unsigned stringHash(const char* data
)
116 unsigned hash
= WTF::stringHashingStartValue
;
120 unsigned char b0
= data
[0];
123 unsigned char b1
= data
[1];
131 unsigned tmp
= (b1
<< 11) ^ hash
;
132 hash
= (hash
<< 16) ^ tmp
;
137 // Force "avalanching" of final 127 bits.
146 // This avoids ever returning a hash code of 0, since that is used to
147 // signal "hash not computed yet", using a value that is likely to be
148 // effectively the same as 0 when the low bits are masked.
157 #endif // WTF_StringHashFunctions_h