• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Hash tables, collision detection

Status
Not open for further replies.
I'm making an item stacking system using hash tables, and I've got everything working except for the hash table collision detection. My formula is ITEM_ID%(modulus)8192. Can anyone tell me how I make it so, if I have one item which has an idea of, say, 300, how I can make it not overwrite an item in my hash table with an id of, say 8492? How do I go about this? I really have no idea...
 

LeP

LeP

Level 13
Joined
Feb 13, 2008
Messages
539
Hash table - Wikipedia, the free encyclopedia

I use linked lists (chaining (not direct, but simmilar)) in my system but i think linear probing is also good in wc3.
e.g.
JASS:
i=hash(id)
loop
exitwhen ids[i]==0 //or datas[i]==0
    set i=i+1
   //this is kinda unsafe, so check if i>8190 and then start from 0 or return
endloop
set ids[i]=id
set datas[i]=data
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
Generally for dynamic support, when you remove something from a hash you have to put in its place an "empty but used" kind of thing. Which means that stuff may exist after that index so it has to check but when asigning a new value, it can be treated as empty.

This is atleast what blizzard did with their MPQs.

Basically for collisions, you just have to use a neighboring index. For efficency use the target index + X until X gives a free index. The system will come to a crawl (slowdown) if you start using too many items (too many hash collisions). The system will completly fail if it passes the 8192 limate when trying to add something unless you have some kind of back up system which handles that problem.
 
Just a small question:
This linear method I'm using at the moment, suggested by LeP, has to loop through the whole 8192 slots if there isn't a stored value for the item index in question. This means when you pick up an item my system doesn't have to stack up, the whole function takes nearly as long, if not longer than it takes if the system does stack the item...

Is there any way around this? LeP mentioned linked lists - what are they?
 
Status
Not open for further replies.
Top