hello? https://metacpan.org/pod/Hash::Util#hash_value
Nice find! But, it doesn't help :(
I can use the PERL_HASH macro to obtain the hash value; but even if I set up an SV with the key for hv_exists_ent() to ignore; it complains: Use of uninitialized value in subroutine entry; for every iteration; and apparently does nothing?
HV* countColors( SV *img ) {
STRLEN l;
U32 *rgba = (U32*)SvPVx( img, l ), i;
HV* counts = newHV();
SV *dummy = newSV( 4 );
l /= 4;
for( i = 0; i < l; ++i ) {
U32 hash;
PERL_HASH( (U32)hash, (char*)&rgba[ i ], 4 );
SvPV_set( dummy, (char*)&rgba[ i ] );
if( hv_exists_ent( counts, dummy, hash ) ) {
SV **val = hv_fetch( counts, (char*)&rgba[ i ], 4, hash );
SvIV_set( *val, SvIV( *val ) + 1 );
}
else {
SV *val = newSViv( 1 );
hv_store( counts, (char*)&rgba[ i ], 4, val, hash );
}
}
return counts;
}
Even if I could work out (why do I have to guess? Why isn't this stuff documented?) how to persuade hv_exists_ent() to use the pre-calculated hash value and ignore the SV it requires, setting that SV up costs more than recalculating the hash does -- which is what I was trying to avoid.
What a dumb api.
|