| [29] | 1 | <html> | 
|---|
 | 2 | <head> | 
|---|
 | 3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
|---|
 | 4 | <title> Combining hash values</title> | 
|---|
 | 5 | <link rel="stylesheet" href="../boostbook.css" type="text/css"> | 
|---|
 | 6 | <meta name="generator" content="DocBook XSL Stylesheets V1.68.1"> | 
|---|
 | 7 | <link rel="start" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> | 
|---|
 | 8 | <link rel="up" href="../hash.html" title="Chapter 7. Boost.Functional/Hash"> | 
|---|
 | 9 | <link rel="prev" href="custom.html" title=" Extending boost::hash for a custom data type"> | 
|---|
 | 10 | <link rel="next" href="portability.html" title=" Portability"> | 
|---|
 | 11 | </head> | 
|---|
 | 12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> | 
|---|
 | 13 | <table cellpadding="2" width="100%"> | 
|---|
 | 14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> | 
|---|
 | 15 | <td align="center"><a href="../../../index.htm">Home</a></td> | 
|---|
 | 16 | <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> | 
|---|
 | 17 | <td align="center"><a href="../../../people/people.htm">People</a></td> | 
|---|
 | 18 | <td align="center"><a href="../../../more/faq.htm">FAQ</a></td> | 
|---|
 | 19 | <td align="center"><a href="../../../more/index.htm">More</a></td> | 
|---|
 | 20 | </table> | 
|---|
 | 21 | <hr> | 
|---|
 | 22 | <div class="spirit-nav"> | 
|---|
 | 23 | <a accesskey="p" href="custom.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../hash.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="portability.html"><img src="../images/next.png" alt="Next"></a> | 
|---|
 | 24 | </div> | 
|---|
 | 25 | <div class="section" lang="en"> | 
|---|
 | 26 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> | 
|---|
 | 27 | <a name="hash.combine"></a> Combining hash values</h2></div></div></div> | 
|---|
 | 28 | <p> | 
|---|
 | 29 |       Say you have a point class, representing a two dimensional location: | 
|---|
 | 30 |     </p> | 
|---|
 | 31 | <pre class="programlisting"> | 
|---|
 | 32 | <span class="keyword">class</span> <span class="identifier">point</span> | 
|---|
 | 33 | <span class="special">{</span> | 
|---|
 | 34 |     <span class="keyword">int</span> <span class="identifier">x</span><span class="special">;</span> | 
|---|
 | 35 |     <span class="keyword">int</span> <span class="identifier">y</span><span class="special">;</span> | 
|---|
 | 36 | <span class="keyword">public</span><span class="special">:</span> | 
|---|
 | 37 |     <span class="identifier">point</span><span class="special">()</span> <span class="special">:</span> <span class="identifier">x</span><span class="special">(</span><span class="number">0</span><span class="special">),</span> <span class="identifier">y</span><span class="special">(</span><span class="number">0</span><span class="special">)</span> <span class="special">{}</span> | 
|---|
 | 38 |     <span class="identifier">point</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">x</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">y</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">x</span><span class="special">(</span><span class="identifier">x</span><span class="special">),</span> <span class="identifier">y</span><span class="special">(</span><span class="identifier">y</span><span class="special">)</span> <span class="special">{}</span> | 
|---|
 | 39 |  | 
|---|
 | 40 |     <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span><span class="identifier">point</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">other</span><span class="special">)</span> <span class="keyword">const</span> | 
|---|
 | 41 |     <span class="special">{</span> | 
|---|
 | 42 |         <span class="keyword">return</span> <span class="identifier">x</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">x</span> <span class="special">&&</span> <span class="identifier">y</span> <span class="special">==</span> <span class="identifier">other</span><span class="special">.</span><span class="identifier">y</span><span class="special">;</span> | 
|---|
 | 43 |     <span class="special">}</span> | 
|---|
 | 44 | <span class="special">};</span> | 
|---|
 | 45 | </pre> | 
|---|
 | 46 | <p> | 
|---|
 | 47 |       and you wish to use it as the key for an <code class="computeroutput"><span class="identifier">unordered_map</span></code>. | 
|---|
 | 48 |       You need to customise the hash for this structure. To do this we need to combine | 
|---|
 | 49 |       the hash values for <code class="computeroutput"><span class="identifier">x</span></code> and | 
|---|
 | 50 |       <code class="computeroutput"><span class="identifier">y</span></code>. The function <code class="computeroutput"><a href="../boost/hash_combine.html" title="Function template hash_combine">boost::hash_combine</a></code> is supplied for | 
|---|
 | 51 |       this purpose: | 
|---|
 | 52 |     </p> | 
|---|
 | 53 | <pre class="programlisting"> | 
|---|
 | 54 | <span class="keyword">class</span> <span class="identifier">point</span> | 
|---|
 | 55 | <span class="special">{</span> | 
|---|
 | 56 |     <span class="special">...</span> | 
|---|
 | 57 |  | 
|---|
 | 58 |     <span class="keyword">friend</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">hash_value</span><span class="special">(</span><span class="identifier">point</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">p</span><span class="special">)</span> | 
|---|
 | 59 |     <span class="special">{</span> | 
|---|
 | 60 |         <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">seed</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> | 
|---|
 | 61 |         <code class="computeroutput"><a href="../boost/hash_combine.html" title="Function template hash_combine">boost::hash_combine</a></code><span class="special">(</span><span class="identifier">seed</span><span class="special">,</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">x</span><span class="special">);</span> | 
|---|
 | 62 |         <code class="computeroutput"><a href="../boost/hash_combine.html" title="Function template hash_combine">boost::hash_combine</a></code><span class="special">(</span><span class="identifier">seed</span><span class="special">,</span> <span class="identifier">p</span><span class="special">.</span><span class="identifier">y</span><span class="special">);</span> | 
|---|
 | 63 |  | 
|---|
 | 64 |         <span class="keyword">return</span> <span class="identifier">seed</span><span class="special">;</span> | 
|---|
 | 65 |     <span class="special">}</span> | 
|---|
 | 66 |  | 
|---|
 | 67 |     <span class="special">...</span> | 
|---|
 | 68 | <span class="special">};</span> | 
|---|
 | 69 | </pre> | 
|---|
 | 70 | <p> | 
|---|
 | 71 |       Calls to hash_combine incrementally build the hash from the different members | 
|---|
 | 72 |       of point, it can be repeatedly called for any number of elements. It calls | 
|---|
 | 73 |       <code class="computeroutput"><a href="../boost/hash_value.html" title="Function hash_value">hash_value</a></code> on the supplied | 
|---|
 | 74 |       element, and combines it with the seed. | 
|---|
 | 75 |     </p> | 
|---|
 | 76 | <p> | 
|---|
 | 77 |       Full code for this example is at <a href="../../../libs/functional/hash/examples/point.cpp" target="_top">/libs/functional/hash/examples/point.cpp</a>. | 
|---|
 | 78 |     </p> | 
|---|
 | 79 | <div class="note"><table border="0" summary="Note"> | 
|---|
 | 80 | <tr> | 
|---|
 | 81 | <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../images/note.png"></td> | 
|---|
 | 82 | <th align="left">Note</th> | 
|---|
 | 83 | </tr> | 
|---|
 | 84 | <tr><td align="left" valign="top"> | 
|---|
 | 85 | <p> | 
|---|
 | 86 |         When using <code class="computeroutput"><a href="../boost/hash_combine.html" title="Function template hash_combine">boost::hash_combine</a></code> | 
|---|
 | 87 |         the order of the calls matters.  | 
|---|
 | 88 | </p> | 
|---|
 | 89 | <pre class="programlisting"> | 
|---|
 | 90 |     std::size_t seed = 0; | 
|---|
 | 91 |     boost::hash_combine(seed, 1); | 
|---|
 | 92 |     boost::hash_combine(seed, 2); | 
|---|
 | 93 | </pre> | 
|---|
 | 94 | <p> | 
|---|
 | 95 | results in a different seed to: | 
|---|
 | 96 | </p> | 
|---|
 | 97 | <pre class="programlisting"> | 
|---|
 | 98 |     std::size_t seed = 0; | 
|---|
 | 99 |     boost::hash_combine(seed, 2); | 
|---|
 | 100 |     boost::hash_combine(seed, 1); | 
|---|
 | 101 | </pre> | 
|---|
 | 102 | <p> | 
|---|
 | 103 |  | 
|---|
 | 104 |         If you are calculating a hash value for data where the order of the data | 
|---|
 | 105 |         doesn't matter in comparisons (e.g. a set) you will have to ensure that the | 
|---|
 | 106 |         data is always supplied in the same order. | 
|---|
 | 107 |       </p> | 
|---|
 | 108 | </td></tr> | 
|---|
 | 109 | </table></div> | 
|---|
 | 110 | <p> | 
|---|
 | 111 |       To calculate the hash of an iterator range you can use <code class="computeroutput"><a href="../boost/hash_range.html" title="Function hash_range">boost::hash_range</a></code>: | 
|---|
 | 112 |     </p> | 
|---|
 | 113 | <pre class="programlisting"> | 
|---|
 | 114 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">></span> <span class="identifier">some_strings</span><span class="special">;</span> | 
|---|
 | 115 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">hash</span> <span class="special">=</span> <code class="computeroutput"><a href="../boost/hash_range.html" title="Function hash_range">boost::hash_range</a></code><span class="special">(</span><span class="identifier">some_strings</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">some_strings</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span> | 
|---|
 | 116 | </pre> | 
|---|
 | 117 | </div> | 
|---|
 | 118 | <table width="100%"><tr> | 
|---|
 | 119 | <td align="left"></td> | 
|---|
 | 120 | <td align="right"><small>Copyright © 2005, 2006 Daniel James</small></td> | 
|---|
 | 121 | </tr></table> | 
|---|
 | 122 | <hr> | 
|---|
 | 123 | <div class="spirit-nav"> | 
|---|
 | 124 | <a accesskey="p" href="custom.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../hash.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="portability.html"><img src="../images/next.png" alt="Next"></a> | 
|---|
 | 125 | </div> | 
|---|
 | 126 | </body> | 
|---|
 | 127 | </html> | 
|---|