Giter Club home page Giter Club logo

Comments (2)

attractivechaos avatar attractivechaos commented on May 18, 2024

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.

from klib.

ArcaneEngineer avatar ArcaneEngineer commented on May 18, 2024

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:
static const int StrPtr = 36;
KHASH_DECLARE(StrPtr, kh_cstr_t, uintptr_t)

In my source (note, I have removed static from khash's SCOPE to get things working; YMMV):

__KHASH_IMPL(StrPtr, kh_inline klib_unused, kh_cstr_t, uintptr_t, 1, kh_str_hash_func, kh_str_hash_equal)

And of course the khash instance is set up as:

khash_t(StrPtr) * devicesByName = kh_init(StrPtr);

And access is as all other examples. For now, this seems to be working. This issue can be closed.

from klib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.