group__group-Hashable.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <!--
  2. Copyright Louis Dionne 2013-2017
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  5. -->
  6. <!-- boost-no-inspect -->
  7. <!-- HTML header for doxygen 1.8.9.1-->
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  9. <html xmlns="http://www.w3.org/1999/xhtml">
  10. <head>
  11. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  12. <meta http-equiv="X-UA-Compatible" content="IE=9"/>
  13. <meta name="generator" content="Doxygen 1.8.15"/>
  14. <title>Boost.Hana: Hashable</title>
  15. <link href="tabs.css" rel="stylesheet" type="text/css"/>
  16. <script type="text/javascript" src="jquery.js"></script>
  17. <script type="text/javascript" src="dynsections.js"></script>
  18. <link href="navtree.css" rel="stylesheet" type="text/css"/>
  19. <script type="text/javascript" src="resize.js"></script>
  20. <script type="text/javascript" src="navtreedata.js"></script>
  21. <script type="text/javascript" src="navtree.js"></script>
  22. <script type="text/javascript">
  23. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  24. $(document).ready(initResizable);
  25. /* @license-end */</script>
  26. <link href="search/search.css" rel="stylesheet" type="text/css"/>
  27. <script type="text/javascript" src="search/searchdata.js"></script>
  28. <script type="text/javascript" src="search/search.js"></script>
  29. <script type="text/javascript">
  30. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  31. $(document).ready(function() { init_search(); });
  32. /* @license-end */
  33. </script>
  34. <script type="text/x-mathjax-config">
  35. MathJax.Hub.Config({
  36. extensions: ["tex2jax.js"],
  37. jax: ["input/TeX","output/HTML-CSS"],
  38. });
  39. // Copyright Louis Dionne 2013-2017
  40. // Distributed under the Boost Software License, Version 1.0.
  41. // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  42. MathJax.Hub.Config({
  43. "HTML-CSS": {
  44. linebreaks: {
  45. automatic: true,
  46. width: "75% container"
  47. }
  48. }
  49. });
  50. </script><script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
  51. <link href="doxygen.css" rel="stylesheet" type="text/css" />
  52. <!-- Additional javascript for drawing charts. -->
  53. <script type="text/javascript" src="highcharts.js"></script>
  54. <script type="text/javascript" src="highcharts-data.js"></script>
  55. <script type="text/javascript" src="highcharts-exporting.js"></script>
  56. <script type="text/javascript" src="chart.js"></script>
  57. <script type="text/javascript" src="hana.js"></script>
  58. </head>
  59. <body>
  60. <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
  61. <div id="titlearea">
  62. <table cellspacing="0" cellpadding="0">
  63. <tbody>
  64. <tr style="height: 56px;">
  65. <td id="projectlogo"><img alt="Logo" src="Boost.png"/></td>
  66. <td style="padding-left: 0.5em;">
  67. <div id="projectname">Boost.Hana
  68. &#160;<span id="projectnumber">1.6.0</span>
  69. </div>
  70. <div id="projectbrief">Your standard library for metaprogramming</div>
  71. </td>
  72. <td> <div id="MSearchBox" class="MSearchBoxInactive">
  73. <span class="left">
  74. <img id="MSearchSelect" src="search/mag_sel.png"
  75. onmouseover="return searchBox.OnSearchSelectShow()"
  76. onmouseout="return searchBox.OnSearchSelectHide()"
  77. alt=""/>
  78. <input type="text" id="MSearchField" value="Search" accesskey="S"
  79. onfocus="searchBox.OnSearchFieldFocus(true)"
  80. onblur="searchBox.OnSearchFieldFocus(false)"
  81. onkeyup="searchBox.OnSearchFieldChange(event)"/>
  82. </span><span class="right">
  83. <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
  84. </span>
  85. </div>
  86. </td>
  87. </tr>
  88. </tbody>
  89. </table>
  90. </div>
  91. <!-- end header part -->
  92. <!-- Generated by Doxygen 1.8.15 -->
  93. <script type="text/javascript">
  94. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  95. var searchBox = new SearchBox("searchBox", "search",false,'Search');
  96. /* @license-end */
  97. </script>
  98. </div><!-- top -->
  99. <div id="side-nav" class="ui-resizable side-nav-resizable">
  100. <div id="nav-tree">
  101. <div id="nav-tree-contents">
  102. <div id="nav-sync" class="sync"></div>
  103. </div>
  104. </div>
  105. <div id="splitbar" style="-moz-user-select:none;"
  106. class="ui-resizable-handle">
  107. </div>
  108. </div>
  109. <script type="text/javascript">
  110. /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  111. $(document).ready(function(){initNavTree('group__group-Hashable.html','');});
  112. /* @license-end */
  113. </script>
  114. <div id="doc-content">
  115. <!-- window showing the filter options -->
  116. <div id="MSearchSelectWindow"
  117. onmouseover="return searchBox.OnSearchSelectShow()"
  118. onmouseout="return searchBox.OnSearchSelectHide()"
  119. onkeydown="return searchBox.OnSearchSelectKey(event)">
  120. </div>
  121. <!-- iframe showing the search results (closed by default) -->
  122. <div id="MSearchResultsWindow">
  123. <iframe src="javascript:void(0)" frameborder="0"
  124. name="MSearchResults" id="MSearchResults">
  125. </iframe>
  126. </div>
  127. <div class="header">
  128. <div class="summary">
  129. <a href="#var-members">Variables</a> </div>
  130. <div class="headertitle">
  131. <div class="title">Hashable<div class="ingroups"><a class="el" href="group__group-concepts.html">Concepts</a></div></div> </div>
  132. </div><!--header-->
  133. <div class="contents">
  134. <a name="details" id="details"></a><h2 class="groupheader">Description</h2>
  135. <p>The <code>Hashable</code> concept represents objects that can be normalized to a type-level hash. </p>
  136. <p>In day to day programming, hashes are very important as a way to efficiently lookup objects in maps. While the implementation of maps is very different, the same idea of using hashes for efficient lookup applies in metaprogramming. The <code>Hashable</code> concept represents objects that can be summarized (possibly with loss of information) to a type, in a way suitable for use in hash-based data structures. Of course, in order for a hash to be well-behaved, it must obey some laws that are explained below.</p>
  137. <h2>Minimal complete definition </h2>
  138. <p><code>hash</code>, satisfying the laws below</p>
  139. <h2>Laws </h2>
  140. <p>First, <code><a class="el" href="group__group-Hashable.html#ga6e906f44f47b210615491385e5c8a7b5" title="Returns a hana::type representing the compile-time hash of an object.Given an arbitrary object x,...">hana::hash</a></code> must return a <code><a class="el" href="structboost_1_1hana_1_1type.html" title="C++ type in value-level representation.">hana::type</a></code>. Furthermore, for any two <code>Hashable</code> objects <code>x</code> and <code>y</code>, it must be the case that </p><div class="fragment"><div class="line">x == y implies <a class="code" href="group__group-Hashable.html#ga6e906f44f47b210615491385e5c8a7b5">hash</a>(x) == <a class="code" href="group__group-Hashable.html#ga6e906f44f47b210615491385e5c8a7b5">hash</a>(y)</div></div><!-- fragment --><p>where <code>==</code> denotes <code><a class="el" href="group__group-Comparable.html#gacaf1ebea6b3ab96ac9dcb82f0e64e547" title="Returns a Logical representing whether x is equal to y.The equal function can be called in two differ...">hana::equal</a></code>. In other words, any two objects that compare equal (with <code><a class="el" href="group__group-Comparable.html#gacaf1ebea6b3ab96ac9dcb82f0e64e547" title="Returns a Logical representing whether x is equal to y.The equal function can be called in two differ...">hana::equal</a></code>) must also have the same hash. However, the reverse is not true, and two different objects may have the same hash. This situation of two different objects having the same hash is called a <em>collision</em>.</p>
  141. <h2>Concrete models </h2>
  142. <p><code><a class="el" href="structboost_1_1hana_1_1integral__constant.html" title="Compile-time value of an integral type.">hana::integral_constant</a></code>, <code><a class="el" href="structboost_1_1hana_1_1type.html" title="C++ type in value-level representation.">hana::type</a></code>, <code><a class="el" href="structboost_1_1hana_1_1string.html" title="Compile-time string.">hana::string</a></code></p>
  143. <h2>Free model for <code><a class="el" href="structboost_1_1hana_1_1IntegralConstant.html" title="The IntegralConstant concept represents compile-time integral values.">IntegralConstant</a></code>s </h2>
  144. <p>Any <code><a class="el" href="structboost_1_1hana_1_1IntegralConstant.html" title="The IntegralConstant concept represents compile-time integral values.">IntegralConstant</a></code> is <code>Hashable</code>, by normalizing its value to a <code><a class="el" href="structboost_1_1hana_1_1integral__constant.html" title="Compile-time value of an integral type.">hana::integral_constant</a></code>. The type of the value held in the normalized <code><a class="el" href="structboost_1_1hana_1_1integral__constant.html" title="Compile-time value of an integral type.">integral_constant</a></code> is <code>unsigned long long</code> for unsigned integral types, and <code>signed long long</code> for signed integral types. </p>
  145. <table class="memberdecls">
  146. <tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="var-members"></a>
  147. Variables</h2></td></tr>
  148. <tr class="memitem:ga6e906f44f47b210615491385e5c8a7b5"><td class="memItemLeft" align="right" valign="top">constexpr auto&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="group__group-Hashable.html#ga6e906f44f47b210615491385e5c8a7b5">boost::hana::hash</a></td></tr>
  149. <tr class="memdesc:ga6e906f44f47b210615491385e5c8a7b5"><td class="mdescLeft">&#160;</td><td class="mdescRight">Returns a <code><a class="el" href="structboost_1_1hana_1_1type.html" title="C++ type in value-level representation.">hana::type</a></code> representing the compile-time hash of an object.Given an arbitrary object <code>x</code>, <code><a class="el" href="group__group-Hashable.html#ga6e906f44f47b210615491385e5c8a7b5" title="Returns a hana::type representing the compile-time hash of an object.Given an arbitrary object x,...">hana::hash</a></code> returns a <code><a class="el" href="structboost_1_1hana_1_1type.html" title="C++ type in value-level representation.">hana::type</a></code> representing the hash of <code>x</code>. In normal programming, hashes are usually numerical values that can be used e.g. as indices in an array as part of the implementation of a hash table. In the context of metaprogramming, we are interested in type-level hashes instead. Thus, <code><a class="el" href="group__group-Hashable.html#ga6e906f44f47b210615491385e5c8a7b5" title="Returns a hana::type representing the compile-time hash of an object.Given an arbitrary object x,...">hana::hash</a></code> must return a <code><a class="el" href="structboost_1_1hana_1_1type.html" title="C++ type in value-level representation.">hana::type</a></code> object instead of an integer. This <code><a class="el" href="structboost_1_1hana_1_1type.html" title="C++ type in value-level representation.">hana::type</a></code> must somehow summarize the object being hashed, but that summary may of course lose some information. <a href="group__group-Hashable.html#ga6e906f44f47b210615491385e5c8a7b5">More...</a><br /></td></tr>
  150. <tr class="separator:ga6e906f44f47b210615491385e5c8a7b5"><td class="memSeparator" colspan="2">&#160;</td></tr>
  151. </table>
  152. <h2 class="groupheader">Variable Documentation</h2>
  153. <a id="ga6e906f44f47b210615491385e5c8a7b5"></a>
  154. <h2 class="memtitle"><span class="permalink"><a href="#ga6e906f44f47b210615491385e5c8a7b5">&#9670;&nbsp;</a></span>hash</h2>
  155. <div class="memitem">
  156. <div class="memproto">
  157. <table class="memname">
  158. <tr>
  159. <td class="memname">constexpr auto boost::hana::hash</td>
  160. </tr>
  161. </table>
  162. </div><div class="memdoc">
  163. <p><code>#include &lt;<a class="el" href="fwd_2hash_8hpp.html">boost/hana/fwd/hash.hpp</a>&gt;</code></p>
  164. <b>Initial value:</b><div class="fragment"><div class="line">= [](<span class="keyword">auto</span> <span class="keyword">const</span>&amp; x) {</div><div class="line"> <span class="keywordflow">return</span> tag-dispatched;</div><div class="line"> }</div></div><!-- fragment -->
  165. <p>Returns a <code><a class="el" href="structboost_1_1hana_1_1type.html" title="C++ type in value-level representation.">hana::type</a></code> representing the compile-time hash of an object.Given an arbitrary object <code>x</code>, <code><a class="el" href="group__group-Hashable.html#ga6e906f44f47b210615491385e5c8a7b5" title="Returns a hana::type representing the compile-time hash of an object.Given an arbitrary object x,...">hana::hash</a></code> returns a <code><a class="el" href="structboost_1_1hana_1_1type.html" title="C++ type in value-level representation.">hana::type</a></code> representing the hash of <code>x</code>. In normal programming, hashes are usually numerical values that can be used e.g. as indices in an array as part of the implementation of a hash table. In the context of metaprogramming, we are interested in type-level hashes instead. Thus, <code><a class="el" href="group__group-Hashable.html#ga6e906f44f47b210615491385e5c8a7b5" title="Returns a hana::type representing the compile-time hash of an object.Given an arbitrary object x,...">hana::hash</a></code> must return a <code><a class="el" href="structboost_1_1hana_1_1type.html" title="C++ type in value-level representation.">hana::type</a></code> object instead of an integer. This <code><a class="el" href="structboost_1_1hana_1_1type.html" title="C++ type in value-level representation.">hana::type</a></code> must somehow summarize the object being hashed, but that summary may of course lose some information. </p>
  166. <p>In order for the <code>hash</code> function to be defined properly, it must be the case that whenever <code>x</code> is equal to <code>y</code>, then <code>hash(x)</code> is equal to <code>hash(y)</code>. This ensures that <code><a class="el" href="group__group-Hashable.html#ga6e906f44f47b210615491385e5c8a7b5" title="Returns a hana::type representing the compile-time hash of an object.Given an arbitrary object x,...">hana::hash</a></code> is a function in the mathematical sense of the term.</p>
  167. <h2>Signature </h2>
  168. <p>Given a <code>Hashable</code> <code>H</code>, the signature is \( \mathtt{hash} : H \to \mathtt{type\_tag} \)</p>
  169. <dl class="params"><dt>Parameters</dt><dd>
  170. <table class="params">
  171. <tr><td class="paramname">x</td><td>An object whose hash is to be computed.</td></tr>
  172. </table>
  173. </dd>
  174. </dl>
  175. <h2>Example </h2>
  176. <div class="fragment"><div class="line"><span class="comment">// Copyright Louis Dionne 2013-2017</span></div><div class="line"><span class="comment">// Distributed under the Boost Software License, Version 1.0.</span></div><div class="line"><span class="comment">// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="assert_8hpp.html">boost/hana/assert.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="equal_8hpp.html">boost/hana/equal.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="hash_8hpp.html">boost/hana/hash.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="integral__constant_8hpp.html">boost/hana/integral_constant.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="type_8hpp.html">boost/hana/type.hpp</a>&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;type_traits&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;utility&gt;</span></div><div class="line"><span class="keyword">namespace </span>hana = <a class="code" href="namespaceboost_1_1hana.html">boost::hana</a>;</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// Sample implementation of a compile-time set data structure. Of course,</span></div><div class="line"><span class="comment">// this naive implementation only works when no two elements of the same</span></div><div class="line"><span class="comment">// set have the same hash.</span></div><div class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><span class="keyword">struct </span>bucket { };</div><div class="line"></div><div class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> ...T&gt;</div><div class="line"><span class="keyword">struct </span>set</div><div class="line"> : bucket&lt;typename decltype(hana::hash(std::declval&lt;T&gt;()))::type&gt;...</div><div class="line">{ };</div><div class="line"></div><div class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> Set, <span class="keyword">typename</span> T&gt;</div><div class="line"><span class="keyword">struct </span><a class="code" href="group__group-Searchable.html#ga38e7748956cbc9f3d9bb035ac8577906">contains</a></div><div class="line"> : std::is_base_of&lt;</div><div class="line"> bucket&lt;typename decltype(hana::hash(std::declval&lt;T&gt;()))::type&gt;,</div><div class="line"> Set</div><div class="line"> &gt;</div><div class="line">{ };</div><div class="line"></div><div class="line"><span class="keyword">using</span> Set = set&lt;hana::int_&lt;1&gt;, hana::ulong&lt;2&gt;, hana::type&lt;char&gt;&gt;;</div><div class="line"></div><div class="line">static_assert(<a class="code" href="group__group-Searchable.html#ga38e7748956cbc9f3d9bb035ac8577906">contains</a>&lt;Set, hana::int_&lt;1&gt;&gt;{}, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line">static_assert(<a class="code" href="group__group-Searchable.html#ga38e7748956cbc9f3d9bb035ac8577906">contains</a>&lt;Set, hana::ulong&lt;2&gt;&gt;{}, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line">static_assert(<a class="code" href="group__group-Searchable.html#ga38e7748956cbc9f3d9bb035ac8577906">contains</a>&lt;Set, hana::type&lt;char&gt;&gt;{}, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"></div><div class="line">static_assert(!<a class="code" href="group__group-Searchable.html#ga38e7748956cbc9f3d9bb035ac8577906">contains</a>&lt;Set, hana::int_&lt;3&gt;&gt;{}, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line">static_assert(!<a class="code" href="group__group-Searchable.html#ga38e7748956cbc9f3d9bb035ac8577906">contains</a>&lt;Set, hana::type&lt;float&gt;&gt;{}, <span class="stringliteral">&quot;&quot;</span>);</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main() { }</div></div><!-- fragment -->
  177. </div>
  178. </div>
  179. </div><!-- contents -->
  180. </div><!-- doc-content -->
  181. <!--
  182. Copyright Louis Dionne 2013-2017
  183. Distributed under the Boost Software License, Version 1.0.
  184. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  185. -->
  186. <!-- boost-no-inspect -->
  187. <!-- HTML footer for doxygen 1.8.9.1-->
  188. <!-- start footer part -->
  189. <div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  190. <ul>
  191. </ul>
  192. </div>
  193. </body>
  194. </html>