Fork me on GitHub

null program

Simple Hash Table in C

Update May 2011: I've made some changes, including putting this into the public domain, so I finally just checked this into a repository. This little thing still gets a lot of use!

git clone git://github.com/skeeto/hashtab.git

I needed a hash table written in C for a project I was working on and I didn't like any the free hash table code that was out there. Plus, I have NIH syndrome and I really wanted to write my own for fun. My goal was to make it extremely generic, so that it would work with any data of any size. A small front end could be placed on top to make it work cleanly for strings (no need to pass the length of the data).

It uses open hashing, with linked lists to store the data. The only penalty from collisions is a slightly slower lookup. This also is what alows the variable entry sizes. I could definitely fix up a few things to make it more efficient. The default hash function uses modulus, which is quite expensive.

It works just fine with all my tests so far, so it suits my needs for the project. The code includes a demo of the hash table (main.c). If you want to read about some better hashing functions and use one with this hash table, take a look at A Hash Function for Hash Table Lookup.

tags: [ c ]

blog comments powered by Disqus