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 <link linkend="glib-Type-Conversion-Macros">Type
36 * Conversion Macros</link>, 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 * Return value: 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 * Return value: 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 * Return value: 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 * Return value: 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, <emphasis>not</emphasis> a list with
379 * more than one element
381 * Adds a new element at the head of the queue.
384 g_queue_push_head_link (GQueue *queue,
387 g_return_if_fail (queue != NULL);
388 g_return_if_fail (link != NULL);
389 g_return_if_fail (link->prev == NULL);
390 g_return_if_fail (link->next == NULL);
392 link->next = queue->head;
394 queue->head->prev = link;
404 * @data: the data for the new element
406 * Adds a new element at the tail of the queue.
409 g_queue_push_tail (GQueue *queue,
412 g_return_if_fail (queue != NULL);
414 queue->tail = g_list_append (queue->tail, data);
415 if (queue->tail->next)
416 queue->tail = queue->tail->next;
418 queue->head = queue->tail;
423 * g_queue_push_tail_link:
425 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
426 * more than one element
428 * Adds a new element at the tail of the queue.
431 g_queue_push_tail_link (GQueue *queue,
434 g_return_if_fail (queue != NULL);
435 g_return_if_fail (link != NULL);
436 g_return_if_fail (link->prev == NULL);
437 g_return_if_fail (link->next == NULL);
439 link->prev = queue->tail;
441 queue->tail->next = link;
449 * g_queue_push_nth_link:
451 * @n: the position to insert the link. If this is negative or larger than
452 * the number of elements in @queue, the link is added to the end of
454 * @link_: the link to add to @queue
456 * Inserts @link into @queue at the given position.
461 g_queue_push_nth_link (GQueue *queue,
468 g_return_if_fail (queue != NULL);
469 g_return_if_fail (link_ != NULL);
471 if (n < 0 || n >= queue->length)
473 g_queue_push_tail_link (queue, link_);
477 g_assert (queue->head);
478 g_assert (queue->tail);
480 next = g_queue_peek_nth_link (queue, n);
490 if (queue->head->prev)
491 queue->head = queue->head->prev;
493 if (queue->tail->next)
494 queue->tail = queue->tail->next;
503 * Removes the first element of the queue and returns its data.
505 * Returns: the data of the first element in the queue, or %NULL
506 * if the queue is empty
509 g_queue_pop_head (GQueue *queue)
511 g_return_val_if_fail (queue != NULL, NULL);
515 GList *node = queue->head;
516 gpointer data = node->data;
518 queue->head = node->next;
520 queue->head->prev = NULL;
523 g_list_free_1 (node);
533 * g_queue_pop_head_link:
536 * Removes and returns the first element of the queue.
538 * Returns: the #GList element at the head of the queue, or %NULL
539 * if the queue is empty
542 g_queue_pop_head_link (GQueue *queue)
544 g_return_val_if_fail (queue != NULL, NULL);
548 GList *node = queue->head;
550 queue->head = node->next;
553 queue->head->prev = NULL;
567 * g_queue_peek_head_link:
570 * Returns the first link in @queue.
572 * Return value: the first link in @queue, or %NULL if @queue is empty
577 g_queue_peek_head_link (GQueue *queue)
579 g_return_val_if_fail (queue != NULL, NULL);
585 * g_queue_peek_tail_link:
588 * Returns the last link in @queue.
590 * Return value: the last link in @queue, or %NULL if @queue is empty
595 g_queue_peek_tail_link (GQueue *queue)
597 g_return_val_if_fail (queue != NULL, NULL);
606 * Removes the last element of the queue and returns its data.
608 * Returns: the data of the last element in the queue, or %NULL
609 * if the queue is empty
612 g_queue_pop_tail (GQueue *queue)
614 g_return_val_if_fail (queue != NULL, NULL);
618 GList *node = queue->tail;
619 gpointer data = node->data;
621 queue->tail = node->prev;
623 queue->tail->next = NULL;
627 g_list_free_1 (node);
638 * @n: the position of the element
640 * Removes the @n'th element of @queue and returns its data.
642 * Return value: the element's data, or %NULL if @n is off the end of @queue
647 g_queue_pop_nth (GQueue *queue,
653 g_return_val_if_fail (queue != NULL, NULL);
655 if (n >= queue->length)
658 nth_link = g_queue_peek_nth_link (queue, n);
659 result = nth_link->data;
661 g_queue_delete_link (queue, nth_link);
667 * g_queue_pop_tail_link:
670 * Removes and returns the last element of the queue.
672 * Returns: the #GList element at the tail of the queue, or %NULL
673 * if the queue is empty
676 g_queue_pop_tail_link (GQueue *queue)
678 g_return_val_if_fail (queue != NULL, NULL);
682 GList *node = queue->tail;
684 queue->tail = node->prev;
687 queue->tail->next = NULL;
701 * g_queue_pop_nth_link:
703 * @n: the link's position
705 * Removes and returns the link at the given position.
707 * Return value: the @n'th link, or %NULL if @n is off the end of @queue
712 g_queue_pop_nth_link (GQueue *queue,
717 g_return_val_if_fail (queue != NULL, NULL);
719 if (n >= queue->length)
722 link = g_queue_peek_nth_link (queue, n);
723 g_queue_unlink (queue, link);
729 * g_queue_peek_nth_link:
731 * @n: the position of the link
733 * Returns the link at the given position
735 * Return value: the link at the @n'th position, or %NULL
736 * if @n is off the end of the list
741 g_queue_peek_nth_link (GQueue *queue,
747 g_return_val_if_fail (queue != NULL, NULL);
749 if (n >= queue->length)
752 if (n > queue->length / 2)
754 n = queue->length - n - 1;
757 for (i = 0; i < n; ++i)
763 for (i = 0; i < n; ++i)
771 * g_queue_link_index:
773 * @link_: a #GList link
775 * Returns the position of @link_ in @queue.
777 * Return value: the position of @link_, or -1 if the link is
783 g_queue_link_index (GQueue *queue,
786 g_return_val_if_fail (queue != NULL, -1);
788 return g_list_position (queue->head, link_);
794 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
796 * Unlinks @link_ so that it will no longer be part of @queue. The link is
799 * @link_ must be part of @queue.
804 g_queue_unlink (GQueue *queue,
807 g_return_if_fail (queue != NULL);
808 g_return_if_fail (link_ != NULL);
810 if (link_ == queue->tail)
811 queue->tail = queue->tail->prev;
813 queue->head = g_list_remove_link (queue->head, link_);
818 * g_queue_delete_link:
820 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
822 * Removes @link_ from @queue and frees it.
824 * @link_ must be part of @queue.
829 g_queue_delete_link (GQueue *queue,
832 g_return_if_fail (queue != NULL);
833 g_return_if_fail (link_ != NULL);
835 g_queue_unlink (queue, link_);
843 * Returns the first element of the queue.
845 * Returns: the data of the first element in the queue, or %NULL
846 * if the queue is empty
849 g_queue_peek_head (GQueue *queue)
851 g_return_val_if_fail (queue != NULL, NULL);
853 return queue->head ? queue->head->data : NULL;
860 * Returns the last element of the queue.
862 * Returns: the data of the last element in the queue, or %NULL
863 * if the queue is empty
866 g_queue_peek_tail (GQueue *queue)
868 g_return_val_if_fail (queue != NULL, NULL);
870 return queue->tail ? queue->tail->data : NULL;
876 * @n: the position of the element
878 * Returns the @n'th element of @queue.
880 * Return value: the data for the @n'th element of @queue,
881 * or %NULL if @n is off the end of @queue
886 g_queue_peek_nth (GQueue *queue,
891 g_return_val_if_fail (queue != NULL, NULL);
893 link = g_queue_peek_nth_link (queue, n);
904 * @data: the data to find
906 * Returns the position of the first element in @queue which contains @data.
908 * Return value: the position of the first element in @queue which
909 * contains @data, or -1 if no element in @queue contains @data
914 g_queue_index (GQueue *queue,
917 g_return_val_if_fail (queue != NULL, -1);
919 return g_list_index (queue->head, data);
925 * @data: the data to remove
927 * Removes the first element in @queue that contains @data.
929 * Return value: %TRUE if @data was found and removed from @queue
934 g_queue_remove (GQueue *queue,
939 g_return_val_if_fail (queue != NULL, FALSE);
941 link = g_list_find (queue->head, data);
944 g_queue_delete_link (queue, link);
946 return (link != NULL);
950 * g_queue_remove_all:
952 * @data: the data to remove
954 * Remove all elements whose data equals @data from @queue.
956 * Return value: the number of elements removed from @queue
961 g_queue_remove_all (GQueue *queue,
967 g_return_val_if_fail (queue != NULL, 0);
969 old_length = queue->length;
974 GList *next = list->next;
976 if (list->data == data)
977 g_queue_delete_link (queue, list);
982 return (old_length - queue->length);
986 * g_queue_insert_before:
988 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
989 * @data: the data to insert
991 * Inserts @data into @queue before @sibling.
993 * @sibling must be part of @queue.
998 g_queue_insert_before (GQueue *queue,
1002 g_return_if_fail (queue != NULL);
1003 g_return_if_fail (sibling != NULL);
1005 queue->head = g_list_insert_before (queue->head, sibling, data);
1010 * g_queue_insert_after:
1012 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
1013 * @data: the data to insert
1015 * Inserts @data into @queue after @sibling
1017 * @sibling must be part of @queue
1022 g_queue_insert_after (GQueue *queue,
1026 g_return_if_fail (queue != NULL);
1027 g_return_if_fail (sibling != NULL);
1029 if (sibling == queue->tail)
1030 g_queue_push_tail (queue, data);
1032 g_queue_insert_before (queue, sibling->next, data);
1036 * g_queue_insert_sorted:
1038 * @data: the data to insert
1039 * @func: the #GCompareDataFunc used to compare elements in the queue. It is
1040 * called with two elements of the @queue and @user_data. It should
1041 * return 0 if the elements are equal, a negative value if the first
1042 * element comes before the second, and a positive value if the second
1043 * element comes before the first.
1044 * @user_data: user data passed to @func
1046 * Inserts @data into @queue using @func to determine the new position.
1051 g_queue_insert_sorted (GQueue *queue,
1053 GCompareDataFunc func,
1058 g_return_if_fail (queue != NULL);
1061 while (list && func (list->data, data, user_data) < 0)
1065 g_queue_insert_before (queue, list, data);
1067 g_queue_push_tail (queue, data);