Last active
January 19, 2023 00:07
-
-
Save vasemkin/419267327036bafbae8cce79ac6b21b2 to your computer and use it in GitHub Desktop.
djb2 hash in Julia
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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