Skip to content

Instantly share code, notes, and snippets.

@vasemkin
Last active January 19, 2023 00:07
Show Gist options
  • Select an option

  • Save vasemkin/419267327036bafbae8cce79ac6b21b2 to your computer and use it in GitHub Desktop.

Select an option

Save vasemkin/419267327036bafbae8cce79ac6b21b2 to your computer and use it in GitHub Desktop.
djb2 hash in Julia
module DJB2
export djb2
# 177573 = 5381 * 33
djb2(s::Char)::Int64 = 177573 + Int(s)
function djb2(s::String)::Int64
# seed value for better performance
hash = 5381
for i in 1:lastindex(s)
## hash * 2^5 + hash
hash = (hash << 5) + hash + Int(s[i])
end
return hash
end
end
include("djb2.jl")
using .DJB2
@assert djb2('c') == djb2('c')
@assert djb2('c') !== djb2('d')
@assert djb2("string") == djb2("string")
@assert djb2("string") !== djb2("ztring")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment