From: Owen Taylor Date: Fri, 13 Nov 1998 20:50:41 +0000 (+0000) Subject: Added g_list_sort() and g_slist_sort() to merge sort GLists and GSLists. X-Git-Url: http://git.openbox.org/?a=commitdiff_plain;h=be7ab912ee3521a4edfdf8e95977d4259da1bc29;p=dana%2Fcg-glib.git Added g_list_sort() and g_slist_sort() to merge sort GLists and GSLists. Fri Nov 13 15:17:34 1998 Owen Taylor * glist.c gslist.c glib.h: Added g_list_sort() and g_slist_sort() to merge sort GLists and GSLists. Submitted by Sven Over over a year ago! * testglib.c: Test the new sort functions. --- diff --git a/ChangeLog b/ChangeLog index 83bdc892..7beeaef5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Fri Nov 13 15:17:34 1998 Owen Taylor + + * glist.c gslist.c glib.h: Added g_list_sort() and + g_slist_sort() to merge sort GLists and GSLists. + Submitted by Sven Over + over a year ago! + + * testglib.c: Test the new sort functions. + Wed Nov 11 23:23:22 EST 1998 Jeff Garzik * 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 Sun Sep 20 18:21:46 1998 Owen Taylor - * 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 diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 83bdc892..7beeaef5 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,12 @@ +Fri Nov 13 15:17:34 1998 Owen Taylor + + * glist.c gslist.c glib.h: Added g_list_sort() and + g_slist_sort() to merge sort GLists and GSLists. + Submitted by Sven Over + over a year ago! + + * testglib.c: Test the new sort functions. + Wed Nov 11 23:23:22 EST 1998 Jeff Garzik * 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 Sun Sep 20 18:21:46 1998 Owen Taylor - * 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 diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 83bdc892..7beeaef5 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,12 @@ +Fri Nov 13 15:17:34 1998 Owen Taylor + + * glist.c gslist.c glib.h: Added g_list_sort() and + g_slist_sort() to merge sort GLists and GSLists. + Submitted by Sven Over + over a year ago! + + * testglib.c: Test the new sort functions. + Wed Nov 11 23:23:22 EST 1998 Jeff Garzik * 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 Sun Sep 20 18:21:46 1998 Owen Taylor - * 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 diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 83bdc892..7beeaef5 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,12 @@ +Fri Nov 13 15:17:34 1998 Owen Taylor + + * glist.c gslist.c glib.h: Added g_list_sort() and + g_slist_sort() to merge sort GLists and GSLists. + Submitted by Sven Over + over a year ago! + + * testglib.c: Test the new sort functions. + Wed Nov 11 23:23:22 EST 1998 Jeff Garzik * 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 Sun Sep 20 18:21:46 1998 Owen Taylor - * 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 diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 83bdc892..7beeaef5 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,12 @@ +Fri Nov 13 15:17:34 1998 Owen Taylor + + * glist.c gslist.c glib.h: Added g_list_sort() and + g_slist_sort() to merge sort GLists and GSLists. + Submitted by Sven Over + over a year ago! + + * testglib.c: Test the new sort functions. + Wed Nov 11 23:23:22 EST 1998 Jeff Garzik * 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 Sun Sep 20 18:21:46 1998 Owen Taylor - * 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 diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 83bdc892..7beeaef5 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,12 @@ +Fri Nov 13 15:17:34 1998 Owen Taylor + + * glist.c gslist.c glib.h: Added g_list_sort() and + g_slist_sort() to merge sort GLists and GSLists. + Submitted by Sven Over + over a year ago! + + * testglib.c: Test the new sort functions. + Wed Nov 11 23:23:22 EST 1998 Jeff Garzik * 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 Sun Sep 20 18:21:46 1998 Owen Taylor - * 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 diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 83bdc892..7beeaef5 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,12 @@ +Fri Nov 13 15:17:34 1998 Owen Taylor + + * glist.c gslist.c glib.h: Added g_list_sort() and + g_slist_sort() to merge sort GLists and GSLists. + Submitted by Sven Over + over a year ago! + + * testglib.c: Test the new sort functions. + Wed Nov 11 23:23:22 EST 1998 Jeff Garzik * 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 Sun Sep 20 18:21:46 1998 Owen Taylor - * 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 diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 83bdc892..7beeaef5 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,12 @@ +Fri Nov 13 15:17:34 1998 Owen Taylor + + * glist.c gslist.c glib.h: Added g_list_sort() and + g_slist_sort() to merge sort GLists and GSLists. + Submitted by Sven Over + over a year ago! + + * testglib.c: Test the new sort functions. + Wed Nov 11 23:23:22 EST 1998 Jeff Garzik * 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 Sun Sep 20 18:21:46 1998 Owen Taylor - * 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 diff --git a/glib.h b/glib.h index 9f6c517a..af2b258d 100644 --- a/glib.h +++ b/glib.h @@ -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) diff --git a/glib/glib.h b/glib/glib.h index 9f6c517a..af2b258d 100644 --- a/glib/glib.h +++ b/glib/glib.h @@ -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) diff --git a/glib/glist.c b/glib/glist.c index dc211580..eaa77d82 100644 --- a/glib/glist.c +++ b/glib/glist.c @@ -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); +} diff --git a/glib/gslist.c b/glib/gslist.c index 3a201b41..e9529002 100644 --- a/glib/gslist.c +++ b/glib/gslist.c @@ -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); +} diff --git a/glist.c b/glist.c index dc211580..eaa77d82 100644 --- a/glist.c +++ b/glist.c @@ -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); +} diff --git a/gslist.c b/gslist.c index 3a201b41..e9529002 100644 --- a/gslist.c +++ b/gslist.c @@ -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); +} diff --git a/testglib.c b/testglib.c index 6a763899..e443a859 100644 --- a/testglib.c +++ b/testglib.c @@ -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); diff --git a/tests/testglib.c b/tests/testglib.c index 6a763899..e443a859 100644 --- a/tests/testglib.c +++ b/tests/testglib.c @@ -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);