Sujet : Re: Good hash for pointers
De : Bonita.Montero (at) *nospam* gmail.com (Bonita Montero)
Groupes : comp.lang.cDate : 30. May 2024, 10:27:17
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v39gpj$1kkhu$1@raubtier-asyl.eternal-september.org>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
User-Agent : Mozilla Thunderbird
I further improved my code to show sth. which hasn't to do anything
with the initial question. If I don't multiply an indexed value but
a random value with the prime I still get a random value. The dis-
tribution of the bucked-depths is then 1 / (e * N!), which is accor-
ding to the coupon collector's problem:
#include <iostream>
#include <vector>
#include <random>
using namespace std;
int main()
{
constexpr bool SIXTEEN = true;
using width_t = conditional_t<SIXTEEN, uint16_t, uint32_t>;
constexpr width_t PRIME = SIXTEEN ? 65521 : 0xFFFFFFFBu;
constexpr size_t N = (size_t)(width_t)-1 + 1;
vector<uint8_t> buckets;
auto distrib = [&]( auto gen )
{
buckets.resize( 0 );
buckets.resize( N );
for( size_t b = buckets.size(); b; )
if( !++buckets[gen( (width_t)--b ) * PRIME & (width_t)-1] )
return;
vector<size_t> loads;
for( uint8_t ld : buckets )
{
if( loads.size() <= ld )
loads.resize( ld + 1 );
++loads[ld];
}
for( size_t l = 0; ptrdiff_t ld : loads )
cout << l++ << ": " << 100.0 * ld / N << "%" << endl;
};
distrib( []( width_t i ) { return i; } );
cout << endl;
mt19937_64 mt;
uniform_int_distribution<uint32_t> uid( 0, -1 );
distrib( [&]( width_t ) { return uid( mt ); } );
}