Hashing
The <boost/int128/hash.hpp> header provides specializations of std::hash for uint128_t and int128_t, allowing the library types to be used as keys in std::unordered_map, std::unordered_set, and any other container that relies on std::hash.
#include <boost/int128/hash.hpp>
Specializations
namespace std {
template <>
struct hash<boost::int128::int128_t>
{
std::size_t operator()(boost::int128::int128_t v) const noexcept;
};
template <>
struct hash<boost::int128::uint128_t>
{
std::size_t operator()(boost::int128::uint128_t v) const noexcept;
};
} // namespace std
Each 64-bit half of the value is first run through a SplitMix64 finalizer so that every input bit influences the lower bits of the result.
This is necessary because std::hash<std::uint64_t> is permitted to truncate to std::size_t, which would lose the upper 32 bits on 32-bit platforms and cause distinct 128-bit values to collide.
The two finalized halves are then combined with the boost::hash_combine mixing formula.
Guarantees
-
Two values comparing equal under
operator==produce the same hash. -
For any non-zero
v,std::hash<int128_t>{}(v) != std::hash<int128_t>{}(-v). -
The mixing function is asymmetric, so
{high, low}and{low, high}do not collide except by chance.
Boost.ContainerHash
The header also injects hash_value overloads into the boost::int128 namespace, so the types integrate with Boost.ContainerHash through argument-dependent lookup.
namespace boost {
namespace int128 {
std::size_t hash_value(uint128_t v) noexcept;
std::size_t hash_value(int128_t v) noexcept;
} // namespace int128
} // namespace boost
These functions delegate to the std::hash specializations above, so every entry point produces the same result for a given value.
With them, boost::hash, boost::hash_combine, and boost::hash_range accept the library types, and the types can be used as keys in boost::unordered_map, boost::unordered_set, and the flat and node container variants (which default to boost::hash) without an explicit hasher.
See the Boost.ContainerHash integration example for a complete program.