Skip to main content
GameDev.net gamedev.net
🔒 Locked

hashmap

Started by ArnoAtWork Jul 6, 2004 at 8:34 AM 13 replies 2.4k views
Original Post
ArnoAtWork
ArnoAtWork
Does anybody has problem with hashmap of STLport with VC7.1? I don't known why, but if I do a loop using iterator from beginning to the end of the map, it never stops... Moreover, the find function always returns the end of the map even if the string is used. Thanks very much.
dalleboy
dalleboy
struct eqstr{  bool operator()(const char* s1, const char* s2) const  {    return strcmp(s1, s2) == 0;  }};typedef std::hash_map<char*, int, std::hash<const char*>, eqstr> StringToIntHashMap;

A note on this code: Don't use char*, use std::string instead...
ArnoAtWork
ArnoAtWork
I don't know if it's the reason of the pb.

Why does an iterator set to the beginning of the map never reach the end(or do not valid the test [iterator == map.end()]) after a loop of ++ite? That never happens.

The only pb that i though was that the conversion string->integer was wrong. However, it seems correct....
ArnoAtWork
ArnoAtWork
Huumm, I am confused.

In STL implementation, I have seen that a fonction already exists to convert string to integer. When you do for example: map[txt]=(int)
you can see that the txt is converted to integer, so after that, it's just a simple map implementation to search, access,....

So, how does the operator == can be right?


VolkerG
VolkerG
Quote:
Original post by ArnoAtWork
Huumm, I am confused.

In STL implementation, I have seen that a fonction already exists to convert string to integer. When you do for example: map[txt]=(int)
you can see that the txt is converted to integer, so after that, it's just a simple map implementation to search, access,....

So, how does the operator == can be right?

Now I'm confused. But I'll try to explain what happens.
- First, the hash for the key (in your case the string) is calculated.
- Then, for every element that has the same hash, the key (not only its hash value) of the element is compared with the key you supplied. Only for an exact match, find returns the iterator for the element.

How the map is implemented in detail is up the the STL implementor.

At what point are you confused?
ArnoAtWork
ArnoAtWork
For me a hash map converts the string to integer to store at the end a kind of map.
So, I do not really understand why I have to give a char*==char* test because at the end, only integer representation of the text is used...

VolkerG
VolkerG
But mapping from string to int may not be unique (the values that can be represented by an int is limeted, but not the number of different strings). Two different strings may produce the same hash, so you always have to compare the strings to make sure the key is really equal.
ArnoAtWork
ArnoAtWork
Haaa, ok.
char* compare is done after int compare...it sounds normal in fact.
Thanks a lot.

VolkerG
VolkerG
Perhaps someone has a good link to the theory of hash maps for you, that could be of help. Can't find one in my bookmarks at the moment.
Perhaps this one.
ArnoAtWork
ArnoAtWork
I have tested, it is working. However data access still seems to fail...

Even, if text is converted to integer and even if I can find element with the find function, iteration fails.

This code leads to an infinite loop:

for(hash_map<X>::iterator it=hash.begin(); it!=hash.end(); ++it){}


even if I have 1 or more elements in it.
Zahlman
Zahlman
Try checking the values of *it on successive iterations and see if any weird patterns crop up. (You might also want to output *hash.begin() and *hash.end() for reference.)
ArnoAtWork
ArnoAtWork
It seems that values of *it are also different from beginning to the end. However, the only problem seems to be the test it == hash.end() which always returns false...
ArnoAtWork
ArnoAtWork
Baaaahhh, I've seen my mistake!

I used a char* for the hash key, but as soon as the program exited the function, the const char* was destroyed and of course, the key became unvalid....

I replaced the char* by std::string and adds the hash functions for them, and it is working perfectly.

But, the reason why invalid char* means a infinite loop with iterators ....

Thanks for all.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.