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.
79 * If queue elements contain dynamically-allocated memory,
80 * you should either use g_queue_free_full() or free them manually
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.
104 g_queue_free_full (GQueue *queue,
105 GDestroyNotify free_func)
107 g_queue_foreach (queue, (GFunc) free_func, NULL);
108 g_queue_free (queue);
113 * @queue: an uninitialized #GQueue
115 * A statically-allocated #GQueue must be initialized with this function
116 * before it can be used. Alternatively you can initialize it with
117 * #G_QUEUE_INIT. It is not necessary to initialize queues created with
123 g_queue_init (GQueue *queue)
125 g_return_if_fail (queue != NULL);
127 queue->head = queue->tail = NULL;
135 * Removes all the elements in @queue. If queue elements contain
136 * dynamically-allocated memory, they should be freed first.
141 g_queue_clear (GQueue *queue)
143 g_return_if_fail (queue != NULL);
145 g_list_free (queue->head);
146 g_queue_init (queue);
153 * Returns %TRUE if the queue is empty.
155 * Returns: %TRUE if the queue is empty
158 g_queue_is_empty (GQueue *queue)
160 g_return_val_if_fail (queue != NULL, TRUE);
162 return queue->head == NULL;
166 * g_queue_get_length:
169 * Returns the number of items in @queue.
171 * Return value: the number of items in @queue
176 g_queue_get_length (GQueue *queue)
178 g_return_val_if_fail (queue != NULL, 0);
180 return queue->length;
187 * Reverses the order of the items in @queue.
192 g_queue_reverse (GQueue *queue)
194 g_return_if_fail (queue != NULL);
196 queue->tail = queue->head;
197 queue->head = g_list_reverse (queue->head);
204 * Copies a @queue. Note that is a shallow copy. If the elements in the
205 * queue consist of pointers to data, the pointers are copied, but the
206 * actual data is not.
208 * Return value: a copy of @queue
213 g_queue_copy (GQueue *queue)
218 g_return_val_if_fail (queue != NULL, NULL);
220 result = g_queue_new ();
222 for (list = queue->head; list != NULL; list = list->next)
223 g_queue_push_tail (result, list->data);
231 * @func: the function to call for each element's data
232 * @user_data: user data to pass to @func
234 * Calls @func for each element in the queue passing @user_data to the
240 g_queue_foreach (GQueue *queue,
246 g_return_if_fail (queue != NULL);
247 g_return_if_fail (func != NULL);
252 GList *next = list->next;
253 func (list->data, user_data);
261 * @data: data to find
263 * Finds the first link in @queue which contains @data.
265 * Return value: the first link in @queue which contains @data
270 g_queue_find (GQueue *queue,
273 g_return_val_if_fail (queue != NULL, NULL);
275 return g_list_find (queue->head, data);
279 * g_queue_find_custom:
281 * @data: user data passed to @func
282 * @func: a #GCompareFunc to call for each element. It should return 0
283 * when the desired element is found
285 * Finds an element in a #GQueue, using a supplied function to find the
286 * desired element. It iterates over the queue, calling the given function
287 * which should return 0 when the desired element is found. The function
288 * takes two gconstpointer arguments, the #GQueue element's data as the
289 * first argument and the given user data as the second argument.
291 * Return value: the found link, or %NULL if it wasn't found
296 g_queue_find_custom (GQueue *queue,
300 g_return_val_if_fail (queue != NULL, NULL);
301 g_return_val_if_fail (func != NULL, NULL);
303 return g_list_find_custom (queue->head, data, func);
309 * @compare_func: the #GCompareDataFunc used to sort @queue. This function
310 * is passed two elements of the queue and should return 0 if they are
311 * equal, a negative value if the first comes before the second, and
312 * a positive value if the second comes before the first.
313 * @user_data: user data passed to @compare_func
315 * Sorts @queue using @compare_func.
320 g_queue_sort (GQueue *queue,
321 GCompareDataFunc compare_func,
324 g_return_if_fail (queue != NULL);
325 g_return_if_fail (compare_func != NULL);
327 queue->head = g_list_sort_with_data (queue->head, compare_func, user_data);
328 queue->tail = g_list_last (queue->head);
334 * @data: the data for the new element.
336 * Adds a new element at the head of the queue.
339 g_queue_push_head (GQueue *queue,
342 g_return_if_fail (queue != NULL);
344 queue->head = g_list_prepend (queue->head, data);
346 queue->tail = queue->head;
353 * @data: the data for the new element
354 * @n: the position to insert the new element. If @n is negative or
355 * larger than the number of elements in the @queue, the element is
356 * added to the end of the queue.
358 * Inserts a new element into @queue at the given position.
363 g_queue_push_nth (GQueue *queue,
367 g_return_if_fail (queue != NULL);
369 if (n < 0 || n >= queue->length)
371 g_queue_push_tail (queue, data);
375 g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
379 * g_queue_push_head_link:
381 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
382 * more than one element
384 * Adds a new element at the head of the queue.
387 g_queue_push_head_link (GQueue *queue,
390 g_return_if_fail (queue != NULL);
391 g_return_if_fail (link != NULL);
392 g_return_if_fail (link->prev == NULL);
393 g_return_if_fail (link->next == NULL);
395 link->next = queue->head;
397 queue->head->prev = link;
407 * @data: the data for the new element
409 * Adds a new element at the tail of the queue.
412 g_queue_push_tail (GQueue *queue,
415 g_return_if_fail (queue != NULL);
417 queue->tail = g_list_append (queue->tail, data);
418 if (queue->tail->next)
419 queue->tail = queue->tail->next;
421 queue->head = queue->tail;
426 * g_queue_push_tail_link:
428 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
429 * more than one element
431 * Adds a new element at the tail of the queue.
434 g_queue_push_tail_link (GQueue *queue,
437 g_return_if_fail (queue != NULL);
438 g_return_if_fail (link != NULL);
439 g_return_if_fail (link->prev == NULL);
440 g_return_if_fail (link->next == NULL);
442 link->prev = queue->tail;
444 queue->tail->next = link;
452 * g_queue_push_nth_link:
454 * @n: the position to insert the link. If this is negative or larger than
455 * the number of elements in @queue, the link is added to the end of
457 * @link_: the link to add to @queue
459 * Inserts @link into @queue at the given position.
464 g_queue_push_nth_link (GQueue *queue,
471 g_return_if_fail (queue != NULL);
472 g_return_if_fail (link_ != NULL);
474 if (n < 0 || n >= queue->length)
476 g_queue_push_tail_link (queue, link_);
480 g_assert (queue->head);
481 g_assert (queue->tail);
483 next = g_queue_peek_nth_link (queue, n);
493 if (queue->head->prev)
494 queue->head = queue->head->prev;
496 if (queue->tail->next)
497 queue->tail = queue->tail->next;
506 * Removes the first element of the queue and returns its data.
508 * Returns: the data of the first element in the queue, or %NULL
509 * if the queue is empty
512 g_queue_pop_head (GQueue *queue)
514 g_return_val_if_fail (queue != NULL, NULL);
518 GList *node = queue->head;
519 gpointer data = node->data;
521 queue->head = node->next;
523 queue->head->prev = NULL;
526 g_list_free_1 (node);
536 * g_queue_pop_head_link:
539 * Removes and returns the first element of the queue.
541 * Returns: the #GList element at the head of the queue, or %NULL
542 * if the queue is empty
545 g_queue_pop_head_link (GQueue *queue)
547 g_return_val_if_fail (queue != NULL, NULL);
551 GList *node = queue->head;
553 queue->head = node->next;
556 queue->head->prev = NULL;
570 * g_queue_peek_head_link:
573 * Returns the first link in @queue.
575 * Return value: the first link in @queue, or %NULL if @queue is empty
580 g_queue_peek_head_link (GQueue *queue)
582 g_return_val_if_fail (queue != NULL, NULL);
588 * g_queue_peek_tail_link:
591 * Returns the last link in @queue.
593 * Return value: the last link in @queue, or %NULL if @queue is empty
598 g_queue_peek_tail_link (GQueue *queue)
600 g_return_val_if_fail (queue != NULL, NULL);
609 * Removes the last element of the queue and returns its data.
611 * Returns: the data of the last element in the queue, or %NULL
612 * if the queue is empty
615 g_queue_pop_tail (GQueue *queue)
617 g_return_val_if_fail (queue != NULL, NULL);
621 GList *node = queue->tail;
622 gpointer data = node->data;
624 queue->tail = node->prev;
626 queue->tail->next = NULL;
630 g_list_free_1 (node);
641 * @n: the position of the element
643 * Removes the @n'th element of @queue and returns its data.
645 * Return value: the element's data, or %NULL if @n is off the end of @queue
650 g_queue_pop_nth (GQueue *queue,
656 g_return_val_if_fail (queue != NULL, NULL);
658 if (n >= queue->length)
661 nth_link = g_queue_peek_nth_link (queue, n);
662 result = nth_link->data;
664 g_queue_delete_link (queue, nth_link);
670 * g_queue_pop_tail_link:
673 * Removes and returns the last element of the queue.
675 * Returns: the #GList element at the tail of the queue, or %NULL
676 * if the queue is empty
679 g_queue_pop_tail_link (GQueue *queue)
681 g_return_val_if_fail (queue != NULL, NULL);
685 GList *node = queue->tail;
687 queue->tail = node->prev;
690 queue->tail->next = NULL;
704 * g_queue_pop_nth_link:
706 * @n: the link's position
708 * Removes and returns the link at the given position.
710 * Return value: the @n'th link, or %NULL if @n is off the end of @queue
715 g_queue_pop_nth_link (GQueue *queue,
720 g_return_val_if_fail (queue != NULL, NULL);
722 if (n >= queue->length)
725 link = g_queue_peek_nth_link (queue, n);
726 g_queue_unlink (queue, link);
732 * g_queue_peek_nth_link:
734 * @n: the position of the link
736 * Returns the link at the given position
738 * Return value: the link at the @n'th position, or %NULL
739 * if @n is off the end of the list
744 g_queue_peek_nth_link (GQueue *queue,
750 g_return_val_if_fail (queue != NULL, NULL);
752 if (n >= queue->length)
755 if (n > queue->length / 2)
757 n = queue->length - n - 1;
760 for (i = 0; i < n; ++i)
766 for (i = 0; i < n; ++i)
774 * g_queue_link_index:
776 * @link_: a #GList link
778 * Returns the position of @link_ in @queue.
780 * Return value: the position of @link_, or -1 if the link is
786 g_queue_link_index (GQueue *queue,
789 g_return_val_if_fail (queue != NULL, -1);
791 return g_list_position (queue->head, link_);
797 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
799 * Unlinks @link_ so that it will no longer be part of @queue. The link is
802 * @link_ must be part of @queue.
807 g_queue_unlink (GQueue *queue,
810 g_return_if_fail (queue != NULL);
811 g_return_if_fail (link_ != NULL);
813 if (link_ == queue->tail)
814 queue->tail = queue->tail->prev;
816 queue->head = g_list_remove_link (queue->head, link_);
821 * g_queue_delete_link:
823 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
825 * Removes @link_ from @queue and frees it.
827 * @link_ must be part of @queue.
832 g_queue_delete_link (GQueue *queue,
835 g_return_if_fail (queue != NULL);
836 g_return_if_fail (link_ != NULL);
838 g_queue_unlink (queue, link_);
846 * Returns the first element of the queue.
848 * Returns: the data of the first element in the queue, or %NULL
849 * if the queue is empty
852 g_queue_peek_head (GQueue *queue)
854 g_return_val_if_fail (queue != NULL, NULL);
856 return queue->head ? queue->head->data : NULL;
863 * Returns the last element of the queue.
865 * Returns: the data of the last element in the queue, or %NULL
866 * if the queue is empty
869 g_queue_peek_tail (GQueue *queue)
871 g_return_val_if_fail (queue != NULL, NULL);
873 return queue->tail ? queue->tail->data : NULL;
879 * @n: the position of the element
881 * Returns the @n'th element of @queue.
883 * Return value: the data for the @n'th element of @queue,
884 * or %NULL if @n is off the end of @queue
889 g_queue_peek_nth (GQueue *queue,
894 g_return_val_if_fail (queue != NULL, NULL);
896 link = g_queue_peek_nth_link (queue, n);
907 * @data: the data to find
909 * Returns the position of the first element in @queue which contains @data.
911 * Return value: the position of the first element in @queue which
912 * contains @data, or -1 if no element in @queue contains @data
917 g_queue_index (GQueue *queue,
920 g_return_val_if_fail (queue != NULL, -1);
922 return g_list_index (queue->head, data);
928 * @data: the data to remove
930 * Removes the first element in @queue that contains @data.
932 * Return value: %TRUE if @data was found and removed from @queue
937 g_queue_remove (GQueue *queue,
942 g_return_val_if_fail (queue != NULL, FALSE);
944 link = g_list_find (queue->head, data);
947 g_queue_delete_link (queue, link);
949 return (link != NULL);
953 * g_queue_remove_all:
955 * @data: the data to remove
957 * Remove all elements whose data equals @data from @queue.
959 * Return value: the number of elements removed from @queue
964 g_queue_remove_all (GQueue *queue,
970 g_return_val_if_fail (queue != NULL, 0);
972 old_length = queue->length;
977 GList *next = list->next;
979 if (list->data == data)
980 g_queue_delete_link (queue, list);
985 return (old_length - queue->length);
989 * g_queue_insert_before:
991 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
992 * @data: the data to insert
994 * Inserts @data into @queue before @sibling.
996 * @sibling must be part of @queue.
1001 g_queue_insert_before (GQueue *queue,
1005 g_return_if_fail (queue != NULL);
1006 g_return_if_fail (sibling != NULL);
1008 queue->head = g_list_insert_before (queue->head, sibling, data);
1013 * g_queue_insert_after:
1015 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
1016 * @data: the data to insert
1018 * Inserts @data into @queue after @sibling
1020 * @sibling must be part of @queue
1025 g_queue_insert_after (GQueue *queue,
1029 g_return_if_fail (queue != NULL);
1030 g_return_if_fail (sibling != NULL);
1032 if (sibling == queue->tail)
1033 g_queue_push_tail (queue, data);
1035 g_queue_insert_before (queue, sibling->next, data);
1039 * g_queue_insert_sorted:
1041 * @data: the data to insert
1042 * @func: the #GCompareDataFunc used to compare elements in the queue. It is
1043 * called with two elements of the @queue and @user_data. It should
1044 * return 0 if the elements are equal, a negative value if the first
1045 * element comes before the second, and a positive value if the second
1046 * element comes before the first.
1047 * @user_data: user data passed to @func
1049 * Inserts @data into @queue using @func to determine the new position.
1054 g_queue_insert_sorted (GQueue *queue,
1056 GCompareDataFunc func,
1061 g_return_if_fail (queue != NULL);
1064 while (list && func (list->data, data, user_data) < 0)
1068 g_queue_insert_before (queue, list, data);
1070 g_queue_push_tail (queue, data);