Boost logo

Boost :

Subject: Re: [boost] [functional/hash] integer hash function
From: Nevin Liber (nevin_at_[hidden])
Date: 2011-10-04 16:22:48


On 4 October 2011 15:03, Tim Blechmann <tim_at_[hidden]> wrote:

>    inline std::size_t hash_value(int v)
>    {
>        return static_cast<std::size_t>(v);
>    }
>
> while this is probably the most efficient hash function, it is also the worst
> possible. is this done intentionally?

Why is this the "worst possible"? It never has any collisions (if
sizeof(int) <= sizeof(size_t)), and typical use is mod-ing it with a
prime number to determine the bucket for an unordered container.

gcc also uses this for trivial hashes.

-- 
 Nevin ":-)" Liber  <mailto:nevin_at_[hidden]>  (847) 691-1404

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk