Merge branch 'fix-gdbus-unix-addresses-test' into 'master'
[glib.git] / glib / gqueue.c
blobbf172f6ffec53fbcb16a1da37c06bd6062826ae6
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/>.
22 * MT safe
25 /**
26 * SECTION:queue
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
44 * g_queue_init().
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().
53 #include "config.h"
55 #include "gqueue.h"
57 #include "gtestutils.h"
58 #include "gslice.h"
60 /**
61 * g_queue_new:
63 * Creates a new #GQueue.
65 * Returns: a newly allocated #GQueue
66 **/
67 GQueue *
68 g_queue_new (void)
70 return g_slice_new0 (GQueue);
73 /**
74 * g_queue_free:
75 * @queue: a #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.
83 **/
84 void
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);
93 /**
94 * g_queue_free_full:
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
102 * element from it).
104 * Since: 2.32
106 void
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);
115 * g_queue_init:
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
121 * g_queue_new().
123 * Since: 2.14
125 void
126 g_queue_init (GQueue *queue)
128 g_return_if_fail (queue != NULL);
130 queue->head = queue->tail = NULL;
131 queue->length = 0;
135 * g_queue_clear:
136 * @queue: a #GQueue
138 * Removes all the elements in @queue. If queue elements contain
139 * dynamically-allocated memory, they should be freed first.
141 * Since: 2.14
143 void
144 g_queue_clear (GQueue *queue)
146 g_return_if_fail (queue != NULL);
148 g_list_free (queue->head);
149 g_queue_init (queue);
153 * g_queue_is_empty:
154 * @queue: a #GQueue.
156 * Returns %TRUE if the queue is empty.
158 * Returns: %TRUE if the queue is empty
160 gboolean
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:
170 * @queue: a #GQueue
172 * Returns the number of items in @queue.
174 * Returns: the number of items in @queue
176 * Since: 2.4
178 guint
179 g_queue_get_length (GQueue *queue)
181 g_return_val_if_fail (queue != NULL, 0);
183 return queue->length;
187 * g_queue_reverse:
188 * @queue: a #GQueue
190 * Reverses the order of the items in @queue.
192 * Since: 2.4
194 void
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);
204 * g_queue_copy:
205 * @queue: a #GQueue
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
213 * Since: 2.4
215 GQueue *
216 g_queue_copy (GQueue *queue)
218 GQueue *result;
219 GList *list;
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);
228 return result;
232 * g_queue_foreach:
233 * @queue: a #GQueue
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
238 * function.
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.
243 * Since: 2.4
245 void
246 g_queue_foreach (GQueue *queue,
247 GFunc func,
248 gpointer user_data)
250 GList *list;
252 g_return_if_fail (queue != NULL);
253 g_return_if_fail (func != NULL);
255 list = queue->head;
256 while (list)
258 GList *next = list->next;
259 func (list->data, user_data);
260 list = next;
265 * g_queue_find:
266 * @queue: a #GQueue
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
273 * Since: 2.4
275 GList *
276 g_queue_find (GQueue *queue,
277 gconstpointer data)
279 g_return_val_if_fail (queue != NULL, NULL);
281 return g_list_find (queue->head, data);
285 * g_queue_find_custom:
286 * @queue: a #GQueue
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
299 * Since: 2.4
301 GList *
302 g_queue_find_custom (GQueue *queue,
303 gconstpointer data,
304 GCompareFunc func)
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);
313 * g_queue_sort:
314 * @queue: a #GQueue
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.
323 * Since: 2.4
325 void
326 g_queue_sort (GQueue *queue,
327 GCompareDataFunc compare_func,
328 gpointer user_data)
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);
338 * g_queue_push_head:
339 * @queue: a #GQueue.
340 * @data: the data for the new element.
342 * Adds a new element at the head of the queue.
344 void
345 g_queue_push_head (GQueue *queue,
346 gpointer data)
348 g_return_if_fail (queue != NULL);
350 queue->head = g_list_prepend (queue->head, data);
351 if (!queue->tail)
352 queue->tail = queue->head;
353 queue->length++;
357 * g_queue_push_nth:
358 * @queue: a #GQueue
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.
366 * Since: 2.4
368 void
369 g_queue_push_nth (GQueue *queue,
370 gpointer data,
371 gint n)
373 g_return_if_fail (queue != NULL);
375 if (n < 0 || n >= queue->length)
377 g_queue_push_tail (queue, data);
378 return;
381 g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
385 * g_queue_push_head_link:
386 * @queue: a #GQueue
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.
391 void
392 g_queue_push_head_link (GQueue *queue,
393 GList *link)
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;
401 if (queue->head)
402 queue->head->prev = link;
403 else
404 queue->tail = link;
405 queue->head = link;
406 queue->length++;
410 * g_queue_push_tail:
411 * @queue: a #GQueue
412 * @data: the data for the new element
414 * Adds a new element at the tail of the queue.
416 void
417 g_queue_push_tail (GQueue *queue,
418 gpointer data)
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;
425 else
426 queue->head = queue->tail;
427 queue->length++;
431 * g_queue_push_tail_link:
432 * @queue: a #GQueue
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.
437 void
438 g_queue_push_tail_link (GQueue *queue,
439 GList *link)
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;
447 if (queue->tail)
448 queue->tail->next = link;
449 else
450 queue->head = link;
451 queue->tail = link;
452 queue->length++;
456 * g_queue_push_nth_link:
457 * @queue: a #GQueue
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
460 * @queue.
461 * @link_: the link to add to @queue
463 * Inserts @link into @queue at the given position.
465 * Since: 2.4
467 void
468 g_queue_push_nth_link (GQueue *queue,
469 gint n,
470 GList *link_)
472 GList *next;
473 GList *prev;
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_);
481 return;
484 g_assert (queue->head);
485 g_assert (queue->tail);
487 next = g_queue_peek_nth_link (queue, n);
488 prev = next->prev;
490 if (prev)
491 prev->next = link_;
492 next->prev = link_;
494 link_->next = next;
495 link_->prev = prev;
497 if (queue->head->prev)
498 queue->head = queue->head->prev;
500 if (queue->tail->next)
501 queue->tail = queue->tail->next;
503 queue->length++;
507 * g_queue_pop_head:
508 * @queue: a #GQueue
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
515 gpointer
516 g_queue_pop_head (GQueue *queue)
518 g_return_val_if_fail (queue != NULL, NULL);
520 if (queue->head)
522 GList *node = queue->head;
523 gpointer data = node->data;
525 queue->head = node->next;
526 if (queue->head)
527 queue->head->prev = NULL;
528 else
529 queue->tail = NULL;
530 g_list_free_1 (node);
531 queue->length--;
533 return data;
536 return NULL;
540 * g_queue_pop_head_link:
541 * @queue: a #GQueue
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
548 GList *
549 g_queue_pop_head_link (GQueue *queue)
551 g_return_val_if_fail (queue != NULL, NULL);
553 if (queue->head)
555 GList *node = queue->head;
557 queue->head = node->next;
558 if (queue->head)
560 queue->head->prev = NULL;
561 node->next = NULL;
563 else
564 queue->tail = NULL;
565 queue->length--;
567 return node;
570 return NULL;
574 * g_queue_peek_head_link:
575 * @queue: a #GQueue
577 * Returns the first link in @queue.
579 * Returns: the first link in @queue, or %NULL if @queue is empty
581 * Since: 2.4
583 GList *
584 g_queue_peek_head_link (GQueue *queue)
586 g_return_val_if_fail (queue != NULL, NULL);
588 return queue->head;
592 * g_queue_peek_tail_link:
593 * @queue: a #GQueue
595 * Returns the last link in @queue.
597 * Returns: the last link in @queue, or %NULL if @queue is empty
599 * Since: 2.4
601 GList *
602 g_queue_peek_tail_link (GQueue *queue)
604 g_return_val_if_fail (queue != NULL, NULL);
606 return queue->tail;
610 * g_queue_pop_tail:
611 * @queue: a #GQueue
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
618 gpointer
619 g_queue_pop_tail (GQueue *queue)
621 g_return_val_if_fail (queue != NULL, NULL);
623 if (queue->tail)
625 GList *node = queue->tail;
626 gpointer data = node->data;
628 queue->tail = node->prev;
629 if (queue->tail)
630 queue->tail->next = NULL;
631 else
632 queue->head = NULL;
633 queue->length--;
634 g_list_free_1 (node);
636 return data;
639 return NULL;
643 * g_queue_pop_nth:
644 * @queue: a #GQueue
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
651 * Since: 2.4
653 gpointer
654 g_queue_pop_nth (GQueue *queue,
655 guint n)
657 GList *nth_link;
658 gpointer result;
660 g_return_val_if_fail (queue != NULL, NULL);
662 if (n >= queue->length)
663 return NULL;
665 nth_link = g_queue_peek_nth_link (queue, n);
666 result = nth_link->data;
668 g_queue_delete_link (queue, nth_link);
670 return result;
674 * g_queue_pop_tail_link:
675 * @queue: a #GQueue
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
682 GList *
683 g_queue_pop_tail_link (GQueue *queue)
685 g_return_val_if_fail (queue != NULL, NULL);
687 if (queue->tail)
689 GList *node = queue->tail;
691 queue->tail = node->prev;
692 if (queue->tail)
694 queue->tail->next = NULL;
695 node->prev = NULL;
697 else
698 queue->head = NULL;
699 queue->length--;
701 return node;
704 return NULL;
708 * g_queue_pop_nth_link:
709 * @queue: a #GQueue
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
716 * Since: 2.4
718 GList*
719 g_queue_pop_nth_link (GQueue *queue,
720 guint n)
722 GList *link;
724 g_return_val_if_fail (queue != NULL, NULL);
726 if (n >= queue->length)
727 return NULL;
729 link = g_queue_peek_nth_link (queue, n);
730 g_queue_unlink (queue, link);
732 return link;
736 * g_queue_peek_nth_link:
737 * @queue: a #GQueue
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
745 * Since: 2.4
747 GList *
748 g_queue_peek_nth_link (GQueue *queue,
749 guint n)
751 GList *link;
752 gint i;
754 g_return_val_if_fail (queue != NULL, NULL);
756 if (n >= queue->length)
757 return NULL;
759 if (n > queue->length / 2)
761 n = queue->length - n - 1;
763 link = queue->tail;
764 for (i = 0; i < n; ++i)
765 link = link->prev;
767 else
769 link = queue->head;
770 for (i = 0; i < n; ++i)
771 link = link->next;
774 return link;
778 * g_queue_link_index:
779 * @queue: a #GQueue
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
785 * not part of @queue
787 * Since: 2.4
789 gint
790 g_queue_link_index (GQueue *queue,
791 GList *link_)
793 g_return_val_if_fail (queue != NULL, -1);
795 return g_list_position (queue->head, link_);
799 * g_queue_unlink:
800 * @queue: a #GQueue
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.
808 * Since: 2.4
810 void
811 g_queue_unlink (GQueue *queue,
812 GList *link_)
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_);
821 queue->length--;
825 * g_queue_delete_link:
826 * @queue: a #GQueue
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.
833 * Since: 2.4
835 void
836 g_queue_delete_link (GQueue *queue,
837 GList *link_)
839 g_return_if_fail (queue != NULL);
840 g_return_if_fail (link_ != NULL);
842 g_queue_unlink (queue, link_);
843 g_list_free (link_);
847 * g_queue_peek_head:
848 * @queue: a #GQueue
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
855 gpointer
856 g_queue_peek_head (GQueue *queue)
858 g_return_val_if_fail (queue != NULL, NULL);
860 return queue->head ? queue->head->data : NULL;
864 * g_queue_peek_tail:
865 * @queue: a #GQueue
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
872 gpointer
873 g_queue_peek_tail (GQueue *queue)
875 g_return_val_if_fail (queue != NULL, NULL);
877 return queue->tail ? queue->tail->data : NULL;
881 * g_queue_peek_nth:
882 * @queue: a #GQueue
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
890 * Since: 2.4
892 gpointer
893 g_queue_peek_nth (GQueue *queue,
894 guint n)
896 GList *link;
898 g_return_val_if_fail (queue != NULL, NULL);
900 link = g_queue_peek_nth_link (queue, n);
902 if (link)
903 return link->data;
905 return NULL;
909 * g_queue_index:
910 * @queue: a #GQueue
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
918 * Since: 2.4
920 gint
921 g_queue_index (GQueue *queue,
922 gconstpointer data)
924 g_return_val_if_fail (queue != NULL, -1);
926 return g_list_index (queue->head, data);
930 * g_queue_remove:
931 * @queue: a #GQueue
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
938 * Since: 2.4
940 gboolean
941 g_queue_remove (GQueue *queue,
942 gconstpointer data)
944 GList *link;
946 g_return_val_if_fail (queue != NULL, FALSE);
948 link = g_list_find (queue->head, data);
950 if (link)
951 g_queue_delete_link (queue, link);
953 return (link != NULL);
957 * g_queue_remove_all:
958 * @queue: a #GQueue
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
965 * Since: 2.4
967 guint
968 g_queue_remove_all (GQueue *queue,
969 gconstpointer data)
971 GList *list;
972 guint old_length;
974 g_return_val_if_fail (queue != NULL, 0);
976 old_length = queue->length;
978 list = queue->head;
979 while (list)
981 GList *next = list->next;
983 if (list->data == data)
984 g_queue_delete_link (queue, list);
986 list = next;
989 return (old_length - queue->length);
993 * g_queue_insert_before:
994 * @queue: a #GQueue
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.
1004 * Since: 2.4
1006 void
1007 g_queue_insert_before (GQueue *queue,
1008 GList *sibling,
1009 gpointer data)
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
1017 * pointer.
1019 g_queue_push_tail (queue, data);
1021 else
1023 queue->head = g_list_insert_before (queue->head, sibling, data);
1024 queue->length++;
1029 * g_queue_insert_after:
1030 * @queue: a #GQueue
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.
1040 * Since: 2.4
1042 void
1043 g_queue_insert_after (GQueue *queue,
1044 GList *sibling,
1045 gpointer data)
1047 g_return_if_fail (queue != NULL);
1049 if (sibling == NULL)
1050 g_queue_push_head (queue, data);
1051 else
1052 g_queue_insert_before (queue, sibling->next, data);
1056 * g_queue_insert_sorted:
1057 * @queue: a #GQueue
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.
1068 * Since: 2.4
1070 void
1071 g_queue_insert_sorted (GQueue *queue,
1072 gpointer data,
1073 GCompareDataFunc func,
1074 gpointer user_data)
1076 GList *list;
1078 g_return_if_fail (queue != NULL);
1080 list = queue->head;
1081 while (list && func (list->data, data, user_data) < 0)
1082 list = list->next;
1084 g_queue_insert_before (queue, list, data);