-
Notifications
You must be signed in to change notification settings - Fork 548
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Storing and returning pointer values? #54
Comments
You can have something like this (you would like to use a better hash function as pointers are not random): #if UINTPTR_MAX == UINT64_MAX
static inline uint32_t hash_intptr(uintptr_t key) // by Thomas Wang
{
key = (~key) + (key << 18);
key = key ^ (key >> 31);
key = (key + (key << 2)) + (key << 4);
key = key ^ (key >> 11);
key = key + (key << 6);
key = key ^ (key >> 22);
return (uint32_t)key;
}
#else
static inline uint32_t hash_intptr(uintptr_t key)
{
key = ~key + (key << 15);
key = key ^ (key >> 12);
key = key + (key << 2);
key = key ^ (key >> 4);
key = (key + (key << 3)) + (key << 11);
key = key ^ (key >> 16);
return key;
}
#endif
#define my_hash_eq(a,b) ((a)==(b))
KHASH_INIT(name, uintptr_t, value_type, 1, hash_intptr, my_hash_eq) Probably there are better ways to test the size of uintptr_t. |
Thanks. Actually my original question didn't make sense (I was thinking of applying hash and equals to values... eh!) It was relatively simple in the end. In my header: In my source (note, I have removed
And of course the khash instance is set up as:
And access is as all other examples. For now, this seems to be working. This issue can be closed. |
I'm replacing my own map implementation with khash in a given project. My old map could return pointers or values. This pattern still makes sense for my project. However, khash is built to return values for performance reasons, I believe, so returning pointers reliably isn't really supported, FWICS (?).
I'd like to use khash for both performance-critical and less demanding work where I just need string-key/pointer-value pairs, so am here to ask, what would you do to store/return a pointer in a khash map?
I can see 3 ways:
int64_t
... this should theoretically work on systems with smaller word sizes as well, but it seems a bit dodgy... my project needs to work for Arm (32-bit) & Intel (64-bit)intptr_t
as the value type, but am worried this will have some problems.Any advice greatly appreciated.
The text was updated successfully, but these errors were encountered: