3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Combining hash values</title>
5 <link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
7 <link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
8 <link rel="up" href="../hash.html" title="Chapter 10. 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">
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <table cellpadding="2" width="100%"><tr>
14 <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
15 <td align="center"><a href="../../../index.html">Home</a></td>
16 <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
17 <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18 <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19 <td align="center"><a href="../../../more/index.htm">More</a></td>
22 <div class="spirit-nav">
23 <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>
26 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
27 <a name="hash.combine"></a><a class="link" href="combine.html" title="Combining hash values">Combining hash values</a>
28 </h2></div></div></div>
30 Say you have a point class, representing a two dimensional location:
32 <pre class="programlisting"><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>
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>
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 class="link" href="reference.html#boost.hash_combine">boost::hash_combine</a></code> is supplied for
53 <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">point</span>
54 <span class="special">{</span>
55 <span class="special">...</span>
57 <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>
58 <span class="special">{</span>
59 <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>
60 <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>
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">y</span><span class="special">);</span>
63 <span class="keyword">return</span> <span class="identifier">seed</span><span class="special">;</span>
64 <span class="special">}</span>
66 <span class="special">...</span>
67 <span class="special">};</span>
70 Calls to hash_combine incrementally build the hash from the different members
71 of point, it can be repeatedly called for any number of elements. It calls
72 <code class="computeroutput"><a class="link" href="reference.html#boost.hash_value_id746661">hash_value</a></code> on the supplied
73 element, and combines it with the seed.
76 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 <div class="note"><table border="0" summary="Note">
80 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
81 <th align="left">Note</th>
83 <tr><td align="left" valign="top">
85 When using <code class="computeroutput"><a class="link" href="reference.html#boost.hash_combine">boost::hash_combine</a></code>
86 the order of the calls matters. </p>
87 <pre class="programlisting">
89 boost::hash_combine(seed, 1);
90 boost::hash_combine(seed, 2);
93 results in a different seed to:
95 <pre class="programlisting">
97 boost::hash_combine(seed, 2);
98 boost::hash_combine(seed, 1);
101 If you are calculating a hash value for data
102 where the order of the data doesn't matter in comparisons (e.g. a set) you
103 will have to ensure that the data is always supplied in the same order.
108 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 <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>
111 <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>
114 Note that when writing template classes, you might not want to include the
115 main hash header as it's quite an expensive include that brings in a lot of
116 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">functional</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>
117 header which forward declares <code class="computeroutput"><a class="link" href="../boost/hash.html" title="Struct template hash">boost::hash</a></code>,
118 <code class="computeroutput"><a class="link" href="reference.html#boost.hash_range">boost::hash_range</a></code> and
119 <code class="computeroutput"><a class="link" href="reference.html#boost.hash_combine">boost::hash_combine</a></code>.
120 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>.
121 When using a container that uses <code class="computeroutput"><a class="link" href="../boost/hash.html" title="Struct template hash">boost::hash</a></code>
122 it should do that for you, so your type will work fine with the boost hash
123 containers. There's an example of this in <a href="../../../libs/functional/hash/examples/template.hpp" target="_top">template.hpp</a>
124 and <a href="../../../libs/functional/hash/examples/template.cpp" target="_top">template.cpp</a>.
127 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
128 <td align="left"></td>
129 <td align="right"><div class="copyright-footer">Copyright © 2005-2008 Daniel
131 Distributed under the Boost Software License, Version 1.0. (See accompanying
132 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>)
137 <div class="spirit-nav">
138 <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>