Skip to content

Commit 4f09418

Browse files
committed
pymap: Use hashing algorithm from collections.abc.Set
1 parent 6140967 commit 4f09418

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

immutables/map.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import collections.abc
22
import reprlib
3+
import sys
34

45

56
def map_hash(o):
@@ -28,11 +29,6 @@ def map_bitindex(bitmap, bit):
2829
return map_bitcount(bitmap & (bit - 1))
2930

3031

31-
def shuffle_bits(h):
32-
# used in Map.__hash__
33-
return ((h ^ 89869747) ^ (h << 16)) * 3644798167
34-
35-
3632
W_EMPTY, W_NEWNODE, W_NOT_FOUND = range(3)
3733

3834

@@ -430,18 +426,28 @@ def __hash__(self):
430426
if self.__hash != -1:
431427
return self.__hash
432428

433-
h = 0
429+
MAX = sys.maxsize
430+
MASK = 2 * MAX + 1
431+
432+
h = 1927868237 * (self.__count * 2 + 1)
433+
h &= MASK
434+
434435
for key, value in self.__root.items():
435-
h ^= shuffle_bits(hash(key))
436-
h ^= shuffle_bits(hash(value))
436+
hx = hash(key)
437+
h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167
438+
h &= MASK
437439

438-
h ^= (self.__count * 2 + 1) * 1927868237
440+
hx = hash(value)
441+
h ^= (hx ^ (hx << 16) ^ 89869747) * 3644798167
442+
h &= MASK
439443

440-
h ^= (h >> 11) ^ (h >> 25)
441444
h = h * 69069 + 907133923
445+
h &= MASK
442446

447+
if h > MAX:
448+
h -= MASK + 1
443449
if h == -1:
444-
h = -2
450+
h = 590923713
445451

446452
self.__hash = h
447453
return h

0 commit comments

Comments
 (0)