I want save values in a map if an hash function return a value that isn't already contained in the map. The data structure is:
map<uint8_t* , MyObject* , lex_compare> mymap;
where uint8_t* point to un array of 128 bit (uint8_t hash_value[16]) that contain the hash function applied to a field of class MyObject. I use lex_compare for a personalized comparison:
struct lex_compare {
bool operator() (const uint8_t *hash1, const uint8_t *hash2) const {
/*for(int i=0 ; i<16 ; i++)
{
cout<<(unsigned int)hash1[i]<<" ";
}
cout<<endl<<endl;
for(int i=0 ; i<16 ; i++)
{
cout<<(unsigned int)hash2[i]<<" ";
}
int m=memcmp(hash2,hash1,16);
cout<<"m is è"<<m<<endl;*/
return memcmp(hash2,hash1,16); //compare 16 byte.
}
};
To ensure insertion only if an hash value is not already contained in the map I use:
while(mymap.size()<R)
{
myObject *temp_o = new myObject(parameters);
uint8_t hash_result = my_hash_function(myObject->return_field()) // return_field is a specific field of myObject
mymap.insert(make_pair(hash_result,temp_o));
}
But only one element is inserted in mymap, thus I go in endless loop. Why? I can not explain it. Look at lex_compare I saw that this function return always zero value (because is called on 2 equals elements). May be is a trivial problem, but I'm not able to see it
Aucun commentaire:
Enregistrer un commentaire