Added g_list_sort() and g_slist_sort() to merge sort GLists and GSLists.

Fri Nov 13 15:17:34 1998  Owen Taylor  <otaylor@redhat.com>

	* glist.c gslist.c glib.h: Added g_list_sort() and
	g_slist_sort() to merge sort GLists and GSLists.
	Submitted by Sven Over <sven.over@ob.kamp.net>
	over a year ago!

	* testglib.c: Test the new sort functions.
This commit is contained in:
Owen Taylor 1998-11-13 20:50:41 +00:00 committed by Owen Taylor
parent 242cb51bfe
commit be7ab912ee
16 changed files with 406 additions and 10 deletions

View File

@ -1,3 +1,12 @@
Fri Nov 13 15:17:34 1998 Owen Taylor <otaylor@redhat.com>
* glist.c gslist.c glib.h: Added g_list_sort() and
g_slist_sort() to merge sort GLists and GSLists.
Submitted by Sven Over <sven.over@ob.kamp.net>
over a year ago!
* testglib.c: Test the new sort functions.
Wed Nov 11 23:23:22 EST 1998 Jeff Garzik <jgarzik@pobox.com>
* Makefile.am : INCLUDES is the right way to add to CFLAGS, not
@ -482,7 +491,7 @@ Mon Sep 21 02:22:12 1998 Tim Janik <timj@gtk.org>
Sun Sep 20 18:21:46 1998 Owen Taylor <otaylor@redhat.com>
* ltconfig.sh: Patch to libtool-1.2b to make --disable-static
* ltmain.sh: Patch to libtool-1.2b to make --disable-static
work.
Sun Sep 20 02:09:44 1998 Josh MacDonald <jmacd@axis.hip.berkeley.edu>

View File

@ -1,3 +1,12 @@
Fri Nov 13 15:17:34 1998 Owen Taylor <otaylor@redhat.com>
* glist.c gslist.c glib.h: Added g_list_sort() and
g_slist_sort() to merge sort GLists and GSLists.
Submitted by Sven Over <sven.over@ob.kamp.net>
over a year ago!
* testglib.c: Test the new sort functions.
Wed Nov 11 23:23:22 EST 1998 Jeff Garzik <jgarzik@pobox.com>
* Makefile.am : INCLUDES is the right way to add to CFLAGS, not
@ -482,7 +491,7 @@ Mon Sep 21 02:22:12 1998 Tim Janik <timj@gtk.org>
Sun Sep 20 18:21:46 1998 Owen Taylor <otaylor@redhat.com>
* ltconfig.sh: Patch to libtool-1.2b to make --disable-static
* ltmain.sh: Patch to libtool-1.2b to make --disable-static
work.
Sun Sep 20 02:09:44 1998 Josh MacDonald <jmacd@axis.hip.berkeley.edu>

View File

@ -1,3 +1,12 @@
Fri Nov 13 15:17:34 1998 Owen Taylor <otaylor@redhat.com>
* glist.c gslist.c glib.h: Added g_list_sort() and
g_slist_sort() to merge sort GLists and GSLists.
Submitted by Sven Over <sven.over@ob.kamp.net>
over a year ago!
* testglib.c: Test the new sort functions.
Wed Nov 11 23:23:22 EST 1998 Jeff Garzik <jgarzik@pobox.com>
* Makefile.am : INCLUDES is the right way to add to CFLAGS, not
@ -482,7 +491,7 @@ Mon Sep 21 02:22:12 1998 Tim Janik <timj@gtk.org>
Sun Sep 20 18:21:46 1998 Owen Taylor <otaylor@redhat.com>
* ltconfig.sh: Patch to libtool-1.2b to make --disable-static
* ltmain.sh: Patch to libtool-1.2b to make --disable-static
work.
Sun Sep 20 02:09:44 1998 Josh MacDonald <jmacd@axis.hip.berkeley.edu>

View File

@ -1,3 +1,12 @@
Fri Nov 13 15:17:34 1998 Owen Taylor <otaylor@redhat.com>
* glist.c gslist.c glib.h: Added g_list_sort() and
g_slist_sort() to merge sort GLists and GSLists.
Submitted by Sven Over <sven.over@ob.kamp.net>
over a year ago!
* testglib.c: Test the new sort functions.
Wed Nov 11 23:23:22 EST 1998 Jeff Garzik <jgarzik@pobox.com>
* Makefile.am : INCLUDES is the right way to add to CFLAGS, not
@ -482,7 +491,7 @@ Mon Sep 21 02:22:12 1998 Tim Janik <timj@gtk.org>
Sun Sep 20 18:21:46 1998 Owen Taylor <otaylor@redhat.com>
* ltconfig.sh: Patch to libtool-1.2b to make --disable-static
* ltmain.sh: Patch to libtool-1.2b to make --disable-static
work.
Sun Sep 20 02:09:44 1998 Josh MacDonald <jmacd@axis.hip.berkeley.edu>

View File

@ -1,3 +1,12 @@
Fri Nov 13 15:17:34 1998 Owen Taylor <otaylor@redhat.com>
* glist.c gslist.c glib.h: Added g_list_sort() and
g_slist_sort() to merge sort GLists and GSLists.
Submitted by Sven Over <sven.over@ob.kamp.net>
over a year ago!
* testglib.c: Test the new sort functions.
Wed Nov 11 23:23:22 EST 1998 Jeff Garzik <jgarzik@pobox.com>
* Makefile.am : INCLUDES is the right way to add to CFLAGS, not
@ -482,7 +491,7 @@ Mon Sep 21 02:22:12 1998 Tim Janik <timj@gtk.org>
Sun Sep 20 18:21:46 1998 Owen Taylor <otaylor@redhat.com>
* ltconfig.sh: Patch to libtool-1.2b to make --disable-static
* ltmain.sh: Patch to libtool-1.2b to make --disable-static
work.
Sun Sep 20 02:09:44 1998 Josh MacDonald <jmacd@axis.hip.berkeley.edu>

View File

@ -1,3 +1,12 @@
Fri Nov 13 15:17:34 1998 Owen Taylor <otaylor@redhat.com>
* glist.c gslist.c glib.h: Added g_list_sort() and
g_slist_sort() to merge sort GLists and GSLists.
Submitted by Sven Over <sven.over@ob.kamp.net>
over a year ago!
* testglib.c: Test the new sort functions.
Wed Nov 11 23:23:22 EST 1998 Jeff Garzik <jgarzik@pobox.com>
* Makefile.am : INCLUDES is the right way to add to CFLAGS, not
@ -482,7 +491,7 @@ Mon Sep 21 02:22:12 1998 Tim Janik <timj@gtk.org>
Sun Sep 20 18:21:46 1998 Owen Taylor <otaylor@redhat.com>
* ltconfig.sh: Patch to libtool-1.2b to make --disable-static
* ltmain.sh: Patch to libtool-1.2b to make --disable-static
work.
Sun Sep 20 02:09:44 1998 Josh MacDonald <jmacd@axis.hip.berkeley.edu>

View File

@ -1,3 +1,12 @@
Fri Nov 13 15:17:34 1998 Owen Taylor <otaylor@redhat.com>
* glist.c gslist.c glib.h: Added g_list_sort() and
g_slist_sort() to merge sort GLists and GSLists.
Submitted by Sven Over <sven.over@ob.kamp.net>
over a year ago!
* testglib.c: Test the new sort functions.
Wed Nov 11 23:23:22 EST 1998 Jeff Garzik <jgarzik@pobox.com>
* Makefile.am : INCLUDES is the right way to add to CFLAGS, not
@ -482,7 +491,7 @@ Mon Sep 21 02:22:12 1998 Tim Janik <timj@gtk.org>
Sun Sep 20 18:21:46 1998 Owen Taylor <otaylor@redhat.com>
* ltconfig.sh: Patch to libtool-1.2b to make --disable-static
* ltmain.sh: Patch to libtool-1.2b to make --disable-static
work.
Sun Sep 20 02:09:44 1998 Josh MacDonald <jmacd@axis.hip.berkeley.edu>

View File

@ -1,3 +1,12 @@
Fri Nov 13 15:17:34 1998 Owen Taylor <otaylor@redhat.com>
* glist.c gslist.c glib.h: Added g_list_sort() and
g_slist_sort() to merge sort GLists and GSLists.
Submitted by Sven Over <sven.over@ob.kamp.net>
over a year ago!
* testglib.c: Test the new sort functions.
Wed Nov 11 23:23:22 EST 1998 Jeff Garzik <jgarzik@pobox.com>
* Makefile.am : INCLUDES is the right way to add to CFLAGS, not
@ -482,7 +491,7 @@ Mon Sep 21 02:22:12 1998 Tim Janik <timj@gtk.org>
Sun Sep 20 18:21:46 1998 Owen Taylor <otaylor@redhat.com>
* ltconfig.sh: Patch to libtool-1.2b to make --disable-static
* ltmain.sh: Patch to libtool-1.2b to make --disable-static
work.
Sun Sep 20 02:09:44 1998 Josh MacDonald <jmacd@axis.hip.berkeley.edu>

4
glib.h
View File

@ -902,6 +902,8 @@ guint g_list_length (GList *list);
void g_list_foreach (GList *list,
GFunc func,
gpointer user_data);
GList* g_list_sort (GList *list,
GCompareFunc compare_func);
gpointer g_list_nth_data (GList *list,
guint n);
#define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
@ -946,6 +948,8 @@ guint g_slist_length (GSList *list);
void g_slist_foreach (GSList *list,
GFunc func,
gpointer user_data);
GSList* g_slist_sort (GSList *list,
GCompareFunc compare_func);
gpointer g_slist_nth_data (GSList *list,
guint n);
#define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)

View File

@ -902,6 +902,8 @@ guint g_list_length (GList *list);
void g_list_foreach (GList *list,
GFunc func,
gpointer user_data);
GList* g_list_sort (GList *list,
GCompareFunc compare_func);
gpointer g_list_nth_data (GList *list,
guint n);
#define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
@ -946,6 +948,8 @@ guint g_slist_length (GSList *list);
void g_slist_foreach (GSList *list,
GFunc func,
gpointer user_data);
GSList* g_slist_sort (GSList *list,
GCompareFunc compare_func);
gpointer g_slist_nth_data (GSList *list,
guint n);
#define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)

View File

@ -106,7 +106,7 @@ void
g_list_free (GList *list)
{
GList *last;
if (list)
{
last = g_list_last (list);
@ -479,3 +479,66 @@ g_list_insert_sorted (GList *list,
else
return list;
}
static GList *
g_list_sort_merge (GList *l1,
GList *l2,
GCompareFunc compare_func)
{
GList list, *l, *lprev;
l = &list;
lprev = NULL;
while (l1 && l2)
{
if (compare_func (l1->data, l2->data) < 0)
{
l->next = l1;
l = l->next;
l->prev = lprev;
lprev = l;
l1 = l1->next;
}
else
{
l->next = l2;
l = l->next;
l->prev = lprev;
lprev = l;
l2 = l2->next;
}
}
l->next = l1 ? l1 : l2;
l->next->prev = l;
return list.next;
}
GList*
g_list_sort (GList *list,
GCompareFunc compare_func)
{
GList *l1, *l2;
if (!list)
return NULL;
if (!list->next)
return list;
l1 = list;
l2 = list->next;
while ((l2 = l2->next) != NULL)
{
if ((l2 = l2->next) == NULL)
break;
l1 = l1->next;
}
l2 = l1->next;
l1->next = NULL;
return g_list_sort_merge (g_list_sort (list, compare_func),
g_list_sort (l2, compare_func),
compare_func);
}

View File

@ -454,3 +454,58 @@ g_slist_insert_sorted (GSList *list,
return new_list;
}
}
static GSList*
g_slist_sort_merge (GSList *l1,
GSList *l2,
GCompareFunc compare_func)
{
GSList list, *l;
l=&list;
while (l1 && l2)
{
if (compare_func(l1->data,l2->data) < 0)
{
l=l->next=l1;
l1=l1->next;
}
else
{
l=l->next=l2;
l2=l2->next;
}
}
l->next= l1 ? l1 : l2;
return list.next;
}
GSList*
g_slist_sort (GSList *list,
GCompareFunc compare_func)
{
GSList *l1, *l2;
if (!list)
return NULL;
if (!list->next)
return list;
l1 = list;
l2 = list->next;
while ((l2 = l2->next) != NULL)
{
if ((l2 = l2->next) == NULL)
break;
l1=l1->next;
}
l2 = l1->next;
l1->next = NULL;
return g_slist_sort_merge (g_slist_sort (list, compare_func),
g_slist_sort (l2, compare_func),
compare_func);
}

65
glist.c
View File

@ -106,7 +106,7 @@ void
g_list_free (GList *list)
{
GList *last;
if (list)
{
last = g_list_last (list);
@ -479,3 +479,66 @@ g_list_insert_sorted (GList *list,
else
return list;
}
static GList *
g_list_sort_merge (GList *l1,
GList *l2,
GCompareFunc compare_func)
{
GList list, *l, *lprev;
l = &list;
lprev = NULL;
while (l1 && l2)
{
if (compare_func (l1->data, l2->data) < 0)
{
l->next = l1;
l = l->next;
l->prev = lprev;
lprev = l;
l1 = l1->next;
}
else
{
l->next = l2;
l = l->next;
l->prev = lprev;
lprev = l;
l2 = l2->next;
}
}
l->next = l1 ? l1 : l2;
l->next->prev = l;
return list.next;
}
GList*
g_list_sort (GList *list,
GCompareFunc compare_func)
{
GList *l1, *l2;
if (!list)
return NULL;
if (!list->next)
return list;
l1 = list;
l2 = list->next;
while ((l2 = l2->next) != NULL)
{
if ((l2 = l2->next) == NULL)
break;
l1 = l1->next;
}
l2 = l1->next;
l1->next = NULL;
return g_list_sort_merge (g_list_sort (list, compare_func),
g_list_sort (l2, compare_func),
compare_func);
}

View File

@ -454,3 +454,58 @@ g_slist_insert_sorted (GSList *list,
return new_list;
}
}
static GSList*
g_slist_sort_merge (GSList *l1,
GSList *l2,
GCompareFunc compare_func)
{
GSList list, *l;
l=&list;
while (l1 && l2)
{
if (compare_func(l1->data,l2->data) < 0)
{
l=l->next=l1;
l1=l1->next;
}
else
{
l=l->next=l2;
l2=l2->next;
}
}
l->next= l1 ? l1 : l2;
return list.next;
}
GSList*
g_slist_sort (GSList *list,
GCompareFunc compare_func)
{
GSList *l1, *l2;
if (!list)
return NULL;
if (!list->next)
return list;
l1 = list;
l2 = list->next;
while ((l2 = l2->next) != NULL)
{
if ((l2 = l2->next) == NULL)
break;
l1=l1->next;
}
l2 = l1->next;
l1->next = NULL;
return g_slist_sort_merge (g_slist_sort (list, compare_func),
g_slist_sort (l2, compare_func),
compare_func);
}

View File

@ -425,6 +425,26 @@ main (int argc,
g_error ("Sorted insert failed");
}
g_list_free (list);
list = NULL;
for (i = 0; i < 10; i++)
list = g_list_prepend (list, &morenums[i]);
list = g_list_sort (list, my_list_compare_two);
/*
g_print("\n");
g_list_foreach (list, my_list_print, NULL);
*/
for (i = 0; i < 10; i++)
{
t = g_list_nth (list, i);
if (*((gint*) t->data) != (9 - i))
g_error ("Merge sort failed");
}
g_list_free (list);
g_print ("ok\n");
@ -473,6 +493,26 @@ main (int argc,
g_slist_foreach (slist, my_list_print, NULL);
*/
for (i = 0; i < 10; i++)
{
st = g_slist_nth (slist, i);
if (*((gint*) st->data) != (9 - i))
g_error("Sorted insert failed");
}
g_slist_free(slist);
slist = NULL;
for (i = 0; i < 10; i++)
slist = g_slist_prepend (slist, &morenums[i]);
slist = g_slist_sort (slist, my_list_compare_two);
/*
g_print("\n");
g_slist_foreach (slist, my_list_print, NULL);
*/
for (i = 0; i < 10; i++)
{
st = g_slist_nth (slist, i);

View File

@ -425,6 +425,26 @@ main (int argc,
g_error ("Sorted insert failed");
}
g_list_free (list);
list = NULL;
for (i = 0; i < 10; i++)
list = g_list_prepend (list, &morenums[i]);
list = g_list_sort (list, my_list_compare_two);
/*
g_print("\n");
g_list_foreach (list, my_list_print, NULL);
*/
for (i = 0; i < 10; i++)
{
t = g_list_nth (list, i);
if (*((gint*) t->data) != (9 - i))
g_error ("Merge sort failed");
}
g_list_free (list);
g_print ("ok\n");
@ -473,6 +493,26 @@ main (int argc,
g_slist_foreach (slist, my_list_print, NULL);
*/
for (i = 0; i < 10; i++)
{
st = g_slist_nth (slist, i);
if (*((gint*) st->data) != (9 - i))
g_error("Sorted insert failed");
}
g_slist_free(slist);
slist = NULL;
for (i = 0; i < 10; i++)
slist = g_slist_prepend (slist, &morenums[i]);
slist = g_slist_sort (slist, my_list_compare_two);
/*
g_print("\n");
g_slist_foreach (slist, my_list_print, NULL);
*/
for (i = 0; i < 10; i++)
{
st = g_slist_nth (slist, i);