How should unordered_map be used correctly ... ?
In the context of what I am trying to do. I am trying to get a
technique implemented where cells are created in a linear table and
indexed using a hash key. So after making a little bit of research on
the web I decided to try to use unordered_map which I thought would
save me time from having to lean how to write my own hash table ;-)
However I can't get it to work apparently.
* I wrote this small program but when I run it it tells me that the
table size of that the end is 1?
* I am guessing I am not using it properly because the hash function
which is recommended in the paper I am trying to implement is
computing an integer while the returned value from the hash function
is a size_t type. So right there I must do something wrong already.
* Now this where I am really confused about unordered map. If the
returned type is size_t for the hash function does that mean that the
map size can not be greater than 255 ? Sorry I am sure that will sound
like an idiotic question from someone who has not understood what they
are and how they work at all. But precisely I seek some light here and
would love to hear some comments back on my questions...
For what I am trying to do (using potentially more than a few million
points positions to index an array of cell in 3D space) unordered_map
are what I need ? Or do I need to write something of my own because
they can't handle table size that big ?
Thanks a lot for your help and sorry for being confused. If you have
also good reference on hash map technique I would be very grateful (of
course I am searching on my side in the meantime).
-coralie
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <tr1/unordered_map>
typedef struct point {
float x, y, z;
};
struct hashfunc {
size_t operator() (const point &pt) const { printf("in here\n");
return 541 * pt.x + 79 * pt.y + 31 * pt.z; }
};
struct setequal {
bool operator() (const point &pa, const point &pb) const { return
(pa.x == pb.x); }
};
typedef std::tr1::unordered_map<point, int, hashfunc, setequal>
tablegrid;
float signedrand() { (drand48()-0.5)*100; }
int main (int argc, const char * argv[])
{
int dim = 128;
int gridsize = dim * dim * dim;
printf("grid size %d\n", gridsize);
tablegrid grid;
point pt;
for (int i = 0; i < gridsize; ++i) {
if (i%64==0) fprintf(stderr, "\b\b\b\b%d%c", int(100*i/
float(gridsize-1)), '%');
pt.x = signedrand(); pt.y = signedrand(); pt.z = signedrand();
grid[pt] = int(drand48() * 100);
}
printf("\nsize of table %d\n", grid.size());
return 0;
}