C
Christian Meier
Hello,
Yes, I know that hash maps aren't in the standard. But I couldn't find any
better newsgroup for this post. (or is there an SGI library newsgroup?)
I am currently testing the hash_map implementation of SGI. And now I am not
sure if it is really true, what I discovered....
Here is my code:
#include <stdint.h> // for uint64_t
#include <iostream>
using namespace std;
#include <ext/hash_map>
using namespace __gnu_cxx;
const int C_BUCKETS = 700;
const int C_INSERTIONS = 800;
struct hashFunc {
size_t operator() (const uint64_t& ujHash) const { return 1; /* return
static_cast<size_t>(ujHash); */ }
};
typedef hash_map<uint64_t, uint64_t, hashFunc> MyHashMap;
int main()
{
MyHashMap myHashMap(C_BUCKETS);
cout << "bucket_count: " << myHashMap.bucket_count() << endl;
for (uint64_t uj = 0; uj < C_INSERTIONS; ++uj) {
myHashMap.insert(make_pair(uj, uj));
} // for
cout << "bucket_count: " << myHashMap.bucket_count() << endl;
} // main()
As you can see, my hash function always returns 1. So all the values go into
the same bucket. When I run this program, I get the following output:
bucket_count: 769
bucket_count: 1543
My question is why??? After inserting the 769th element, the number of
buckets is doubled. I could understand this behaviour if each element went
into a own bucket and all buckets were used. But I use only one bucket
because of my hash function. The hash map never has less buckets than number
of elements.... When I set C_INSERTIONS to (1543 + 1) then the bucket_count
returns 3079...
Now, what am I doing wrong?
Or is this really the meaning of the implementation of the SGI hash_map? If
so, why is this done like that?
Thanks for your answers!
Chris
Yes, I know that hash maps aren't in the standard. But I couldn't find any
better newsgroup for this post. (or is there an SGI library newsgroup?)
I am currently testing the hash_map implementation of SGI. And now I am not
sure if it is really true, what I discovered....
Here is my code:
#include <stdint.h> // for uint64_t
#include <iostream>
using namespace std;
#include <ext/hash_map>
using namespace __gnu_cxx;
const int C_BUCKETS = 700;
const int C_INSERTIONS = 800;
struct hashFunc {
size_t operator() (const uint64_t& ujHash) const { return 1; /* return
static_cast<size_t>(ujHash); */ }
};
typedef hash_map<uint64_t, uint64_t, hashFunc> MyHashMap;
int main()
{
MyHashMap myHashMap(C_BUCKETS);
cout << "bucket_count: " << myHashMap.bucket_count() << endl;
for (uint64_t uj = 0; uj < C_INSERTIONS; ++uj) {
myHashMap.insert(make_pair(uj, uj));
} // for
cout << "bucket_count: " << myHashMap.bucket_count() << endl;
} // main()
As you can see, my hash function always returns 1. So all the values go into
the same bucket. When I run this program, I get the following output:
bucket_count: 769
bucket_count: 1543
My question is why??? After inserting the 769th element, the number of
buckets is doubled. I could understand this behaviour if each element went
into a own bucket and all buckets were used. But I use only one bucket
because of my hash function. The hash map never has less buckets than number
of elements.... When I set C_INSERTIONS to (1543 + 1) then the bucket_count
returns 3079...
Now, what am I doing wrong?
Or is this really the meaning of the implementation of the SGI hash_map? If
so, why is this done like that?
Thanks for your answers!
Chris