Sign in to follow this  

HASHTABLES

This topic is 3453 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have heard a lot of talk about hashtables from time to time. They are supposed to be very cool ways of storing data where the index you use to look up the data is somehow the data itself. But hashtables are not part of the C++ standard template library... I think. So does anyone have any tips on how to write a hashtable?

Share this post


Link to post
Share on other sites
Generally, what one does is Google or read a book. I suggest you consider actually investing some time in learning fundamental data structures and algorithms, given how fundamental much of the knowledge is.

Share this post


Link to post
Share on other sites
Although the C++ standard doesn't have hash tables, most STL implementations will give you hash_map and hash_set.

Share this post


Link to post
Share on other sites
std::tr1::unordered_map is a hashtable, and is available in the VC 2008 Feature Pack (need standard or higher to install, unfortunately.)

stdext::hash_map should be available in MSVC 2005 and 2008, but is non-standard (#include <hash_map>.)

You can also use boost::multi_index_container with a single index as a hashtable.

Share this post


Link to post
Share on other sites
Use a hash algorithm with a good distribution to minimize collision and clustering. According to Wikipedia Donald Knuth advocates the following algorithm:

uint32_t jenkins_one_at_a_time_hash(unsigned char *key, size_t key_len)
{
uint32_t hash = 0;
size_t i;

for (i = 0; i < key_len; i++) {
hash += key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}




If you plan to implement your own hash table.

Share this post


Link to post
Share on other sites

This topic is 3453 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this