Posted: Fri Jul 27, 2012 2:11 am Post subject: [Module] Dictionary
For those who are interested, I wrote this module to provide a hash table type easily. I got tired of writing a new one in every program, so I figured I'd make it a class. It's a very basic implementation, but it seems to be more than sufficient to me (about 10x faster than searching an array, in my programs anyways). The algorithm is very simple; ord the values of the key, add them together and mod it by the number of buckets == your bucket number. Then it just searches for your key.
Anyways, the basic functions are:
code:
initialize (number_of_buckets) = Needs to be called after making the dictionary. Takes in a number of buckets parameter.
add (key, value : string) = Adds a key and value pair
remove (key) = Removes a key and value pair
getValue (key) = Get's the value of a given key
containsKey (key) = Tells whether the key is in the dictionary
It's worth noting that if a key/value pair is added and the key already exists, the value will be over written.