0.0.3 • Published 3 years ago

mersenne-hash-table v0.0.3

Weekly downloads
5
License
MIT
Repository
github
Last release
3 years ago

mersenne-hash-table

How it works

This hashtable uses a particular property of the mersenne primes to reduce a typically large modulo (%) instruction to a blazing fast bitwise and (&) operation. Consider the typical use cases for a hash-table, when you have a bucket of size k and a hash function f s.t. f(x) > k: to determine the index i to place your data, likely you're doing something like i = f(x) % k. However, there exists a property of mersenne primes (primes which take the form 2^s - 1 for some natural number s) which can summarized as: for a mesenne prime m, while m is larger than k, k % m = k & m.

See the above (very small) sample set:

kmk % mk & m
112711
501275050
100127100100

To see how this works, consider the form that mersenne primes take:

mm binary representationkk binary representationk & m
71113011011
151111501010101
3111111171000110001

Since a mersenne prime m_s is represented in binary as s repeating 1 bits, the result of any bitwise & between k_i and m_i is simply k_i since k_i & 1 = k_i.