OLD | NEW |
1 # Copyright (C) 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org> | 1 # Copyright (C) 2005, 2006, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org> |
2 # Copyright (C) 2006 Anders Carlsson <andersca@mac.com> | 2 # Copyright (C) 2006 Anders Carlsson <andersca@mac.com> |
3 # Copyright (C) 2006, 2007 Samuel Weinig <sam@webkit.org> | 3 # Copyright (C) 2006, 2007 Samuel Weinig <sam@webkit.org> |
4 # Copyright (C) 2006 Alexey Proskuryakov <ap@webkit.org> | 4 # Copyright (C) 2006 Alexey Proskuryakov <ap@webkit.org> |
5 # Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. | 5 # Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. |
6 # Copyright (C) 2009 Cameron McCormack <cam@mcc.id.au> | 6 # Copyright (C) 2009 Cameron McCormack <cam@mcc.id.au> |
7 # Copyright (C) Research In Motion Limited 2010. All rights reserved. | 7 # Copyright (C) Research In Motion Limited 2010. All rights reserved. |
8 # Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) | 8 # Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) |
9 # Copyright (C) 2011 Patrick Gansterer <paroga@webkit.org> | 9 # Copyright (C) 2011 Patrick Gansterer <paroga@webkit.org> |
10 # Copyright (C) 2012 Ericsson AB. All rights reserved. | 10 # Copyright (C) 2012 Ericsson AB. All rights reserved. |
(...skipping 24 matching lines...) Expand all Loading... |
35 | 35 |
36 # Paul Hsieh's SuperFastHash | 36 # Paul Hsieh's SuperFastHash |
37 # http://www.azillionmonkeys.com/qed/hash.html | 37 # http://www.azillionmonkeys.com/qed/hash.html |
38 sub GenerateHashValue | 38 sub GenerateHashValue |
39 { | 39 { |
40 my @chars = split(/ */, $_[0]); | 40 my @chars = split(/ */, $_[0]); |
41 | 41 |
42 # This hash is designed to work on 16-bit chunks at a time. But since the no
rmal case | 42 # This hash is designed to work on 16-bit chunks at a time. But since the no
rmal case |
43 # (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if
they | 43 # (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if
they |
44 # were 16-bit chunks, which should give matching results | 44 # were 16-bit chunks, which should give matching results |
45 | 45 |
46 my $EXP2_32 = 4294967296; | 46 my $EXP2_32 = 4294967296; |
47 | 47 |
48 my $hash = 0x9e3779b9; | 48 my $hash = 0x9e3779b9; |
49 my $l = scalar @chars; #I wish this was in Ruby --- Maks | 49 my $l = scalar @chars; #I wish this was in Ruby --- Maks |
50 my $rem = $l & 1; | 50 my $rem = $l & 1; |
51 $l = $l >> 1; | 51 $l = $l >> 1; |
52 | 52 |
53 my $s = 0; | 53 my $s = 0; |
54 | 54 |
55 # Main loop | 55 # Main loop |
56 for (; $l > 0; $l--) { | 56 for (; $l > 0; $l--) { |
57 $hash += ord($chars[$s]); | 57 $hash += ord($chars[$s]); |
58 my $tmp = leftShift(ord($chars[$s+1]), 11) ^ $hash; | 58 my $tmp = leftShift(ord($chars[$s+1]), 11) ^ $hash; |
59 $hash = (leftShift($hash, 16)% $EXP2_32) ^ $tmp; | 59 $hash = (leftShift($hash, 16)% $EXP2_32) ^ $tmp; |
60 $s += 2; | 60 $s += 2; |
61 $hash += $hash >> 11; | 61 $hash += $hash >> 11; |
62 $hash %= $EXP2_32; | 62 $hash %= $EXP2_32; |
63 } | 63 } |
64 | 64 |
65 # Handle end case | 65 # Handle end case |
66 if ($rem != 0) { | 66 if ($rem != 0) { |
67 $hash += ord($chars[$s]); | 67 $hash += ord($chars[$s]); |
68 $hash ^= (leftShift($hash, 11)% $EXP2_32); | 68 $hash ^= (leftShift($hash, 11)% $EXP2_32); |
69 $hash += $hash >> 17; | 69 $hash += $hash >> 17; |
70 } | 70 } |
71 | 71 |
72 # Force "avalanching" of final 127 bits | 72 # Force "avalanching" of final 127 bits |
73 $hash ^= leftShift($hash, 3); | 73 $hash ^= leftShift($hash, 3); |
74 $hash += ($hash >> 5); | 74 $hash += ($hash >> 5); |
75 $hash = ($hash% $EXP2_32); | 75 $hash = ($hash% $EXP2_32); |
76 $hash ^= (leftShift($hash, 2)% $EXP2_32); | 76 $hash ^= (leftShift($hash, 2)% $EXP2_32); |
77 $hash += ($hash >> 15); | 77 $hash += ($hash >> 15); |
78 $hash = $hash% $EXP2_32; | 78 $hash = $hash% $EXP2_32; |
79 $hash ^= (leftShift($hash, 10)% $EXP2_32); | 79 $hash ^= (leftShift($hash, 10)% $EXP2_32); |
80 | 80 |
81 # Save 8 bits for StringImpl to use as flags. | 81 # Save 8 bits for StringImpl to use as flags. |
82 $hash &= 0xffffff; | 82 $hash &= 0xffffff; |
83 | 83 |
84 # This avoids ever returning a hash code of 0, since that is used to | 84 # This avoids ever returning a hash code of 0, since that is used to |
85 # signal "hash not computed yet". Setting the high bit maintains | 85 # signal "hash not computed yet". Setting the high bit maintains |
86 # reasonable fidelity to a hash code of 0 because it is likely to yield | 86 # reasonable fidelity to a hash code of 0 because it is likely to yield |
87 # exactly 0 when hash lookup masks out the high bits. | 87 # exactly 0 when hash lookup masks out the high bits. |
88 $hash = (0x80000000 >> 8) if ($hash == 0); | 88 $hash = (0x80000000 >> 8) if ($hash == 0); |
89 | 89 |
90 return $hash; | 90 return $hash; |
91 } | 91 } |
92 | 92 |
93 1; | 93 1; |
OLD | NEW |