1 # Copyright (C) 2013 Google, Inc.
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Library General Public
5 # License as published by the Free Software Foundation; either
6 # version 2 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Library General Public License for more details.
13 # You should have received a copy of the GNU Library General Public License
14 # along with this library; see the file COPYING.LIB. If not, write to
15 # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 # Boston, MA 02110-1301, USA.
18 # This implementaiton of SuperFastHash is based on the Python implementation
19 # by Victor Perron at <https://github.com/vperron/python-superfasthash>.
20 # We've modified Victor's version to output hash values that match WTFString,
21 # which involves using a specific seed and some different constants.
24 def __rshift__(self
, other
):
25 return uint32_t(long.__rshift
__(self
, other
) & ((1L << 32) - 1))
27 def __lshift__(self
, other
):
28 return uint32_t(long.__lshift
__(self
, other
) & ((1L << 32) - 1))
30 def __add__(self
, other
):
31 return uint32_t(long.__add
__(self
, other
) & ((1L << 32) - 1))
33 def __xor__(self
, other
):
34 return uint32_t(long.__xor
__(self
, other
) & ((1L << 32) - 1))
39 Stream-adapted SuperFastHash algorithm from Paul Hsieh,
40 http://www.azillionmonkeys.com/qed/hash.html
42 Python version with no dependencies.
43 Victor Perron <victor@iso3103.net>
49 result
= uint32_t(0x9E3779B9L
)
51 remainder
= length
& 1
57 result
+= ord(string
[i
])
58 temp
= (ord(string
[i
+ 1]) << 11) ^ result
59 result
= (result
<< 16) ^ temp
61 result
+= (result
>> 11)
64 result
+= ord(string
[i
])
65 result ^
= (result
<< 11)
66 result
+= (result
>> 17)
68 # Force "avalanching" of final 127 bits
69 result ^
= (result
<< 3)
70 result
+= (result
>> 5)
71 result ^
= (result
<< 2)
72 result
+= (result
>> 15)
73 result ^
= (result
<< 10)
75 # Save 8 bits for StringImpl to use as flags.
78 # This avoids ever returning a hash code of 0, since that is used to
79 # signal "hash not computed yet".