Sujet : Re: Good hash for pointers
De : malcolm.arthur.mclean (at) *nospam* gmail.com (Malcolm McLean)
Groupes : comp.lang.cDate : 24. May 2024, 19:57:17
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v2qnue$2evlu$1@dont-email.me>
References : 1 2
User-Agent : Mozilla Thunderbird
On 24/05/2024 19:28, Bonita Montero wrote:
Am 23.05.2024 um 13:11 schrieb Malcolm McLean:
>
What is a good hash function for pointers to use in portable ANSI C?
>
The pointers are nodes of a tree, which are read only, and I want to associate read/write data with them. So potentially a lage number of pointers,and they might be consecutively ordered if they are taken from an array, or they might be returned from repeared calls to malloc() with small allocations. Obviously I have no control over pointer size or internal representation.
>
Use FNV.
>
Here's an attempt.
/* FNV hash of a pointer */
static unsigned int hash(void *address)
{
int i;
unsigned long answer = 2166136261;
unsigned char *byte = (unsigned char *) &address;
for (i = 0; i < sizeof(void *); i++)
{
answer *= 16777619;
answer ^= byte[i];
}
return (unsigned int) (answer & 0xFFFFFFFF);
}
Now what will compilers make of that?
-- Check out Basic Algorithms and my other books:https://www.lulu.com/spotlight/bgy1mm