mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-02-10 12:55:48 +01:00
- Added g_list_insert_sorted_with_data () and
* docs/reference/glib/glib-sections.txt: * docs/reference/glib/tmpl/linked_lists_double.sgml: * docs/reference/glib/tmpl/linked_lists_single.sgml: * glib/glist.[ch]: * glib/gslist.[ch]: - Added g_list_insert_sorted_with_data () and g_slist_insert_sorted_with_data (). - Removed the extra check in g_list_sort() and g_slist_sort() for GCompareDataFunc vs. GCompareFunc.
This commit is contained in:
parent
d63b58e8c5
commit
a77f5ff0b6
@ -1620,6 +1620,7 @@ g_list_copy
|
|||||||
g_list_reverse
|
g_list_reverse
|
||||||
g_list_sort
|
g_list_sort
|
||||||
GCompareFunc
|
GCompareFunc
|
||||||
|
g_list_insert_sorted_with_data
|
||||||
g_list_sort_with_data
|
g_list_sort_with_data
|
||||||
GCompareDataFunc
|
GCompareDataFunc
|
||||||
g_list_concat
|
g_list_concat
|
||||||
@ -1670,6 +1671,7 @@ g_slist_free1
|
|||||||
g_slist_length
|
g_slist_length
|
||||||
g_slist_copy
|
g_slist_copy
|
||||||
g_slist_reverse
|
g_slist_reverse
|
||||||
|
g_slist_insert_sorted_with_data
|
||||||
g_slist_sort
|
g_slist_sort
|
||||||
g_slist_sort_with_data
|
g_slist_sort_with_data
|
||||||
g_slist_concat
|
g_slist_concat
|
||||||
|
@ -176,6 +176,22 @@ the sort order.
|
|||||||
@Returns: the new start of the #GList.
|
@Returns: the new start of the #GList.
|
||||||
|
|
||||||
|
|
||||||
|
<!-- ##### FUNCTION g_list_insert_sorted_with_data ##### -->
|
||||||
|
<para>
|
||||||
|
Inserts a new element into the list, using the given comparison function
|
||||||
|
to determine its position.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
@list: a pointer to a #GList.
|
||||||
|
@data: the data for the new element.
|
||||||
|
@func: the function to compare elements in the list. It should return a
|
||||||
|
number > 0 if the first parameter comes after the second parameter in
|
||||||
|
the sort order.
|
||||||
|
@user_data: user data to pass to comparison function.
|
||||||
|
@Returns: the new start of the #GList.
|
||||||
|
@Since 2.10
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### FUNCTION g_list_remove ##### -->
|
<!-- ##### FUNCTION g_list_remove ##### -->
|
||||||
<para>
|
<para>
|
||||||
Removes an element from a #GList.
|
Removes an element from a #GList.
|
||||||
|
@ -183,6 +183,21 @@ number > 0 if the first parameter comes after the second parameter in
|
|||||||
the sort order.
|
the sort order.
|
||||||
@Returns: the new start of the #GSList.
|
@Returns: the new start of the #GSList.
|
||||||
|
|
||||||
|
<!-- ##### FUNCTION g_slist_insert_sorted_with_data ##### -->
|
||||||
|
<para>
|
||||||
|
Inserts a new element into the list, using the given comparison function
|
||||||
|
to determine its position.
|
||||||
|
</para>
|
||||||
|
|
||||||
|
@list: a #GSList.
|
||||||
|
@data: the data for the new element.
|
||||||
|
@func: the function to compare elements in the list. It should return a
|
||||||
|
number > 0 if the first parameter comes after the second parameter in
|
||||||
|
the sort order.
|
||||||
|
@user_data: data to pass to comparison function.
|
||||||
|
@Returns: the new start of the #GSList.
|
||||||
|
@Since 2.10
|
||||||
|
|
||||||
|
|
||||||
<!-- ##### FUNCTION g_slist_remove ##### -->
|
<!-- ##### FUNCTION g_slist_remove ##### -->
|
||||||
<para>
|
<para>
|
||||||
|
44
glib/glist.c
44
glib/glist.c
@ -494,11 +494,11 @@ g_list_foreach (GList *list,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static GList*
|
||||||
GList*
|
g_list_insert_sorted_real (GList *list,
|
||||||
g_list_insert_sorted (GList *list,
|
|
||||||
gpointer data,
|
gpointer data,
|
||||||
GCompareFunc func)
|
GFunc func,
|
||||||
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
GList *tmp_list = list;
|
GList *tmp_list = list;
|
||||||
GList *new_list;
|
GList *new_list;
|
||||||
@ -513,12 +513,13 @@ g_list_insert_sorted (GList *list,
|
|||||||
return new_list;
|
return new_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
cmp = (*func) (data, tmp_list->data);
|
cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
|
||||||
|
|
||||||
while ((tmp_list->next) && (cmp > 0))
|
while ((tmp_list->next) && (cmp > 0))
|
||||||
{
|
{
|
||||||
tmp_list = tmp_list->next;
|
tmp_list = tmp_list->next;
|
||||||
cmp = (*func) (data, tmp_list->data);
|
|
||||||
|
cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
new_list = _g_list_alloc0 ();
|
new_list = _g_list_alloc0 ();
|
||||||
@ -545,11 +546,27 @@ g_list_insert_sorted (GList *list,
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GList*
|
||||||
|
g_list_insert_sorted (GList *list,
|
||||||
|
gpointer data,
|
||||||
|
GCompareFunc func)
|
||||||
|
{
|
||||||
|
return g_list_insert_sorted_real (list, data, (GFunc) func, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
GList*
|
||||||
|
g_list_insert_sorted_with_data (GList *list,
|
||||||
|
gpointer data,
|
||||||
|
GCompareDataFunc func,
|
||||||
|
gpointer user_data)
|
||||||
|
{
|
||||||
|
return g_list_insert_sorted_real (list, data, (GFunc) func, user_data);
|
||||||
|
}
|
||||||
|
|
||||||
static GList *
|
static GList *
|
||||||
g_list_sort_merge (GList *l1,
|
g_list_sort_merge (GList *l1,
|
||||||
GList *l2,
|
GList *l2,
|
||||||
GFunc compare_func,
|
GFunc compare_func,
|
||||||
gboolean use_data,
|
|
||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
GList list, *l, *lprev;
|
GList list, *l, *lprev;
|
||||||
@ -560,10 +577,7 @@ g_list_sort_merge (GList *l1,
|
|||||||
|
|
||||||
while (l1 && l2)
|
while (l1 && l2)
|
||||||
{
|
{
|
||||||
if (use_data)
|
|
||||||
cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
|
cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
|
||||||
else
|
|
||||||
cmp = ((GCompareFunc) compare_func) (l1->data, l2->data);
|
|
||||||
|
|
||||||
if (cmp <= 0)
|
if (cmp <= 0)
|
||||||
{
|
{
|
||||||
@ -588,7 +602,6 @@ g_list_sort_merge (GList *l1,
|
|||||||
static GList*
|
static GList*
|
||||||
g_list_sort_real (GList *list,
|
g_list_sort_real (GList *list,
|
||||||
GFunc compare_func,
|
GFunc compare_func,
|
||||||
gboolean use_data,
|
|
||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
GList *l1, *l2;
|
GList *l1, *l2;
|
||||||
@ -610,10 +623,9 @@ g_list_sort_real (GList *list,
|
|||||||
l2 = l1->next;
|
l2 = l1->next;
|
||||||
l1->next = NULL;
|
l1->next = NULL;
|
||||||
|
|
||||||
return g_list_sort_merge (g_list_sort_real (list, compare_func, use_data, user_data),
|
return g_list_sort_merge (g_list_sort_real (list, compare_func, user_data),
|
||||||
g_list_sort_real (l2, compare_func, use_data, user_data),
|
g_list_sort_real (l2, compare_func, user_data),
|
||||||
compare_func,
|
compare_func,
|
||||||
use_data,
|
|
||||||
user_data);
|
user_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -621,7 +633,7 @@ GList *
|
|||||||
g_list_sort (GList *list,
|
g_list_sort (GList *list,
|
||||||
GCompareFunc compare_func)
|
GCompareFunc compare_func)
|
||||||
{
|
{
|
||||||
return g_list_sort_real (list, (GFunc) compare_func, FALSE, NULL);
|
return g_list_sort_real (list, (GFunc) compare_func, NULL);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -630,7 +642,7 @@ g_list_sort_with_data (GList *list,
|
|||||||
GCompareDataFunc compare_func,
|
GCompareDataFunc compare_func,
|
||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
return g_list_sort_real (list, (GFunc) compare_func, TRUE, user_data);
|
return g_list_sort_real (list, (GFunc) compare_func, user_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define __G_LIST_C__
|
#define __G_LIST_C__
|
||||||
|
@ -56,6 +56,10 @@ GList* g_list_insert (GList *list,
|
|||||||
GList* g_list_insert_sorted (GList *list,
|
GList* g_list_insert_sorted (GList *list,
|
||||||
gpointer data,
|
gpointer data,
|
||||||
GCompareFunc func);
|
GCompareFunc func);
|
||||||
|
GList* g_list_insert_sorted_with_data (GList *list,
|
||||||
|
gpointer data,
|
||||||
|
GCompareDataFunc func,
|
||||||
|
gpointer user_data);
|
||||||
GList* g_list_insert_before (GList *list,
|
GList* g_list_insert_before (GList *list,
|
||||||
GList *sibling,
|
GList *sibling,
|
||||||
gpointer data);
|
gpointer data);
|
||||||
@ -98,6 +102,7 @@ GList* g_list_sort_with_data (GList *list,
|
|||||||
gpointer g_list_nth_data (GList *list,
|
gpointer g_list_nth_data (GList *list,
|
||||||
guint n);
|
guint n);
|
||||||
|
|
||||||
|
|
||||||
#define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
|
#define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
|
||||||
#define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
|
#define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
|
||||||
|
|
||||||
|
@ -463,10 +463,11 @@ g_slist_foreach (GSList *list,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GSList*
|
static GSList*
|
||||||
g_slist_insert_sorted (GSList *list,
|
g_slist_insert_sorted_real (GSList *list,
|
||||||
gpointer data,
|
gpointer data,
|
||||||
GCompareFunc func)
|
GFunc func,
|
||||||
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
GSList *tmp_list = list;
|
GSList *tmp_list = list;
|
||||||
GSList *prev_list = NULL;
|
GSList *prev_list = NULL;
|
||||||
@ -482,13 +483,14 @@ g_slist_insert_sorted (GSList *list,
|
|||||||
return new_list;
|
return new_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
cmp = (*func) (data, tmp_list->data);
|
cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
|
||||||
|
|
||||||
while ((tmp_list->next) && (cmp > 0))
|
while ((tmp_list->next) && (cmp > 0))
|
||||||
{
|
{
|
||||||
prev_list = tmp_list;
|
prev_list = tmp_list;
|
||||||
tmp_list = tmp_list->next;
|
tmp_list = tmp_list->next;
|
||||||
cmp = (*func) (data, tmp_list->data);
|
|
||||||
|
cmp = ((GCompareDataFunc) func) (data, tmp_list->data, user_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
new_list = _g_slist_alloc0 ();
|
new_list = _g_slist_alloc0 ();
|
||||||
@ -513,11 +515,27 @@ g_slist_insert_sorted (GSList *list,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GSList*
|
||||||
|
g_slist_insert_sorted (GSList *list,
|
||||||
|
gpointer data,
|
||||||
|
GCompareFunc func)
|
||||||
|
{
|
||||||
|
return g_slist_insert_sorted_real (list, data, (GFunc) func, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
GSList*
|
||||||
|
g_slist_insert_sorted_with_data (GSList *list,
|
||||||
|
gpointer data,
|
||||||
|
GCompareDataFunc func,
|
||||||
|
gpointer user_data)
|
||||||
|
{
|
||||||
|
return g_slist_insert_sorted_real (list, data, (GFunc) func, user_data);
|
||||||
|
}
|
||||||
|
|
||||||
static GSList *
|
static GSList *
|
||||||
g_slist_sort_merge (GSList *l1,
|
g_slist_sort_merge (GSList *l1,
|
||||||
GSList *l2,
|
GSList *l2,
|
||||||
GFunc compare_func,
|
GFunc compare_func,
|
||||||
gboolean use_data,
|
|
||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
GSList list, *l;
|
GSList list, *l;
|
||||||
@ -527,10 +545,7 @@ g_slist_sort_merge (GSList *l1,
|
|||||||
|
|
||||||
while (l1 && l2)
|
while (l1 && l2)
|
||||||
{
|
{
|
||||||
if (use_data)
|
|
||||||
cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
|
cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
|
||||||
else
|
|
||||||
cmp = ((GCompareFunc) compare_func) (l1->data, l2->data);
|
|
||||||
|
|
||||||
if (cmp <= 0)
|
if (cmp <= 0)
|
||||||
{
|
{
|
||||||
@ -551,7 +566,6 @@ g_slist_sort_merge (GSList *l1,
|
|||||||
static GSList *
|
static GSList *
|
||||||
g_slist_sort_real (GSList *list,
|
g_slist_sort_real (GSList *list,
|
||||||
GFunc compare_func,
|
GFunc compare_func,
|
||||||
gboolean use_data,
|
|
||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
GSList *l1, *l2;
|
GSList *l1, *l2;
|
||||||
@ -573,10 +587,9 @@ g_slist_sort_real (GSList *list,
|
|||||||
l2 = l1->next;
|
l2 = l1->next;
|
||||||
l1->next = NULL;
|
l1->next = NULL;
|
||||||
|
|
||||||
return g_slist_sort_merge (g_slist_sort_real (list, compare_func, use_data, user_data),
|
return g_slist_sort_merge (g_slist_sort_real (list, compare_func, user_data),
|
||||||
g_slist_sort_real (l2, compare_func, use_data, user_data),
|
g_slist_sort_real (l2, compare_func, user_data),
|
||||||
compare_func,
|
compare_func,
|
||||||
use_data,
|
|
||||||
user_data);
|
user_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -584,7 +597,7 @@ GSList *
|
|||||||
g_slist_sort (GSList *list,
|
g_slist_sort (GSList *list,
|
||||||
GCompareFunc compare_func)
|
GCompareFunc compare_func)
|
||||||
{
|
{
|
||||||
return g_slist_sort_real (list, (GFunc) compare_func, FALSE, NULL);
|
return g_slist_sort_real (list, (GFunc) compare_func, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
GSList *
|
GSList *
|
||||||
@ -592,7 +605,7 @@ g_slist_sort_with_data (GSList *list,
|
|||||||
GCompareDataFunc compare_func,
|
GCompareDataFunc compare_func,
|
||||||
gpointer user_data)
|
gpointer user_data)
|
||||||
{
|
{
|
||||||
return g_slist_sort_real (list, (GFunc) compare_func, TRUE, user_data);
|
return g_slist_sort_real (list, (GFunc) compare_func, user_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define __G_SLIST_C__
|
#define __G_SLIST_C__
|
||||||
|
@ -55,6 +55,10 @@ GSList* g_slist_insert (GSList *list,
|
|||||||
GSList* g_slist_insert_sorted (GSList *list,
|
GSList* g_slist_insert_sorted (GSList *list,
|
||||||
gpointer data,
|
gpointer data,
|
||||||
GCompareFunc func);
|
GCompareFunc func);
|
||||||
|
GSList* g_slist_insert_sorted_with_data (GSList *list,
|
||||||
|
gpointer data,
|
||||||
|
GCompareDataFunc func,
|
||||||
|
gpointer user_data);
|
||||||
GSList* g_slist_insert_before (GSList *slist,
|
GSList* g_slist_insert_before (GSList *slist,
|
||||||
GSList *sibling,
|
GSList *sibling,
|
||||||
gpointer data);
|
gpointer data);
|
||||||
@ -93,6 +97,7 @@ GSList* g_slist_sort_with_data (GSList *list,
|
|||||||
gpointer user_data);
|
gpointer user_data);
|
||||||
gpointer g_slist_nth_data (GSList *list,
|
gpointer g_slist_nth_data (GSList *list,
|
||||||
guint n);
|
guint n);
|
||||||
|
|
||||||
#define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
|
#define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
|
||||||
|
|
||||||
#ifndef G_DISABLE_DEPRECATED
|
#ifndef G_DISABLE_DEPRECATED
|
||||||
|
Loading…
x
Reference in New Issue
Block a user