Jesus4Lyf
Good Idea™
- Reaction score
- 397
Rawcode Hashing
Requirements:
- Jass NewGen
Code:
JASS:
globals
private constant integer HASH_NEXT=53
private constant integer MAX_HASH_VALUE=8191
//===================================
private integer array HashedInt
endglobals
private function Hash takes integer int returns integer
local integer hash=int-(int/MAX_HASH_VALUE)*MAX_HASH_VALUE
loop
exitwhen HashedInt[hash]==int
if HashedInt[hash]==0 then
set HashedInt[hash]=int
return hash
endif
set hash=hash+HASH_NEXT
if hash>=MAX_HASH_VALUE then
set hash=hash-MAX_HASH_VALUE
endif
endloop
return hash
endfunction
A simple, simple hashing function. Put a (positive) integer in, get an integer out between 0 and 8190. You will be sure to get the same integer each time.
Why this?
- 'AXXX' can't be subtracted from to be within 0-8190, but this allows that to happen (and for attaching).
- 'u000' has the same issue.
- data[GetHandleId(handle)-0x100000] isn't always reliable, and this should be used instead in published systems.
The reason this has been submitted is an increase in people wishing to attach to rawcodes (abil ids and unit/item types - 'xxxx' things, basically) and not knowing how to go about that. This makes it simple. Copy/paste it into your system/scope and away you go. :thup:
Complexity may increase significantly if you exceed around 1000 entries.
So basically, this functions like an indexer for ability ids, unit types, item types, order ids or any other integer type you can think of and want to attach to.
Very efficient.