Sign in to follow this  

problem using custom hash_map functor

This topic is 4300 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've been working on this problem for a while now and its driving me crazy. When I insert multiple <key, val> pairs into the hash map I only ever get a map size of 1, the keys (structures) have different data (random in this case). I am trying to use a structure as the key type. The code below is what i've come up with after reading SGI's page on hash_maps and trying to decipher the msdn resources.
#include <iostream>
#include <hash_map>

using namespace std;
using namespace stdext;

inline float randfloat()
{
    return rand()/(float(RAND_MAX)+1);
}

struct StructTest
{
	float x, y, z;
	float u, v;
};

void setRandValue(StructTest &s)
{
	s.x = randfloat();
	s.y = randfloat();
	s.z = randfloat();

	s.u = randfloat();
	s.v = randfloat();
}

struct eqvert			// comparitor for hash map
{
	enum
	{
        bucket_size = 20,
        min_buckets = 16
	};

  bool operator()(const StructTest& v1, const StructTest& v2) const		// Test for equality.
  {
	  bool retval = true;	// optimistic

	  // positions
	  retval &=	(v1.x == v2.x) ? true : false;
	  retval &=	(v1.y == v2.y) ? true : false;
	  retval &=	(v1.z == v2.z) ? true : false;

	  // UV's
	  retval &=	(v1.u == v2.u) ? true : false;
	  retval &=	(v1.v == v2.v) ? true : false;

	  return retval;
  }

  size_t operator()(const StructTest& v1) const
  {
	  size_t retVal = 0;

	  retVal += *((unsigned int*)&v1.x) << 2;
	  retVal += *((unsigned int*)&v1.y) << 2;
	  retVal += *((unsigned int*)&v1.z);

	  return retVal;
  }

};

hash_map<StructTest, unsigned int, eqvert>				gHashMap;
hash_map<StructTest, unsigned int, eqvert>::iterator	gHashItr;

typedef pair<StructTest, unsigned int> vertPair;

void main(void)
{
	unsigned int idx = 0;

	for(int i = 0; i < 20; ++i)
	{
		StructTest	v1, v2, v3;

		setRandValue(v1);
		setRandValue(v2);
		setRandValue(v3);

		gHashItr = gHashMap.find(v1);
		if(gHashItr == gHashMap.end())
			gHashMap.insert(vertPair(v1, idx++));
		else
			cout << "Key Val Pair exists?" << endl;

		gHashItr = gHashMap.find(v2);
		if(gHashItr == gHashMap.end())
			gHashMap.insert(vertPair(v2, idx++));
		else
			cout << "Key Val Pair exists?" << endl;

		gHashItr = gHashMap.find(v3);
		if(gHashItr == gHashMap.end())
			gHashMap.insert(vertPair(v3, idx++));
		else
			cout << "Key Val Pair exists?" << endl;

		cout << endl;
	}

	size_t mapSize = gHashMap.size();

	cout << "Size of hash map is " << (unsigned int)mapSize << endl;
}


Any STL masters out there that can help?

Share this post


Link to post
Share on other sites

This topic is 4300 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