c++ - Efficiently "Hash" Set of Values to Indice -
first of all, disclaimer; hash inaccurate term i'm aiming for, please, feel free suggest better title.
at rate, i'm attempting program complex spatial algorithm running in real-time. in order save cycles, i've decided generate lookup table contains of 32,000 possibilities.
if conventionally, values(inclusive range , field count) 2x +0 -> +15 , 3x -2 -> +2 mapped 2 four-bit , 3 three-bit values respectively, giving me lookup-table size of 2 ^ (2*4 + 3*3) = 131,072 entries, 410% waste.
given nature of algorithm, collisions absolutely cripple functionality (so no traditional hash functions unless guarantee no collisions relevant values). beyond that, structure i'm working rather large (ie, /really/ avoid allocating more 200% of need). finally, since table referenced often, i'd avoid overhead of traditional hash-table in both bucket lookups , excessively complex hash function.
having taken more traditional computer-science approach, i'm beginning believe solution lies in mathematics of base-conversion i'm ignorant of. idea if case?
you can calculate index same way calculated maximum number of combinations, multiplying each element. take each element significant least significant, add constant make range 0 n-1, , multiply number of combinations remaining.
given 0 15 values of a, b (range of 16) , -2 +2 values of c, d, e (range of 5):
index = * 16*5*5*5 + b * 5*5*5 + (c+2) * 5*5 + (d+2) * 5 + (e+2);
Comments
Post a Comment