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.

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
If this question can be reworded to fit the rules in the help center, please edit the question.

11

The idea is for SO to organically become the Wikipedia of programming. Don't force questions; this smells of karma farming.
–
xanadontJul 18 '09 at 14:36

9 Answers
9

I think you need to be a little more specific. There are several variations on hashtables with regards to the following options

Is the hashtable fixed-size or dynamic?

What type of hash function is used?

Are there any performance constraints when the hashtable is resized?

The list can go on and on. Each of these constraints could lead to multiple implementations in any language.

Personally, I would just use the built-in hashtable that is available in my language of choice. The only reason I would even consider implementing my own would be due to performance issues, and even then it is difficult to beat most existing implementations.

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:

hash function

variable bucket count

collision behavior

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...

Agreed this may not answer the question, and that the question may not be constructive, but at least I finally understand why hash tables are so quick to retrieve values! Very embarrassing that until now I thought it was voodoo.
–
Robert GrantJan 5 at 8:32

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:

I'm not sure you coould use char* value = FindNode("10"); since FindNode returns HashTable*. So you would be looking at something along the lines of: char* value = FindNode("10")->value;
–
Jimmy HuchJan 17 '14 at 5:25

I was looking for a completely portable C hash table implementation and became interested in how to implement one myself. After searching around a bit I found:
Julienne Walker's The Art of Hashing which has some great tutorials on hashing and hash tables. Implementing them is a bit more complex than I thought but it was a great read.

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:

A minor fix is needed in the line 3 of the above snippet: k.GetHashCode() may return a negative number (for example, "Key with negative hashcode".GetHashCode() returns -648767821), which, in turn, will cause System.IndexOutOfRangeException when calculating a bucket number for a such key by function f.
–
Gene BelitskiSep 2 '11 at 11:16

Not sure what that list.add(new KeyValuePair(key, value)); is doing at the end of the add function?
–
Robert GrantJan 5 at 8:33

I don't really get that question. It adds the key -> value mapping to the internal array so it can be retrieved later in the get() call.
–
KorbiJan 14 at 15:46

Sorry, I wasn't thinking straight. I was thinking it was defined in that method, so it's just going to be garbage collected, but you're using it as a handle onto an element in the values member variable.
–
Robert GrantJan 15 at 12:52