M
Murali
I have a requirement where I have to use two unsigned ints as a key in a STL
hash map.
A couple of ways to do this is
1. create a struct with two unsigned ints and use that as key (write my own
HashFcn and EqualKey template args) or,
2. convert the two unsigned ints to char*s, concatenate them and use that as
Key.
For method 1, the difficulty I am having is in writing the HashFcn. HashFcn
requires the following method
size_t operator()(const T& x)
I cannot create a unique hash for two unsigned ints for the hash_map that is
a size_t.
To try method 2, I thought of first trying with just one unsigned int
converted to a char* and use that as Key.
The result of it was that the hash key generated happened to be the same for
two different unsigned ints
causing one of them to be dropped.
Please suggest solution that would work to create an STL hash_map with 2
unsigned ints as Key.
Here is the code that does that:
#include <iostream>
#include <ext/hash_map>
using namespace std;
using namespace __gnu_cxx;
struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};
int main()
{
hash_map<const char*, unsigned int, hash<const char*>, eqstr> hash_obj;
hash<const char*> hashfun;
char key[32];
sprintf(key, "%u", (((unsigned int)~0) / 2) + 250000000);
hash_obj[key] = (((unsigned int)~0) / 2) + 250000000;
cout << "Hash for " << key << " = " << hashfun(key) << endl;
sprintf(key, "%u", (((unsigned int)~0) / 2) + 300000000);
hash_obj[key] = (((unsigned int)~0) / 2) + 300000000;
cout << "Hash for " << key << " = " << hashfun(key) << endl;
for(hash_map<const char*, unsigned int, hash<const char*>,
eqstr>::const_iterator i = hash_obj.begin();
i != hash_obj.end(); i++)
{
cout << "hash_obj[" << (*i).first << "] = " << (*i).second << endl;
}
}
/* g++ /usr/include/c++/3.3 -o post post.cc -lstdc++ */
The output was
Hash for 2397483647 = 123096165
Hash for 2447483647 = 123096165
hash_obj[2447483647] = 2447483647
hash map.
A couple of ways to do this is
1. create a struct with two unsigned ints and use that as key (write my own
HashFcn and EqualKey template args) or,
2. convert the two unsigned ints to char*s, concatenate them and use that as
Key.
For method 1, the difficulty I am having is in writing the HashFcn. HashFcn
requires the following method
size_t operator()(const T& x)
I cannot create a unique hash for two unsigned ints for the hash_map that is
a size_t.
To try method 2, I thought of first trying with just one unsigned int
converted to a char* and use that as Key.
The result of it was that the hash key generated happened to be the same for
two different unsigned ints
causing one of them to be dropped.
Please suggest solution that would work to create an STL hash_map with 2
unsigned ints as Key.
Here is the code that does that:
#include <iostream>
#include <ext/hash_map>
using namespace std;
using namespace __gnu_cxx;
struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};
int main()
{
hash_map<const char*, unsigned int, hash<const char*>, eqstr> hash_obj;
hash<const char*> hashfun;
char key[32];
sprintf(key, "%u", (((unsigned int)~0) / 2) + 250000000);
hash_obj[key] = (((unsigned int)~0) / 2) + 250000000;
cout << "Hash for " << key << " = " << hashfun(key) << endl;
sprintf(key, "%u", (((unsigned int)~0) / 2) + 300000000);
hash_obj[key] = (((unsigned int)~0) / 2) + 300000000;
cout << "Hash for " << key << " = " << hashfun(key) << endl;
for(hash_map<const char*, unsigned int, hash<const char*>,
eqstr>::const_iterator i = hash_obj.begin();
i != hash_obj.end(); i++)
{
cout << "hash_obj[" << (*i).first << "] = " << (*i).second << endl;
}
}
/* g++ /usr/include/c++/3.3 -o post post.cc -lstdc++ */
The output was
Hash for 2397483647 = 123096165
Hash for 2447483647 = 123096165
hash_obj[2447483647] = 2447483647