[29] | 1 | <html> |
---|
| 2 | <head> |
---|
| 3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
---|
| 4 | <title> Extending boost::hash for a custom data type</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="tutorial.html" title=" Tutorial"> |
---|
| 10 | <link rel="next" href="combine.html" title=" Combining hash values"> |
---|
| 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="tutorial.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="combine.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.custom"></a> Extending boost::hash for a custom data type</h2></div></div></div> |
---|
| 28 | <p> |
---|
| 29 | <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code> is implemented by calling |
---|
| 30 | the function <code class="computeroutput"><a href="../boost/hash_value.html" title="Function hash_value">hash_value</a></code>. |
---|
| 31 | The namespace isn't specified so that it can detect overloads via argument |
---|
| 32 | dependant lookup. So if there is a free function <code class="computeroutput"><span class="identifier">hash_value</span></code> |
---|
| 33 | in the same namespace as a custom type, it will get called. |
---|
| 34 | </p> |
---|
| 35 | <p> |
---|
| 36 | If you have a structure <code class="computeroutput"><span class="identifier">library</span><span class="special">::</span><span class="identifier">book</span></code>, where |
---|
| 37 | each <code class="computeroutput"><span class="identifier">book</span></code> is uniquely defined |
---|
| 38 | by it's member <code class="computeroutput"><span class="identifier">id</span></code>: |
---|
| 39 | </p> |
---|
| 40 | <pre class="programlisting"> |
---|
| 41 | <span class="keyword">namespace</span> <span class="identifier">library</span> |
---|
| 42 | <span class="special">{</span> |
---|
| 43 | <span class="keyword">struct</span> <span class="identifier">book</span> |
---|
| 44 | <span class="special">{</span> |
---|
| 45 | <span class="keyword">int</span> <span class="identifier">id</span><span class="special">;</span> |
---|
| 46 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">author</span><span class="special">;</span> |
---|
| 47 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">title</span><span class="special">;</span> |
---|
| 48 | |
---|
| 49 | <span class="comment">// .... |
---|
| 50 | </span> <span class="special">};</span> |
---|
| 51 | |
---|
| 52 | <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span><span class="identifier">book</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">a</span><span class="special">,</span> <span class="identifier">book</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">b</span><span class="special">)</span> |
---|
| 53 | <span class="special">{</span> |
---|
| 54 | <span class="keyword">return</span> <span class="identifier">a</span><span class="special">.</span><span class="identifier">id</span> <span class="special">==</span> <span class="identifier">b</span><span class="special">.</span><span class="identifier">id</span><span class="special">;</span> |
---|
| 55 | <span class="special">}</span> |
---|
| 56 | <span class="special">}</span> |
---|
| 57 | </pre> |
---|
| 58 | <p> |
---|
| 59 | Then all you would need to do is write the function <code class="computeroutput"><span class="identifier">library</span><span class="special">::</span><span class="identifier">hash_value</span></code>: |
---|
| 60 | </p> |
---|
| 61 | <pre class="programlisting"> |
---|
| 62 | <span class="keyword">namespace</span> <span class="identifier">library</span> |
---|
| 63 | <span class="special">{</span> |
---|
| 64 | <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">book</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">b</span><span class="special">)</span> |
---|
| 65 | <span class="special">{</span> |
---|
| 66 | <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">hasher</span><span class="special">;</span> |
---|
| 67 | <span class="keyword">return</span> <span class="identifier">hasher</span><span class="special">(</span><span class="identifier">b</span><span class="special">.</span><span class="identifier">id</span><span class="special">);</span> |
---|
| 68 | <span class="special">}</span> |
---|
| 69 | <span class="special">}</span> |
---|
| 70 | </pre> |
---|
| 71 | <p> |
---|
| 72 | And you can now use <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code> with |
---|
| 73 | book: |
---|
| 74 | </p> |
---|
| 75 | <pre class="programlisting"> |
---|
| 76 | <span class="identifier">library</span><span class="special">::</span><span class="identifier">book</span> <span class="identifier">knife</span><span class="special">(</span><span class="number">3458</span><span class="special">,</span> <span class="string">"Zane Grey"</span><span class="special">,</span> <span class="string">"The Hash Knife Outfit"</span><span class="special">);</span> |
---|
| 77 | <span class="identifier">library</span><span class="special">::</span><span class="identifier">book</span> <span class="identifier">dandelion</span><span class="special">(</span><span class="number">1354</span><span class="special">,</span> <span class="string">"Paul J. Shanley"</span><span class="special">,</span> |
---|
| 78 | <span class="string">"Hash & Dandelion Greens"</span><span class="special">);</span> |
---|
| 79 | |
---|
| 80 | <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="identifier">library</span><span class="special">::</span><span class="identifier">book</span><span class="special">></span> <span class="identifier">book_hasher</span><span class="special">;</span> |
---|
| 81 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">knife_hash_value</span> <span class="special">=</span> <span class="identifier">book_hasher</span><span class="special">(</span><span class="identifier">knife</span><span class="special">);</span> |
---|
| 82 | |
---|
| 83 | <span class="comment">// If std::unordered_set is available: |
---|
| 84 | </span><span class="identifier">std</span><span class="special">::</span><span class="identifier">unordered_set</span><span class="special"><</span><span class="identifier">library</span><span class="special">::</span><span class="identifier">book</span><span class="special">,</span> <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="identifier">library</span><span class="special">::</span><span class="identifier">book</span><span class="special">></span> <span class="special">></span> <span class="identifier">books</span><span class="special">;</span> |
---|
| 85 | <span class="identifier">books</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">knife</span><span class="special">);</span> |
---|
| 86 | <span class="identifier">books</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">library</span><span class="special">::</span><span class="identifier">book</span><span class="special">(</span><span class="number">2443</span><span class="special">,</span> <span class="string">"Lindgren, Torgny"</span><span class="special">,</span> <span class="string">"Hash"</span><span class="special">));</span> |
---|
| 87 | <span class="identifier">books</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">library</span><span class="special">::</span><span class="identifier">book</span><span class="special">(</span><span class="number">1953</span><span class="special">,</span> <span class="string">"Snyder, Bernadette M."</span><span class="special">,</span> |
---|
| 88 | <span class="string">"Heavenly Hash: A Tasty Mix of a Mother's Meditations"</span><span class="special">));</span> |
---|
| 89 | |
---|
| 90 | <span class="identifier">assert</span><span class="special">(</span><span class="identifier">books</span><span class="special">.</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">knife</span><span class="special">)</span> <span class="special">!=</span> <span class="identifier">books</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span> |
---|
| 91 | <span class="identifier">assert</span><span class="special">(</span><span class="identifier">books</span><span class="special">.</span><span class="identifier">find</span><span class="special">(</span><span class="identifier">dandelion</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">books</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span> |
---|
| 92 | </pre> |
---|
| 93 | <p> |
---|
| 94 | The full example can be found in: <a href="../../../libs/functional/hash/examples/books.cpp" target="_top">/libs/functional/hash/examples/books.hpp</a> |
---|
| 95 | and <a href="../../../libs/functional/hash/examples/books.cpp" target="_top">/libs/functional/hash/examples/books.cpp</a>. |
---|
| 96 | </p> |
---|
| 97 | <div class="tip"><table border="0" summary="Tip"> |
---|
| 98 | <tr> |
---|
| 99 | <td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../images/tip.png"></td> |
---|
| 100 | <th align="left">Tip</th> |
---|
| 101 | </tr> |
---|
| 102 | <tr><td align="left" valign="top"><p> |
---|
| 103 | When writing a hash function, first look at how the equality function works. |
---|
| 104 | Objects that are equal must generate the same hash value. When objects are |
---|
| 105 | not equal they should generate different hash values. In this object equality |
---|
| 106 | was based just on the id so the hash function only hash the id. If it was |
---|
| 107 | based on the objects name and author then the hash function should take them |
---|
| 108 | into account (how to do this is discussed in the next section). |
---|
| 109 | </p></td></tr> |
---|
| 110 | </table></div> |
---|
| 111 | </div> |
---|
| 112 | <table width="100%"><tr> |
---|
| 113 | <td align="left"></td> |
---|
| 114 | <td align="right"><small>Copyright © 2005, 2006 Daniel James</small></td> |
---|
| 115 | </tr></table> |
---|
| 116 | <hr> |
---|
| 117 | <div class="spirit-nav"> |
---|
| 118 | <a accesskey="p" href="tutorial.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="combine.html"><img src="../images/next.png" alt="Next"></a> |
---|
| 119 | </div> |
---|
| 120 | </body> |
---|
| 121 | </html> |
---|