Boost logo

Boost Users :

From: Daniel James (daniel_james_at_[hidden])
Date: 2006-12-04 15:00:44


Boris Mansencal wrote:
>
> indeed,
> return (size_t)(p.x()*P_height + p.y())
> seems to give slightly better results...
> (but actually I still do not really grasp why...)
>

If P_height happens to be a multiple of the number of buckets in the
container and the container uses a plain modulus of the hash value then
the x value will have no effect on the chosen bucket. If you want your
program to be portable to different container implementations this could
be a problem.

A possible way to work around this is to apply something like Thomas
Wang's integer hash function to the result.

http://www.cris.com/~Ttwang/tech/inthash.htm

Although the function is specific to the number of bits.

But I'm surprised that boost::hash is slow. With full optimization, I'd
expect it to work okay. Maybe a simpler hash_value might be better:

     std::size_t hash_value(point const& p)
     {
         std::size_t seed = p.x();
         seed ^= (std::size_t) p.y() + (seed<<6) + (seed>>2);
         return seed;
     }

If possible, I'd be interested in looking at your tests (you can email
me off list about them).

Daniel


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net