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.1 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 * As with all other GLib data structures, #GQueue is not thread-safe.
39 * For a thread-safe queue, use #GAsyncQueue.
41 * To create a new GQueue, use g_queue_new().
43 * To initialize a statically-allocated GQueue, use #G_QUEUE_INIT or
46 * To add elements, use g_queue_push_head(), g_queue_push_head_link(),
47 * g_queue_push_tail() and g_queue_push_tail_link().
49 * To remove elements, use g_queue_pop_head() and g_queue_pop_tail().
51 * To free the entire queue, use g_queue_free().
57 #include "gtestutils.h"
63 * Creates a new #GQueue.
65 * Returns: a newly allocated #GQueue
70 return g_slice_new0 (GQueue
);
77 * Frees the memory allocated for the #GQueue. Only call this function
78 * if @queue was created with g_queue_new(). If queue elements contain
79 * dynamically-allocated memory, they should be freed first.
81 * If queue elements contain dynamically-allocated memory, you should
82 * either use g_queue_free_full() or free them manually first.
85 g_queue_free (GQueue
*queue
)
87 g_return_if_fail (queue
!= NULL
);
89 g_list_free (queue
->head
);
90 g_slice_free (GQueue
, queue
);
95 * @queue: a pointer to a #GQueue
96 * @free_func: the function to be called to free each element's data
98 * Convenience method, which frees all the memory used by a #GQueue,
99 * and calls the specified destroy function on every element's data.
101 * @free_func should not modify the queue (eg, by removing the freed
107 g_queue_free_full (GQueue
*queue
,
108 GDestroyNotify free_func
)
110 g_queue_foreach (queue
, (GFunc
) free_func
, NULL
);
111 g_queue_free (queue
);
116 * @queue: an uninitialized #GQueue
118 * A statically-allocated #GQueue must be initialized with this function
119 * before it can be used. Alternatively you can initialize it with
120 * #G_QUEUE_INIT. It is not necessary to initialize queues created with
126 g_queue_init (GQueue
*queue
)
128 g_return_if_fail (queue
!= NULL
);
130 queue
->head
= queue
->tail
= NULL
;
138 * Removes all the elements in @queue. If queue elements contain
139 * dynamically-allocated memory, they should be freed first.
144 g_queue_clear (GQueue
*queue
)
146 g_return_if_fail (queue
!= NULL
);
148 g_list_free (queue
->head
);
149 g_queue_init (queue
);
156 * Returns %TRUE if the queue is empty.
158 * Returns: %TRUE if the queue is empty
161 g_queue_is_empty (GQueue
*queue
)
163 g_return_val_if_fail (queue
!= NULL
, TRUE
);
165 return queue
->head
== NULL
;
169 * g_queue_get_length:
172 * Returns the number of items in @queue.
174 * Returns: the number of items in @queue
179 g_queue_get_length (GQueue
*queue
)
181 g_return_val_if_fail (queue
!= NULL
, 0);
183 return queue
->length
;
190 * Reverses the order of the items in @queue.
195 g_queue_reverse (GQueue
*queue
)
197 g_return_if_fail (queue
!= NULL
);
199 queue
->tail
= queue
->head
;
200 queue
->head
= g_list_reverse (queue
->head
);
207 * Copies a @queue. Note that is a shallow copy. If the elements in the
208 * queue consist of pointers to data, the pointers are copied, but the
209 * actual data is not.
211 * Returns: a copy of @queue
216 g_queue_copy (GQueue
*queue
)
221 g_return_val_if_fail (queue
!= NULL
, NULL
);
223 result
= g_queue_new ();
225 for (list
= queue
->head
; list
!= NULL
; list
= list
->next
)
226 g_queue_push_tail (result
, list
->data
);
234 * @func: the function to call for each element's data
235 * @user_data: user data to pass to @func
237 * Calls @func for each element in the queue passing @user_data to the
240 * It is safe for @func to remove the element from @queue, but it must
241 * not modify any part of the queue after that element.
246 g_queue_foreach (GQueue
*queue
,
252 g_return_if_fail (queue
!= NULL
);
253 g_return_if_fail (func
!= NULL
);
258 GList
*next
= list
->next
;
259 func (list
->data
, user_data
);
267 * @data: data to find
269 * Finds the first link in @queue which contains @data.
271 * Returns: the first link in @queue which contains @data
276 g_queue_find (GQueue
*queue
,
279 g_return_val_if_fail (queue
!= NULL
, NULL
);
281 return g_list_find (queue
->head
, data
);
285 * g_queue_find_custom:
287 * @data: user data passed to @func
288 * @func: a #GCompareFunc to call for each element. It should return 0
289 * when the desired element is found
291 * Finds an element in a #GQueue, using a supplied function to find the
292 * desired element. It iterates over the queue, calling the given function
293 * which should return 0 when the desired element is found. The function
294 * takes two gconstpointer arguments, the #GQueue element's data as the
295 * first argument and the given user data as the second argument.
297 * Returns: the found link, or %NULL if it wasn't found
302 g_queue_find_custom (GQueue
*queue
,
306 g_return_val_if_fail (queue
!= NULL
, NULL
);
307 g_return_val_if_fail (func
!= NULL
, NULL
);
309 return g_list_find_custom (queue
->head
, data
, func
);
315 * @compare_func: the #GCompareDataFunc used to sort @queue. This function
316 * is passed two elements of the queue and should return 0 if they are
317 * equal, a negative value if the first comes before the second, and
318 * a positive value if the second comes before the first.
319 * @user_data: user data passed to @compare_func
321 * Sorts @queue using @compare_func.
326 g_queue_sort (GQueue
*queue
,
327 GCompareDataFunc compare_func
,
330 g_return_if_fail (queue
!= NULL
);
331 g_return_if_fail (compare_func
!= NULL
);
333 queue
->head
= g_list_sort_with_data (queue
->head
, compare_func
, user_data
);
334 queue
->tail
= g_list_last (queue
->head
);
340 * @data: the data for the new element.
342 * Adds a new element at the head of the queue.
345 g_queue_push_head (GQueue
*queue
,
348 g_return_if_fail (queue
!= NULL
);
350 queue
->head
= g_list_prepend (queue
->head
, data
);
352 queue
->tail
= queue
->head
;
359 * @data: the data for the new element
360 * @n: the position to insert the new element. If @n is negative or
361 * larger than the number of elements in the @queue, the element is
362 * added to the end of the queue.
364 * Inserts a new element into @queue at the given position.
369 g_queue_push_nth (GQueue
*queue
,
373 g_return_if_fail (queue
!= NULL
);
375 if (n
< 0 || n
>= queue
->length
)
377 g_queue_push_tail (queue
, data
);
381 g_queue_insert_before (queue
, g_queue_peek_nth_link (queue
, n
), data
);
385 * g_queue_push_head_link:
387 * @link_: a single #GList element, not a list with more than one element
389 * Adds a new element at the head of the queue.
392 g_queue_push_head_link (GQueue
*queue
,
395 g_return_if_fail (queue
!= NULL
);
396 g_return_if_fail (link
!= NULL
);
397 g_return_if_fail (link
->prev
== NULL
);
398 g_return_if_fail (link
->next
== NULL
);
400 link
->next
= queue
->head
;
402 queue
->head
->prev
= link
;
412 * @data: the data for the new element
414 * Adds a new element at the tail of the queue.
417 g_queue_push_tail (GQueue
*queue
,
420 g_return_if_fail (queue
!= NULL
);
422 queue
->tail
= g_list_append (queue
->tail
, data
);
423 if (queue
->tail
->next
)
424 queue
->tail
= queue
->tail
->next
;
426 queue
->head
= queue
->tail
;
431 * g_queue_push_tail_link:
433 * @link_: a single #GList element, not a list with more than one element
435 * Adds a new element at the tail of the queue.
438 g_queue_push_tail_link (GQueue
*queue
,
441 g_return_if_fail (queue
!= NULL
);
442 g_return_if_fail (link
!= NULL
);
443 g_return_if_fail (link
->prev
== NULL
);
444 g_return_if_fail (link
->next
== NULL
);
446 link
->prev
= queue
->tail
;
448 queue
->tail
->next
= link
;
456 * g_queue_push_nth_link:
458 * @n: the position to insert the link. If this is negative or larger than
459 * the number of elements in @queue, the link is added to the end of
461 * @link_: the link to add to @queue
463 * Inserts @link into @queue at the given position.
468 g_queue_push_nth_link (GQueue
*queue
,
475 g_return_if_fail (queue
!= NULL
);
476 g_return_if_fail (link_
!= NULL
);
478 if (n
< 0 || n
>= queue
->length
)
480 g_queue_push_tail_link (queue
, link_
);
484 g_assert (queue
->head
);
485 g_assert (queue
->tail
);
487 next
= g_queue_peek_nth_link (queue
, n
);
497 if (queue
->head
->prev
)
498 queue
->head
= queue
->head
->prev
;
500 if (queue
->tail
->next
)
501 queue
->tail
= queue
->tail
->next
;
510 * Removes the first element of the queue and returns its data.
512 * Returns: the data of the first element in the queue, or %NULL
513 * if the queue is empty
516 g_queue_pop_head (GQueue
*queue
)
518 g_return_val_if_fail (queue
!= NULL
, NULL
);
522 GList
*node
= queue
->head
;
523 gpointer data
= node
->data
;
525 queue
->head
= node
->next
;
527 queue
->head
->prev
= NULL
;
530 g_list_free_1 (node
);
540 * g_queue_pop_head_link:
543 * Removes and returns the first element of the queue.
545 * Returns: the #GList element at the head of the queue, or %NULL
546 * if the queue is empty
549 g_queue_pop_head_link (GQueue
*queue
)
551 g_return_val_if_fail (queue
!= NULL
, NULL
);
555 GList
*node
= queue
->head
;
557 queue
->head
= node
->next
;
560 queue
->head
->prev
= NULL
;
574 * g_queue_peek_head_link:
577 * Returns the first link in @queue.
579 * Returns: the first link in @queue, or %NULL if @queue is empty
584 g_queue_peek_head_link (GQueue
*queue
)
586 g_return_val_if_fail (queue
!= NULL
, NULL
);
592 * g_queue_peek_tail_link:
595 * Returns the last link in @queue.
597 * Returns: the last link in @queue, or %NULL if @queue is empty
602 g_queue_peek_tail_link (GQueue
*queue
)
604 g_return_val_if_fail (queue
!= NULL
, NULL
);
613 * Removes the last element of the queue and returns its data.
615 * Returns: the data of the last element in the queue, or %NULL
616 * if the queue is empty
619 g_queue_pop_tail (GQueue
*queue
)
621 g_return_val_if_fail (queue
!= NULL
, NULL
);
625 GList
*node
= queue
->tail
;
626 gpointer data
= node
->data
;
628 queue
->tail
= node
->prev
;
630 queue
->tail
->next
= NULL
;
634 g_list_free_1 (node
);
645 * @n: the position of the element
647 * Removes the @n'th element of @queue and returns its data.
649 * Returns: the element's data, or %NULL if @n is off the end of @queue
654 g_queue_pop_nth (GQueue
*queue
,
660 g_return_val_if_fail (queue
!= NULL
, NULL
);
662 if (n
>= queue
->length
)
665 nth_link
= g_queue_peek_nth_link (queue
, n
);
666 result
= nth_link
->data
;
668 g_queue_delete_link (queue
, nth_link
);
674 * g_queue_pop_tail_link:
677 * Removes and returns the last element of the queue.
679 * Returns: the #GList element at the tail of the queue, or %NULL
680 * if the queue is empty
683 g_queue_pop_tail_link (GQueue
*queue
)
685 g_return_val_if_fail (queue
!= NULL
, NULL
);
689 GList
*node
= queue
->tail
;
691 queue
->tail
= node
->prev
;
694 queue
->tail
->next
= NULL
;
708 * g_queue_pop_nth_link:
710 * @n: the link's position
712 * Removes and returns the link at the given position.
714 * Returns: the @n'th link, or %NULL if @n is off the end of @queue
719 g_queue_pop_nth_link (GQueue
*queue
,
724 g_return_val_if_fail (queue
!= NULL
, NULL
);
726 if (n
>= queue
->length
)
729 link
= g_queue_peek_nth_link (queue
, n
);
730 g_queue_unlink (queue
, link
);
736 * g_queue_peek_nth_link:
738 * @n: the position of the link
740 * Returns the link at the given position
742 * Returns: the link at the @n'th position, or %NULL
743 * if @n is off the end of the list
748 g_queue_peek_nth_link (GQueue
*queue
,
754 g_return_val_if_fail (queue
!= NULL
, NULL
);
756 if (n
>= queue
->length
)
759 if (n
> queue
->length
/ 2)
761 n
= queue
->length
- n
- 1;
764 for (i
= 0; i
< n
; ++i
)
770 for (i
= 0; i
< n
; ++i
)
778 * g_queue_link_index:
780 * @link_: a #GList link
782 * Returns the position of @link_ in @queue.
784 * Returns: the position of @link_, or -1 if the link is
790 g_queue_link_index (GQueue
*queue
,
793 g_return_val_if_fail (queue
!= NULL
, -1);
795 return g_list_position (queue
->head
, link_
);
801 * @link_: a #GList link that must be part of @queue
803 * Unlinks @link_ so that it will no longer be part of @queue.
804 * The link is not freed.
806 * @link_ must be part of @queue.
811 g_queue_unlink (GQueue
*queue
,
814 g_return_if_fail (queue
!= NULL
);
815 g_return_if_fail (link_
!= NULL
);
817 if (link_
== queue
->tail
)
818 queue
->tail
= queue
->tail
->prev
;
820 queue
->head
= g_list_remove_link (queue
->head
, link_
);
825 * g_queue_delete_link:
827 * @link_: a #GList link that must be part of @queue
829 * Removes @link_ from @queue and frees it.
831 * @link_ must be part of @queue.
836 g_queue_delete_link (GQueue
*queue
,
839 g_return_if_fail (queue
!= NULL
);
840 g_return_if_fail (link_
!= NULL
);
842 g_queue_unlink (queue
, link_
);
850 * Returns the first element of the queue.
852 * Returns: the data of the first element in the queue, or %NULL
853 * if the queue is empty
856 g_queue_peek_head (GQueue
*queue
)
858 g_return_val_if_fail (queue
!= NULL
, NULL
);
860 return queue
->head
? queue
->head
->data
: NULL
;
867 * Returns the last element of the queue.
869 * Returns: the data of the last element in the queue, or %NULL
870 * if the queue is empty
873 g_queue_peek_tail (GQueue
*queue
)
875 g_return_val_if_fail (queue
!= NULL
, NULL
);
877 return queue
->tail
? queue
->tail
->data
: NULL
;
883 * @n: the position of the element
885 * Returns the @n'th element of @queue.
887 * Returns: the data for the @n'th element of @queue,
888 * or %NULL if @n is off the end of @queue
893 g_queue_peek_nth (GQueue
*queue
,
898 g_return_val_if_fail (queue
!= NULL
, NULL
);
900 link
= g_queue_peek_nth_link (queue
, n
);
911 * @data: the data to find
913 * Returns the position of the first element in @queue which contains @data.
915 * Returns: the position of the first element in @queue which
916 * contains @data, or -1 if no element in @queue contains @data
921 g_queue_index (GQueue
*queue
,
924 g_return_val_if_fail (queue
!= NULL
, -1);
926 return g_list_index (queue
->head
, data
);
932 * @data: the data to remove
934 * Removes the first element in @queue that contains @data.
936 * Returns: %TRUE if @data was found and removed from @queue
941 g_queue_remove (GQueue
*queue
,
946 g_return_val_if_fail (queue
!= NULL
, FALSE
);
948 link
= g_list_find (queue
->head
, data
);
951 g_queue_delete_link (queue
, link
);
953 return (link
!= NULL
);
957 * g_queue_remove_all:
959 * @data: the data to remove
961 * Remove all elements whose data equals @data from @queue.
963 * Returns: the number of elements removed from @queue
968 g_queue_remove_all (GQueue
*queue
,
974 g_return_val_if_fail (queue
!= NULL
, 0);
976 old_length
= queue
->length
;
981 GList
*next
= list
->next
;
983 if (list
->data
== data
)
984 g_queue_delete_link (queue
, list
);
989 return (old_length
- queue
->length
);
993 * g_queue_insert_before:
995 * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
996 * push at the tail of the queue.
997 * @data: the data to insert
999 * Inserts @data into @queue before @sibling.
1001 * @sibling must be part of @queue. Since GLib 2.44 a %NULL sibling pushes the
1002 * data at the tail of the queue.
1007 g_queue_insert_before (GQueue
*queue
,
1011 g_return_if_fail (queue
!= NULL
);
1013 if (sibling
== NULL
)
1015 /* We don't use g_list_insert_before() with a NULL sibling because it
1016 * would be a O(n) operation and we would need to update manually the tail
1019 g_queue_push_tail (queue
, data
);
1023 queue
->head
= g_list_insert_before (queue
->head
, sibling
, data
);
1029 * g_queue_insert_after:
1031 * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
1032 * push at the head of the queue.
1033 * @data: the data to insert
1035 * Inserts @data into @queue after @sibling.
1037 * @sibling must be part of @queue. Since GLib 2.44 a %NULL sibling pushes the
1038 * data at the head of the queue.
1043 g_queue_insert_after (GQueue
*queue
,
1047 g_return_if_fail (queue
!= NULL
);
1049 if (sibling
== NULL
)
1050 g_queue_push_head (queue
, data
);
1052 g_queue_insert_before (queue
, sibling
->next
, data
);
1056 * g_queue_insert_sorted:
1058 * @data: the data to insert
1059 * @func: the #GCompareDataFunc used to compare elements in the queue. It is
1060 * called with two elements of the @queue and @user_data. It should
1061 * return 0 if the elements are equal, a negative value if the first
1062 * element comes before the second, and a positive value if the second
1063 * element comes before the first.
1064 * @user_data: user data passed to @func
1066 * Inserts @data into @queue using @func to determine the new position.
1071 g_queue_insert_sorted (GQueue
*queue
,
1073 GCompareDataFunc func
,
1078 g_return_if_fail (queue
!= NULL
);
1081 while (list
&& func (list
->data
, data
, user_data
) < 0)
1084 g_queue_insert_before (queue
, list
, data
);