GeneralHashFunctions_3.py 518 B

1234567891011121314
  1. def pjw_hash(key):
  2. bits_in_unsigned_int = 4 * 8
  3. three_quarters = (bits_in_unsigned_int * 3) / 4
  4. one_eighth = bits_in_unsigned_int / 8
  5. high_bits = 0xFFFFFFFF << int(bits_in_unsigned_int - one_eighth)
  6. hash_value = 0
  7. test = 0
  8. for i in range(len(key)):
  9. hash_value = (hash_value << int(one_eighth)) + ord(key[i])
  10. test = hash_value & high_bits
  11. if test != 0:
  12. hash_value = ((hash_value ^ (test >> int(three_quarters))) & (~high_bits))
  13. return hash_value & 0x7FFFFFFF