Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-03-15 12:37:10


Dave Harris wrote:
> In-Reply-To: <d1409a$kc1$1_at_[hidden]>
> daniel_at_[hidden] (Daniel James) wrote (abridged):
>> Think about how such a class would be stored in the proposed
>> unordered associative containers. If you use unordered_set<base*>
>> then the hash function will be called for the pointer, not the class.
>
> I imagine you would use a hasher which dereferenced the pointer.
>
> template<typename T>
> struct hash_ptr {
> size_t operator()(const T *p) const {
> return hash_value( *p );
> }
> };
>
> And perhaps this should be included in boost too? Eg this:
>
> unordered_set< shared_ptr<MyType>, hash_ptr<MyType> >
>
> looks reasonable to me.

You also need equal_ptr<MyType>. Also, it is easy to break the above
unordered_set my modifying some of the MyTypes.

> So it is tempting not to bother, and instead use:
>
> size_t Derived::hash_value() const {
> // return combine_hash( Base::hash_value(), m_value );
> size_t hash = Base::hash_value();
> combine_hash( hash, m_value );
> return hash;
> }
>
> which is correct, but will produce more collisions than if we'd used
> the typeid.

size_t Derived::hash_value() const
{
    size_t seed = my_unique_value;

    hash_combine( seed, Base::hash_value() );
    hash_combine( seed, m_value );

    return seed;
}


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