1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! From http://www.cse.yorku.ca/~oz/hash.html.
//!
//! > A comprehensive collection of hash functions, a hash visualiser
//! > and some test results [see Mckenzie et al. Selecting a Hashing
//! > Algorithm, SP&E 20(2):209-224, Feb 1990] will be available
//! > someday. If you just want to have a good hash function, and cannot
//! > wait, djb2 is one of the best string hash functions i know. it has
//! > excellent distribution and speed on many different sets of keys
//! > and table sizes. you are not likely to do better with one of the
//! > "well known" functions such as PJW, K&R, etc. Also see tpop
//! > pp. 126 for graphing hash functions.
//!
//! "tpop" is *The Practice of Programming*. This page shows three
//! classic hashing algorithms.
use Hasher;
use Wrapping;
// ====================================
// DJB2
/// Dan Bernstein's famous hashing function.
///
/// This Hasher is allegedly good for small tables with lowercase
/// ASCII keys. It is also dirt-simple, although other hash
/// functions are better and almost as simple.
///
/// From http://www.cse.yorku.ca/~oz/hash.html:
///
/// > this algorithm (k=33) was first reported by dan bernstein many
/// > years ago in comp.lang.c. another version of this algorithm (now
/// > favored by bernstein) uses xor: `hash(i) = hash(i - 1) * 33 ^
/// > str[i];` the magic of number 33 (why it works better than many
/// > other constants, prime or not)
/// > has never been adequately explained.
///
/// From http://www.burtleburtle.net/bob/hash/doobs.html:
///
/// > If your keys are lowercase English words, this will fit 6
/// > characters into a 32-bit hash with no collisions (you'd
/// > have to compare all 32 bits). If your keys are mixed case
/// > English words, `65 * hash+key[i]` fits 5 characters into a 32-bit
/// > hash with no collisions. That means this type of hash can
/// > produce (for the right type of keys) fewer collisions than
/// > a hash that gives a more truly random distribution. If your
/// > platform doesn't have fast multiplies, no sweat, 33 * hash =
/// > hash+(hash<<5) and most compilers will figure that out for
/// > you.
/// >
/// > On the down side, if you don't have short text keys, this hash
/// > has a easily detectable flaws. For example, there's a 3-into-2
/// > funnel that 0x0021 and 0x0100 both have the same hash (hex
/// > 0x21, decimal 33) (you saw that one coming, yes?).
;
default_for_constant!;
hasher_to_fcn!;
// ------------------------------------
// ====================================
// sdbm
/// The hash function from SDBM (and gawk?). It might be good for
/// something.
///
/// From http://www.cse.yorku.ca/~oz/hash.html:
///
/// > this algorithm was created for sdbm (a public-domain
/// > reimplementation of ndbm) database library. it was found
/// > to do well in scrambling bits, causing better distribution
/// > of the keys and fewer splits. it also happens to be a good
/// > general hashing function with good distribution. the actual
/// > function is `hash(i) = hash(i - 1) * 65599 + str[i];` what is
/// > included below is the faster version used in gawk. [there is
/// > even a faster, duff-device version] the magic constant 65599
/// > was picked out of thin air while experimenting with different
/// > constants, and turns out to be a prime. this is one of the
/// > algorithms used in berkeley db (see sleepycat) and elsewhere.
;
default_for_constant!;
hasher_to_fcn!;
// ------------------------------------
// ====================================
// lose_lose
/// A radically bad hash function from the 1st edition of the K&R C
/// book. Do not use; it's horrible.
///
/// From http://www.cse.yorku.ca/~oz/hash.html
///
/// > This hash function appeared in K&R (1st ed) but at least the
/// > reader was warned: "This is not the best possible algorithm,
/// > but it has the merit of extreme simplicity." This is an
/// > understatement; It is a terrible hashing algorithm, and it
/// > could have been much better without sacrificing its "extreme
/// > simplicity." [see the second edition!] Many C programmers
/// > use this function without actually testing it, or checking
/// > something like Knuth's Sorting and Searching, so it stuck. It
/// > is now found mixed with otherwise respectable code, eg. cnews.
/// > sigh. [see also: tpop]
;
default_for_constant!;
hasher_to_fcn!;
// ------------------------------------