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
This commit is contained in:
Sébastien Wilmet 2014-11-05 14:03:30 +01:00
parent 012c9dcd82
commit 8a90f5e9f6
2 changed files with 26 additions and 11 deletions

View File

@ -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);
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);
}

View File

@ -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;