mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-04-16 04:28:05 +02:00
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
This commit is contained in:
parent
6f8073d44a
commit
54e3ed17f0
@ -386,6 +386,32 @@ g_slist_concat (GSList *list1, GSList *list2)
|
|||||||
return list1;
|
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:
|
* g_slist_remove:
|
||||||
* @list: a #GSList
|
* @list: a #GSList
|
||||||
@ -401,26 +427,7 @@ GSList*
|
|||||||
g_slist_remove (GSList *list,
|
g_slist_remove (GSList *list,
|
||||||
gconstpointer data)
|
gconstpointer data)
|
||||||
{
|
{
|
||||||
GSList *tmp, *prev = NULL;
|
return _g_slist_remove_data (list, data, FALSE);
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -439,58 +446,27 @@ GSList*
|
|||||||
g_slist_remove_all (GSList *list,
|
g_slist_remove_all (GSList *list,
|
||||||
gconstpointer data)
|
gconstpointer data)
|
||||||
{
|
{
|
||||||
GSList *tmp, *prev = NULL;
|
return _g_slist_remove_data (list, data, TRUE);
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline GSList*
|
static inline GSList*
|
||||||
_g_slist_remove_link (GSList *list,
|
_g_slist_remove_link (GSList *list,
|
||||||
GSList *link)
|
GSList *link)
|
||||||
{
|
{
|
||||||
GSList *tmp;
|
GSList *tmp = NULL;
|
||||||
GSList *prev;
|
GSList **previous_ptr = &list;
|
||||||
|
|
||||||
prev = NULL;
|
while (*previous_ptr)
|
||||||
tmp = list;
|
|
||||||
|
|
||||||
while (tmp)
|
|
||||||
{
|
{
|
||||||
|
tmp = *previous_ptr;
|
||||||
if (tmp == link)
|
if (tmp == link)
|
||||||
{
|
{
|
||||||
if (prev)
|
*previous_ptr = tmp->next;
|
||||||
prev->next = tmp->next;
|
|
||||||
if (list == tmp)
|
|
||||||
list = list->next;
|
|
||||||
|
|
||||||
tmp->next = NULL;
|
tmp->next = NULL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
prev = tmp;
|
previous_ptr = &tmp->next;
|
||||||
tmp = tmp->next;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user