c - g_hash_table: int64 as key -
i using glib
g_hash_table
create hash table, int64 key , pointer value.
i tried code fails:
ghashtable* hash = g_hash_table_new(g_int64_hash, g_int64_equal); uint64_t mer_v = 0; exist_m = g_hash_table_lookup(hash, mer_v);
it reports error:
(gdb) bt #0 ia__g_int64_hash (v=0x1d89e81700000) @ /build/buildd/glib2.0-2.24.1/glib/gutils.c:3294 #1 0x00007ff2de966ded in g_hash_table_lookup_node (hash_table=0x13a4050, key=0x1d89e81700000) @ /build/buildd/glib2.0-2.24.1/glib/ghash.c:309 #2 ia__g_hash_table_lookup (hash_table=0x13a4050, key=0x1d89e81700000) @ /build/buildd/glib2.0-2.24.1/glib/ghash.c:898
i used glib
data structure quite often, never tried hash_table key int64. cannot find google. tutorial not have hits well: http://www.ibm.com/developerworks/linux/tutorials/l-glib/section5.html.
please help. thanks.
to use g_int64_hash
, g_int64_equal
need store addresses of 64-bit keys in hash table. so, correct lookup be:
exist_m = g_hash_table_lookup(hash, &mer_v);
to use hasher/comparator, keys need dynamically allocated, , addresses passed both g_hash_table_insert
, g_hash_table_lookup
:
uint64_t *mer_p = malloc(sizeof(uint64_t)); *mer_p = mer_v; g_hash_table_insert(hash, (gpointer) mer_p, (gpointer) m); exists = g_hash_table_lookup(hash, (gpointer) &mer_v);
a common optimization store integer values directly hash table keys, opposed addresses. hasher , comparator g_direct_hash
, g_direct_equal
. requires integer keys fit size of pointer (guaranteed if can use uintptr_t
), , arbitrary integer can cast pointer , (not guaranteed iso c, respected major platforms). in case insertion , lookup this:
g_hash_table_insert(hash, (gpointer) mer_v, (gpointer) m); exists = g_hash_table_lookup(hash, (gpointer) mer_v);
Comments
Post a Comment