hash of mpz_t and mpq_t
Hans Åberg
haberg-1 at telia.com
Tue Jun 15 17:03:39 UTC 2021
> On 15 Jun 2021, at 18:44, Marco Bodrato <bodrato at mail.dm.unipi.it> wrote:
>
> Ciao,
Hljóðs bið ek allar helgar kindir!
> Il 2021-06-15 14:56 Hans Åberg ha scritto:
>> size_t operator()(gmp::integer const& x) const {
>> std::size_t h = 0;
>> for (int i = 0; i < abs(x.value_->_mp_size); ++i)
>> h ^= std::hash<mp_limb_t>()(x.value_->_mp_d[i]);
>> return h;
>> }
>
>> Possibly, it may not be stable because of calling GMP internals, so
>> there might be a point in adding that.
>
> I'd suggest to look at https://gmplib.org/manual/Integer-Special-Functions ,
> looking in particular for mpz_size and mpz_getlimbn ;-) (or even mpz_limbs_read!)
> I'm sure that the same function can be rewritten using only documented access to the GMP types :-P
Thanks. I wrote this a long time ago.
> You may also look at https://gmplib.org/manual/C_002b_002b-Interface-General ,
> in particular the function mpz_t mpz_class::get_mpz_t (), if you want to use the mpz_class in C++.
GMP C++ has several issues: requires a special compile, very complicated templates, and move operators implemented using swap.
> Anyway, the features that different users may ask for a "hash" function are very different...
The C++ standard comes with hash functions for use with its containers std::unordered_map etc, which is was asked for. Typically, one wants something fast here.
More information about the gmp-discuss
mailing list