Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Hash Function Support

All of the types in this library support hashing via boost::hash or std::hash. That means we can use multiprecision types directly in hashed containers such as std::unordered_set:

using namespace boost::multiprecision;
using namespace boost::random;

mt19937 mt;
uniform_int_distribution<uint256_t> ui;

std::unordered_set<uint256_t> set;
// Put 1000 random values into the container:
for(unsigned i = 0; i < 1000; ++i)
   set.insert(ui(mt));

Or we can define our own hash function, for example in this case based on Google's CityHash:

struct cityhash
{
   std::size_t operator()(const boost::multiprecision::uint256_t& val)const
   {
      // create a hash from all the limbs of the argument, this function is probably x64 specific,
      // and requires that we access the internals of the data type:
      std::size_t result = CityHash64(reinterpret_cast<const char*>(val.backend().limbs()), val.backend().size() * sizeof(val.backend().limbs()[0]));
      // modify the returned hash based on sign:
      return val < 0 ? ~result : result;
   }
};

As before insert some values into a container, this time using our custom hasher:

std::unordered_set<uint256_t, cityhash> set2;
for(unsigned i = 0; i < 1000; ++i)
   set2.insert(ui(mt));

PrevUpHomeNext