hash of mpz_t and mpq_t
Hans Åberg
haberg-1 at telia.com
Thu Jun 17 08:32:41 UTC 2021
> On 16 Jun 2021, at 22:54, Marco Bodrato <bodrato at mail.dm.unipi.it> wrote:
>
> Ciao Hans,
>
> Il 2021-06-16 22:29 Hans Åberg ha scritto:
>> namespace std {
>> template<> struct hash<gmp::integer> {
>> size_t operator()(gmp::integer const& x) const {
>> std::size_t h = 0;
>> for (mp_size_t i = 0; i < mpz_size(x.value_); ++i)
>> h ^= std::hash<mp_limb_t>()(mpz_getlimbn(x.value_, i));
>> return h ^ std::hash<mp_size_t>()(x.value_->_mp_size);
>> }
>> };
>> } // namespace std
>
> A micro-optimisation: you can avoid an operation if you don't initialise h with 0.
>
> namespace std {
> template<> struct hash<gmp::integer> {
> size_t operator()(gmp::integer const& x) const {
> std::size_t h = std::hash<mp_size_t>()(x.value_->_mp_size);
> for (mp_size_t i = 0; i < mpz_size(x.value_); ++i)
> h ^= std::hash<mp_limb_t>()(mpz_getlimbn(x.value_, i));
> return h;
> }
> };
> } // namespace std
Yes, that is better. Thanks!
But one still has to call _mp_size, underneath the API.
More information about the gmp-discuss
mailing list