GQueue: accept a NULL sibling for insert_before() and insert_after()
authorSébastien Wilmet <swilmet@gnome.org>
Wed, 5 Nov 2014 13:03:30 +0000 (14:03 +0100)
committerSébastien Wilmet <swilmet@gnome.org>
Sun, 9 Nov 2014 19:42:49 +0000 (20:42 +0100)
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
glib/tests/queue.c

index e93886d..d3c5ca5 100644 (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);
 
-  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);
 }
index f7c7276..4f5e753 100644 (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;