Open main menu
Home
Random
Log in
Settings
About AddOn Studio
Disclaimers
AddOn Studio
Search
Editing
WoW:USERAPI StringHash
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{userfunc}} <!-- Leave this line in! --> <center>'''StringHash''' ''- by {{user|Mikk}} -''</center> Return a fair-quality 32-bit hash of a string hashVal = StringHash("string") == Example == > print(StringHash("")) 1 > print(StringHash("ab")) 3458343178 > print(StringHash("ba")) 3466747145 > print(StringHash("AB")) 2653593770 > print(StringHash("BA")) 2661997737 > print(StringHash("The quick brown fox jumps over the lazy dog")) 3402772626 ==Details== * This algorithm is ~30% faster than a Lua implementation of the Java [http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#hashCode() String.hashCode] library call * The bit transmutation patterns and resulting collision rates are MUCH better than the results from the Java hash * See [[StringHash/Analysis]] ==Code== local function StringHash(text) local counter = 1 local len = string.len(text) for i = 1, len, 3 do counter = math.fmod(counter*8161, 4294967279) + -- 2^32 - 17: Prime! (string.byte(text,i)*16776193) + ((string.byte(text,i+1) or (len-i+256))*8372226) + ((string.byte(text,i+2) or (len-i+256))*3932164) end return math.fmod(counter, 4294967291) -- 2^32 - 5: Prime (and different from the prime in the loop) end __NOTOC__ [[Category:User defined functions]]
Summary:
Please note that all contributions to AddOn Studio are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
AddOn Studio Wiki:Copyrights
for details).
Submissions must be written by you, or copied from a public domain or similar free resource (see
AddOn Studio Wiki:Copyrights
for details).
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Apinav
(
edit
)
Template:Editlink
(
edit
)
Template:Notebox
(
edit
)
Template:User
(
edit
)
Template:Userfunc
(
edit
)
Close
Loading editor…