API for iterating the g_hash_table
Hi, Is there any API for iterating GHashTable? 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. Currently, there is g_hash_table_size(), we could easily add iteration by providing the following two new APIs, gconstpointer g_hash_table_get_key(GHashTable *hash_table, int pos); gconspointer g_hash_table_get_value(GHashTable *hash_table, int pos); The two APIs allows one to iterate the hash table, for example, for (i = 0; i g_hash_table_size(hash_table); i++) { gconstpointer value = g_hash_table_get_value(hash_table, i); /* Do some processing here */ if (is_match(value)) return TRUE; } Should we add the APIs? Regards, Tony Cheung ___ gtk-list mailing list [EMAIL PROTECTED] http://mail.gnome.org/mailman/listinfo/gtk-list
glib API for iterating the g_hash_table
Hi, Is there any API for iterating GHashTable? 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. Currently, there is g_hash_table_size(), we could easily add iteration by providing the following two new APIs, gconstpointer g_hash_table_get_key(GHashTable *hash_table, int pos); gconspointer g_hash_table_get_value(GHashTable *hash_table, int pos); The two APIs allows one to iterate the hash table, for example, for (i = 0; i g_hash_table_size(hash_table); i++) { gconstpointer value = g_hash_table_get_value(hash_table, i); /* Do some processing here */ if (is_match(value)) return TRUE; } Should we add the APIs? Regards, Tony Cheung ___ gtk-list mailing list [EMAIL PROTECTED] http://mail.gnome.org/mailman/listinfo/gtk-list
Re: API for iterating the g_hash_table
Sven Neumann wrote: Hi, Tony Yat-Tung Cheung [EMAIL PROTECTED] writes: Currently, there is g_hash_table_size(), we could easily add iteration by providing the following two new APIs, gconstpointer g_hash_table_get_key(GHashTable *hash_table, int pos); gconspointer g_hash_table_get_value(GHashTable *hash_table, int pos); The two APIs allows one to iterate the hash table, for example, for (i = 0; i g_hash_table_size(hash_table); i++) { gconstpointer value = g_hash_table_get_value(hash_table, i); /* Do some processing here */ if (is_match(value)) return TRUE; } This looks rather akward since it exposes internals like the position of an element in the hash table. If this functionality is actually needed (do you have a use case?), then it should be an extended version of g_hash_table_foreach() that stops traversal when the callback function returns TRUE (just like g_tree_foreach). Sven Yes, the proposed API may expose too much implementation details in some views. For perfect API, one may want to use an iterator, instead of an integer index. Then APIs will become, gconstpointer g_hash_table_get_key(GHashTable *hash_table, GHashTableIterator iterator); gconspointer g_hash_table_get_value(GHashTable *hash_table, GHashTableIterator iterator); It's certainly an improvement for g_hash_table_foreach() to stop traversal when the callback function returns TRUE. We should have this, except that it breaks the current API. Even with it, sometimes the user may also want to do more than just stopping the trasversal. He/she may also want to get the current node, return a special value from the current function...etc. I think the most convenient API is an iterator, either in a simple integer index or an iterator structure. Thanks. Tony Cheung
Re: API for iterating the g_hash_table
[EMAIL PROTECTED] 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