Home » Community » Forums » » An Introduction to Hash Tables with Chaining
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 An Introduction to Hash Tables with Chaining
Post Reply 
Don't implement hash tables, use std::hashmap instead (requires C++)

http://www.sgi.com/tech/stl/

Ricardo, Brazil.

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

I still think it might be good to implement one for learning purposes (it is always good to know what goes on behind the scenes). Therefore I much appreciated this article, as I haven't really bothered to read up on hash tables before. I am off to do something like this now.

 User Rating: 1039   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

not all implementations of STL include hashmap.

besides, it is a good learning excercise. in addition, it is possible that by rolling your own and playing with the hashing algorithm (as in this article) you can get better distribution than a generic hashmap.

 User Rating: 1214   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

I bet using std::hash in conjunction with a gperf generated hash function would deliver a fairly solid performance when you know the characteristics of the data you're working on. Otherwise using a generic hash function seems sensible.

It's true on the other hand that std::hash is not part of the standard and varies from implementation to implementation.

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

I have used hash tables before reading this article mainly from my experiences with old text based MUDs but never thought of using one that indexed values based on strings. I am still working out the explaination in the middle of that article about the table size and the multiplier and the significance of them being prime etc. but all in all I found the article to be very helpful. I could use a little help though on how the algorithm actually adds weight to the order of the string? Anyone? Anyway, thanks for the ideas



Evillive2
E-Mail

 User Rating: 1261   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

This article was very useful

 User Rating: 1035   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

One of the most important issues raised in the article is the need to select a prime number such that it is "far away from a power of 2". The author failed to elaborate on this. To be more specific, prime numbers should be selected as close to the average of the two nearest powers of 2. For example:

2^4 = 16
2^5 = 32

The average of those two numbers is 24 ((16 + 32) / 2 = 24). The nearest prime numbers are 23 and 29. Since 23 is the closest value to the average, you would pick that one. 23 is a good hash table size for storage of a reasonable number of values (roughly 150 values is a good guesstimate of the limit before performance quickly degrades).


 User Rating: 1015    Report this Post to a Moderator | Link

Quote:
Original post by rdfodra
Don't implement hash tables, use std::hashmap instead (requires C++)

http://www.sgi.com/tech/stl/

Ricardo, Brazil.


Why is that? There are many reasons I could think of too implement your own hash tables. For instance, does std::hash_map support quadratic probing? Can you implement a closed-hash-table with std::hash_map? Do you know the default behavior of std::hash_map? Can you build perfect hash-tables with std::hash_map?

Nope, because it's not officially standard, and every compiler may have a different implementation of it.



 User Rating: 1015    Report this Post to a Moderator | Link

Quote:
Original post by rdfodra
Don't implement hash tables, use std::hashmap instead (requires C++)

http://www.sgi.com/tech/stl/

Ricardo, Brazil.


First, it's a good learning experience. Second, std::hash_map is not standard.

 User Rating: 1463   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

The code snippet in Enter function seems to be incomplete.
Does this form a complete linked list, with pointer to the head being pointed by table[index].

/* put the new node at the head of the list at table [index] */
node->next = table [index];
table [index] = node;





 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by Anonymous Poster
One of the most important issues raised in the article is the need to select a prime number such that it is "far away from a power of 2". The author failed to elaborate on this. To be more specific, prime numbers should be selected as close to the average of the two nearest powers of 2. For example:

2^4 = 16
2^5 = 32

The average of those two numbers is 24 ((16 + 32) / 2 = 24). The nearest prime numbers are 23 and 29. Since 23 is the closest value to the average, you would pick that one. 23 is a good hash table size for storage of a reasonable number of values (roughly 150 values is a good guesstimate of the limit before performance quickly degrades).

The whole prime number sized table is one way to do it. Using a good hash function can let you use power-of-two sized tables, where the modulus function becomes very quick "x & size_minus_one", and simple quadratic probing can probe the entire table of size N in exactly N tries. Google's hash map implementation uses this method, and is extremely quick (for the dense_hash_map). I use the power-of-two method myself, and it is very quick and simple to implement.

lonesock
Piranha are people too.
www.lonesock.net
SOIL: Simple OpenGL Image Library
"The Core" iRiff
"The Mummy" iRiff

 User Rating: 1462   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

I implemented a hash table today, and it implements it with a hash table of link lists to handle values that hash to the same hash value. (Therefore the same hash index)

That's english versus the term buckets or chaining.

Hash Table

 User Rating: 792   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: