Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Hash Tables (DBP)

Author
Message
Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 7th Oct 2005 05:48 Edited at: 12th Oct 2005 00:50
I was explaining to Aura on MSN how to do Hash Tables, and he suggested that I post the functions up here for others to use. A hash table allows you to store and retrieve values in a table based on a string 'key'. This has many uses, such as storing user details by name, managing variables in a script interpreter, etc. These functions don't allow a dynamic hash table, but I'll do that later(or you can, if you can work out how).

Functions

result = HashTable_Insert(key as string, value as integer)
Inserts a value into the table based on the key. You later use the key to access this data. This function returns 1 on success, or 0 if the table is full.

result = HashTable_Remove(key as string)
Removes an entry from the table.

result = HashTable_GetValue(key as string)
Retrieves a value from the table that corresponds with key. If there is no such entry in the table, this function returns 0.

result = HashTable_GetKeyEntry(key as string)
Used by the other functions. This gives you the index of an entry in the table's array that has a matching key. If there is no such entry in the table, this function returns -1.

result = HashTable_ResetTable(key as string)
Removes all entries from the table.

result = HashTable_HashGen(key as string)
Used by other functions. A very simple hash generating function. This function returns a hash based on the key you give it. You may want to have a go at creating your own hashing function however, as you may be able to make it more efficient to your needs.



How to use them
The type, hash table size, and the table itself must be created (this goes before any other code):


Next you need to copy and paste the functions to the bottom of your code(or in an include file).

And now a little example of them in action:


Tempest - P2P UDP Multiplayer Plugin - 80% - 15%
Want to try the beta? E-mail me.
Kohaku
21
Years of Service
User Offline
Joined: 3rd May 2004
Location: The not very United Kingdom
Posted: 7th Oct 2005 05:54
You are important.


You are not alone.
Mnemonix
22
Years of Service
User Offline
Joined: 2nd Dec 2002
Location: Skaro
Posted: 8th Oct 2005 20:46
I think I prefer the term retarded.

WE SHALL BECOME ALL POWERFUL! CRUSH THE LESSER RACES! CONQUER THE GALAXY! UNIMAGINABLE POWER! UNLIMITED RICE PUDDING ! ! ! ETC. ! ! ! ETC.! ! !
UnderLord
21
Years of Service
User Offline
Joined: 2nd Aug 2003
Location:
Posted: 11th Oct 2005 04:13
WHy not make a tutorial for it? or put it in the code base "If your code works well put it in the code base" right?

When we talk to god, we're praying. When god talks to us, we're schizophrenic.
http://dbworlds.darkuniverse.net/
Raven
20
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 11th Oct 2005 05:11
Aren't hashtables a tad pointless in a non-Object Orientated Language? Surely a LinkList would make more sense and be quicker?

the_winch
22
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 11th Oct 2005 06:33
Erm isn't this a form of an associative array not a hash table?

A hash table hashes the key then uses the hash to determine the position the value is stored.
The point being that when you want to look up a value it is quick because you just hash the key and jump straight to the position in the array and get the value.
If you have lots of values then it will be quicker than looping through checking every item to see if it matches your key.

Not sure why a hashtable would be any more or less useful in an oop or non-oop language.
Raven
20
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 11th Oct 2005 07:52
Well I've always seen it as something best used for OO, because standard code you wouldn't really see much of a benefit unless you programmed with huge structures.

For OO, it's best cause often you have layer-upon-layer, so rather than searching each layer and accessing useless random parts of memory to get what you want... you just grab the hash and have a more direct route.

atleast that how I see it. don't use them much myself, tend to stick to strip arrays.

Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 12th Oct 2005 00:23 Edited at: 12th Oct 2005 00:39
Quote: " Erm isn't this a form of an associative array not a hash table?"

My mistake.. oh well. Thanks for pointing that out. I wasn't really sure what hash tables were, and just assumed thats what they were because externally they work the same as hash tables in mIRC. Maybe I should look things up before I assume..

Anyway, I've written real hash table functions now, and edited my first post. They are kind of incomplete(I need to add dynamic resizing) but I don't feel too good right now so I'll do it some other time.

Quote: " WHy not make a tutorial for it? "

For what?

Quote: "or put it in the code base "If your code works well put it in the code base" right?"

Yes, I might just do that.

Quote: "Aren't hashtables a tad pointless in a non-Object Orientated Language? Surely a LinkList would make more sense and be quicker?"

Nope, with linked lists you would search through loads of links before you find the data you want. With hash tables you don't have to do that, its more direct.

Tempest - P2P UDP Multiplayer Plugin - 80% - 15%
Want to try the beta? E-mail me.
blanky
20
Years of Service
User Offline
Joined: 3rd Aug 2004
Location: ./
Posted: 12th Oct 2005 00:43 Edited at: 12th Oct 2005 00:45
I am the wookie and you will eat my cookies.

Well, it seems pretty 1337 from what I can gather; Performance Is A Good Thing, and being able to access arrays (effectively) by würdz is pretty.. cool. YEAH.

Numbahs are for dead people.

[Edit: 'Tiz would be good for scripting languages; I mean look at it, it's practically a variable system all on its lonesome.]

Part of the 'Emergency Response Noob Shooting Team' :: Feel free to add me to MSN, but don't expect any big favours.
I AM _NOT_ A MOD, I AM ONLY HUMAN (although I fly in my spare time).
Raven
20
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 12th Oct 2005 02:40
true, never really thought about the applications of it before.
question is how is the speed in pro though, can't imagine it being good being string based.

Benjamin
22
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 12th Oct 2005 02:50
Well for example say you have an array of user profiles, and you wanted to find the data of a particular user. Fair enough if you have the index of the data already, but if you only have the username to go by, you have to loop through the array to find the user with the matching username. Here hash tables are much faster, as you don't have to search through loads of profiles.

Tempest - P2P UDP Multiplayer Plugin - 80% - 15%
Want to try the beta? E-mail me.

Login to post a reply

Server time is: 2025-05-16 08:05:20
Your offset time is: 2025-05-16 08:05:20