Skip to content

Instantly share code, notes, and snippets.

@jshahbazi
Last active April 3, 2020 19:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jshahbazi/5941740 to your computer and use it in GitHub Desktop.
Save jshahbazi/5941740 to your computer and use it in GitHub Desktop.
Daniel J. Bernstein's hash algorithm written in Fortran
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
@yantosca
Copy link

yantosca commented Apr 3, 2020

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