- 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:
Martyn James Russell 2005-12-07 11:35:27 +00:00
parent d63b58e8c5
commit a77f5ff0b6
7 changed files with 220 additions and 152 deletions

View File

@ -1620,6 +1620,7 @@ g_list_copy
g_list_reverse
g_list_sort
GCompareFunc
g_list_insert_sorted_with_data
g_list_sort_with_data
GCompareDataFunc
g_list_concat
@ -1670,6 +1671,7 @@ g_slist_free1
g_slist_length
g_slist_copy
g_slist_reverse
g_slist_insert_sorted_with_data
g_slist_sort
g_slist_sort_with_data
g_slist_concat

View File

@ -176,6 +176,22 @@ the sort order.
@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 ##### -->
<para>
Removes an element from a #GList.

View File

@ -183,6 +183,21 @@ number > 0 if the first parameter comes after the second parameter in
the sort order.
@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 ##### -->
<para>

View File

@ -494,11 +494,11 @@ g_list_foreach (GList *list,
}
}
GList*
g_list_insert_sorted (GList *list,
static GList*
g_list_insert_sorted_real (GList *list,
gpointer data,
GCompareFunc func)
GFunc func,
gpointer user_data)
{
GList *tmp_list = list;
GList *new_list;
@ -513,12 +513,13 @@ g_list_insert_sorted (GList *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))
{
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 ();
@ -545,11 +546,27 @@ g_list_insert_sorted (GList *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 *
g_list_sort_merge (GList *l1,
GList *l2,
GFunc compare_func,
gboolean use_data,
gpointer user_data)
{
GList list, *l, *lprev;
@ -560,10 +577,7 @@ g_list_sort_merge (GList *l1,
while (l1 && l2)
{
if (use_data)
cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
else
cmp = ((GCompareFunc) compare_func) (l1->data, l2->data);
if (cmp <= 0)
{
@ -588,7 +602,6 @@ g_list_sort_merge (GList *l1,
static GList*
g_list_sort_real (GList *list,
GFunc compare_func,
gboolean use_data,
gpointer user_data)
{
GList *l1, *l2;
@ -610,10 +623,9 @@ g_list_sort_real (GList *list,
l2 = l1->next;
l1->next = NULL;
return g_list_sort_merge (g_list_sort_real (list, compare_func, use_data, user_data),
g_list_sort_real (l2, 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, user_data),
compare_func,
use_data,
user_data);
}
@ -621,7 +633,7 @@ GList *
g_list_sort (GList *list,
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,
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__

View File

@ -56,6 +56,10 @@ GList* g_list_insert (GList *list,
GList* g_list_insert_sorted (GList *list,
gpointer data,
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 *sibling,
gpointer data);
@ -98,6 +102,7 @@ GList* g_list_sort_with_data (GList *list,
gpointer g_list_nth_data (GList *list,
guint n);
#define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
#define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)

View File

@ -463,10 +463,11 @@ g_slist_foreach (GSList *list,
}
}
GSList*
g_slist_insert_sorted (GSList *list,
static GSList*
g_slist_insert_sorted_real (GSList *list,
gpointer data,
GCompareFunc func)
GFunc func,
gpointer user_data)
{
GSList *tmp_list = list;
GSList *prev_list = NULL;
@ -482,13 +483,14 @@ g_slist_insert_sorted (GSList *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))
{
prev_list = tmp_list;
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 ();
@ -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 *
g_slist_sort_merge (GSList *l1,
GSList *l2,
GFunc compare_func,
gboolean use_data,
gpointer user_data)
{
GSList list, *l;
@ -527,10 +545,7 @@ g_slist_sort_merge (GSList *l1,
while (l1 && l2)
{
if (use_data)
cmp = ((GCompareDataFunc) compare_func) (l1->data, l2->data, user_data);
else
cmp = ((GCompareFunc) compare_func) (l1->data, l2->data);
if (cmp <= 0)
{
@ -551,7 +566,6 @@ g_slist_sort_merge (GSList *l1,
static GSList *
g_slist_sort_real (GSList *list,
GFunc compare_func,
gboolean use_data,
gpointer user_data)
{
GSList *l1, *l2;
@ -573,10 +587,9 @@ g_slist_sort_real (GSList *list,
l2 = l1->next;
l1->next = NULL;
return g_slist_sort_merge (g_slist_sort_real (list, compare_func, use_data, user_data),
g_slist_sort_real (l2, 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, user_data),
compare_func,
use_data,
user_data);
}
@ -584,7 +597,7 @@ GSList *
g_slist_sort (GSList *list,
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 *
@ -592,7 +605,7 @@ g_slist_sort_with_data (GSList *list,
GCompareDataFunc compare_func,
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__

View File

@ -55,6 +55,10 @@ GSList* g_slist_insert (GSList *list,
GSList* g_slist_insert_sorted (GSList *list,
gpointer data,
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 *sibling,
gpointer data);
@ -93,6 +97,7 @@ GSList* g_slist_sort_with_data (GSList *list,
gpointer user_data);
gpointer g_slist_nth_data (GSList *list,
guint n);
#define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
#ifndef G_DISABLE_DEPRECATED