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>Combining hash values</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="custom.html" title="Extending boost::hash for a custom data type"> 11<link rel="next" href="portability.html" title="Portability"> 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="custom.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="portability.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.combine"></a><a class="link" href="combine.html" title="Combining hash values">Combining hash values</a> 29</h2></div></div></div> 30<p> 31 Say you have a point class, representing a two dimensional location: 32 </p> 33<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">point</span> 34<span class="special">{</span> 35 <span class="keyword">int</span> <span class="identifier">x</span><span class="special">;</span> 36 <span class="keyword">int</span> <span class="identifier">y</span><span class="special">;</span> 37<span class="keyword">public</span><span class="special">:</span> 38 <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> 39 <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> 40 41 <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> 42 <span class="special">{</span> 43 <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> 44 <span class="special">}</span> 45<span class="special">};</span> 46</pre> 47<p> 48 and you wish to use it as the key for an <code class="computeroutput"><span class="identifier">unordered_map</span></code>. 49 You need to customise the hash for this structure. To do this we need to combine 50 the hash values for <code class="computeroutput"><span class="identifier">x</span></code> and 51 <code class="computeroutput"><span class="identifier">y</span></code>. The function <code class="computeroutput"><a class="link" href="reference.html#boost.hash_combine">boost::hash_combine</a></code> is supplied for 52 this purpose: 53 </p> 54<pre class="programlisting"><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 class="link" href="reference.html#boost.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 class="link" href="reference.html#boost.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 class="link" href="reference.html#boost.hash_va_1_3_11_11_2_2_27_1">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/container_hash/examples/point.cpp" target="_top">/libs/container_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="../../../doc/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 class="link" href="reference.html#boost.hash_combine">boost::hash_combine</a></code> 87 the order of the calls matters. </p> 88<pre class="programlisting"> 89 std::size_t seed = 0; 90 boost::hash_combine(seed, 1); 91 boost::hash_combine(seed, 2); 92</pre> 93<p> 94results in a different seed to: 95</p> 96<pre class="programlisting"> 97 std::size_t seed = 0; 98 boost::hash_combine(seed, 2); 99 boost::hash_combine(seed, 1); 100</pre> 101<p> 102If you are calculating a hash value for data 103 where the order of the data doesn't matter in comparisons (e.g. a set) you 104 will have to ensure that the data is always supplied in the same order. 105 </p> 106</td></tr> 107</table></div> 108<p> 109 To calculate the hash of an iterator range you can use <code class="computeroutput"><a class="link" href="reference.html#boost.hash_range">boost::hash_range</a></code>: 110 </p> 111<pre class="programlisting"><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> 112<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 class="link" href="reference.html#boost.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> 113</pre> 114<p> 115 Note that when writing template classes, you might not want to include the 116 main hash header as it's quite an expensive include that brings in a lot of 117 other headers, so instead you can include the <code class="computeroutput"><span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">container_hash</span><span class="special">/</span><span class="identifier">hash_fwd</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span></code> 118 header which forward declares <code class="computeroutput"><a class="link" href="../boost/hash.html" title="Struct template hash">boost::hash</a></code>, 119 <code class="computeroutput"><a class="link" href="reference.html#boost.hash_range">boost::hash_range</a></code> and 120 <code class="computeroutput"><a class="link" href="reference.html#boost.hash_combine">boost::hash_combine</a></code>. 121 You'll need to include the main header before instantiating <code class="computeroutput"><a class="link" href="../boost/hash.html" title="Struct template hash">boost::hash</a></code>. 122 When using a container that uses <code class="computeroutput"><a class="link" href="../boost/hash.html" title="Struct template hash">boost::hash</a></code> 123 it should do that for you, so your type will work fine with the boost hash 124 containers. There's an example of this in <a href="../../../libs/container_hash/examples/template.hpp" target="_top">template.hpp</a> 125 and <a href="../../../libs/container_hash/examples/template.cpp" target="_top">template.cpp</a>. 126 </p> 127</div> 128<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 129<td align="left"></td> 130<td align="right"><div class="copyright-footer">Copyright © 2005-2008 Daniel 131 James<p> 132 Distributed under the Boost Software License, Version 1.0. (See accompanying 133 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>) 134 </p> 135</div></td> 136</tr></table> 137<hr> 138<div class="spirit-nav"> 139<a accesskey="p" href="custom.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="portability.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 140</div> 141</body> 142</html> 143