Imported Upstream version 2.72.3
[platform/upstream/glib.git] / glib / gqueue.c
index 926539c..d53af7d 100644 (file)
@@ -7,7 +7,7 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  *
  * The #GQueue structure and its associated functions provide a standard
  * queue data structure. Internally, GQueue uses the same data structure
- * as #GList to store elements.
+ * as #GList to store elements with the same complexity over
+ * insertion/deletion (O(1)) and access/search (O(n)) operations.
  *
  * The data contained in each element can be either integer values, by
- * using one of the <link linkend="glib-Type-Conversion-Macros">Type
- * Conversion Macros</link>, or simply pointers to any type of data.
+ * using one of the [Type Conversion Macros][glib-Type-Conversion-Macros],
+ * or simply pointers to any type of data.
+ *
+ * As with all other GLib data structures, #GQueue is not thread-safe.
+ * For a thread-safe queue, use #GAsyncQueue.
  *
  * To create a new GQueue, use g_queue_new().
  *
- * To initialize a statically-allocated GQueue, use #G_QUEUE_INIT or
+ * To initialize a statically-allocated GQueue, use %G_QUEUE_INIT or
  * g_queue_init().
  *
  * To add elements, use g_queue_push_head(), g_queue_push_head_link(),
@@ -95,6 +99,9 @@ g_queue_free (GQueue *queue)
  * Convenience method, which frees all the memory used by a #GQueue,
  * and calls the specified destroy function on every element's data.
  *
+ * @free_func should not modify the queue (eg, by removing the freed
+ * element from it).
+ *
  * Since: 2.32
  */
 void
@@ -111,7 +118,7 @@ g_queue_free_full (GQueue        *queue,
  *
  * A statically-allocated #GQueue must be initialized with this function
  * before it can be used. Alternatively you can initialize it with
- * #G_QUEUE_INIT. It is not necessary to initialize queues created with
+ * %G_QUEUE_INIT. It is not necessary to initialize queues created with
  * g_queue_new().
  *
  * Since: 2.14
@@ -144,6 +151,28 @@ g_queue_clear (GQueue *queue)
 }
 
 /**
+ * g_queue_clear_full:
+ * @queue: a pointer to a #GQueue
+ * @free_func: (nullable): the function to be called to free memory allocated
+ *
+ * Convenience method, which frees all the memory used by a #GQueue,
+ * and calls the provided @free_func on each item in the #GQueue.
+ *
+ * Since: 2.60
+ */
+void
+g_queue_clear_full (GQueue          *queue,
+                    GDestroyNotify  free_func)
+{
+  g_return_if_fail (queue != NULL);
+
+  if (free_func != NULL)
+    g_queue_foreach (queue, (GFunc) free_func, NULL);
+
+  g_queue_clear (queue);
+}
+
+/**
  * g_queue_is_empty:
  * @queue: a #GQueue.
  *
@@ -165,7 +194,7 @@ g_queue_is_empty (GQueue *queue)
  * 
  * Returns the number of items in @queue.
  * 
- * Return value: the number of items in @queue
+ * Returns: the number of items in @queue
  * 
  * Since: 2.4
  */
@@ -202,7 +231,7 @@ g_queue_reverse (GQueue *queue)
  * queue consist of pointers to data, the pointers are copied, but the
  * actual data is not.
  * 
- * Return value: a copy of @queue
+ * Returns: a copy of @queue
  * 
  * Since: 2.4
  */
@@ -231,6 +260,9 @@ g_queue_copy (GQueue *queue)
  * Calls @func for each element in the queue passing @user_data to the
  * function.
  * 
+ * It is safe for @func to remove the element from @queue, but it must
+ * not modify any part of the queue after that element.
+ *
  * Since: 2.4
  */
 void
@@ -259,7 +291,7 @@ g_queue_foreach (GQueue   *queue,
  * 
  * Finds the first link in @queue which contains @data.
  * 
- * Return value: the first link in @queue which contains @data
+ * Returns: the first link in @queue which contains @data
  * 
  * Since: 2.4
  */
@@ -285,7 +317,7 @@ g_queue_find (GQueue        *queue,
  * takes two gconstpointer arguments, the #GQueue element's data as the
  * first argument and the given user data as the second argument.
  * 
- * Return value: the found link, or %NULL if it wasn't found
+ * Returns: the found link, or %NULL if it wasn't found
  * 
  * Since: 2.4
  */
@@ -363,7 +395,7 @@ g_queue_push_nth (GQueue   *queue,
 {
   g_return_if_fail (queue != NULL);
 
-  if (n < 0 || n >= queue->length)
+  if (n < 0 || (guint) n >= queue->length)
     {
       g_queue_push_tail (queue, data);
       return;
@@ -375,8 +407,7 @@ g_queue_push_nth (GQueue   *queue,
 /**
  * g_queue_push_head_link:
  * @queue: a #GQueue
- * @link_: a single #GList element, <emphasis>not</emphasis> a list with
- *     more than one element
+ * @link_: a single #GList element, not a list with more than one element
  *
  * Adds a new element at the head of the queue.
  */
@@ -422,8 +453,7 @@ g_queue_push_tail (GQueue   *queue,
 /**
  * g_queue_push_tail_link:
  * @queue: a #GQueue
- * @link_: a single #GList element, <emphasis>not</emphasis> a list with
- *   more than one element
+ * @link_: a single #GList element, not a list with more than one element
  *
  * Adds a new element at the tail of the queue.
  */
@@ -468,7 +498,7 @@ g_queue_push_nth_link (GQueue *queue,
   g_return_if_fail (queue != NULL);
   g_return_if_fail (link_ != NULL);
 
-  if (n < 0 || n >= queue->length)
+  if (n < 0 || (guint) n >= queue->length)
     {
       g_queue_push_tail_link (queue, link_);
       return;
@@ -490,8 +520,10 @@ g_queue_push_nth_link (GQueue *queue,
   if (queue->head->prev)
     queue->head = queue->head->prev;
 
-  if (queue->tail->next)
-    queue->tail = queue->tail->next;
+  /* The case where we’re pushing @link_ at the end of @queue is handled above
+   * using g_queue_push_tail_link(), so we should never have to manually adjust
+   * queue->tail. */
+  g_assert (queue->tail->next == NULL);
   
   queue->length++;
 }
@@ -569,7 +601,7 @@ g_queue_pop_head_link (GQueue *queue)
  * 
  * Returns the first link in @queue.
  * 
- * Return value: the first link in @queue, or %NULL if @queue is empty
+ * Returns: the first link in @queue, or %NULL if @queue is empty
  * 
  * Since: 2.4
  */
@@ -587,7 +619,7 @@ g_queue_peek_head_link (GQueue *queue)
  * 
  * Returns the last link in @queue.
  * 
- * Return value: the last link in @queue, or %NULL if @queue is empty
+ * Returns: the last link in @queue, or %NULL if @queue is empty
  * 
  * Since: 2.4
  */
@@ -639,7 +671,7 @@ g_queue_pop_tail (GQueue *queue)
  * 
  * Removes the @n'th element of @queue and returns its data.
  * 
- * Return value: the element's data, or %NULL if @n is off the end of @queue
+ * Returns: the element's data, or %NULL if @n is off the end of @queue
  * 
  * Since: 2.4
  */
@@ -704,7 +736,7 @@ g_queue_pop_tail_link (GQueue *queue)
  * 
  * Removes and returns the link at the given position.
  * 
- * Return value: the @n'th link, or %NULL if @n is off the end of @queue
+ * Returns: the @n'th link, or %NULL if @n is off the end of @queue
  * 
  * Since: 2.4
  */
@@ -732,7 +764,7 @@ g_queue_pop_nth_link (GQueue *queue,
  * 
  * Returns the link at the given position
  * 
- * Return value: the link at the @n'th position, or %NULL
+ * Returns: the link at the @n'th position, or %NULL
  *     if @n is off the end of the list
  * 
  * Since: 2.4
@@ -742,7 +774,7 @@ g_queue_peek_nth_link (GQueue *queue,
                        guint   n)
 {
   GList *link;
-  gint i;
+  guint i;
   
   g_return_val_if_fail (queue != NULL, NULL);
 
@@ -774,7 +806,7 @@ g_queue_peek_nth_link (GQueue *queue,
  * 
  * Returns the position of @link_ in @queue.
  * 
- * Return value: the position of @link_, or -1 if the link is
+ * Returns: the position of @link_, or -1 if the link is
  *     not part of @queue
  * 
  * Since: 2.4
@@ -791,10 +823,10 @@ g_queue_link_index (GQueue *queue,
 /**
  * g_queue_unlink:
  * @queue: a #GQueue
- * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
+ * @link_: a #GList link that must be part of @queue
  *
- * Unlinks @link_ so that it will no longer be part of @queue. The link is
- * not freed.
+ * Unlinks @link_ so that it will no longer be part of @queue.
+ * The link is not freed.
  *
  * @link_ must be part of @queue.
  * 
@@ -817,7 +849,7 @@ g_queue_unlink (GQueue *queue,
 /**
  * g_queue_delete_link:
  * @queue: a #GQueue
- * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
+ * @link_: a #GList link that must be part of @queue
  *
  * Removes @link_ from @queue and frees it.
  *
@@ -877,7 +909,7 @@ g_queue_peek_tail (GQueue *queue)
  * 
  * Returns the @n'th element of @queue. 
  * 
- * Return value: the data for the @n'th element of @queue,
+ * Returns: the data for the @n'th element of @queue,
  *     or %NULL if @n is off the end of @queue
  * 
  * Since: 2.4
@@ -905,7 +937,7 @@ g_queue_peek_nth (GQueue *queue,
  * 
  * Returns the position of the first element in @queue which contains @data.
  * 
- * Return value: the position of the first element in @queue which
+ * Returns: the position of the first element in @queue which
  *     contains @data, or -1 if no element in @queue contains @data
  * 
  * Since: 2.4
@@ -926,7 +958,7 @@ g_queue_index (GQueue        *queue,
  * 
  * Removes the first element in @queue that contains @data. 
  * 
- * Return value: %TRUE if @data was found and removed from @queue
+ * Returns: %TRUE if @data was found and removed from @queue
  *
  * Since: 2.4
  */
@@ -953,7 +985,7 @@ g_queue_remove (GQueue        *queue,
  * 
  * Remove all elements whose data equals @data from @queue.
  * 
- * Return value: the number of elements removed from @queue
+ * Returns: the number of elements removed from @queue
  *
  * Since: 2.4
  */
@@ -985,12 +1017,14 @@ g_queue_remove_all (GQueue        *queue,
 /**
  * g_queue_insert_before:
  * @queue: a #GQueue
- * @sibling: a #GList link that <emphasis>must</emphasis> 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
  */
@@ -1000,21 +1034,71 @@ 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_before_link:
+ * @queue: a #GQueue
+ * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
+ *   push at the tail of the queue.
+ * @link_: a #GList link to insert which must not be part of any other list.
+ *
+ * Inserts @link_ into @queue before @sibling.
+ *
+ * @sibling must be part of @queue.
+ *
+ * Since: 2.62
+ */
+void
+g_queue_insert_before_link (GQueue   *queue,
+                            GList    *sibling,
+                            GList    *link_)
+{
+  g_return_if_fail (queue != NULL);
+  g_return_if_fail (link_ != NULL);
+  g_return_if_fail (link_->prev == NULL);
+  g_return_if_fail (link_->next == NULL);
+
+  if G_UNLIKELY (sibling == NULL)
+    {
+      /* We don't use g_list_insert_before_link() 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_link (queue, link_);
+    }
+  else
+    {
+      queue->head = g_list_insert_before_link (queue->head, sibling, link_);
+      queue->length++;
+    }
 }
 
 /**
  * g_queue_insert_after:
  * @queue: a #GQueue
- * @sibling: a #GList link that <emphasis>must</emphasis> 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
  */
@@ -1024,15 +1108,43 @@ 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);
 }
 
 /**
+ * g_queue_insert_after_link:
+ * @queue: a #GQueue
+ * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
+ *   push at the head of the queue.
+ * @link_: a #GList link to insert which must not be part of any other list.
+ *
+ * Inserts @link_ into @queue after @sibling.
+ *
+ * @sibling must be part of @queue.
+ *
+ * Since: 2.62
+ */
+void
+g_queue_insert_after_link (GQueue   *queue,
+                           GList    *sibling,
+                           GList    *link_)
+{
+  g_return_if_fail (queue != NULL);
+  g_return_if_fail (link_ != NULL);
+  g_return_if_fail (link_->prev == NULL);
+  g_return_if_fail (link_->next == NULL);
+
+  if G_UNLIKELY (sibling == NULL)
+    g_queue_push_head_link (queue, link_);
+  else
+    g_queue_insert_before_link (queue, sibling->next, link_);
+}
+
+/**
  * g_queue_insert_sorted:
  * @queue: a #GQueue
  * @data: the data to insert
@@ -1061,8 +1173,5 @@ g_queue_insert_sorted (GQueue           *queue,
   while (list && func (list->data, data, user_data) < 0)
     list = list->next;
 
-  if (list)
-    g_queue_insert_before (queue, list, data);
-  else
-    g_queue_push_tail (queue, data);
+  g_queue_insert_before (queue, list, data);
 }