Re: g_int_hash function

Fredrick Meunier wrote:
> Hello,
> I am using the glib hash functions, and by my reading of the reference
> manual, g_int_hash (and for that matter g_int_equal) should be taking an
> integer value that has been cast to a gconstpointer and comparing them
> by value. In practice though, g_int_hash takes a pointer to an int and
> manipulates that, whilst g_direct_hash seems to act the way g_int_hash
> is documented (for unsigned ints).
> Which is correct, the documentation or the implementation?

I've updated the docs to agree with the implementation, but the
implementation looks a bit odd to me.

It seems that if you use g_int_hash() and g_int_equal() for a hash table
you have to pass in pointers to integers as keys.
I thought it would be using the integers themselves as the keys.

i.e. you must do something like:

  gint *key;

  key = g_new (gint, 1);
  *key = 37;
  g_hash_table_insert (hashtable, key, value);

I thought it would be just:

  g_hash_table_insert (hashtable, GINT_TO_POINTER (37), value);

Or was that what g_direct_hash() and g_direct_equal() were intended for?


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]