Sujet : Re: Good hash for pointers
De : chris.m.thomasson.1 (at) *nospam* gmail.com (Chris M. Thomasson)
Groupes : comp.lang.cDate : 24. May 2024, 18:51:45
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v2qk3h$2e6vs$2@dont-email.me>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Mozilla Thunderbird
On 5/24/2024 10:49 AM, Chris M. Thomasson wrote:
On 5/24/2024 4:05 AM, Malcolm McLean wrote:
[...]
Then I can't be the first person to want to use a pointer as a key for hash table.
>
And I don't want things to break if pointer representation changes.
Check this out, it relies on hashing pointers, the multex... ;^)
https://groups.google.com/g/comp.lang.c++/c/sV4WC_cBb9Q/m/SkSqpSxGCAAJ
It can be used for many different things...
The naive hash is:
______________________
// A table of locks
struct mutex_table
{
std::vector<std::mutex> m_locks;
mutex_table(std::size_t size) : m_locks(size) { assert(size > 0); }
// totally contrived simple hash...
std::size_t hash(void const* ptr)
{
std::uintptr_t ptr_uint = (std::uintptr_t)ptr;
return (std::size_t)(((ptr_uint << 9) * 103) % m_locks.size());
}
};
______________________
;^)