Click to See Complete Forum and Search --> : hash map not working


jerrro
March 11th, 2008, 10:25 PM
Hello,

I am having serious problems with hash maps in STL.
The thing I am try to do is very simple: have a hash map with a unique key.
I am sure my key is unique per object, and my equality function checks if two keys are completely identical. My hash function depends on a random component, though - and I don't get the same size of the hash map for different hash functions (depending on that random factor). If I am not mistaken, the hash map .size() method does not depend on the hash function, but on the number of unique elements in the hash map. Still, I seem to get a different number of unique elements for different hash functions. That should not happen. Any ideas what could be the problem? I am really confused here.

Thanks.


Jerr.

spoon!
March 11th, 2008, 10:52 PM
post your code

jerrro
March 12th, 2008, 12:07 AM
Okay, here is the code:


/// this is the key - just a sequence of 32 bytes
struct HandleValue {
HandleValue()
{
for (int i=0; i<32; i++) {
c[i] = 0;
}
}
HandleValue(const HandleValue& h)
{
for (int i=0; i<32; i++) { c[i] = h.c[i]; }
}
HandleValue(const char* p, int len)
{
if (len > 32) { assert(0); }

for (int i=0; i<32; i++) {
c[i] = 0;
}

for (int i=0; i<len; i++)
{
c[i] = *p;
p++;
}
}

HandleValue() { assert(0); }

char c[4*8];
};

bool operator==(const HandleValue h1, const HandleValue h2)
{
for (int i=0; i<32; i++)
{
if (h1.c[i] != h2.c[i])
{
return false;
}
}
return true;
}

bool operator!=(const HandleValue h1, const HandleValue h2)
{
return (!(h1==h2));
}

size_t HandleValue2SizeT(const HandleValue h)
{
// depends on a random fixed value that is determined when
// the program loads
return ((*((size_t*)h.c)) ^ randFixedValue);
}

// for hash_set
struct eqHandleValue {
bool operator()(const HandleValue h1, const HandleValue h2) const {
return (h1 == h2);
}
};

// define hash key
namespace gnu_namespace {
template<>
struct hash<HandleValue> {
size_t operator()(const HandleValue h) const {
return (HandleValue2SizeT(h));
}
};
}

hash_map<const HandleValue, string, hash<HandleValue>, eqHandleValue> ValuesMap;



The thing that happens is that in different runs of the program the hashmap ValuesMap has a different size, only because of the randomFixedValue - the rest of the data/variables is EXACTLY the same.

Thanks.


Jerr.