After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 60198 - Want functions to convert between glib data structures
Want functions to convert between glib data structures
Status: RESOLVED WONTFIX
Product: glib
Classification: Platform
Component: general
unspecified
Other All
: Normal enhancement
: ---
Assigned To: gtkdev
gtkdev
: 117994 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2001-09-07 18:53 UTC by Morten Welinder
Modified: 2013-02-03 05:55 UTC
See Also:
GNOME target: ---
GNOME version: Unversioned Enhancement



Description Morten Welinder 2001-09-07 18:53:54 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?
Comment 1 Morten Welinder 2001-09-07 18:56:24 UTC
Adding keywords.

For the record, I am suggesting this for 2.0.  I know my chances of
getting that are, shall we say, slim.
Comment 2 Morten Welinder 2002-03-14 18:55:57 UTC
/me spots g_hash_table_slist_values in gtk+/gtk/gtkaccelmap.c

These functions are clearly needed -- any hope?
Comment 3 Matthias Clasen 2003-07-22 14:47:13 UTC
*** Bug 117994 has been marked as a duplicate of this bug. ***
Comment 4 Paolo Borelli 2003-07-22 17:50:01 UTC
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);
}
Comment 5 Andrew Paprocki 2005-11-21 02:32:08 UTC
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?
Comment 6 Behdad Esfahbod 2005-11-21 04:24:31 UTC
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.
Comment 7 Paul Evans 2005-12-21 00:31:58 UTC
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.
Comment 8 Matthias Clasen 2007-12-07 16:21:29 UTC
Morten just pointed out that some part of this is done, at least
g_hash_table_get_keys/values are there now.