1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Rationale</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../hash.html" title="Chapter 10. Boost.ContainerHash"> 10<link rel="prev" href="changes.html" title="Change Log"> 11<link rel="next" href="reference.html" title="Reference"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="changes.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../hash.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="hash.rationale"></a><a class="link" href="rationale.html" title="Rationale">Rationale</a> 29</h2></div></div></div> 30<p> 31 The rationale can be found in the original design <a href="#ftn.hash.rationale.f0" class="footnote" name="hash.rationale.f0"><sup class="footnote">[2]</sup></a>. 32 </p> 33<h4> 34<a name="hash.rationale.h0"></a> 35 <span class="phrase"><a name="hash.rationale.quality_of_the_hash_function"></a></span><a class="link" href="rationale.html#hash.rationale.quality_of_the_hash_function">Quality 36 of the hash function</a> 37 </h4> 38<p> 39 Many hash functions strive to have little correlation between the input and 40 output values. They attempt to uniformally distribute the output values for 41 very similar inputs. This hash function makes no such attempt. In fact, for 42 integers, the result of the hash function is often just the input value. So 43 similar but different input values will often result in similar but different 44 output values. This means that it is not appropriate as a general hash function. 45 For example, a hash table may discard bits from the hash function resulting 46 in likely collisions, or might have poor collision resolution when hash values 47 are clustered together. In such cases this hash function will preform poorly. 48 </p> 49<p> 50 But the standard has no such requirement for the hash function, it just requires 51 that the hashes of two different values are unlikely to collide. Containers 52 or algorithms designed to work with the standard hash function will have to 53 be implemented to work well when the hash function's output is correlated to 54 its input. Since they are paying that cost a higher quality hash function would 55 be wasteful. 56 </p> 57<p> 58 For other use cases, if you do need a higher quality hash function, then neither 59 the standard hash function or <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">hash</span></code> are 60 appropriate. There are several options available. One is to use a second hash 61 on the output of this hash function, such as <a href="http://web.archive.org/web/20121102023700/http://www.concentric.net/~Ttwang/tech/inthash.htm" target="_top">Thomas 62 Wang's hash function</a>. This this may not work as well as a hash algorithm 63 tailored for the input. 64 </p> 65<p> 66 For strings there are several fast, high quality hash functions available (for 67 example <a href="http://code.google.com/p/smhasher/" target="_top">MurmurHash3</a> 68 and <a href="http://code.google.com/p/cityhash/" target="_top">Google's CityHash</a>), 69 although they tend to be more machine specific. These may also be appropriate 70 for hashing a binary representation of your data - providing that all equal 71 values have an equal representation, which is not always the case (e.g. for 72 floating point values). 73 </p> 74<div class="footnotes"> 75<br><hr style="width:100; text-align:left;margin-left: 0"> 76<div id="ftn.hash.rationale.f0" class="footnote"><p><a href="#hash.rationale.f0" class="para"><sup class="para">[2] </sup></a> 77 issue 6.18 of the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1837.pdf" target="_top">Library 78 Extension Technical Report Issues List</a> (page 63) 79 </p></div> 80</div> 81</div> 82<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 83<td align="left"></td> 84<td align="right"><div class="copyright-footer">Copyright © 2005-2008 Daniel 85 James<p> 86 Distributed under the Boost Software License, Version 1.0. (See accompanying 87 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 88 </p> 89</div></td> 90</tr></table> 91<hr> 92<div class="spirit-nav"> 93<a accesskey="p" href="changes.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../hash.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="reference.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 94</div> 95</body> 96</html> 97