rationale.qbk 2.3 KB

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