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.

AppGameKit Classic Chat / [Tier 1] HashTable Include

Author
Message
PartTimeCoder
AGK Tool Maker
4
Years of Service
User Offline
Joined: 9th Mar 2015
Location: Suffolk UK
Posted: 24th Jun 2017 08:40 Edited at: 25th Jun 2017 18:40
My last include for today, working with hashtables (key+value pairs or maps) is something that's been discussed in the forums a few times recently, this is my solution to the problem

supports the following functions
HashLoad(table, filepath) - Loads a saved HashTable
HashSave(table, filepath) - Saves a HashTable to disk
HashSetString(table, key, value) - Sets a string value
HashSetFloat(table, key, value) - Sets a float value
HashSetInt(table, key, value) - Sets a integer value()
HashGetString(table, key) - Gets a string value
HashGetFloat(table, key) - Gets a float value
HashGetInt(table, key) - Gets a integer value
HashCount(table) - Counts the number of hash pairs
HashRemove(table, key) - removes a hash pair
HashGetKey(table, index) - Gets the name of a key by its index

EDIT: Updated to use .insertsorted() and .sort() on item removal.

HashTable.agc


I'v lots of other 'drop in' includes but most are unfinished, I'll see if I can get some ready for posting.
tmu
2
Years of Service
User Offline
Joined: 2nd Feb 2017
Location:
Posted: 25th Jun 2017 12:08
Thanks part timer. This could be useful to manage small amounts of dynamic data. Just a few questions/comments.

Q: Your array is of type HashPair[] and you run find() on it with a string parameter. How does AppGameKit handle running find on custom types with a primitive type? that is, what actually happens when you search an array of HashType for a string?

C: Your implementation is not a real hashmap as it runs find on the whole array and replaces. You'd generally use a hash function to index the array etc. This does not scale so well on larger data but could be a useful abstraction for smaller data sets when needed.

C: You run find() to get the possible index of existing items. Yet your insert functions run insert() on the array. find() expects items to be always sorted, if you insert them in a non-sorted order this method will fail. Your example only works because you insert once in the beginning and the insert is done in already sorted order of 1,2,3,4,5.

PartTimeCoder
AGK Tool Maker
4
Years of Service
User Offline
Joined: 9th Mar 2015
Location: Suffolk UK
Posted: 25th Jun 2017 18:35
Quote: "Q: Your array is of type HashPair[] and you run find() on it with a string parameter. How does AppGameKit handle running find on custom types with a primitive type? that is, what actually happens when you search an array of HashType for a string?"


when you run .find on an array of types the first item of the type is used as the key, I'm not sure how this works when the first item is another type or an array but in my case its a string so it works as I need, I shall experiment a little on the latter to see how it responds.

Quote: "C: Your implementation is not a real hashmap as it runs find on the whole array and replaces. You'd generally use a hash function to index the array etc. This does not scale so well on larger data but could be a useful abstraction for smaller data sets when needed."


I found the opposite to be true, Im my test I ran a function to index an array of 100000 string items and it took 0.035474 ms to complete and using .find() took 0.000004 ms, a remarkable improvement and when searching further into the array like item at 90000 the difference is even greater, overall I found the .find() function to be far more robust than a recursive function so I stuck with it, when searching arrays in game loops the speed difference could have a huge negative impact.



Quote: "C: You run find() to get the possible index of existing items. Yet your insert functions run insert() on the array. find() expects items to be always sorted, if you insert them in a non-sorted order this method will fail. Your example only works because you insert once in the beginning and the insert is done in already sorted order of 1,2,3,4,5."


You are right that is something I overlooked, I just ran a quick test and it does fail, I will update my code, calling .sort() before the search also works but I shall opt to use insertsorted

Thank you for your comments

Login to post a reply

Server time is: 2019-08-24 18:20:59
Your offset time is: 2019-08-24 18:20:59