From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-03-20 17:50:19
Daniel James wrote:
> Daniel James wrote:
>> Peter Dimov wrote:
>>> For larger integer types, we probably need something along the
>>> lines of
>>> std::size_t hash_value( val )
>>> size_t r = val;
>>> while( val >>= size_t_bits )
>>> hash_combine( r, (size_t)val );
>>> return r;
>>> It has the property of preserving the hash_value of all _values_
>>> that can fit into a size_t, even though the underlying type is
>>> capable of holding bigger integers.
>> I don't think it does.
> Actually, it does for positive number. Sorry I misread it. What you
> were doing clicked a couple of seconds after I clicked on 'send'. But
> I'm not sure it works for negative numbers.
You're right, it doesn't. It'd need a check against (size_t)-1 instead of 0
in the while loop.
std::size_t hash_value( signed T val )
unsigned T v2( val );
size_t r = v2;
size_t s = val >= 0? 0: (size_t)-1;
while( (size_t)(v2 >>= size_t_bits) != s )
hash_combine( r, (size_t)v2 );
or something like that.
Now that I think about it, I'm not sure whether under the existing
implementation -1 and -1L are guaranteed to hash to the same value, so it
might not matter.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk