Last active
April 3, 2020 19:27
-
-
Save jshahbazi/5941740 to your computer and use it in GitHub Desktop.
Daniel J. Bernstein's hash algorithm written in Fortran
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
integer function djb_hash(str) result(hash) | |
implicit none | |
character(len=*),intent(in) :: str | |
integer :: hash | |
integer :: i | |
hash = 5381 | |
do i=1,len(str) | |
hash = (ishft(hash,5) + hash) + ichar(str(i:i)) | |
end do | |
end function DJB_hash |
I actually tried this and you still get hash collisions under certain circumstances:
### hash of text "IO" : 615763273
### hash of text "INA": 615763273
I think that by the nature of hashing, there will always be cases where collisions occur. You will probably always need some kind of "tiebreaker" algorithm to resolve collisions.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The hash codes are similar because ishft is used instead of ishftc. Indeed, ishft is not circular, and left bits are discarded as the shift is performed. My guess is that ishftc should be used instead. Just a letter more :
function djb_hash(str) result(hash)
end function DJB_hash