Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#12171 closed Feature Requests (fixed)

boost::multiprecision should provide a hash function

Reported by: Tassilo Glander <tassilo.glander@…> Owned by: John Maddock
Milestone: To Be Determined Component: multiprecision
Version: Boost 1.60.0 Severity: Optimization
Keywords: Cc:

Description

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:

size_t getHashCode() const {

std::size_t result = _number.sign(); std::size_t size = _number.backend().size(); auto p = _number.backend().limbs();

for (std::size_t i = 0; i < size; ++i) {

boost::hash_combine(result, *p); ++p;

} return result;

} and the accompanying hash functor.

Change History (3)

comment:1 by Tassilo Glander <tassilo.glander@…>, 6 years ago

On stack overflow, an alternative is discussed using serialization support: http://stackoverflow.com/questions/30097385/hash-an-arbitrary-precision-value-boostmultiprecisioncpp-int?rq=1

comment:2 by John Maddock, 6 years ago

Resolution: fixed
Status: newclosed

Fixed in develop.

As a workaround for Boost-1.61 and earlier, see the custom hasher example here: https://htmlpreview.github.io/?https://github.com/boostorg/multiprecision/blob/develop/doc/html/boost_multiprecision/tut/hash.html

comment:3 by Tassilo Glander <tassilo.glander@…>, 6 years ago

Great, thanks! I was not sure from your response on the mailinglist what you were up to, so I opened the tickets :-)

Note: See TracTickets for help on using tickets.