[glib] GQueue: accept a NULL sibling for insert_before() and insert_after()
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] GQueue: accept a NULL sibling for insert_before() and insert_after()
- Date: Mon, 10 Nov 2014 17:09:06 +0000 (UTC)
commit 8a90f5e9f6da778743aaec365ee4ceb62b717130
Author: Sébastien Wilmet <swilmet gnome org>
Date: Wed Nov 5 14:03:30 2014 +0100
GQueue: accept a NULL sibling for insert_before() and insert_after()
It simplifies a little bit some code that inserts data relative to a
GList location, that might be NULL for the tail of the queue. A NULL
sibling is probably less useful for insert_after(), so it's more for
consistency with insert_before().
https://bugzilla.gnome.org/show_bug.cgi?id=736620
glib/gqueue.c | 35 ++++++++++++++++++++++++-----------
glib/tests/queue.c | 2 ++
2 files changed, 26 insertions(+), 11 deletions(-)
---
diff --git a/glib/gqueue.c b/glib/gqueue.c
index e93886d..d3c5ca5 100644
--- a/glib/gqueue.c
+++ b/glib/gqueue.c
@@ -983,12 +983,14 @@ g_queue_remove_all (GQueue *queue,
/**
* g_queue_insert_before:
* @queue: a #GQueue
- * @sibling: a #GList link that must be part of @queue
+ * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
+ * push at the tail of the queue.
* @data: the data to insert
*
* Inserts @data into @queue before @sibling.
*
- * @sibling must be part of @queue.
+ * @sibling must be part of @queue. Since GLib 2.44 a %NULL sibling pushes the
+ * data at the tail of the queue.
*
* Since: 2.4
*/
@@ -998,21 +1000,33 @@ g_queue_insert_before (GQueue *queue,
gpointer data)
{
g_return_if_fail (queue != NULL);
- g_return_if_fail (sibling != NULL);
- queue->head = g_list_insert_before (queue->head, sibling, data);
- queue->length++;
+ if (sibling == NULL)
+ {
+ /* We don't use g_list_insert_before() with a NULL sibling because it
+ * would be a O(n) operation and we would need to update manually the tail
+ * pointer.
+ */
+ g_queue_push_tail (queue, data);
+ }
+ else
+ {
+ queue->head = g_list_insert_before (queue->head, sibling, data);
+ queue->length++;
+ }
}
/**
* g_queue_insert_after:
* @queue: a #GQueue
- * @sibling: a #GList link that must be part of @queue
+ * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
+ * push at the head of the queue.
* @data: the data to insert
*
- * Inserts @data into @queue after @sibling
+ * Inserts @data into @queue after @sibling.
*
- * @sibling must be part of @queue
+ * @sibling must be part of @queue. Since GLib 2.44 a %NULL sibling pushes the
+ * data at the head of the queue.
*
* Since: 2.4
*/
@@ -1022,10 +1036,9 @@ g_queue_insert_after (GQueue *queue,
gpointer data)
{
g_return_if_fail (queue != NULL);
- g_return_if_fail (sibling != NULL);
- if (sibling == queue->tail)
- g_queue_push_tail (queue, data);
+ if (sibling == NULL)
+ g_queue_push_head (queue, data);
else
g_queue_insert_before (queue, sibling->next, data);
}
diff --git a/glib/tests/queue.c b/glib/tests/queue.c
index f7c7276..4f5e753 100644
--- a/glib/tests/queue.c
+++ b/glib/tests/queue.c
@@ -529,6 +529,7 @@ random_test (gconstpointer d)
g_queue_insert_before (q, qinf->tail, x);
g_queue_insert_before (q, qinf->head, x);
g_queue_insert_before (q, g_queue_find (q, x), x);
+ g_queue_insert_before (q, NULL, x);
}
qinf->head = q->head;
qinf->tail = q->tail;
@@ -542,6 +543,7 @@ random_test (gconstpointer d)
g_queue_insert_after (q, qinf->tail, x);
g_queue_insert_after (q, qinf->head, x);
g_queue_insert_after (q, g_queue_find (q, x), x);
+ g_queue_insert_after (q, NULL, x);
}
qinf->head = q->head;
qinf->tail = q->tail;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]