1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GQueue: Double ended queue implementation, piggy backed on GList.
5 * Copyright (C) 1998 Tim Janik
7 * SPDX-License-Identifier: LGPL-2.1-or-later
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
31 #include "gtestutils.h"
37 * Creates a new #GQueue.
39 * Returns: a newly allocated #GQueue
44 return g_slice_new0 (GQueue);
51 * Frees the memory allocated for the #GQueue. Only call this function
52 * if @queue was created with g_queue_new(). If queue elements contain
53 * dynamically-allocated memory, they should be freed first.
55 * If queue elements contain dynamically-allocated memory, you should
56 * either use g_queue_free_full() or free them manually first.
59 g_queue_free (GQueue *queue)
61 g_return_if_fail (queue != NULL);
63 g_list_free (queue->head);
64 g_slice_free (GQueue, queue);
69 * @queue: a pointer to a #GQueue
70 * @free_func: the function to be called to free each element's data
72 * Convenience method, which frees all the memory used by a #GQueue,
73 * and calls the specified destroy function on every element's data.
75 * @free_func should not modify the queue (eg, by removing the freed
81 g_queue_free_full (GQueue *queue,
82 GDestroyNotify free_func)
84 g_queue_foreach (queue, (GFunc) free_func, NULL);
90 * @queue: an uninitialized #GQueue
92 * A statically-allocated #GQueue must be initialized with this function
93 * before it can be used. Alternatively you can initialize it with
94 * %G_QUEUE_INIT. It is not necessary to initialize queues created with
100 g_queue_init (GQueue *queue)
102 g_return_if_fail (queue != NULL);
104 queue->head = queue->tail = NULL;
112 * Removes all the elements in @queue. If queue elements contain
113 * dynamically-allocated memory, they should be freed first.
118 g_queue_clear (GQueue *queue)
120 g_return_if_fail (queue != NULL);
122 g_list_free (queue->head);
123 g_queue_init (queue);
127 * g_queue_clear_full:
128 * @queue: a pointer to a #GQueue
129 * @free_func: (nullable): the function to be called to free memory allocated
131 * Convenience method, which frees all the memory used by a #GQueue,
132 * and calls the provided @free_func on each item in the #GQueue.
137 g_queue_clear_full (GQueue *queue,
138 GDestroyNotify free_func)
140 g_return_if_fail (queue != NULL);
142 if (free_func != NULL)
143 g_queue_foreach (queue, (GFunc) free_func, NULL);
145 g_queue_clear (queue);
152 * Returns %TRUE if the queue is empty.
154 * Returns: %TRUE if the queue is empty
157 g_queue_is_empty (GQueue *queue)
159 g_return_val_if_fail (queue != NULL, TRUE);
161 return queue->head == NULL;
165 * g_queue_get_length:
168 * Returns the number of items in @queue.
170 * Returns: the number of items in @queue
175 g_queue_get_length (GQueue *queue)
177 g_return_val_if_fail (queue != NULL, 0);
179 return queue->length;
186 * Reverses the order of the items in @queue.
191 g_queue_reverse (GQueue *queue)
193 g_return_if_fail (queue != NULL);
195 queue->tail = queue->head;
196 queue->head = g_list_reverse (queue->head);
203 * Copies a @queue. Note that is a shallow copy. If the elements in the
204 * queue consist of pointers to data, the pointers are copied, but the
205 * actual data is not.
207 * Returns: a copy of @queue
212 g_queue_copy (GQueue *queue)
217 g_return_val_if_fail (queue != NULL, NULL);
219 result = g_queue_new ();
221 for (list = queue->head; list != NULL; list = list->next)
222 g_queue_push_tail (result, list->data);
230 * @func: (scope call): the function to call for each element's data
231 * @user_data: user data to pass to @func
233 * Calls @func for each element in the queue passing @user_data to the
236 * It is safe for @func to remove the element from @queue, but it must
237 * not modify any part of the queue after that element.
242 g_queue_foreach (GQueue *queue,
248 g_return_if_fail (queue != NULL);
249 g_return_if_fail (func != NULL);
254 GList *next = list->next;
255 func (list->data, user_data);
263 * @data: data to find
265 * Finds the first link in @queue which contains @data.
267 * Returns: the first link in @queue which contains @data
272 g_queue_find (GQueue *queue,
275 g_return_val_if_fail (queue != NULL, NULL);
277 return g_list_find (queue->head, data);
281 * g_queue_find_custom:
283 * @data: user data passed to @func
284 * @func: (scope call): a #GCompareFunc to call for each element. It should return 0
285 * when the desired element is found
287 * Finds an element in a #GQueue, using a supplied function to find the
288 * desired element. It iterates over the queue, calling the given function
289 * which should return 0 when the desired element is found. The function
290 * takes two gconstpointer arguments, the #GQueue element's data as the
291 * first argument and the given user data as the second argument.
293 * Returns: the found link, or %NULL if it wasn't found
298 g_queue_find_custom (GQueue *queue,
302 g_return_val_if_fail (queue != NULL, NULL);
303 g_return_val_if_fail (func != NULL, NULL);
305 return g_list_find_custom (queue->head, data, func);
311 * @compare_func: (scope call): the #GCompareDataFunc used to sort @queue. This function
312 * is passed two elements of the queue and should return 0 if they are
313 * equal, a negative value if the first comes before the second, and
314 * a positive value if the second comes before the first.
315 * @user_data: user data passed to @compare_func
317 * Sorts @queue using @compare_func.
322 g_queue_sort (GQueue *queue,
323 GCompareDataFunc compare_func,
326 g_return_if_fail (queue != NULL);
327 g_return_if_fail (compare_func != NULL);
329 queue->head = g_list_sort_with_data (queue->head, compare_func, user_data);
330 queue->tail = g_list_last (queue->head);
336 * @data: the data for the new element.
338 * Adds a new element at the head of the queue.
341 g_queue_push_head (GQueue *queue,
344 g_return_if_fail (queue != NULL);
346 queue->head = g_list_prepend (queue->head, data);
348 queue->tail = queue->head;
355 * @data: the data for the new element
356 * @n: the position to insert the new element. If @n is negative or
357 * larger than the number of elements in the @queue, the element is
358 * added to the end of the queue.
360 * Inserts a new element into @queue at the given position.
365 g_queue_push_nth (GQueue *queue,
369 g_return_if_fail (queue != NULL);
371 if (n < 0 || (guint) n >= queue->length)
373 g_queue_push_tail (queue, data);
377 g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
381 * g_queue_push_head_link:
383 * @link_: a single #GList element, not a list with more than one element
385 * Adds a new element at the head of the queue.
388 g_queue_push_head_link (GQueue *queue,
391 g_return_if_fail (queue != NULL);
392 g_return_if_fail (link != NULL);
393 g_return_if_fail (link->prev == NULL);
394 g_return_if_fail (link->next == NULL);
396 link->next = queue->head;
398 queue->head->prev = link;
408 * @data: the data for the new element
410 * Adds a new element at the tail of the queue.
413 g_queue_push_tail (GQueue *queue,
416 g_return_if_fail (queue != NULL);
418 queue->tail = g_list_append (queue->tail, data);
419 if (queue->tail->next)
420 queue->tail = queue->tail->next;
422 queue->head = queue->tail;
427 * g_queue_push_tail_link:
429 * @link_: a single #GList element, not a list with more than one element
431 * Adds a new element at the tail of the queue.
434 g_queue_push_tail_link (GQueue *queue,
437 g_return_if_fail (queue != NULL);
438 g_return_if_fail (link != NULL);
439 g_return_if_fail (link->prev == NULL);
440 g_return_if_fail (link->next == NULL);
442 link->prev = queue->tail;
444 queue->tail->next = link;
452 * g_queue_push_nth_link:
454 * @n: the position to insert the link. If this is negative or larger than
455 * the number of elements in @queue, the link is added to the end of
457 * @link_: the link to add to @queue
459 * Inserts @link into @queue at the given position.
464 g_queue_push_nth_link (GQueue *queue,
471 g_return_if_fail (queue != NULL);
472 g_return_if_fail (link_ != NULL);
474 if (n < 0 || (guint) n >= queue->length)
476 g_queue_push_tail_link (queue, link_);
480 g_assert (queue->head);
481 g_assert (queue->tail);
483 next = g_queue_peek_nth_link (queue, n);
493 if (queue->head->prev)
494 queue->head = queue->head->prev;
496 /* The case where we’re pushing @link_ at the end of @queue is handled above
497 * using g_queue_push_tail_link(), so we should never have to manually adjust
499 g_assert (queue->tail->next == NULL);
508 * Removes the first element of the queue and returns its data.
510 * Returns: the data of the first element in the queue, or %NULL
511 * if the queue is empty
514 g_queue_pop_head (GQueue *queue)
516 g_return_val_if_fail (queue != NULL, NULL);
520 GList *node = queue->head;
521 gpointer data = node->data;
523 queue->head = node->next;
525 queue->head->prev = NULL;
528 g_list_free_1 (node);
538 * g_queue_pop_head_link:
541 * Removes and returns the first element of the queue.
543 * Returns: the #GList element at the head of the queue, or %NULL
544 * if the queue is empty
547 g_queue_pop_head_link (GQueue *queue)
549 g_return_val_if_fail (queue != NULL, NULL);
553 GList *node = queue->head;
555 queue->head = node->next;
558 queue->head->prev = NULL;
572 * g_queue_peek_head_link:
575 * Returns the first link in @queue.
577 * Returns: the first link in @queue, or %NULL if @queue is empty
582 g_queue_peek_head_link (GQueue *queue)
584 g_return_val_if_fail (queue != NULL, NULL);
590 * g_queue_peek_tail_link:
593 * Returns the last link in @queue.
595 * Returns: the last link in @queue, or %NULL if @queue is empty
600 g_queue_peek_tail_link (GQueue *queue)
602 g_return_val_if_fail (queue != NULL, NULL);
611 * Removes the last element of the queue and returns its data.
613 * Returns: the data of the last element in the queue, or %NULL
614 * if the queue is empty
617 g_queue_pop_tail (GQueue *queue)
619 g_return_val_if_fail (queue != NULL, NULL);
623 GList *node = queue->tail;
624 gpointer data = node->data;
626 queue->tail = node->prev;
628 queue->tail->next = NULL;
632 g_list_free_1 (node);
643 * @n: the position of the element
645 * Removes the @n'th element of @queue and returns its data.
647 * Returns: the element's data, or %NULL if @n is off the end of @queue
652 g_queue_pop_nth (GQueue *queue,
658 g_return_val_if_fail (queue != NULL, NULL);
660 if (n >= queue->length)
663 nth_link = g_queue_peek_nth_link (queue, n);
664 result = nth_link->data;
666 g_queue_delete_link (queue, nth_link);
672 * g_queue_pop_tail_link:
675 * Removes and returns the last element of the queue.
677 * Returns: the #GList element at the tail of the queue, or %NULL
678 * if the queue is empty
681 g_queue_pop_tail_link (GQueue *queue)
683 g_return_val_if_fail (queue != NULL, NULL);
687 GList *node = queue->tail;
689 queue->tail = node->prev;
692 queue->tail->next = NULL;
706 * g_queue_pop_nth_link:
708 * @n: the link's position
710 * Removes and returns the link at the given position.
712 * Returns: the @n'th link, or %NULL if @n is off the end of @queue
717 g_queue_pop_nth_link (GQueue *queue,
722 g_return_val_if_fail (queue != NULL, NULL);
724 if (n >= queue->length)
727 link = g_queue_peek_nth_link (queue, n);
728 g_queue_unlink (queue, link);
734 * g_queue_peek_nth_link:
736 * @n: the position of the link
738 * Returns the link at the given position
740 * Returns: the link at the @n'th position, or %NULL
741 * if @n is off the end of the list
746 g_queue_peek_nth_link (GQueue *queue,
752 g_return_val_if_fail (queue != NULL, NULL);
754 if (n >= queue->length)
757 if (n > queue->length / 2)
759 n = queue->length - n - 1;
762 for (i = 0; i < n; ++i)
768 for (i = 0; i < n; ++i)
776 * g_queue_link_index:
778 * @link_: a #GList link
780 * Returns the position of @link_ in @queue.
782 * Returns: the position of @link_, or -1 if the link is
788 g_queue_link_index (GQueue *queue,
791 g_return_val_if_fail (queue != NULL, -1);
793 return g_list_position (queue->head, link_);
799 * @link_: a #GList link that must be part of @queue
801 * Unlinks @link_ so that it will no longer be part of @queue.
802 * The link is not freed.
804 * @link_ must be part of @queue.
809 g_queue_unlink (GQueue *queue,
812 g_return_if_fail (queue != NULL);
813 g_return_if_fail (link_ != NULL);
815 if (link_ == queue->tail)
816 queue->tail = queue->tail->prev;
818 queue->head = g_list_remove_link (queue->head, link_);
823 * g_queue_delete_link:
825 * @link_: a #GList link that must be part of @queue
827 * Removes @link_ from @queue and frees it.
829 * @link_ must be part of @queue.
834 g_queue_delete_link (GQueue *queue,
837 g_return_if_fail (queue != NULL);
838 g_return_if_fail (link_ != NULL);
840 g_queue_unlink (queue, link_);
848 * Returns the first element of the queue.
850 * Returns: the data of the first element in the queue, or %NULL
851 * if the queue is empty
854 g_queue_peek_head (GQueue *queue)
856 g_return_val_if_fail (queue != NULL, NULL);
858 return queue->head ? queue->head->data : NULL;
865 * Returns the last element of the queue.
867 * Returns: the data of the last element in the queue, or %NULL
868 * if the queue is empty
871 g_queue_peek_tail (GQueue *queue)
873 g_return_val_if_fail (queue != NULL, NULL);
875 return queue->tail ? queue->tail->data : NULL;
881 * @n: the position of the element
883 * Returns the @n'th element of @queue.
885 * Returns: the data for the @n'th element of @queue,
886 * or %NULL if @n is off the end of @queue
891 g_queue_peek_nth (GQueue *queue,
896 g_return_val_if_fail (queue != NULL, NULL);
898 link = g_queue_peek_nth_link (queue, n);
909 * @data: the data to find
911 * Returns the position of the first element in @queue which contains @data.
913 * Returns: the position of the first element in @queue which
914 * contains @data, or -1 if no element in @queue contains @data
919 g_queue_index (GQueue *queue,
922 g_return_val_if_fail (queue != NULL, -1);
924 return g_list_index (queue->head, data);
930 * @data: the data to remove
932 * Removes the first element in @queue that contains @data.
934 * Returns: %TRUE if @data was found and removed from @queue
939 g_queue_remove (GQueue *queue,
944 g_return_val_if_fail (queue != NULL, FALSE);
946 link = g_list_find (queue->head, data);
949 g_queue_delete_link (queue, link);
951 return (link != NULL);
955 * g_queue_remove_all:
957 * @data: the data to remove
959 * Remove all elements whose data equals @data from @queue.
961 * Returns: the number of elements removed from @queue
966 g_queue_remove_all (GQueue *queue,
972 g_return_val_if_fail (queue != NULL, 0);
974 old_length = queue->length;
979 GList *next = list->next;
981 if (list->data == data)
982 g_queue_delete_link (queue, list);
987 return (old_length - queue->length);
991 * g_queue_insert_before:
993 * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
994 * push at the tail of the queue.
995 * @data: the data to insert
997 * Inserts @data into @queue before @sibling.
999 * @sibling must be part of @queue. Since GLib 2.44 a %NULL sibling pushes the
1000 * data at the tail of the queue.
1005 g_queue_insert_before (GQueue *queue,
1009 g_return_if_fail (queue != NULL);
1011 if (sibling == NULL)
1013 /* We don't use g_list_insert_before() with a NULL sibling because it
1014 * would be a O(n) operation and we would need to update manually the tail
1017 g_queue_push_tail (queue, data);
1021 queue->head = g_list_insert_before (queue->head, sibling, data);
1027 * g_queue_insert_before_link:
1029 * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
1030 * push at the tail of the queue.
1031 * @link_: a #GList link to insert which must not be part of any other list.
1033 * Inserts @link_ into @queue before @sibling.
1035 * @sibling must be part of @queue.
1040 g_queue_insert_before_link (GQueue *queue,
1044 g_return_if_fail (queue != NULL);
1045 g_return_if_fail (link_ != NULL);
1046 g_return_if_fail (link_->prev == NULL);
1047 g_return_if_fail (link_->next == NULL);
1049 if G_UNLIKELY (sibling == NULL)
1051 /* We don't use g_list_insert_before_link() with a NULL sibling because it
1052 * would be a O(n) operation and we would need to update manually the tail
1055 g_queue_push_tail_link (queue, link_);
1059 queue->head = g_list_insert_before_link (queue->head, sibling, link_);
1065 * g_queue_insert_after:
1067 * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
1068 * push at the head of the queue.
1069 * @data: the data to insert
1071 * Inserts @data into @queue after @sibling.
1073 * @sibling must be part of @queue. Since GLib 2.44 a %NULL sibling pushes the
1074 * data at the head of the queue.
1079 g_queue_insert_after (GQueue *queue,
1083 g_return_if_fail (queue != NULL);
1085 if (sibling == NULL)
1086 g_queue_push_head (queue, data);
1088 g_queue_insert_before (queue, sibling->next, data);
1092 * g_queue_insert_after_link:
1094 * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
1095 * push at the head of the queue.
1096 * @link_: a #GList link to insert which must not be part of any other list.
1098 * Inserts @link_ into @queue after @sibling.
1100 * @sibling must be part of @queue.
1105 g_queue_insert_after_link (GQueue *queue,
1109 g_return_if_fail (queue != NULL);
1110 g_return_if_fail (link_ != NULL);
1111 g_return_if_fail (link_->prev == NULL);
1112 g_return_if_fail (link_->next == NULL);
1114 if G_UNLIKELY (sibling == NULL)
1115 g_queue_push_head_link (queue, link_);
1117 g_queue_insert_before_link (queue, sibling->next, link_);
1121 * g_queue_insert_sorted:
1123 * @data: the data to insert
1124 * @func: (scope call): the #GCompareDataFunc used to compare elements in the queue. It is
1125 * called with two elements of the @queue and @user_data. It should
1126 * return 0 if the elements are equal, a negative value if the first
1127 * element comes before the second, and a positive value if the second
1128 * element comes before the first.
1129 * @user_data: user data passed to @func
1131 * Inserts @data into @queue using @func to determine the new position.
1136 g_queue_insert_sorted (GQueue *queue,
1138 GCompareDataFunc func,
1143 g_return_if_fail (queue != NULL);
1146 while (list && func (list->data, data, user_data) < 0)
1149 g_queue_insert_before (queue, list, data);