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 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 * @Title: Double-ended Queues
28 * @Short_description: double-ended queue data structure
30 * The #GQueue structure and its associated functions provide a standard
31 * queue data structure. Internally, GQueue uses the same data structure
32 * as #GList to store elements.
34 * The data contained in each element can be either integer values, by
35 * using one of the [Type Conversion Macros][glib-Type-Conversion-Macros],
36 * or simply pointers to any type of data.
38 * To create a new GQueue, use g_queue_new().
40 * To initialize a statically-allocated GQueue, use #G_QUEUE_INIT or
43 * To add elements, use g_queue_push_head(), g_queue_push_head_link(),
44 * g_queue_push_tail() and g_queue_push_tail_link().
46 * To remove elements, use g_queue_pop_head() and g_queue_pop_tail().
48 * To free the entire queue, use g_queue_free().
54 #include "gtestutils.h"
60 * Creates a new #GQueue.
62 * Returns: a newly allocated #GQueue
67 return g_slice_new0 (GQueue);
74 * Frees the memory allocated for the #GQueue. Only call this function
75 * if @queue was created with g_queue_new(). If queue elements contain
76 * dynamically-allocated memory, they should be freed first.
78 * If queue elements contain dynamically-allocated memory, you should
79 * either use g_queue_free_full() or free them manually first.
82 g_queue_free (GQueue *queue)
84 g_return_if_fail (queue != NULL);
86 g_list_free (queue->head);
87 g_slice_free (GQueue, queue);
92 * @queue: a pointer to a #GQueue
93 * @free_func: the function to be called to free each element's data
95 * Convenience method, which frees all the memory used by a #GQueue,
96 * and calls the specified destroy function on every element's data.
101 g_queue_free_full (GQueue *queue,
102 GDestroyNotify free_func)
104 g_queue_foreach (queue, (GFunc) free_func, NULL);
105 g_queue_free (queue);
110 * @queue: an uninitialized #GQueue
112 * A statically-allocated #GQueue must be initialized with this function
113 * before it can be used. Alternatively you can initialize it with
114 * #G_QUEUE_INIT. It is not necessary to initialize queues created with
120 g_queue_init (GQueue *queue)
122 g_return_if_fail (queue != NULL);
124 queue->head = queue->tail = NULL;
132 * Removes all the elements in @queue. If queue elements contain
133 * dynamically-allocated memory, they should be freed first.
138 g_queue_clear (GQueue *queue)
140 g_return_if_fail (queue != NULL);
142 g_list_free (queue->head);
143 g_queue_init (queue);
150 * Returns %TRUE if the queue is empty.
152 * Returns: %TRUE if the queue is empty
155 g_queue_is_empty (GQueue *queue)
157 g_return_val_if_fail (queue != NULL, TRUE);
159 return queue->head == NULL;
163 * g_queue_get_length:
166 * Returns the number of items in @queue.
168 * Returns: the number of items in @queue
173 g_queue_get_length (GQueue *queue)
175 g_return_val_if_fail (queue != NULL, 0);
177 return queue->length;
184 * Reverses the order of the items in @queue.
189 g_queue_reverse (GQueue *queue)
191 g_return_if_fail (queue != NULL);
193 queue->tail = queue->head;
194 queue->head = g_list_reverse (queue->head);
201 * Copies a @queue. Note that is a shallow copy. If the elements in the
202 * queue consist of pointers to data, the pointers are copied, but the
203 * actual data is not.
205 * Returns: a copy of @queue
210 g_queue_copy (GQueue *queue)
215 g_return_val_if_fail (queue != NULL, NULL);
217 result = g_queue_new ();
219 for (list = queue->head; list != NULL; list = list->next)
220 g_queue_push_tail (result, list->data);
228 * @func: the function to call for each element's data
229 * @user_data: user data to pass to @func
231 * Calls @func for each element in the queue passing @user_data to the
237 g_queue_foreach (GQueue *queue,
243 g_return_if_fail (queue != NULL);
244 g_return_if_fail (func != NULL);
249 GList *next = list->next;
250 func (list->data, user_data);
258 * @data: data to find
260 * Finds the first link in @queue which contains @data.
262 * Returns: the first link in @queue which contains @data
267 g_queue_find (GQueue *queue,
270 g_return_val_if_fail (queue != NULL, NULL);
272 return g_list_find (queue->head, data);
276 * g_queue_find_custom:
278 * @data: user data passed to @func
279 * @func: a #GCompareFunc to call for each element. It should return 0
280 * when the desired element is found
282 * Finds an element in a #GQueue, using a supplied function to find the
283 * desired element. It iterates over the queue, calling the given function
284 * which should return 0 when the desired element is found. The function
285 * takes two gconstpointer arguments, the #GQueue element's data as the
286 * first argument and the given user data as the second argument.
288 * Returns: the found link, or %NULL if it wasn't found
293 g_queue_find_custom (GQueue *queue,
297 g_return_val_if_fail (queue != NULL, NULL);
298 g_return_val_if_fail (func != NULL, NULL);
300 return g_list_find_custom (queue->head, data, func);
306 * @compare_func: the #GCompareDataFunc used to sort @queue. This function
307 * is passed two elements of the queue and should return 0 if they are
308 * equal, a negative value if the first comes before the second, and
309 * a positive value if the second comes before the first.
310 * @user_data: user data passed to @compare_func
312 * Sorts @queue using @compare_func.
317 g_queue_sort (GQueue *queue,
318 GCompareDataFunc compare_func,
321 g_return_if_fail (queue != NULL);
322 g_return_if_fail (compare_func != NULL);
324 queue->head = g_list_sort_with_data (queue->head, compare_func, user_data);
325 queue->tail = g_list_last (queue->head);
331 * @data: the data for the new element.
333 * Adds a new element at the head of the queue.
336 g_queue_push_head (GQueue *queue,
339 g_return_if_fail (queue != NULL);
341 queue->head = g_list_prepend (queue->head, data);
343 queue->tail = queue->head;
350 * @data: the data for the new element
351 * @n: the position to insert the new element. If @n is negative or
352 * larger than the number of elements in the @queue, the element is
353 * added to the end of the queue.
355 * Inserts a new element into @queue at the given position.
360 g_queue_push_nth (GQueue *queue,
364 g_return_if_fail (queue != NULL);
366 if (n < 0 || n >= queue->length)
368 g_queue_push_tail (queue, data);
372 g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
376 * g_queue_push_head_link:
378 * @link_: a single #GList element, not a list with more than one element
380 * Adds a new element at the head of the queue.
383 g_queue_push_head_link (GQueue *queue,
386 g_return_if_fail (queue != NULL);
387 g_return_if_fail (link != NULL);
388 g_return_if_fail (link->prev == NULL);
389 g_return_if_fail (link->next == NULL);
391 link->next = queue->head;
393 queue->head->prev = link;
403 * @data: the data for the new element
405 * Adds a new element at the tail of the queue.
408 g_queue_push_tail (GQueue *queue,
411 g_return_if_fail (queue != NULL);
413 queue->tail = g_list_append (queue->tail, data);
414 if (queue->tail->next)
415 queue->tail = queue->tail->next;
417 queue->head = queue->tail;
422 * g_queue_push_tail_link:
424 * @link_: a single #GList element, not a list with more than one element
426 * Adds a new element at the tail of the queue.
429 g_queue_push_tail_link (GQueue *queue,
432 g_return_if_fail (queue != NULL);
433 g_return_if_fail (link != NULL);
434 g_return_if_fail (link->prev == NULL);
435 g_return_if_fail (link->next == NULL);
437 link->prev = queue->tail;
439 queue->tail->next = link;
447 * g_queue_push_nth_link:
449 * @n: the position to insert the link. If this is negative or larger than
450 * the number of elements in @queue, the link is added to the end of
452 * @link_: the link to add to @queue
454 * Inserts @link into @queue at the given position.
459 g_queue_push_nth_link (GQueue *queue,
466 g_return_if_fail (queue != NULL);
467 g_return_if_fail (link_ != NULL);
469 if (n < 0 || n >= queue->length)
471 g_queue_push_tail_link (queue, link_);
475 g_assert (queue->head);
476 g_assert (queue->tail);
478 next = g_queue_peek_nth_link (queue, n);
488 if (queue->head->prev)
489 queue->head = queue->head->prev;
491 if (queue->tail->next)
492 queue->tail = queue->tail->next;
501 * Removes the first element of the queue and returns its data.
503 * Returns: the data of the first element in the queue, or %NULL
504 * if the queue is empty
507 g_queue_pop_head (GQueue *queue)
509 g_return_val_if_fail (queue != NULL, NULL);
513 GList *node = queue->head;
514 gpointer data = node->data;
516 queue->head = node->next;
518 queue->head->prev = NULL;
521 g_list_free_1 (node);
531 * g_queue_pop_head_link:
534 * Removes and returns the first element of the queue.
536 * Returns: the #GList element at the head of the queue, or %NULL
537 * if the queue is empty
540 g_queue_pop_head_link (GQueue *queue)
542 g_return_val_if_fail (queue != NULL, NULL);
546 GList *node = queue->head;
548 queue->head = node->next;
551 queue->head->prev = NULL;
565 * g_queue_peek_head_link:
568 * Returns the first link in @queue.
570 * Returns: the first link in @queue, or %NULL if @queue is empty
575 g_queue_peek_head_link (GQueue *queue)
577 g_return_val_if_fail (queue != NULL, NULL);
583 * g_queue_peek_tail_link:
586 * Returns the last link in @queue.
588 * Returns: the last link in @queue, or %NULL if @queue is empty
593 g_queue_peek_tail_link (GQueue *queue)
595 g_return_val_if_fail (queue != NULL, NULL);
604 * Removes the last element of the queue and returns its data.
606 * Returns: the data of the last element in the queue, or %NULL
607 * if the queue is empty
610 g_queue_pop_tail (GQueue *queue)
612 g_return_val_if_fail (queue != NULL, NULL);
616 GList *node = queue->tail;
617 gpointer data = node->data;
619 queue->tail = node->prev;
621 queue->tail->next = NULL;
625 g_list_free_1 (node);
636 * @n: the position of the element
638 * Removes the @n'th element of @queue and returns its data.
640 * Returns: the element's data, or %NULL if @n is off the end of @queue
645 g_queue_pop_nth (GQueue *queue,
651 g_return_val_if_fail (queue != NULL, NULL);
653 if (n >= queue->length)
656 nth_link = g_queue_peek_nth_link (queue, n);
657 result = nth_link->data;
659 g_queue_delete_link (queue, nth_link);
665 * g_queue_pop_tail_link:
668 * Removes and returns the last element of the queue.
670 * Returns: the #GList element at the tail of the queue, or %NULL
671 * if the queue is empty
674 g_queue_pop_tail_link (GQueue *queue)
676 g_return_val_if_fail (queue != NULL, NULL);
680 GList *node = queue->tail;
682 queue->tail = node->prev;
685 queue->tail->next = NULL;
699 * g_queue_pop_nth_link:
701 * @n: the link's position
703 * Removes and returns the link at the given position.
705 * Returns: the @n'th link, or %NULL if @n is off the end of @queue
710 g_queue_pop_nth_link (GQueue *queue,
715 g_return_val_if_fail (queue != NULL, NULL);
717 if (n >= queue->length)
720 link = g_queue_peek_nth_link (queue, n);
721 g_queue_unlink (queue, link);
727 * g_queue_peek_nth_link:
729 * @n: the position of the link
731 * Returns the link at the given position
733 * Returns: the link at the @n'th position, or %NULL
734 * if @n is off the end of the list
739 g_queue_peek_nth_link (GQueue *queue,
745 g_return_val_if_fail (queue != NULL, NULL);
747 if (n >= queue->length)
750 if (n > queue->length / 2)
752 n = queue->length - n - 1;
755 for (i = 0; i < n; ++i)
761 for (i = 0; i < n; ++i)
769 * g_queue_link_index:
771 * @link_: a #GList link
773 * Returns the position of @link_ in @queue.
775 * Returns: the position of @link_, or -1 if the link is
781 g_queue_link_index (GQueue *queue,
784 g_return_val_if_fail (queue != NULL, -1);
786 return g_list_position (queue->head, link_);
792 * @link_: a #GList link that must be part of @queue
794 * Unlinks @link_ so that it will no longer be part of @queue.
795 * The link is not freed.
797 * @link_ must be part of @queue.
802 g_queue_unlink (GQueue *queue,
805 g_return_if_fail (queue != NULL);
806 g_return_if_fail (link_ != NULL);
808 if (link_ == queue->tail)
809 queue->tail = queue->tail->prev;
811 queue->head = g_list_remove_link (queue->head, link_);
816 * g_queue_delete_link:
818 * @link_: a #GList link that must be part of @queue
820 * Removes @link_ from @queue and frees it.
822 * @link_ must be part of @queue.
827 g_queue_delete_link (GQueue *queue,
830 g_return_if_fail (queue != NULL);
831 g_return_if_fail (link_ != NULL);
833 g_queue_unlink (queue, link_);
841 * Returns the first element of the queue.
843 * Returns: the data of the first element in the queue, or %NULL
844 * if the queue is empty
847 g_queue_peek_head (GQueue *queue)
849 g_return_val_if_fail (queue != NULL, NULL);
851 return queue->head ? queue->head->data : NULL;
858 * Returns the last element of the queue.
860 * Returns: the data of the last element in the queue, or %NULL
861 * if the queue is empty
864 g_queue_peek_tail (GQueue *queue)
866 g_return_val_if_fail (queue != NULL, NULL);
868 return queue->tail ? queue->tail->data : NULL;
874 * @n: the position of the element
876 * Returns the @n'th element of @queue.
878 * Returns: the data for the @n'th element of @queue,
879 * or %NULL if @n is off the end of @queue
884 g_queue_peek_nth (GQueue *queue,
889 g_return_val_if_fail (queue != NULL, NULL);
891 link = g_queue_peek_nth_link (queue, n);
902 * @data: the data to find
904 * Returns the position of the first element in @queue which contains @data.
906 * Returns: the position of the first element in @queue which
907 * contains @data, or -1 if no element in @queue contains @data
912 g_queue_index (GQueue *queue,
915 g_return_val_if_fail (queue != NULL, -1);
917 return g_list_index (queue->head, data);
923 * @data: the data to remove
925 * Removes the first element in @queue that contains @data.
927 * Returns: %TRUE if @data was found and removed from @queue
932 g_queue_remove (GQueue *queue,
937 g_return_val_if_fail (queue != NULL, FALSE);
939 link = g_list_find (queue->head, data);
942 g_queue_delete_link (queue, link);
944 return (link != NULL);
948 * g_queue_remove_all:
950 * @data: the data to remove
952 * Remove all elements whose data equals @data from @queue.
954 * Returns: the number of elements removed from @queue
959 g_queue_remove_all (GQueue *queue,
965 g_return_val_if_fail (queue != NULL, 0);
967 old_length = queue->length;
972 GList *next = list->next;
974 if (list->data == data)
975 g_queue_delete_link (queue, list);
980 return (old_length - queue->length);
984 * g_queue_insert_before:
986 * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
987 * push at the tail of the queue.
988 * @data: the data to insert
990 * Inserts @data into @queue before @sibling.
992 * @sibling must be part of @queue. Since GLib 2.44 a %NULL sibling pushes the
993 * data at the tail of the queue.
998 g_queue_insert_before (GQueue *queue,
1002 g_return_if_fail (queue != NULL);
1004 if (sibling == NULL)
1006 /* We don't use g_list_insert_before() with a NULL sibling because it
1007 * would be a O(n) operation and we would need to update manually the tail
1010 g_queue_push_tail (queue, data);
1014 queue->head = g_list_insert_before (queue->head, sibling, data);
1020 * g_queue_insert_after:
1022 * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
1023 * push at the head of the queue.
1024 * @data: the data to insert
1026 * Inserts @data into @queue after @sibling.
1028 * @sibling must be part of @queue. Since GLib 2.44 a %NULL sibling pushes the
1029 * data at the head of the queue.
1034 g_queue_insert_after (GQueue *queue,
1038 g_return_if_fail (queue != NULL);
1040 if (sibling == NULL)
1041 g_queue_push_head (queue, data);
1043 g_queue_insert_before (queue, sibling->next, data);
1047 * g_queue_insert_sorted:
1049 * @data: the data to insert
1050 * @func: the #GCompareDataFunc used to compare elements in the queue. It is
1051 * called with two elements of the @queue and @user_data. It should
1052 * return 0 if the elements are equal, a negative value if the first
1053 * element comes before the second, and a positive value if the second
1054 * element comes before the first.
1055 * @user_data: user data passed to @func
1057 * Inserts @data into @queue using @func to determine the new position.
1062 g_queue_insert_sorted (GQueue *queue,
1064 GCompareDataFunc func,
1069 g_return_if_fail (queue != NULL);
1072 while (list && func (list->data, data, user_data) < 0)
1075 g_queue_insert_before (queue, list, data);