Re: API for iterating the g_hash_table



dhaude physnet uni-hamburg de wrote:
I only see the the g_hash_table_foreach() here. But sometimes people may 
like to iterate through a hash table and stop the iteration when certain 
conditions are met.
    

It is rather trivial to construct a GList of hash table keys (or
values) using g_hash_table_foreach. Of course it's only useful on
a table whose contents change never or seldom because the list
has to be updated on each change. Anyway, here's what I use. I
was too lazy to make Append_key() the right type.

void append_key(void *k, void *v, GList **keys)
{
}

GList *get_hash_keys(GHashTable *hash)
{
        GList *keys = NULL;
        
        g_hash_table_foreach(hash, append_key, &keys);
        return keys;
}


--Daniel
  
Hi Daniel,

Good suggestion.

Apart from your suggestion, we may also use a GList together with a GHashTable. When inserting a node into the GHashTable, we also insert the element to the GList. It's a bit more trouble and less efficient, but it works for simple cases. Until we have add new APIs for iterating through the GHashTable, I am afraid we have to do this way...

Thanks.

Regards,
Tony Cheung


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