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 440732 - Add split functions for the linked lists
Add split functions for the linked lists
Status: RESOLVED OBSOLETE
Product: glib
Classification: Platform
Component: glist
unspecified
Other Linux
: Normal enhancement
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2007-05-23 14:56 UTC by Emmanuele Bassi (:ebassi)
Modified: 2018-05-24 11:01 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Add split function for lists (5.81 KB, patch)
2007-05-23 14:57 UTC, Emmanuele Bassi (:ebassi)
none Details | Review

Description Emmanuele Bassi (:ebassi) 2007-05-23 14:56:06 UTC
lately, I've been using a lot this code pattern to clamp a list:

  if (g_list_length (list) > limit)
    {
      GList *l, *clamp;

      clamp = g_list_nth (list, limit - 1);
      l = clamp->next;
      l->prev = NULL;
      clamp->next = NULL;

      g_list_foreach (l, (GFunc) my_free_func, NULL);
      g_list_free (l);
    }

it would be interesting to have the clamping code directly inside GLib:

  GList *l = g_list_split (list, limit);
  g_list_foreach (l, (GFunc) my_free_func, NULL);
  g_list_free (l);

the attached patch implements g_list_split() and g_slist_split().
Comment 1 Emmanuele Bassi (:ebassi) 2007-05-23 14:57:03 UTC
Created attachment 88674 [details] [review]
Add split function for lists

This patch adds a g_list_split() and a g_slist_split() pair of functions.
Thos are the inverse functions of g_list_concat() and g_slist_concat(), and
split a G(S)List in two at the given position, returning the head of the
second list.

These functions are useful for clamping a list at a given number of elements,
and freeing the rest using g_list_foreach()+g_list_free().

This patch also adds the needed documentation for the newly added functions.

Signed-off-by: Emmanuele Bassi <ebassi@openedhand.com>
---
 docs/reference/glib/tmpl/linked_lists_double.sgml |   12 ++++++++++++
 docs/reference/glib/tmpl/linked_lists_single.sgml |   12 ++++++++++++
 glib/glib.symbols                                 |    2 ++
 glib/glist.c                                      |   19 +++++++++++++++++++
 glib/glist.h                                      |    2 ++
 glib/gslist.c                                     |   19 +++++++++++++++++++
 glib/gslist.h                                     |    2 ++
 7 files changed, 68 insertions(+), 0 deletions(-)
Comment 2 Soren Sandmann Pedersen 2007-05-23 15:50:19 UTC
What happens if n == 0? Don't you lose the head of the first list in that case?
Comment 3 Emmanuele Bassi (:ebassi) 2007-05-23 16:01:00 UTC
mmh, if n == 0 it segfaults segfaults at the moment, as g_list_nth(n - 1) would return an unchecked NULL.

either a g_return_val_if_fail (n > 0, NULL) should be added or if (n == 0) set the passed head pointer to NULL and return the same list. I must admit that the 0 case is a bit pathological, so I'd add a n > 0 guard.
Comment 4 Benjamin Otte (Company) 2007-08-22 11:58:34 UTC
Wouldn't it be nicer to have g_list_split take the new list head?
void g_list_split (GList *list, GList *head_of_split_list);

You could get the same behaviour as the current patch by using g_list_split (list, g_list_nth (list, limit)) that way, it'd be a lot easier to implement and be a lot more functional if you iterate through the list without keeping track of the index.

It would be O(n) for GSList though.
Comment 5 GNOME Infrastructure Team 2018-05-24 11:01:48 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/glib/issues/93.