blob: 8621081c8da4f71a59fa7ed438d70fae030b5732 [file] [log] [blame]
Jeff Thompsona28eed82013-08-22 16:21:10 -07001
2[/ Copyright 2011 Daniel James.
3 / Distributed under the Boost Software License, Version 1.0. (See accompanying
4 / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) ]
5
6[section:rationale Rationale]
7
8The rationale can be found in the original design
9[footnote issue 6.18 of the __issues__ (page 63)].
10
11[heading Quality of the hash function]
12
13Many hash functions strive to have little correlation between the input
14and output values. They attempt to uniformally distribute the output
15values for very similar inputs. This hash function makes no such
16attempt. In fact, for integers, the result of the hash function is often
17just the input value. So similar but different input values will often
18result in similar but different output values.
19This means that it is not appropriate as a general hash function. For
20example, a hash table may discard bits from the hash function resulting
21in likely collisions, or might have poor collision resolution when hash
22values are clustered together. In such cases this hash function will
23preform poorly.
24
25But the standard has no such requirement for the hash function,
26it just requires that the hashes of two different values are unlikely
27to collide. Containers or algorithms
28designed to work with the standard hash function will have to be
29implemented to work well when the hash function's output is correlated
30to its input. Since they are paying that cost a higher quality hash function
31would be wasteful.
32
33For other use cases, if you do need a higher quality hash function,
34then neither the standard hash function or `boost::hash` are appropriate.
35There are several options
36available. One is to use a second hash on the output of this hash
37function, such as [@http://www.concentric.net/~ttwang/tech/inthash.htm
38Thomas Wang's hash function]. This this may not work as
39well as a hash algorithm tailored for the input.
40
41For strings there are several fast, high quality hash functions
42available (for example [@http://code.google.com/p/smhasher/ MurmurHash3]
43and [@http://code.google.com/p/cityhash/ Google's CityHash]),
44although they tend to be more machine specific.
45These may also be appropriate for hashing a binary representation of
46your data - providing that all equal values have an equal
47representation, which is not always the case (e.g. for floating point
48values).
49
50[endsect]