[glib] gslist: Simplified node removal and got rid of some code duplication



commit 54e3ed17f05f3bb2843024a828ba0591c34dcff4
Author: Christian Schramm <christian h m schramm gmail com>
Date:   Wed Jan 15 12:07:40 2014 +0100

    gslist: Simplified node removal and got rid of some code duplication
    
    Merged two almost identical functions for removing at most one
    or all nodes containing some data. Also simplified the code a bit
    by using a pointer to a pointer (see
    http://wordaligned.org/articles/two-star-programming).
    
    (Modified by Philip Withnall to fix two code formatting nitpicks.)
    
    https://bugzilla.gnome.org/show_bug.cgi?id=722256

 glib/gslist.c |   92 +++++++++++++++++++++------------------------------------
 1 files changed, 34 insertions(+), 58 deletions(-)
---
diff --git a/glib/gslist.c b/glib/gslist.c
index 518ee08..028237d 100644
--- a/glib/gslist.c
+++ b/glib/gslist.c
@@ -386,6 +386,32 @@ g_slist_concat (GSList *list1, GSList *list2)
   return list1;
 }
 
+static GSList*
+_g_slist_remove_data (GSList        *list,
+                      gconstpointer  data,
+                      gboolean       all)
+{
+  GSList *tmp = NULL;
+  GSList **previous_ptr = &list;
+
+  while (*previous_ptr)
+    {
+      tmp = *previous_ptr;
+      if (tmp->data == data)
+        {
+          *previous_ptr = tmp->next;
+          g_slist_free_1 (tmp);
+          if (!all)
+            break;
+        }
+      else
+        {
+          previous_ptr = &tmp->next;
+        }
+    }
+
+  return list;
+}
 /**
  * g_slist_remove:
  * @list: a #GSList
@@ -401,26 +427,7 @@ GSList*
 g_slist_remove (GSList        *list,
                 gconstpointer  data)
 {
-  GSList *tmp, *prev = NULL;
-
-  tmp = list;
-  while (tmp)
-    {
-      if (tmp->data == data)
-        {
-          if (prev)
-            prev->next = tmp->next;
-          else
-            list = tmp->next;
-
-          g_slist_free_1 (tmp);
-          break;
-        }
-      prev = tmp;
-      tmp = prev->next;
-    }
-
-  return list;
+  return _g_slist_remove_data (list, data, FALSE);
 }
 
 /**
@@ -439,58 +446,27 @@ GSList*
 g_slist_remove_all (GSList        *list,
                     gconstpointer  data)
 {
-  GSList *tmp, *prev = NULL;
-
-  tmp = list;
-  while (tmp)
-    {
-      if (tmp->data == data)
-        {
-          GSList *next = tmp->next;
-
-          if (prev)
-            prev->next = next;
-          else
-            list = next;
-
-          g_slist_free_1 (tmp);
-          tmp = next;
-        }
-      else
-        {
-          prev = tmp;
-          tmp = prev->next;
-        }
-    }
-
-  return list;
+  return _g_slist_remove_data (list, data, TRUE);
 }
 
 static inline GSList*
 _g_slist_remove_link (GSList *list,
                       GSList *link)
 {
-  GSList *tmp;
-  GSList *prev;
+  GSList *tmp = NULL;
+  GSList **previous_ptr = &list;
 
-  prev = NULL;
-  tmp = list;
-
-  while (tmp)
+  while (*previous_ptr)
     {
+      tmp = *previous_ptr;
       if (tmp == link)
         {
-          if (prev)
-            prev->next = tmp->next;
-          if (list == tmp)
-            list = list->next;
-
+          *previous_ptr = tmp->next;
           tmp->next = NULL;
           break;
         }
 
-      prev = tmp;
-      tmp = tmp->next;
+      previous_ptr = &tmp->next;
     }
 
   return list;


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