utility, uses Bribe's Table library.
contains some code by other people.
Main utility. you want to use a string key to index in to a hashtable but the keys used are not known ahead of time. a naive approach might use the StringHash() native to turn the string in to an integer key conveniently, however StringHash is not guaranteed (at least that i know of) to only map 1 string to 1 integer. most usages of StringHash assume that no 2 values returned by StringHash be the same if the given inputs differ.
The StringHashEx function provides a convenient replacement for the StringHash() native with the added guarantee that returned hashes be unique (as long as you call it with no more than 2^32 distinct inputs). this is conventient for ex: creating a reverse lookup table for player names to player_id, mapping chat strings to commands in game, .. places where the amount of text processed is not too large yet a hash collision would have rly bad effects.. (like kicking the wrong player or desyncing for most extreme examples)
pros:
contains some code by other people.
Main utility. you want to use a string key to index in to a hashtable but the keys used are not known ahead of time. a naive approach might use the StringHash() native to turn the string in to an integer key conveniently, however StringHash is not guaranteed (at least that i know of) to only map 1 string to 1 integer. most usages of StringHash assume that no 2 values returned by StringHash be the same if the given inputs differ.
The StringHashEx function provides a convenient replacement for the StringHash() native with the added guarantee that returned hashes be unique (as long as you call it with no more than 2^32 distinct inputs). this is conventient for ex: creating a reverse lookup table for player names to player_id, mapping chat strings to commands in game, .. places where the amount of text processed is not too large yet a hash collision would have rly bad effects.. (like kicking the wrong player or desyncing for most extreme examples)
pros:
- never collides
- slower than bare StringHash, makes use of hashtable.
JASS:
if StringHash(a) == StringHash(b) and a != b /*and this is bad for some reason(hashtable index maybe?)*/ then
StringHashEx(a) != StringHashEx(b)
endif
JASS:
library StringHashEx requires Table
/*********************************************************
*
* API:
*
* function StringHashEx takes string returns integer
* for any string parameter this function is guaranteed to return a distinct integer.
*
*
*********************************************************/
globals
private constant integer REHASH = 1222483
private key tbKey
private Table t = tbKey //allowed due to the way Table works
endglobals
function StringHashEx takes string key returns integer
local integer sh = StringHash(key)
loop
if not t.string.has(sh) then
set t.string[sh] = key
exitwhen true
endif
exitwhen t.string[sh] == key
set sh = sh + REHASH
endloop
return sh
endfunction
endlibrary
Last edited: