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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
29 * @Title: Double-ended Queues
30 * @Short_description: double-ended queue data structure
32 * The #GQueue structure and its associated functions provide a standard
33 * queue data structure. Internally, GQueue uses the same data structure
34 * as #GList to store elements.
36 * The data contained in each element can be either integer values, by
37 * using one of the <link linkend="glib-Type-Conversion-Macros">Type
38 * Conversion Macros</link>, or simply pointers to any type of data.
40 * To create a new GQueue, use g_queue_new().
42 * To initialize a statically-allocated GQueue, use #G_QUEUE_INIT or
45 * To add elements, use g_queue_push_head(), g_queue_push_head_link(),
46 * g_queue_push_tail() and g_queue_push_tail_link().
48 * To remove elements, use g_queue_pop_head() and g_queue_pop_tail().
50 * To free the entire queue, use g_queue_free().
56 #include "gtestutils.h"
62 * Creates a new #GQueue.
64 * Returns: a new #GQueue.
69 return g_slice_new0 (GQueue);
76 * Frees the memory allocated for the #GQueue. Only call this function if
77 * @queue was created with g_queue_new(). If queue elements contain
78 * dynamically-allocated memory, they should be freed first.
81 g_queue_free (GQueue *queue)
83 g_return_if_fail (queue != NULL);
85 g_list_free (queue->head);
86 g_slice_free (GQueue, queue);
91 * @queue: an uninitialized #GQueue
93 * A statically-allocated #GQueue must be initialized with this function
94 * before it can be used. Alternatively you can initialize it with
95 * #G_QUEUE_INIT. It is not necessary to initialize queues created with
101 g_queue_init (GQueue *queue)
103 g_return_if_fail (queue != NULL);
105 queue->head = queue->tail = NULL;
113 * Removes all the elements in @queue. If queue elements contain
114 * dynamically-allocated memory, they should be freed first.
119 g_queue_clear (GQueue *queue)
121 g_return_if_fail (queue != NULL);
123 g_list_free (queue->head);
124 g_queue_init (queue);
131 * Returns %TRUE if the queue is empty.
133 * Returns: %TRUE if the queue is empty.
136 g_queue_is_empty (GQueue *queue)
138 g_return_val_if_fail (queue != NULL, TRUE);
140 return queue->head == NULL;
144 * g_queue_get_length:
147 * Returns the number of items in @queue.
149 * Return value: The number of items in @queue.
154 g_queue_get_length (GQueue *queue)
156 g_return_val_if_fail (queue != NULL, 0);
158 return queue->length;
165 * Reverses the order of the items in @queue.
170 g_queue_reverse (GQueue *queue)
172 g_return_if_fail (queue != NULL);
174 queue->tail = queue->head;
175 queue->head = g_list_reverse (queue->head);
182 * Copies a @queue. Note that is a shallow copy. If the elements in the
183 * queue consist of pointers to data, the pointers are copied, but the
184 * actual data is not.
186 * Return value: A copy of @queue
191 g_queue_copy (GQueue *queue)
196 g_return_val_if_fail (queue != NULL, NULL);
198 result = g_queue_new ();
200 for (list = queue->head; list != NULL; list = list->next)
201 g_queue_push_tail (result, list->data);
209 * @func: the function to call for each element's data
210 * @user_data: user data to pass to @func
212 * Calls @func for each element in the queue passing @user_data to the
218 g_queue_foreach (GQueue *queue,
224 g_return_if_fail (queue != NULL);
225 g_return_if_fail (func != NULL);
230 GList *next = list->next;
231 func (list->data, user_data);
239 * @data: data to find
241 * Finds the first link in @queue which contains @data.
243 * Return value: The first link in @queue which contains @data.
248 g_queue_find (GQueue *queue,
251 g_return_val_if_fail (queue != NULL, NULL);
253 return g_list_find (queue->head, data);
257 * g_queue_find_custom:
259 * @data: user data passed to @func
260 * @func: a #GCompareFunc to call for each element. It should return 0
261 * when the desired element is found
263 * Finds an element in a #GQueue, using a supplied function to find the
264 * desired element. It iterates over the queue, calling the given function
265 * which should return 0 when the desired element is found. The function
266 * takes two gconstpointer arguments, the #GQueue element's data as the
267 * first argument and the given user data as the second argument.
269 * Return value: The found link, or %NULL if it wasn't found
274 g_queue_find_custom (GQueue *queue,
278 g_return_val_if_fail (queue != NULL, NULL);
279 g_return_val_if_fail (func != NULL, NULL);
281 return g_list_find_custom (queue->head, data, func);
287 * @compare_func: the #GCompareDataFunc used to sort @queue. This function
288 * is passed two elements of the queue and should return 0 if they are
289 * equal, a negative value if the first comes before the second, and
290 * a positive value if the second comes before the first.
291 * @user_data: user data passed to @compare_func
293 * Sorts @queue using @compare_func.
298 g_queue_sort (GQueue *queue,
299 GCompareDataFunc compare_func,
302 g_return_if_fail (queue != NULL);
303 g_return_if_fail (compare_func != NULL);
305 queue->head = g_list_sort_with_data (queue->head, compare_func, user_data);
306 queue->tail = g_list_last (queue->head);
312 * @data: the data for the new element.
314 * Adds a new element at the head of the queue.
317 g_queue_push_head (GQueue *queue,
320 g_return_if_fail (queue != NULL);
322 queue->head = g_list_prepend (queue->head, data);
324 queue->tail = queue->head;
331 * @data: the data for the new element
332 * @n: the position to insert the new element. If @n is negative or
333 * larger than the number of elements in the @queue, the element is
334 * added to the end of the queue.
336 * Inserts a new element into @queue at the given position
341 g_queue_push_nth (GQueue *queue,
345 g_return_if_fail (queue != NULL);
347 if (n < 0 || n >= queue->length)
349 g_queue_push_tail (queue, data);
353 g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
357 * g_queue_push_head_link:
359 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
360 * more than one element.
362 * Adds a new element at the head of the queue.
365 g_queue_push_head_link (GQueue *queue,
368 g_return_if_fail (queue != NULL);
369 g_return_if_fail (link != NULL);
370 g_return_if_fail (link->prev == NULL);
371 g_return_if_fail (link->next == NULL);
373 link->next = queue->head;
375 queue->head->prev = link;
385 * @data: the data for the new element.
387 * Adds a new element at the tail of the queue.
390 g_queue_push_tail (GQueue *queue,
393 g_return_if_fail (queue != NULL);
395 queue->tail = g_list_append (queue->tail, data);
396 if (queue->tail->next)
397 queue->tail = queue->tail->next;
399 queue->head = queue->tail;
404 * g_queue_push_tail_link:
406 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
407 * more than one element.
409 * Adds a new element at the tail of the queue.
412 g_queue_push_tail_link (GQueue *queue,
415 g_return_if_fail (queue != NULL);
416 g_return_if_fail (link != NULL);
417 g_return_if_fail (link->prev == NULL);
418 g_return_if_fail (link->next == NULL);
420 link->prev = queue->tail;
422 queue->tail->next = link;
430 * g_queue_push_nth_link:
432 * @n: the position to insert the link. If this is negative or larger than
433 * the number of elements in @queue, the link is added to the end of
435 * @link_: the link to add to @queue
437 * Inserts @link into @queue at the given position.
442 g_queue_push_nth_link (GQueue *queue,
449 g_return_if_fail (queue != NULL);
450 g_return_if_fail (link_ != NULL);
452 if (n < 0 || n >= queue->length)
454 g_queue_push_tail_link (queue, link_);
458 g_assert (queue->head);
459 g_assert (queue->tail);
461 next = g_queue_peek_nth_link (queue, n);
471 if (queue->head->prev)
472 queue->head = queue->head->prev;
474 if (queue->tail->next)
475 queue->tail = queue->tail->next;
484 * Removes the first element of the queue.
486 * Returns: the data of the first element in the queue, or %NULL if the queue
490 g_queue_pop_head (GQueue *queue)
492 g_return_val_if_fail (queue != NULL, NULL);
496 GList *node = queue->head;
497 gpointer data = node->data;
499 queue->head = node->next;
501 queue->head->prev = NULL;
504 g_list_free_1 (node);
514 * g_queue_pop_head_link:
517 * Removes the first element of the queue.
519 * Returns: the #GList element at the head of the queue, or %NULL if the queue
523 g_queue_pop_head_link (GQueue *queue)
525 g_return_val_if_fail (queue != NULL, NULL);
529 GList *node = queue->head;
531 queue->head = node->next;
534 queue->head->prev = NULL;
548 * g_queue_peek_head_link:
551 * Returns the first link in @queue
553 * Return value: the first link in @queue, or %NULL if @queue is empty
558 g_queue_peek_head_link (GQueue *queue)
560 g_return_val_if_fail (queue != NULL, NULL);
566 * g_queue_peek_tail_link:
569 * Returns the last link @queue.
571 * Return value: the last link in @queue, or %NULL if @queue is empty
576 g_queue_peek_tail_link (GQueue *queue)
578 g_return_val_if_fail (queue != NULL, NULL);
587 * Removes the last element of the queue.
589 * Returns: the data of the last element in the queue, or %NULL if the queue
593 g_queue_pop_tail (GQueue *queue)
595 g_return_val_if_fail (queue != NULL, NULL);
599 GList *node = queue->tail;
600 gpointer data = node->data;
602 queue->tail = node->prev;
604 queue->tail->next = NULL;
608 g_list_free_1 (node);
619 * @n: the position of the element.
621 * Removes the @n'th element of @queue.
623 * Return value: the element's data, or %NULL if @n is off the end of @queue.
628 g_queue_pop_nth (GQueue *queue,
634 g_return_val_if_fail (queue != NULL, NULL);
636 if (n >= queue->length)
639 nth_link = g_queue_peek_nth_link (queue, n);
640 result = nth_link->data;
642 g_queue_delete_link (queue, nth_link);
648 * g_queue_pop_tail_link:
651 * Removes the last element of the queue.
653 * Returns: the #GList element at the tail of the queue, or %NULL if the queue
657 g_queue_pop_tail_link (GQueue *queue)
659 g_return_val_if_fail (queue != NULL, NULL);
663 GList *node = queue->tail;
665 queue->tail = node->prev;
668 queue->tail->next = NULL;
682 * g_queue_pop_nth_link:
684 * @n: the link's position
686 * Removes and returns the link at the given position.
688 * Return value: The @n'th link, or %NULL if @n is off the end of @queue.
693 g_queue_pop_nth_link (GQueue *queue,
698 g_return_val_if_fail (queue != NULL, NULL);
700 if (n >= queue->length)
703 link = g_queue_peek_nth_link (queue, n);
704 g_queue_unlink (queue, link);
710 * g_queue_peek_nth_link:
712 * @n: the position of the link
714 * Returns the link at the given position
716 * Return value: The link at the @n'th position, or %NULL if @n is off the
722 g_queue_peek_nth_link (GQueue *queue,
728 g_return_val_if_fail (queue != NULL, NULL);
730 if (n >= queue->length)
733 if (n > queue->length / 2)
735 n = queue->length - n - 1;
738 for (i = 0; i < n; ++i)
744 for (i = 0; i < n; ++i)
752 * g_queue_link_index:
754 * @link_: A #GList link
756 * Returns the position of @link_ in @queue.
758 * Return value: The position of @link_, or -1 if the link is
764 g_queue_link_index (GQueue *queue,
767 g_return_val_if_fail (queue != NULL, -1);
769 return g_list_position (queue->head, link_);
775 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
777 * Unlinks @link_ so that it will no longer be part of @queue. The link is
780 * @link_ must be part of @queue,
785 g_queue_unlink (GQueue *queue,
788 g_return_if_fail (queue != NULL);
789 g_return_if_fail (link_ != NULL);
791 if (link_ == queue->tail)
792 queue->tail = queue->tail->prev;
794 queue->head = g_list_remove_link (queue->head, link_);
799 * g_queue_delete_link:
801 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
803 * Removes @link_ from @queue and frees it.
805 * @link_ must be part of @queue.
810 g_queue_delete_link (GQueue *queue,
813 g_return_if_fail (queue != NULL);
814 g_return_if_fail (link_ != NULL);
816 g_queue_unlink (queue, link_);
824 * Returns the first element of the queue.
826 * Returns: the data of the first element in the queue, or %NULL if the queue
830 g_queue_peek_head (GQueue *queue)
832 g_return_val_if_fail (queue != NULL, NULL);
834 return queue->head ? queue->head->data : NULL;
841 * Returns the last element of the queue.
843 * Returns: the data of the last element in the queue, or %NULL if the queue
847 g_queue_peek_tail (GQueue *queue)
849 g_return_val_if_fail (queue != NULL, NULL);
851 return queue->tail ? queue->tail->data : NULL;
857 * @n: the position of the element.
859 * Returns the @n'th element of @queue.
861 * Return value: The data for the @n'th element of @queue, or %NULL if @n is
862 * off the end of @queue.
867 g_queue_peek_nth (GQueue *queue,
872 g_return_val_if_fail (queue != NULL, NULL);
874 link = g_queue_peek_nth_link (queue, n);
885 * @data: the data to find.
887 * Returns the position of the first element in @queue which contains @data.
889 * Return value: The position of the first element in @queue which contains @data, or -1 if no element in @queue contains @data.
894 g_queue_index (GQueue *queue,
897 g_return_val_if_fail (queue != NULL, -1);
899 return g_list_index (queue->head, data);
905 * @data: data to remove.
907 * Removes the first element in @queue that contains @data.
909 * Return value: %TRUE if @data was found and removed from @queue
914 g_queue_remove (GQueue *queue,
919 g_return_val_if_fail (queue != NULL, FALSE);
921 link = g_list_find (queue->head, data);
924 g_queue_delete_link (queue, link);
926 return (link != NULL);
930 * g_queue_remove_all:
932 * @data: data to remove
934 * Remove all elements whose data equals @data from @queue.
936 * Return value: the number of elements removed from @queue
941 g_queue_remove_all (GQueue *queue,
947 g_return_val_if_fail (queue != NULL, 0);
949 old_length = queue->length;
954 GList *next = list->next;
956 if (list->data == data)
957 g_queue_delete_link (queue, list);
962 return (old_length - queue->length);
966 * g_queue_insert_before:
968 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
969 * @data: the data to insert
971 * Inserts @data into @queue before @sibling.
973 * @sibling must be part of @queue.
978 g_queue_insert_before (GQueue *queue,
982 g_return_if_fail (queue != NULL);
983 g_return_if_fail (sibling != NULL);
985 queue->head = g_list_insert_before (queue->head, sibling, data);
990 * g_queue_insert_after:
992 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
993 * @data: the data to insert
995 * Inserts @data into @queue after @sibling
997 * @sibling must be part of @queue
1002 g_queue_insert_after (GQueue *queue,
1006 g_return_if_fail (queue != NULL);
1007 g_return_if_fail (sibling != NULL);
1009 if (sibling == queue->tail)
1010 g_queue_push_tail (queue, data);
1012 g_queue_insert_before (queue, sibling->next, data);
1016 * g_queue_insert_sorted:
1018 * @data: the data to insert
1019 * @func: the #GCompareDataFunc used to compare elements in the queue. It is
1020 * called with two elements of the @queue and @user_data. It should
1021 * return 0 if the elements are equal, a negative value if the first
1022 * element comes before the second, and a positive value if the second
1023 * element comes before the first.
1024 * @user_data: user data passed to @func.
1026 * Inserts @data into @queue using @func to determine the new position.
1031 g_queue_insert_sorted (GQueue *queue,
1033 GCompareDataFunc func,
1038 g_return_if_fail (queue != NULL);
1041 while (list && func (list->data, data, user_data) < 0)
1045 g_queue_insert_before (queue, list, data);
1047 g_queue_push_tail (queue, data);