Boost logo

Boost :

From: Dave Harris (brangdon_at_[hidden])
Date: 2005-03-16 12:07:14


In-Reply-To: <d17nlh$8o3$1_at_[hidden]>
daniel_at_[hidden] (Daniel James) wrote (abridged):
> I'm glad that I'm not the only person who's worried about alignment.
> But this is a problems for a cross-platform library. Either I'll have
> to introduce a load of macros to tweak it on various platforms (I'll
> need help there) or just accept it.

We can probably find something which gives reasonable results across a
wide range of platforms. Shuffling the bits in a hash_combine-like manner
would help.

Eg something like:

    template <typename T>
    size_t hash_value( const T *p ) {
        size_t value = reinterpret_cast<size_t>(p);
        return value + value / sizeof(T);
    }

If there's an easy way to turn sizeof(T) into a power of 2, use it. In
many cases it will already be a power of 2, and I suspect the performance
difference isn't crucial anyway.

Alternatively we could just use:

        return value + (value >> 3);

which ought to be reasonable for most small objects. It may also be
reasonable for large objects that are heap-allocated, depending on how the
heap is implemented. Anyway, I think it is better to do something like
this than to just return value.

-- Dave Harris, Nottingham, UK


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