Re: How should unordered_map be used correctly ... ?
On Dec 19, 3:33 pm, mast4as <mast...@yahoo.com> wrote:
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.
There's an implicit conversion of int to size_t, although in
general, calculating a hash function is something you do using
unsigned values.
* 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 ?
From where do you get the value 255? And of course, the maximum
hash value doesn't limit the number of elements in an unordered
map.
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).
#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; }
};
Not too sure that this is a good hash function. (In fact, I'm
fairly sure it isn't, in general.) But generating a good hash
function for floating point values is a bit tricky. What sort
of values do x, y and z take on?
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;
There seems to be an inconsistency in these three definitions.
An essential constraint of unordered_map is that if Pred(a) ==
Pred(b), Hash(a) == Hash(b). In your case, Pred only takes into
account the x values, Hash takes all three into account. So
that something like:
point { 10, 20, 30 };
and
point { 10, 0, 0 };
will compare equal, but have different hash values.
Not meeting the constraints of the instantiation arguments of
a template results in undefined behavior. Anything can happen.
float signedrand() { (drand48()-0.5)*100; }
What's this? What do your return? (This is, I think, what is
actually causing your error: when I try it with g++, the
function systematically returns nan. But again, it's undefined
behavior.)
--
James Kanze