Collections-C icon indicating copy to clipboard operation
Collections-C copied to clipboard

Q: purpose of hashtable_hash_ptr

Open scossu opened this issue 5 years ago • 1 comments

Newbie here, so please bear with me, but I think I am missing something obvious.

What is the purpose of hashtable_hash_ptr @ https://github.com/srdja/Collections-C/blob/master/src/hashtable.c#L918 ? My understanding is that it would take a pointer value and compute a hash of it.

But since a pointer is 4-8 bytes in most systems, would it not be more convenient to just use the pointer value itself as the "hash" and save the hashing operations?

Thanks.

scossu avatar Jan 21 '19 20:01 scossu

Usually you want your hash function to distribute the values evenly in the hashtable. Values of pointers tend to be clumped together. Also pointers are usually aligned to N-bytes, so the last log2(N) bits are zero, therefore after doing modulo of the hash table size, even more values would clump together.

Kobzol avatar Jun 07 '19 12:06 Kobzol