Pregunta

I need to use GMPs mpf_t/mpz_t as keys in a hashtable. Is there any efficient way to access the raw bytes of the number representation so I can run a hash function over them?

I already read the documentation but I don't really feel smarter now. ;)

Thanks! Regards, Ethon

¿Fue útil?

Solución

Out of curiosity, why use hashing when you can sort by value? Comparison is very quick, as it compares the bytes / limbs from MSB to LSB, returning a result as soon as they differ.

You can access the raw data using the platform-dependent mp_limb_t type. Both mpz_t and mpf_t have an mp_limb_t vector stored at the address specified by _mp_d, with the number of significant limbs given by the absolute value of the _mp_size field. (the definitions are in gmp.h)

Of course, if the hash function depends on an 8-bit byte vector, you will need to convert the limb vector. Fortunately, the number of bits in a mp_limb_t - GMP_LIMB_BITS - is always going to be divisible by 8 on any sane platform.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top