hash_map (with VS 2003)

Started by
6 comments, last by vs322 16 years, 6 months ago
So I have set up my hash_map like this:

struct hashkey
{
	int x,z;
	enum
	{
		bucket_size = 4,
		min_buckets = 8
	};
	bool operator()(hashkey s1, hashkey s2) const
	{
		if (s1.x < s1.x)
			return true;
		if (s1.x == s1.x)
		{
			if(s1.z < s1.z)
				return true;
			else
				return false;
		}
		else 
			return false;
	}
	hashkey(int ix,int iz){x=ix;z=iz;}
	hashkey(){x=0;z=0;}
};
typedef std::hash_map<hashkey, MapSquare*, hashkey> hashSquares;
hashSquares::iterator sItor;
hashSquares::iterator sDDItor;
hashSquares hSquares;


My problem comes with how to set up the size_t operator() function. Is this my Hashing function?!? I have found some help on the forum, but this is still not working. ANYWAY: I am using this hash to store pointers to elements which have a static x,z cord and I would like them to be able to be fairly sparse. But I need quick lookup and my old method was going through a vector and compairing x,z values to even find if anything even existed at that location. I also need a way to iterate through the structure for filling other data structurs. If anyone has a better solution, or a way to set up an operator() function that returns a size_t properly it would be great. -vs
Advertisement
The key class probably should not be used as a functor for comparison. That seems bizarre/broken. Instead, you normally create a separate comparison class.

The comparison itself is wrong. For a hash_map, the comparison is for equality, not less-than. The default comparision class is std::equal_to<Key>, so all you really have to do is to implement operator==() for your key class:
    bool operator ==(hashkey const & s) const    {         return s.x == x && s.z == z;    } 

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Quote:Original post by vs322
If anyone has a better solution


stdext::hash_map
Quote:Original post by Driv3MeFar
Quote:Original post by vs322
If anyone has a better solution


stdext::hash_map


in VS 2003 std::hash_map resolves to stdext::hash_map
Quote:Original post by vs322
Quote:Original post by Driv3MeFar
Quote:Original post by vs322
If anyone has a better solution


stdext::hash_map


in VS 2003 std::hash_map resolves to stdext::hash_map


Yeah, never mind what I said. I'm an idiot.
Even after making an == operator I couldn't get my key to work. In the meantime I am going to simply use plane old std::map with a LONG as its key.. and have that LONG be made from my 2 int x,z values.(thanks for your time).

Just if some one has personal experience with hash_map vs map is one dramatically faster than the other when retreating values in an extremely non-liner manner?
I find boost's multi_index hashed_unique to be quite good. A couple other people and myself wrote up a quick test a little while ago comparing std::map with stdext::hash_map and boost's multi_index. The test is at http://rafb.net/p/14CHPC26.html. Pardon the misnomers, this code underwent several revisions and was just thrown together. I'm sure someone could rip into this test and tell me how bad it is, but oh well. PWToolBox is a library my team wrote.

The results of this, running on Windows XP 2.4GHz with 1GB of RAM is http://rafb.net/p/1pbx1P57.html. All times are in seconds.

Thanks for the test numbers njpaul. I'm not gonna rip your test :)

Well It looks like the boost multi_index is the way to go. ty

This topic is closed to new replies.

Advertisement