+2005-12-07 Martyn Russell <martyn@imendio.com>
+
+ * 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 <martyn@imendio.com>
* docs/reference/glib/glib-sections.txt:
+2005-12-07 Martyn Russell <martyn@imendio.com>
+
+ * 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 <martyn@imendio.com>
* docs/reference/glib/glib-sections.txt:
+2005-12-07 Martyn Russell <martyn@imendio.com>
+
+ * 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 <martyn@imendio.com>
* docs/reference/glib/glib-sections.txt:
gint32 ref_count;
};
+typedef struct {
+ GCompareDataFunc func;
+ gpointer user_data;
+} SortData;
+
/**
* g_async_queue_new:
*
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
GCompareDataFunc func,
gpointer user_data)
{
- GQueue *q;
- GList *list;
+ SortData sd;
g_return_if_fail (queue != NULL);
- q = queue->queue;
-
- list = q->head;
- while (list && func (list->data, data, user_data) < 0)
- list = list->next;
+ sd.func = func;
+ sd.user_data = user_data;
- 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
* 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:
* <informalexample><programlisting>
- * 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);
* </programlisting></informalexample>
*
* Since: 2.10
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"
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