Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-12-04 09:15:50


Boris Mansencal wrote:
> Hi,
>
> A newbie question regarding functional/hash.
>
> I would like to use TR1 unordered_map for a 2D point custom data type.
> For this, I use hash_value from Boost. But I would like to give/pass a
> context to this function : here, width & height of domain in which are
> my 2D points.
> Currently, I declare theses width and height static. My code (in my
> cpp file) looks like :
>
> static int P_width;
> static int P_height;
> std::size_t hash_value(point const& p)
> {
> return ((size_t)p.x()<<1)*P_width + (size_t)(p.y()<<1);
> }
>
> But this solution is not satisfactory as I can not have several
> instances of my class including the unordered_map, i.e., several
> domains of different width & height.
>
> Is there a straightforward solution that I have overlooked ?

Is the context-free solution

std::size_t hash_value(point const& p)
{
    std::size_t seed( 0 );

    hash_combine( seed, p.x() );
    hash_combine( seed, p.y() );

    return seed;
}

not working for you?

http://boost.org/doc/html/hash/combine.html


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