samedi 14 mars 2015

Insert failure in unordered_map


I have an unordered map defined as (within an ordered map):



std::map<ClassId, std::unordered_map <KeySPtr, ObjectSPtr,KeyClassKeyDataHasher, KeyClassEquals>> collection_;


The hash function is basically fnv:



// The FNV hash function
static inline size_t
fnvHash (const void *data_ptr, int len)
{
uint8_t *ptr = (uint8_t *)data_ptr;
uint32_t hv = FNV_32BIT_OFFSET_BASIS;
int i;

cout << "data_ptr: " << data_ptr << " length: " << len << endl;

for (i = 0; i < len; i++) {
hv = (hv * FNV_32BIT_PRIME) ^ ptr[i];
}

return hv % 0xffffffff;
}


The insert is done as follows:



std::pair<
std::unordered_map<KeySPtr,ObjectSPtr,KeyClassKeyDataHasher, KeyClassEquals>::iterator,
bool> ret;

ret = collection_[class_id].insert(
std::pair<KeySPtr, ObjectSPtr>(key, object));


What I'm seeing is, for some cases even though the generated hash is unique, the insert fails. And when I check the return iterator, it has some keySPtr, which was inserted with a totally different hash.


What am I missing here? Is it something to do with the 32-bit hash that is being generated, and it is not working with my unordered map hs table


Thanks!




Aucun commentaire:

Enregistrer un commentaire