• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Hash Function Support</title>
5<link rel="stylesheet" href="../../multiprecision.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Multiprecision">
8<link rel="up" href="../tut.html" title="Tutorial">
9<link rel="prev" href="input_output.html" title="Input Output">
10<link rel="next" href="eigen.html" title="Eigen Interoperability">
11</head>
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>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="input_output.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tut.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="eigen.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="boost_multiprecision.tut.hash"></a><a class="link" href="hash.html" title="Hash Function Support">Hash Function Support</a>
28</h3></div></div></div>
29<p>
30        All of the types in this library support hashing via boost::hash or std::hash.
31        That means we can use multiprecision types directly in hashed containers
32        such as std::unordered_set:
33      </p>
34<pre class="programlisting"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">;</span>
35<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">random</span><span class="special">;</span>
36
37<span class="identifier">mt19937</span> <span class="identifier">mt</span><span class="special">;</span>
38<span class="identifier">uniform_int_distribution</span><span class="special">&lt;</span><span class="identifier">uint256_t</span><span class="special">&gt;</span> <span class="identifier">ui</span><span class="special">;</span>
39
40<span class="identifier">std</span><span class="special">::</span><span class="identifier">unordered_set</span><span class="special">&lt;</span><span class="identifier">uint256_t</span><span class="special">&gt;</span> <span class="identifier">set</span><span class="special">;</span>
41<span class="comment">// Put 1000 random values into the container:</span>
42<span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">1000</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span>
43   <span class="identifier">set</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">ui</span><span class="special">(</span><span class="identifier">mt</span><span class="special">));</span>
44</pre>
45<p>
46        Or we can define our own hash function, for example in this case based on
47        Google's CityHash:
48      </p>
49<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">cityhash</span>
50<span class="special">{</span>
51   <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="keyword">operator</span><span class="special">()(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">uint256_t</span><span class="special">&amp;</span> <span class="identifier">val</span><span class="special">)</span><span class="keyword">const</span>
52   <span class="special">{</span>
53      <span class="comment">// create a hash from all the limbs of the argument, this function is probably x64 specific,</span>
54      <span class="comment">// and requires that we access the internals of the data type:</span>
55      <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">CityHash64</span><span class="special">(</span><span class="keyword">reinterpret_cast</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="keyword">char</span><span class="special">*&gt;(</span><span class="identifier">val</span><span class="special">.</span><span class="identifier">backend</span><span class="special">().</span><span class="identifier">limbs</span><span class="special">()),</span> <span class="identifier">val</span><span class="special">.</span><span class="identifier">backend</span><span class="special">().</span><span class="identifier">size</span><span class="special">()</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">val</span><span class="special">.</span><span class="identifier">backend</span><span class="special">().</span><span class="identifier">limbs</span><span class="special">()[</span><span class="number">0</span><span class="special">]));</span>
56      <span class="comment">// modify the returned hash based on sign:</span>
57      <span class="keyword">return</span> <span class="identifier">val</span> <span class="special">&lt;</span> <span class="number">0</span> <span class="special">?</span> <span class="special">~</span><span class="identifier">result</span> <span class="special">:</span> <span class="identifier">result</span><span class="special">;</span>
58   <span class="special">}</span>
59<span class="special">};</span>
60</pre>
61<p>
62        As before insert some values into a container, this time using our custom
63        hasher:
64      </p>
65<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">unordered_set</span><span class="special">&lt;</span><span class="identifier">uint256_t</span><span class="special">,</span> <span class="identifier">cityhash</span><span class="special">&gt;</span> <span class="identifier">set2</span><span class="special">;</span>
66<span class="keyword">for</span><span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="number">1000</span><span class="special">;</span> <span class="special">++</span><span class="identifier">i</span><span class="special">)</span>
67   <span class="identifier">set2</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">ui</span><span class="special">(</span><span class="identifier">mt</span><span class="special">));</span>
68</pre>
69</div>
70<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
71<td align="left"></td>
72<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John
73      Maddock and Christopher Kormanyos<p>
74        Distributed under the Boost Software License, Version 1.0. (See accompanying
75        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>)
76      </p>
77</div></td>
78</tr></table>
79<hr>
80<div class="spirit-nav">
81<a accesskey="p" href="input_output.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tut.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="eigen.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
82</div>
83</body>
84</html>
85