Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

djb hash function

Byoungho_Park
Beginner
1,042 Views
I write dbj hash function in Fortran.


[bash]integer, parameter :: hashsize = 65536 function hash(key) character(32), intent(in) :: key integer (kind=4) :: hash integer :: i, keylen, uv integer (kind=4) :: hashval hashval = 5381 keylen = len_trim(key) do i = 1, keylen uv = mod(hashval * 33, 65536_4) hashval = mod(uv + iachar(key(i:i)), 65536_4) end do hash = mod (hashval, HASHSIZE) + 1 end function [/bash]

If I want to handle 10,000,000 data in hash data structure, how much hashsize is good?
I will resolve the collision with chaining.Regard.Park.

0 Kudos
1 Reply
mecej4
Honored Contributor III
1,042 Views
A compromise has to be made between (i) having the hash table so large that a significant part goes unused and (ii) making the hash table too small, causing too many collisions, which then have to be resolved by following a chain.

Much depends on the hash values that occur and their distribution, but you should make the hash table about a third larger than the minimum size.

Note that in some cases the slower but more memory-efficient perfect-hashing algorithms may be appropriate.
0 Kudos
Reply