The point of this question is to collect a list of examples of hashtable implementations using arrays in different languages. It would also be nice if someone could throw in a pretty detailed overview of how they work, and what is happening with each example.
Edit:
Why not just use the built in hash functions in your specific language?
Because we should know how hash tables work and be able to implement them. This may not seem like a super important topic, but knowing how one of the most used data structures works seems pretty important to me. If this is to become the wikipedia of programming, then these are some of the types of questions that I will come here for. I'm not looking for a CS book to be written here. I could go pull Intro to Algorithms off the shelf and read up on the chapter on hash tables and get that type of info. More specifically what I am looking for are code examples. Not only for me in particular, but also for others who would maybe one day be searching for similar info and stumble across this page.
To be more specific: If you had to implement them, and could not use built-in functions, how would you do it?
You don't need to put the code here. Put it in pastebin and just link it.
A hash table a data structure that allows lookup of items in constant time. It works by hashing a value and converting that value to an offset in an array. The concept of a hash table is fairly easy to understand, but implementing is obviously harder. I'm not pasting the whole hash table here, but here are some snippets of a hash table I made in C a few weeks ago...
One of the basics of creating a hash table is having a good hash function. I used the djb2 hash function in my hash table:
Then comes the actual code itself for creating and managing the buckets in the table
AppendLinkedNode finds the last node in the linked list and appends a new node to it.
The code would be used like this:
Finding a node is a simple as:
And is used as follows:
I went and read some of the Wikipedia-page on hashing: http://en.wikipedia.org/wiki/Hash_table. It seems like a lot of work, to put up code for a hashtable here, especially since most languages I use allready have them built in. Why would you want implementations here? This stuff really belongs in a languages library.
Please elaborate on what your expected solutions should include:
Also state what the purpose of collecting them here is. Any serious implementation will easily be quite a mouthfull = this will lead to very long answers (possibly a few pages long each). You might also be enticing people to copy code from a library...
A HashTable is a really simple concept: it is an array in which key and value pairs are placed into, (like an associative array) by the following scheme:
A hash function hashes the key to a (hopefully) unused index into the array. the value is then placed into the array at that particular index.
Data retrieval is easy, as the index into the array can be calculated via the hash function, thus look up is ~ O(1).
A problem arises when a hash function maps 2 different keys to the same index...there are many ways of handling this which I will not detail here...
Hash tables are a fundamental way of storing and retrieving data quickly, and are "built in" in nearly all programming language libraries.
For those who may use the code above, note the memory leak:
And to complete the code:
Minimal implementation in F# as a function that builds a hash table from a given sequence of key-value pairs and returns a function that searches the hash table for the value associated with the given key:
Here is my code for a Hash Table, implemented in Java. Suffers from a minor glitch- the key and value fields are not the same. Might edit that in the future.