diff options
author | Damien George <damien.p.george@gmail.com> | 2014-07-24 14:21:37 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-07-24 14:21:37 +0100 |
commit | ffe911d228c3b7e35ef6ae775c802cdd05cffa47 (patch) | |
tree | 0cb1c7c2e8ef4724a8a8f2c21613e65d88cb69e0 /py/mpz.c | |
parent | 4ecb700fe34491c563372ffa72387ef6d45ce5d8 (diff) |
py: Make long ints hashable.
Addresses issue #765.
Diffstat (limited to 'py/mpz.c')
-rw-r--r-- | py/mpz.c | 16 |
1 files changed, 16 insertions, 0 deletions
@@ -1213,6 +1213,22 @@ mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs) { } #endif +// must return actual int value if it fits in mp_int_t +mp_int_t mpz_hash(const mpz_t *z) { + mp_int_t val = 0; + mpz_dig_t *d = z->dig + z->len; + + while (--d >= z->dig) { + val = (val << DIG_SIZE) | *d; + } + + if (z->neg != 0) { + val = -val; + } + + return val; +} + // TODO check that this correctly handles overflow in all cases mp_int_t mpz_as_int(const mpz_t *i) { mp_int_t val = 0; |