GNOME Bugzilla – Bug 60198
Want functions to convert between glib data structures
Last modified: 2013-02-03 05:55:45 UTC
> While Gnumeric-hacking, I quite often find it necessary to convert > from one glib data structure to another. Glib has no provisions > for that, it seems. > > Typically, things have been built and maintained as a hash for > speed, but is needed as a list in the end. This can certainly > be done with the current method of _foreach, but it's cumbersome > and quite inelegant in C. > > Would it be acceptable to add a bunch of functions to glib? > Specifically... > > GSList *g_hash_table_to_slist (GHash *h, gboolean keys, gboolean values); > GList *g_hash_table_to_list (GHash *h, gboolean keys, gboolean values); > GPtrArray *g_hash_table_to_ptrarray (GHash *h, gboolean keys, gboolean values); > > And possibly, for naming convenience: > > #define g_hash_table_domain_slist(h) \ > g_hash_table_to_slist (h, TRUE, FALSE) > #define g_hash_table_range_slist(h) \ > g_hash_table_to_slist (h, FALSE, TRUE) > > etc. > > These should probably be placed in a file of their own, to avoid tying > the .o files together with inter-file references. > > Comments?
Adding keywords. For the record, I am suggesting this for 2.0. I know my chances of getting that are, shall we say, slim.
/me spots g_hash_table_slist_values in gtk+/gtk/gtkaccelmap.c These functions are clearly needed -- any hope?
*** Bug 117994 has been marked as a duplicate of this bug. ***
Just for the record (since I don't think people reads duplicates) the functions suggested in bug 117994 are the following and are currently included in the glib extensions of the eel library: GList * g_list_from_g_slist (GSList *slist) { GList *list; GSList *node; list = NULL; for (node = slist; node; node = node->next) { list = g_list_prepend (list, node->data); } return g_list_reverse (list); } GSList * g_slist_from_g_list (GList *list) { GSList *slist; GList *node; slist = NULL; for (node = list; node; node = node->next) { slist = g_slist_prepend (slist, node->data); } return g_slist_reverse (slist); }
Rather than the g_hash_table_to_slist/g_hash_table_to_list functions described above, why not create uniform conversions that follow the form <destination>_from_<source>? I think it would be a good idea to omit the extra "g_" from the source data type: GList *g_list_from_slist(GSList*) GList *g_list_from_hash_table_keys(GHashTable*) GList *g_list_from_hash_table_values(GHashTable*) GSList *g_slist_from_list(GList*) GSList *g_slist_from_hash_table_keys(GHashTable*) GSList *g_slist_from_hash_table_values(GHashTable*) Thoughts?
The current naming convention is that a GHashTable "method" takes a GHashTable* as its first argument, and is named g_hash_table_*. That's a good convention IMHO.
Alright. So: GList *g_slist_to_list(GSList*) GList *g_hash_table_keys_to_list(GHashTable*) GList *g_hash_table_values_to_list(GHashTable*) ... I often find with such operations, you sometimes want to combine many sources. Consider also: void g_hash_table_keys_into_slist(GHashTable*, GSList*); would append its keys to the end of the given GSList.
Morten just pointed out that some part of this is done, at least g_hash_table_get_keys/values are there now.