1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * GQueue: Double ended queue implementation, piggy backed on GList.
5 * Copyright (C) 1998 Tim Janik
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21 /*
22 * MT safe
23 */
24
25 /**
26 * SECTION:queue
27 * @Title: Double-ended Queues
28 * @Short_description: double-ended queue data structure
29 *
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.
33 *
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.
37 *
38 * As with all other GLib data structures, #GQueue is not thread-safe.
39 * For a thread-safe queue, use #GAsyncQueue.
40 *
41 * To create a new GQueue, use g_queue_new().
42 *
43 * To initialize a statically-allocated GQueue, use #G_QUEUE_INIT or
44 * g_queue_init().
45 *
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().
48 *
49 * To remove elements, use g_queue_pop_head() and g_queue_pop_tail().
50 *
51 * To free the entire queue, use g_queue_free().
52 */
53 #include "config.h"
54
55 #include "gqueue.h"
56
57 #include "gtestutils.h"
58 #include "gslice.h"
59
60 /**
61 * g_queue_new:
62 *
63 * Creates a new #GQueue.
64 *
65 * Returns: a newly allocated #GQueue
66 **/
67 GQueue *
g_queue_new(void)68 g_queue_new (void)
69 {
70 return g_slice_new0 (GQueue);
71 }
72
73 /**
74 * g_queue_free:
75 * @queue: a #GQueue
76 *
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.
80 *
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
g_queue_free(GQueue * queue)85 g_queue_free (GQueue *queue)
86 {
87 g_return_if_fail (queue != NULL);
88
89 g_list_free (queue->head);
90 g_slice_free (GQueue, queue);
91 }
92
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
97 *
98 * Convenience method, which frees all the memory used by a #GQueue,
99 * and calls the specified destroy function on every element's data.
100 *
101 * @free_func should not modify the queue (eg, by removing the freed
102 * element from it).
103 *
104 * Since: 2.32
105 */
106 void
g_queue_free_full(GQueue * queue,GDestroyNotify free_func)107 g_queue_free_full (GQueue *queue,
108 GDestroyNotify free_func)
109 {
110 g_queue_foreach (queue, (GFunc) free_func, NULL);
111 g_queue_free (queue);
112 }
113
114 /**
115 * g_queue_init:
116 * @queue: an uninitialized #GQueue
117 *
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().
122 *
123 * Since: 2.14
124 */
125 void
g_queue_init(GQueue * queue)126 g_queue_init (GQueue *queue)
127 {
128 g_return_if_fail (queue != NULL);
129
130 queue->head = queue->tail = NULL;
131 queue->length = 0;
132 }
133
134 /**
135 * g_queue_clear:
136 * @queue: a #GQueue
137 *
138 * Removes all the elements in @queue. If queue elements contain
139 * dynamically-allocated memory, they should be freed first.
140 *
141 * Since: 2.14
142 */
143 void
g_queue_clear(GQueue * queue)144 g_queue_clear (GQueue *queue)
145 {
146 g_return_if_fail (queue != NULL);
147
148 g_list_free (queue->head);
149 g_queue_init (queue);
150 }
151
152 /**
153 * g_queue_clear_full:
154 * @queue: a pointer to a #GQueue
155 * @free_func: (nullable): the function to be called to free memory allocated
156 *
157 * Convenience method, which frees all the memory used by a #GQueue,
158 * and calls the provided @free_func on each item in the #GQueue.
159 *
160 * Since: 2.60
161 */
162 void
g_queue_clear_full(GQueue * queue,GDestroyNotify free_func)163 g_queue_clear_full (GQueue *queue,
164 GDestroyNotify free_func)
165 {
166 g_return_if_fail (queue != NULL);
167
168 if (free_func != NULL)
169 g_queue_foreach (queue, (GFunc) free_func, NULL);
170
171 g_queue_clear (queue);
172 }
173
174 /**
175 * g_queue_is_empty:
176 * @queue: a #GQueue.
177 *
178 * Returns %TRUE if the queue is empty.
179 *
180 * Returns: %TRUE if the queue is empty
181 */
182 gboolean
g_queue_is_empty(GQueue * queue)183 g_queue_is_empty (GQueue *queue)
184 {
185 g_return_val_if_fail (queue != NULL, TRUE);
186
187 return queue->head == NULL;
188 }
189
190 /**
191 * g_queue_get_length:
192 * @queue: a #GQueue
193 *
194 * Returns the number of items in @queue.
195 *
196 * Returns: the number of items in @queue
197 *
198 * Since: 2.4
199 */
200 guint
g_queue_get_length(GQueue * queue)201 g_queue_get_length (GQueue *queue)
202 {
203 g_return_val_if_fail (queue != NULL, 0);
204
205 return queue->length;
206 }
207
208 /**
209 * g_queue_reverse:
210 * @queue: a #GQueue
211 *
212 * Reverses the order of the items in @queue.
213 *
214 * Since: 2.4
215 */
216 void
g_queue_reverse(GQueue * queue)217 g_queue_reverse (GQueue *queue)
218 {
219 g_return_if_fail (queue != NULL);
220
221 queue->tail = queue->head;
222 queue->head = g_list_reverse (queue->head);
223 }
224
225 /**
226 * g_queue_copy:
227 * @queue: a #GQueue
228 *
229 * Copies a @queue. Note that is a shallow copy. If the elements in the
230 * queue consist of pointers to data, the pointers are copied, but the
231 * actual data is not.
232 *
233 * Returns: a copy of @queue
234 *
235 * Since: 2.4
236 */
237 GQueue *
g_queue_copy(GQueue * queue)238 g_queue_copy (GQueue *queue)
239 {
240 GQueue *result;
241 GList *list;
242
243 g_return_val_if_fail (queue != NULL, NULL);
244
245 result = g_queue_new ();
246
247 for (list = queue->head; list != NULL; list = list->next)
248 g_queue_push_tail (result, list->data);
249
250 return result;
251 }
252
253 /**
254 * g_queue_foreach:
255 * @queue: a #GQueue
256 * @func: the function to call for each element's data
257 * @user_data: user data to pass to @func
258 *
259 * Calls @func for each element in the queue passing @user_data to the
260 * function.
261 *
262 * It is safe for @func to remove the element from @queue, but it must
263 * not modify any part of the queue after that element.
264 *
265 * Since: 2.4
266 */
267 void
g_queue_foreach(GQueue * queue,GFunc func,gpointer user_data)268 g_queue_foreach (GQueue *queue,
269 GFunc func,
270 gpointer user_data)
271 {
272 GList *list;
273
274 g_return_if_fail (queue != NULL);
275 g_return_if_fail (func != NULL);
276
277 list = queue->head;
278 while (list)
279 {
280 GList *next = list->next;
281 func (list->data, user_data);
282 list = next;
283 }
284 }
285
286 /**
287 * g_queue_find:
288 * @queue: a #GQueue
289 * @data: data to find
290 *
291 * Finds the first link in @queue which contains @data.
292 *
293 * Returns: the first link in @queue which contains @data
294 *
295 * Since: 2.4
296 */
297 GList *
g_queue_find(GQueue * queue,gconstpointer data)298 g_queue_find (GQueue *queue,
299 gconstpointer data)
300 {
301 g_return_val_if_fail (queue != NULL, NULL);
302
303 return g_list_find (queue->head, data);
304 }
305
306 /**
307 * g_queue_find_custom:
308 * @queue: a #GQueue
309 * @data: user data passed to @func
310 * @func: a #GCompareFunc to call for each element. It should return 0
311 * when the desired element is found
312 *
313 * Finds an element in a #GQueue, using a supplied function to find the
314 * desired element. It iterates over the queue, calling the given function
315 * which should return 0 when the desired element is found. The function
316 * takes two gconstpointer arguments, the #GQueue element's data as the
317 * first argument and the given user data as the second argument.
318 *
319 * Returns: the found link, or %NULL if it wasn't found
320 *
321 * Since: 2.4
322 */
323 GList *
g_queue_find_custom(GQueue * queue,gconstpointer data,GCompareFunc func)324 g_queue_find_custom (GQueue *queue,
325 gconstpointer data,
326 GCompareFunc func)
327 {
328 g_return_val_if_fail (queue != NULL, NULL);
329 g_return_val_if_fail (func != NULL, NULL);
330
331 return g_list_find_custom (queue->head, data, func);
332 }
333
334 /**
335 * g_queue_sort:
336 * @queue: a #GQueue
337 * @compare_func: the #GCompareDataFunc used to sort @queue. This function
338 * is passed two elements of the queue and should return 0 if they are
339 * equal, a negative value if the first comes before the second, and
340 * a positive value if the second comes before the first.
341 * @user_data: user data passed to @compare_func
342 *
343 * Sorts @queue using @compare_func.
344 *
345 * Since: 2.4
346 */
347 void
g_queue_sort(GQueue * queue,GCompareDataFunc compare_func,gpointer user_data)348 g_queue_sort (GQueue *queue,
349 GCompareDataFunc compare_func,
350 gpointer user_data)
351 {
352 g_return_if_fail (queue != NULL);
353 g_return_if_fail (compare_func != NULL);
354
355 queue->head = g_list_sort_with_data (queue->head, compare_func, user_data);
356 queue->tail = g_list_last (queue->head);
357 }
358
359 /**
360 * g_queue_push_head:
361 * @queue: a #GQueue.
362 * @data: the data for the new element.
363 *
364 * Adds a new element at the head of the queue.
365 */
366 void
g_queue_push_head(GQueue * queue,gpointer data)367 g_queue_push_head (GQueue *queue,
368 gpointer data)
369 {
370 g_return_if_fail (queue != NULL);
371
372 queue->head = g_list_prepend (queue->head, data);
373 if (!queue->tail)
374 queue->tail = queue->head;
375 queue->length++;
376 }
377
378 /**
379 * g_queue_push_nth:
380 * @queue: a #GQueue
381 * @data: the data for the new element
382 * @n: the position to insert the new element. If @n is negative or
383 * larger than the number of elements in the @queue, the element is
384 * added to the end of the queue.
385 *
386 * Inserts a new element into @queue at the given position.
387 *
388 * Since: 2.4
389 */
390 void
g_queue_push_nth(GQueue * queue,gpointer data,gint n)391 g_queue_push_nth (GQueue *queue,
392 gpointer data,
393 gint n)
394 {
395 g_return_if_fail (queue != NULL);
396
397 if (n < 0 || (guint) n >= queue->length)
398 {
399 g_queue_push_tail (queue, data);
400 return;
401 }
402
403 g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
404 }
405
406 /**
407 * g_queue_push_head_link:
408 * @queue: a #GQueue
409 * @link_: a single #GList element, not a list with more than one element
410 *
411 * Adds a new element at the head of the queue.
412 */
413 void
g_queue_push_head_link(GQueue * queue,GList * link)414 g_queue_push_head_link (GQueue *queue,
415 GList *link)
416 {
417 g_return_if_fail (queue != NULL);
418 g_return_if_fail (link != NULL);
419 g_return_if_fail (link->prev == NULL);
420 g_return_if_fail (link->next == NULL);
421
422 link->next = queue->head;
423 if (queue->head)
424 queue->head->prev = link;
425 else
426 queue->tail = link;
427 queue->head = link;
428 queue->length++;
429 }
430
431 /**
432 * g_queue_push_tail:
433 * @queue: a #GQueue
434 * @data: the data for the new element
435 *
436 * Adds a new element at the tail of the queue.
437 */
438 void
g_queue_push_tail(GQueue * queue,gpointer data)439 g_queue_push_tail (GQueue *queue,
440 gpointer data)
441 {
442 g_return_if_fail (queue != NULL);
443
444 queue->tail = g_list_append (queue->tail, data);
445 if (queue->tail->next)
446 queue->tail = queue->tail->next;
447 else
448 queue->head = queue->tail;
449 queue->length++;
450 }
451
452 /**
453 * g_queue_push_tail_link:
454 * @queue: a #GQueue
455 * @link_: a single #GList element, not a list with more than one element
456 *
457 * Adds a new element at the tail of the queue.
458 */
459 void
g_queue_push_tail_link(GQueue * queue,GList * link)460 g_queue_push_tail_link (GQueue *queue,
461 GList *link)
462 {
463 g_return_if_fail (queue != NULL);
464 g_return_if_fail (link != NULL);
465 g_return_if_fail (link->prev == NULL);
466 g_return_if_fail (link->next == NULL);
467
468 link->prev = queue->tail;
469 if (queue->tail)
470 queue->tail->next = link;
471 else
472 queue->head = link;
473 queue->tail = link;
474 queue->length++;
475 }
476
477 /**
478 * g_queue_push_nth_link:
479 * @queue: a #GQueue
480 * @n: the position to insert the link. If this is negative or larger than
481 * the number of elements in @queue, the link is added to the end of
482 * @queue.
483 * @link_: the link to add to @queue
484 *
485 * Inserts @link into @queue at the given position.
486 *
487 * Since: 2.4
488 */
489 void
g_queue_push_nth_link(GQueue * queue,gint n,GList * link_)490 g_queue_push_nth_link (GQueue *queue,
491 gint n,
492 GList *link_)
493 {
494 GList *next;
495 GList *prev;
496
497 g_return_if_fail (queue != NULL);
498 g_return_if_fail (link_ != NULL);
499
500 if (n < 0 || (guint) n >= queue->length)
501 {
502 g_queue_push_tail_link (queue, link_);
503 return;
504 }
505
506 g_assert (queue->head);
507 g_assert (queue->tail);
508
509 next = g_queue_peek_nth_link (queue, n);
510 prev = next->prev;
511
512 if (prev)
513 prev->next = link_;
514 next->prev = link_;
515
516 link_->next = next;
517 link_->prev = prev;
518
519 if (queue->head->prev)
520 queue->head = queue->head->prev;
521
522 /* The case where we’re pushing @link_ at the end of @queue is handled above
523 * using g_queue_push_tail_link(), so we should never have to manually adjust
524 * queue->tail. */
525 g_assert (queue->tail->next == NULL);
526
527 queue->length++;
528 }
529
530 /**
531 * g_queue_pop_head:
532 * @queue: a #GQueue
533 *
534 * Removes the first element of the queue and returns its data.
535 *
536 * Returns: the data of the first element in the queue, or %NULL
537 * if the queue is empty
538 */
539 gpointer
g_queue_pop_head(GQueue * queue)540 g_queue_pop_head (GQueue *queue)
541 {
542 g_return_val_if_fail (queue != NULL, NULL);
543
544 if (queue->head)
545 {
546 GList *node = queue->head;
547 gpointer data = node->data;
548
549 queue->head = node->next;
550 if (queue->head)
551 queue->head->prev = NULL;
552 else
553 queue->tail = NULL;
554 g_list_free_1 (node);
555 queue->length--;
556
557 return data;
558 }
559
560 return NULL;
561 }
562
563 /**
564 * g_queue_pop_head_link:
565 * @queue: a #GQueue
566 *
567 * Removes and returns the first element of the queue.
568 *
569 * Returns: the #GList element at the head of the queue, or %NULL
570 * if the queue is empty
571 */
572 GList *
g_queue_pop_head_link(GQueue * queue)573 g_queue_pop_head_link (GQueue *queue)
574 {
575 g_return_val_if_fail (queue != NULL, NULL);
576
577 if (queue->head)
578 {
579 GList *node = queue->head;
580
581 queue->head = node->next;
582 if (queue->head)
583 {
584 queue->head->prev = NULL;
585 node->next = NULL;
586 }
587 else
588 queue->tail = NULL;
589 queue->length--;
590
591 return node;
592 }
593
594 return NULL;
595 }
596
597 /**
598 * g_queue_peek_head_link:
599 * @queue: a #GQueue
600 *
601 * Returns the first link in @queue.
602 *
603 * Returns: the first link in @queue, or %NULL if @queue is empty
604 *
605 * Since: 2.4
606 */
607 GList *
g_queue_peek_head_link(GQueue * queue)608 g_queue_peek_head_link (GQueue *queue)
609 {
610 g_return_val_if_fail (queue != NULL, NULL);
611
612 return queue->head;
613 }
614
615 /**
616 * g_queue_peek_tail_link:
617 * @queue: a #GQueue
618 *
619 * Returns the last link in @queue.
620 *
621 * Returns: the last link in @queue, or %NULL if @queue is empty
622 *
623 * Since: 2.4
624 */
625 GList *
g_queue_peek_tail_link(GQueue * queue)626 g_queue_peek_tail_link (GQueue *queue)
627 {
628 g_return_val_if_fail (queue != NULL, NULL);
629
630 return queue->tail;
631 }
632
633 /**
634 * g_queue_pop_tail:
635 * @queue: a #GQueue
636 *
637 * Removes the last element of the queue and returns its data.
638 *
639 * Returns: the data of the last element in the queue, or %NULL
640 * if the queue is empty
641 */
642 gpointer
g_queue_pop_tail(GQueue * queue)643 g_queue_pop_tail (GQueue *queue)
644 {
645 g_return_val_if_fail (queue != NULL, NULL);
646
647 if (queue->tail)
648 {
649 GList *node = queue->tail;
650 gpointer data = node->data;
651
652 queue->tail = node->prev;
653 if (queue->tail)
654 queue->tail->next = NULL;
655 else
656 queue->head = NULL;
657 queue->length--;
658 g_list_free_1 (node);
659
660 return data;
661 }
662
663 return NULL;
664 }
665
666 /**
667 * g_queue_pop_nth:
668 * @queue: a #GQueue
669 * @n: the position of the element
670 *
671 * Removes the @n'th element of @queue and returns its data.
672 *
673 * Returns: the element's data, or %NULL if @n is off the end of @queue
674 *
675 * Since: 2.4
676 */
677 gpointer
g_queue_pop_nth(GQueue * queue,guint n)678 g_queue_pop_nth (GQueue *queue,
679 guint n)
680 {
681 GList *nth_link;
682 gpointer result;
683
684 g_return_val_if_fail (queue != NULL, NULL);
685
686 if (n >= queue->length)
687 return NULL;
688
689 nth_link = g_queue_peek_nth_link (queue, n);
690 result = nth_link->data;
691
692 g_queue_delete_link (queue, nth_link);
693
694 return result;
695 }
696
697 /**
698 * g_queue_pop_tail_link:
699 * @queue: a #GQueue
700 *
701 * Removes and returns the last element of the queue.
702 *
703 * Returns: the #GList element at the tail of the queue, or %NULL
704 * if the queue is empty
705 */
706 GList *
g_queue_pop_tail_link(GQueue * queue)707 g_queue_pop_tail_link (GQueue *queue)
708 {
709 g_return_val_if_fail (queue != NULL, NULL);
710
711 if (queue->tail)
712 {
713 GList *node = queue->tail;
714
715 queue->tail = node->prev;
716 if (queue->tail)
717 {
718 queue->tail->next = NULL;
719 node->prev = NULL;
720 }
721 else
722 queue->head = NULL;
723 queue->length--;
724
725 return node;
726 }
727
728 return NULL;
729 }
730
731 /**
732 * g_queue_pop_nth_link:
733 * @queue: a #GQueue
734 * @n: the link's position
735 *
736 * Removes and returns the link at the given position.
737 *
738 * Returns: the @n'th link, or %NULL if @n is off the end of @queue
739 *
740 * Since: 2.4
741 */
742 GList*
g_queue_pop_nth_link(GQueue * queue,guint n)743 g_queue_pop_nth_link (GQueue *queue,
744 guint n)
745 {
746 GList *link;
747
748 g_return_val_if_fail (queue != NULL, NULL);
749
750 if (n >= queue->length)
751 return NULL;
752
753 link = g_queue_peek_nth_link (queue, n);
754 g_queue_unlink (queue, link);
755
756 return link;
757 }
758
759 /**
760 * g_queue_peek_nth_link:
761 * @queue: a #GQueue
762 * @n: the position of the link
763 *
764 * Returns the link at the given position
765 *
766 * Returns: the link at the @n'th position, or %NULL
767 * if @n is off the end of the list
768 *
769 * Since: 2.4
770 */
771 GList *
g_queue_peek_nth_link(GQueue * queue,guint n)772 g_queue_peek_nth_link (GQueue *queue,
773 guint n)
774 {
775 GList *link;
776 guint i;
777
778 g_return_val_if_fail (queue != NULL, NULL);
779
780 if (n >= queue->length)
781 return NULL;
782
783 if (n > queue->length / 2)
784 {
785 n = queue->length - n - 1;
786
787 link = queue->tail;
788 for (i = 0; i < n; ++i)
789 link = link->prev;
790 }
791 else
792 {
793 link = queue->head;
794 for (i = 0; i < n; ++i)
795 link = link->next;
796 }
797
798 return link;
799 }
800
801 /**
802 * g_queue_link_index:
803 * @queue: a #GQueue
804 * @link_: a #GList link
805 *
806 * Returns the position of @link_ in @queue.
807 *
808 * Returns: the position of @link_, or -1 if the link is
809 * not part of @queue
810 *
811 * Since: 2.4
812 */
813 gint
g_queue_link_index(GQueue * queue,GList * link_)814 g_queue_link_index (GQueue *queue,
815 GList *link_)
816 {
817 g_return_val_if_fail (queue != NULL, -1);
818
819 return g_list_position (queue->head, link_);
820 }
821
822 /**
823 * g_queue_unlink:
824 * @queue: a #GQueue
825 * @link_: a #GList link that must be part of @queue
826 *
827 * Unlinks @link_ so that it will no longer be part of @queue.
828 * The link is not freed.
829 *
830 * @link_ must be part of @queue.
831 *
832 * Since: 2.4
833 */
834 void
g_queue_unlink(GQueue * queue,GList * link_)835 g_queue_unlink (GQueue *queue,
836 GList *link_)
837 {
838 g_return_if_fail (queue != NULL);
839 g_return_if_fail (link_ != NULL);
840
841 if (link_ == queue->tail)
842 queue->tail = queue->tail->prev;
843
844 queue->head = g_list_remove_link (queue->head, link_);
845 queue->length--;
846 }
847
848 /**
849 * g_queue_delete_link:
850 * @queue: a #GQueue
851 * @link_: a #GList link that must be part of @queue
852 *
853 * Removes @link_ from @queue and frees it.
854 *
855 * @link_ must be part of @queue.
856 *
857 * Since: 2.4
858 */
859 void
g_queue_delete_link(GQueue * queue,GList * link_)860 g_queue_delete_link (GQueue *queue,
861 GList *link_)
862 {
863 g_return_if_fail (queue != NULL);
864 g_return_if_fail (link_ != NULL);
865
866 g_queue_unlink (queue, link_);
867 g_list_free (link_);
868 }
869
870 /**
871 * g_queue_peek_head:
872 * @queue: a #GQueue
873 *
874 * Returns the first element of the queue.
875 *
876 * Returns: the data of the first element in the queue, or %NULL
877 * if the queue is empty
878 */
879 gpointer
g_queue_peek_head(GQueue * queue)880 g_queue_peek_head (GQueue *queue)
881 {
882 g_return_val_if_fail (queue != NULL, NULL);
883
884 return queue->head ? queue->head->data : NULL;
885 }
886
887 /**
888 * g_queue_peek_tail:
889 * @queue: a #GQueue
890 *
891 * Returns the last element of the queue.
892 *
893 * Returns: the data of the last element in the queue, or %NULL
894 * if the queue is empty
895 */
896 gpointer
g_queue_peek_tail(GQueue * queue)897 g_queue_peek_tail (GQueue *queue)
898 {
899 g_return_val_if_fail (queue != NULL, NULL);
900
901 return queue->tail ? queue->tail->data : NULL;
902 }
903
904 /**
905 * g_queue_peek_nth:
906 * @queue: a #GQueue
907 * @n: the position of the element
908 *
909 * Returns the @n'th element of @queue.
910 *
911 * Returns: the data for the @n'th element of @queue,
912 * or %NULL if @n is off the end of @queue
913 *
914 * Since: 2.4
915 */
916 gpointer
g_queue_peek_nth(GQueue * queue,guint n)917 g_queue_peek_nth (GQueue *queue,
918 guint n)
919 {
920 GList *link;
921
922 g_return_val_if_fail (queue != NULL, NULL);
923
924 link = g_queue_peek_nth_link (queue, n);
925
926 if (link)
927 return link->data;
928
929 return NULL;
930 }
931
932 /**
933 * g_queue_index:
934 * @queue: a #GQueue
935 * @data: the data to find
936 *
937 * Returns the position of the first element in @queue which contains @data.
938 *
939 * Returns: the position of the first element in @queue which
940 * contains @data, or -1 if no element in @queue contains @data
941 *
942 * Since: 2.4
943 */
944 gint
g_queue_index(GQueue * queue,gconstpointer data)945 g_queue_index (GQueue *queue,
946 gconstpointer data)
947 {
948 g_return_val_if_fail (queue != NULL, -1);
949
950 return g_list_index (queue->head, data);
951 }
952
953 /**
954 * g_queue_remove:
955 * @queue: a #GQueue
956 * @data: the data to remove
957 *
958 * Removes the first element in @queue that contains @data.
959 *
960 * Returns: %TRUE if @data was found and removed from @queue
961 *
962 * Since: 2.4
963 */
964 gboolean
g_queue_remove(GQueue * queue,gconstpointer data)965 g_queue_remove (GQueue *queue,
966 gconstpointer data)
967 {
968 GList *link;
969
970 g_return_val_if_fail (queue != NULL, FALSE);
971
972 link = g_list_find (queue->head, data);
973
974 if (link)
975 g_queue_delete_link (queue, link);
976
977 return (link != NULL);
978 }
979
980 /**
981 * g_queue_remove_all:
982 * @queue: a #GQueue
983 * @data: the data to remove
984 *
985 * Remove all elements whose data equals @data from @queue.
986 *
987 * Returns: the number of elements removed from @queue
988 *
989 * Since: 2.4
990 */
991 guint
g_queue_remove_all(GQueue * queue,gconstpointer data)992 g_queue_remove_all (GQueue *queue,
993 gconstpointer data)
994 {
995 GList *list;
996 guint old_length;
997
998 g_return_val_if_fail (queue != NULL, 0);
999
1000 old_length = queue->length;
1001
1002 list = queue->head;
1003 while (list)
1004 {
1005 GList *next = list->next;
1006
1007 if (list->data == data)
1008 g_queue_delete_link (queue, list);
1009
1010 list = next;
1011 }
1012
1013 return (old_length - queue->length);
1014 }
1015
1016 /**
1017 * g_queue_insert_before:
1018 * @queue: a #GQueue
1019 * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
1020 * push at the tail of the queue.
1021 * @data: the data to insert
1022 *
1023 * Inserts @data into @queue before @sibling.
1024 *
1025 * @sibling must be part of @queue. Since GLib 2.44 a %NULL sibling pushes the
1026 * data at the tail of the queue.
1027 *
1028 * Since: 2.4
1029 */
1030 void
g_queue_insert_before(GQueue * queue,GList * sibling,gpointer data)1031 g_queue_insert_before (GQueue *queue,
1032 GList *sibling,
1033 gpointer data)
1034 {
1035 g_return_if_fail (queue != NULL);
1036
1037 if (sibling == NULL)
1038 {
1039 /* We don't use g_list_insert_before() with a NULL sibling because it
1040 * would be a O(n) operation and we would need to update manually the tail
1041 * pointer.
1042 */
1043 g_queue_push_tail (queue, data);
1044 }
1045 else
1046 {
1047 queue->head = g_list_insert_before (queue->head, sibling, data);
1048 queue->length++;
1049 }
1050 }
1051
1052 /**
1053 * g_queue_insert_before_link:
1054 * @queue: a #GQueue
1055 * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
1056 * push at the tail of the queue.
1057 * @link_: a #GList link to insert which must not be part of any other list.
1058 *
1059 * Inserts @link_ into @queue before @sibling.
1060 *
1061 * @sibling must be part of @queue.
1062 *
1063 * Since: 2.62
1064 */
1065 void
g_queue_insert_before_link(GQueue * queue,GList * sibling,GList * link_)1066 g_queue_insert_before_link (GQueue *queue,
1067 GList *sibling,
1068 GList *link_)
1069 {
1070 g_return_if_fail (queue != NULL);
1071 g_return_if_fail (link_ != NULL);
1072 g_return_if_fail (link_->prev == NULL);
1073 g_return_if_fail (link_->next == NULL);
1074
1075 if G_UNLIKELY (sibling == NULL)
1076 {
1077 /* We don't use g_list_insert_before_link() with a NULL sibling because it
1078 * would be a O(n) operation and we would need to update manually the tail
1079 * pointer.
1080 */
1081 g_queue_push_tail_link (queue, link_);
1082 }
1083 else
1084 {
1085 queue->head = g_list_insert_before_link (queue->head, sibling, link_);
1086 queue->length++;
1087 }
1088 }
1089
1090 /**
1091 * g_queue_insert_after:
1092 * @queue: a #GQueue
1093 * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
1094 * push at the head of the queue.
1095 * @data: the data to insert
1096 *
1097 * Inserts @data into @queue after @sibling.
1098 *
1099 * @sibling must be part of @queue. Since GLib 2.44 a %NULL sibling pushes the
1100 * data at the head of the queue.
1101 *
1102 * Since: 2.4
1103 */
1104 void
g_queue_insert_after(GQueue * queue,GList * sibling,gpointer data)1105 g_queue_insert_after (GQueue *queue,
1106 GList *sibling,
1107 gpointer data)
1108 {
1109 g_return_if_fail (queue != NULL);
1110
1111 if (sibling == NULL)
1112 g_queue_push_head (queue, data);
1113 else
1114 g_queue_insert_before (queue, sibling->next, data);
1115 }
1116
1117 /**
1118 * g_queue_insert_after_link:
1119 * @queue: a #GQueue
1120 * @sibling: (nullable): a #GList link that must be part of @queue, or %NULL to
1121 * push at the head of the queue.
1122 * @link_: a #GList link to insert which must not be part of any other list.
1123 *
1124 * Inserts @link_ into @queue after @sibling.
1125 *
1126 * @sibling must be part of @queue.
1127 *
1128 * Since: 2.62
1129 */
1130 void
g_queue_insert_after_link(GQueue * queue,GList * sibling,GList * link_)1131 g_queue_insert_after_link (GQueue *queue,
1132 GList *sibling,
1133 GList *link_)
1134 {
1135 g_return_if_fail (queue != NULL);
1136 g_return_if_fail (link_ != NULL);
1137 g_return_if_fail (link_->prev == NULL);
1138 g_return_if_fail (link_->next == NULL);
1139
1140 if G_UNLIKELY (sibling == NULL)
1141 g_queue_push_head_link (queue, link_);
1142 else
1143 g_queue_insert_before_link (queue, sibling->next, link_);
1144 }
1145
1146 /**
1147 * g_queue_insert_sorted:
1148 * @queue: a #GQueue
1149 * @data: the data to insert
1150 * @func: the #GCompareDataFunc used to compare elements in the queue. It is
1151 * called with two elements of the @queue and @user_data. It should
1152 * return 0 if the elements are equal, a negative value if the first
1153 * element comes before the second, and a positive value if the second
1154 * element comes before the first.
1155 * @user_data: user data passed to @func
1156 *
1157 * Inserts @data into @queue using @func to determine the new position.
1158 *
1159 * Since: 2.4
1160 */
1161 void
g_queue_insert_sorted(GQueue * queue,gpointer data,GCompareDataFunc func,gpointer user_data)1162 g_queue_insert_sorted (GQueue *queue,
1163 gpointer data,
1164 GCompareDataFunc func,
1165 gpointer user_data)
1166 {
1167 GList *list;
1168
1169 g_return_if_fail (queue != NULL);
1170
1171 list = queue->head;
1172 while (list && func (list->data, data, user_data) < 0)
1173 list = list->next;
1174
1175 g_queue_insert_before (queue, list, data);
1176 }
1177