Boost C++ Libraries: Ticket #12171: boost::multiprecision should provide a hash function https://svn.boost.org/trac10/ticket/12171 <p> For boost::multiprecision types to be used in std::unordered_map etc., they need to provide a hash function. A straightforward way on cpp_int would be to use the internal limbs like: </p> <p> size_t getHashCode() const { </p> <blockquote> <p> std::size_t result = _number.sign(); std::size_t size = _number.backend().size(); auto p = _number.backend().limbs(); </p> </blockquote> <blockquote> <p> for (std::size_t i = 0; i &lt; size; ++i) { </p> <blockquote> <p> boost::hash_combine(result, *p); ++p; </p> </blockquote> <p> } return result; </p> </blockquote> <p> } and the accompanying hash functor. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12171 Trac 1.4.3 Tassilo Glander <tassilo.glander@…> Mon, 02 May 2016 09:59:11 GMT <link>https://svn.boost.org/trac10/ticket/12171#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12171#comment:1</guid> <description> <p> On stack overflow, an alternative is discussed using serialization support: <a class="ext-link" href="http://stackoverflow.com/questions/30097385/hash-an-arbitrary-precision-value-boostmultiprecisioncpp-int?rq=1"><span class="icon">​</span>http://stackoverflow.com/questions/30097385/hash-an-arbitrary-precision-value-boostmultiprecisioncpp-int?rq=1</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Mon, 02 May 2016 11:42:54 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/12171#comment:2 https://svn.boost.org/trac10/ticket/12171#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Fixed in develop. </p> <p> As a workaround for Boost-1.61 and earlier, see the custom hasher example here: <a class="ext-link" href="https://htmlpreview.github.io/?https://github.com/boostorg/multiprecision/blob/develop/doc/html/boost_multiprecision/tut/hash.html"><span class="icon">​</span>https://htmlpreview.github.io/?https://github.com/boostorg/multiprecision/blob/develop/doc/html/boost_multiprecision/tut/hash.html</a> </p> Ticket Tassilo Glander <tassilo.glander@…> Mon, 02 May 2016 12:48:59 GMT <link>https://svn.boost.org/trac10/ticket/12171#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12171#comment:3</guid> <description> <p> Great, thanks! I was not sure from your response on the mailinglist what you were up to, so I opened the tickets :-) </p> </description> <category>Ticket</category> </item> </channel> </rss>