bug in std::hash_map?

Started by
13 comments, last by IFooBar 20 years, 9 months ago
quote:Original post by Fruny
a) you have an infinite loop in your unary operator.


Yes I do. forgot "str++". It was correct in the original though.

quote:Original post by Fruny
b) you should not try to replace the hash_compare class, but pass it a custom comparison predicate.


If I do that then the hash key dosnt get hashed properly. It just gets cast to a size_t. So I would have make the function
"operator size_t ( const char* p );" Either that or me no get what you're saying.



quote:
Before VS.NET 2003, hash_map was in std, even though it is not (yet) standard. Change the code accordingly.


ok. thanks

quote:
Additionally, I strongly suggest you use std::string instead of char* as the map's key. It will safe you much grief


Actually thats what I did at first. But the framerate seems to increase dramatically if I use char pointers instead. In the main game loop all I was doing was

Texture* p1 = TextureMgr["Font.tga"];
Texture* p2 = TextureMgr["Ariel"];

Blit( x, y, p1 );
Blit( x, y, p2 );

When I uses std::string as the key I would get an ok frame rate, then when i used const char* instead, the frame rate shot up 10 times higher.

thanks for replies

[edit] quote insanity [/edit]
:::: [ Triple Buffer V2.0 ] ::::

[edited by - ifoobar on July 10, 2003 8:18:46 AM]
[size=2]aliak.net
Advertisement
quote:
If I do that then the hash key dosnt get hashed properly. It just gets cast to a size_t. So I would have make the function
"operator size_t ( const char* p );" Either that or me no get what you''re saying.


Have you tried the code I gave you ?

I created a predicate intended to compare char*-based strings, and then use with in the hash_compare template to produce a Traits parameter of the form hash_map needs, including typedefs and stuff your class doesn''t provide.

quote:
When I uses std::string as the key I would get an ok frame rate, then when i used const char* instead, the frame rate shot up 10 times higher.


Better a slower solution that works than a fast one which doesn''t Having your graphic system rely on strings (whatever their kind) is not a good idea anyway, you''d go faster with integer IDs.

Anyway, size_t is an integral type, usually unsigned long int, and is the type of array offsets (32 bits on a ''32-bit platform'', 64 bits on a ''64-bit platform'' & so on). It is a perfectly valid type for a hashed key.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Having your graphic system rely on strings (whatever their kind) is not a good idea anyway, you''d go faster with integer IDs.


heeeey! thanks for that. peh...something so simple and never even crossed my mind.


quote:
Anyway, size_t is an integral type, usually unsigned long int, and is the type of array offsets (32 bits on a ''32-bit platform'', 64 bits on a ''64-bit platform'' & so on). It is a perfectly valid type for a hashed key.


yes size_t is an integral type. And yes it is a valid type for a hashkey. However I think you missed my problem. The problem was that all the hash_compare class was doing was casting the KeyType to a size_t type without doing any hashing on it. SO I had to change the source code in xhash for it to actually call the unary operator on the key.

:::: [ Triple Buffer V2.0 ] ::::
[size=2]aliak.net
Ah, well. If you''re using VC6, did you install all the service packs ? Did you patch the lib (see link in sig).

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Im using msvc7 the 2002 one.
[size=2]aliak.net

This topic is closed to new replies.

Advertisement