diff --git a/ChangeLog b/ChangeLog index 4ae4699b0..d7dfc52bd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2005-12-07 Martyn Russell + + * glib/gasyncqueue.c: + - Call g_queue_insert_sorted() instead of duplicating the code. + - Call g_queue_sort() instead of duplicating the code. + - Invert sort function results to make sure the same sort function + gives the same results across glist, gslist, gqueue and + gasyncqueue. + + * tests/asyncqueue-test.c: + - Updated the sort function to reflect the example in the + documentation for gasyncqueue.c. + 2005-12-07 Martyn Russell * docs/reference/glib/glib-sections.txt: diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 4ae4699b0..d7dfc52bd 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,16 @@ +2005-12-07 Martyn Russell + + * glib/gasyncqueue.c: + - Call g_queue_insert_sorted() instead of duplicating the code. + - Call g_queue_sort() instead of duplicating the code. + - Invert sort function results to make sure the same sort function + gives the same results across glist, gslist, gqueue and + gasyncqueue. + + * tests/asyncqueue-test.c: + - Updated the sort function to reflect the example in the + documentation for gasyncqueue.c. + 2005-12-07 Martyn Russell * docs/reference/glib/glib-sections.txt: diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 4ae4699b0..d7dfc52bd 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,16 @@ +2005-12-07 Martyn Russell + + * glib/gasyncqueue.c: + - Call g_queue_insert_sorted() instead of duplicating the code. + - Call g_queue_sort() instead of duplicating the code. + - Invert sort function results to make sure the same sort function + gives the same results across glist, gslist, gqueue and + gasyncqueue. + + * tests/asyncqueue-test.c: + - Updated the sort function to reflect the example in the + documentation for gasyncqueue.c. + 2005-12-07 Martyn Russell * docs/reference/glib/glib-sections.txt: diff --git a/glib/gasyncqueue.c b/glib/gasyncqueue.c index 2cd252f92..b44cd287b 100644 --- a/glib/gasyncqueue.c +++ b/glib/gasyncqueue.c @@ -39,6 +39,11 @@ struct _GAsyncQueue gint32 ref_count; }; +typedef struct { + GCompareDataFunc func; + gpointer user_data; +} SortData; + /** * g_async_queue_new: * @@ -255,6 +260,14 @@ g_async_queue_push_sorted (GAsyncQueue *queue, g_mutex_unlock (queue->mutex); } +static gint +g_async_queue_invert_compare (gpointer v1, + gpointer v2, + SortData *sd) +{ + return -sd->func (v1, v2, sd->user_data); +} + /** * g_async_queue_push_sorted_unlocked: * @queue: a #GAsyncQueue @@ -284,21 +297,17 @@ g_async_queue_push_sorted_unlocked (GAsyncQueue *queue, GCompareDataFunc func, gpointer user_data) { - GQueue *q; - GList *list; + SortData sd; g_return_if_fail (queue != NULL); - q = queue->queue; + sd.func = func; + sd.user_data = user_data; - list = q->head; - while (list && func (list->data, data, user_data) < 0) - list = list->next; - - if (list) - g_queue_insert_before (q, list, data); - else - g_queue_push_tail (q, data); + g_queue_insert_sorted (queue->queue, + data, + (GCompareDataFunc)g_async_queue_invert_compare, + &sd); } static gpointer @@ -554,13 +563,13 @@ g_async_queue_length_unlocked (GAsyncQueue* queue) * If you were sorting a list of priority numbers to make sure the * lowest priority would be at the top of the queue, you could use: * - * gint id1; - * gint id2; + * gint32 id1; + * gint32 id2; * * id1 = GPOINTER_TO_INT (element1); * id2 = GPOINTER_TO_INT (element2); * - * return (id2 - id1); + * return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1); * * * Since: 2.10 @@ -600,17 +609,18 @@ g_async_queue_sort_unlocked (GAsyncQueue *queue, GCompareDataFunc func, gpointer user_data) { - GQueue *q; + SortData sd; g_return_if_fail (queue != NULL); g_return_if_fail (func != NULL); - q = queue->queue; + sd.func = func; + sd.user_data = user_data; - q->head = g_list_sort_with_data (q->head, func, user_data); - q->tail = g_list_last (q->head); + g_queue_sort (queue->queue, + (GCompareDataFunc)g_async_queue_invert_compare, + &sd); } - #define __G_ASYNCQUEUE_C__ #include "galiasdef.c" diff --git a/tests/asyncqueue-test.c b/tests/asyncqueue-test.c index 810705bc2..3072994d5 100644 --- a/tests/asyncqueue-test.c +++ b/tests/asyncqueue-test.c @@ -37,16 +37,16 @@ static GAsyncQueue *async_queue = NULL; static gint sort_compare (gconstpointer p1, gconstpointer p2, gpointer user_data) { - gint id1; - gint id2; + gint32 id1; + gint32 id2; id1 = GPOINTER_TO_INT (p1); id2 = GPOINTER_TO_INT (p2); d(g_print ("comparing #1:%d and #2:%d, returning %d\n", - id1, id2, (id2 - id1))); + id1, id2, (id1 > id2 ? +1 : id1 == id2 ? 0 : -1))); - return (id2 - id1); + return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1); } static gboolean