Hash tables.

Share your advanced PureBasic knowledge/code with the community.
Motu
Enthusiast
Enthusiast
Posts: 160
Joined: Tue Oct 19, 2004 12:24 pm

Post by Motu »

Take a look at this code - this way it's working :)

The remove function was buggy I belive...

Please note that I removed the hash Table parameter as well as the mutexes because I only need one table at a time and no multithreading support - lock and unlock mutex is very slow compared to entercriticalsection_() btw :)

Have fun with it.

Code: Select all

Procedure HT_RemoveItem(key$)
  Protected numelements, hash, index, firstinlist, lastinlist, item
  numelements=PeekL(HTid)
  If numelements
    ;Good to go! First job is to create the hash index.
    hash=HT_CreateHash(numelements-1, key$)+1
    ;hash is now between 1 and 'numelements'.
    ;First check if there is list of elements with the corresponding hash.
    index=HTid+hash*4
    firstinlist=PeekL(index)
    item = 0
    If firstinlist
        ChangeCurrentElement(_HTentries(),firstinlist)
        Repeat
          If _HTentries()\key=key$ And _HTentries()\tableid=HTid ;Key found!
            item=_HTentries()
          ElseIf _HTentries()\hash<>hash Or _HTentries()\tableid<>HTid ;Gone too far through the list.
            lastinlist = _HTentries()
            Break
          EndIf
        Until NextElement(_HTentries())=0
;        lastinlist=_HTentries()
        If item ;If there is an entry to remove.
          ;We now have 3 pointers to elements within the list; firstinlist, item, lastinlist.
          ;We need to use these to update the list accordingly.
          If item=firstinlist ;Need to alter the hash table entry to point to next item.
           ChangeCurrentElement(_HTentries(),item)
           DeleteElement(_HTentries())
           NextElement(_HTentries())
           PokeL(index, _HTentries())
           If _HTentries()\hash<>hash
            PokeL(index, 0)
           EndIf
          ElseIf item = lastinlist
           ChangeCurrentElement(_HTentries(),item)
           DeleteElement(_HTentries())
          Else
           ChangeCurrentElement(_HTentries(),item)
           DeleteElement(_HTentries())
          EndIf
          
        Else
         MessageRequester("There is no item to remove","",0)
        EndIf
    EndIf
  EndIf
EndProcedure
Motu
Enthusiast
Enthusiast
Posts: 160
Joined: Tue Oct 19, 2004 12:24 pm

Post by Motu »

And you may want to remove the message request :oops:
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Motu, this version of hash tables is no longer supported - I now use the much enhanced OOP version which I posted. Thanks for the bug fix, but as I say this is an old hash table library which is really not very good. :)

As for critical sections, you do realise that Purebasic's mutex objects are in fact critical sections don't you?


**EDIT : the bug was in the line

Code: Select all

PokeL(index, PeekL(item-8))
within the HT_RemoveItem() function.

Change it to

Code: Select all

PokeL(index, PeekL(item-8)+8)
I may look like a mule, but I'm not a complete ass.
Post Reply