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.
31 #include "gtestutils.h"
36 * Creates a new #GQueue.
38 * Returns: a new #GQueue.
43 return g_slice_new0 (GQueue);
50 * Frees the memory allocated for the #GQueue. Only call this function if
51 * @queue was created with g_queue_new(). If queue elements contain
52 * dynamically-allocated memory, they should be freed first.
55 g_queue_free (GQueue *queue)
57 g_return_if_fail (queue != NULL);
59 g_list_free (queue->head);
60 g_slice_free (GQueue, queue);
65 * @queue: an uninitialized #GQueue
67 * A statically-allocated #GQueue must be initialized with this function
68 * before it can be used. Alternatively you can initialize it with
69 * #G_QUEUE_INIT. It is not necessary to initialize queues created with
75 g_queue_init (GQueue *queue)
77 g_return_if_fail (queue != NULL);
79 queue->head = queue->tail = NULL;
87 * Removes all the elements in @queue. If queue elements contain
88 * dynamically-allocated memory, they should be freed first.
93 g_queue_clear (GQueue *queue)
95 g_return_if_fail (queue != NULL);
97 g_list_free (queue->head);
105 * Returns %TRUE if the queue is empty.
107 * Returns: %TRUE if the queue is empty.
110 g_queue_is_empty (GQueue *queue)
112 g_return_val_if_fail (queue != NULL, TRUE);
114 return queue->head == NULL;
118 * g_queue_get_length:
121 * Returns the number of items in @queue.
123 * Return value: The number of items in @queue.
128 g_queue_get_length (GQueue *queue)
130 g_return_val_if_fail (queue != NULL, 0);
132 return queue->length;
139 * Reverses the order of the items in @queue.
144 g_queue_reverse (GQueue *queue)
146 g_return_if_fail (queue != NULL);
148 queue->tail = queue->head;
149 queue->head = g_list_reverse (queue->head);
156 * Copies a @queue. Note that is a shallow copy. If the elements in the
157 * queue consist of pointers to data, the pointers are copied, but the
158 * actual data is not.
160 * Return value: A copy of @queue
165 g_queue_copy (GQueue *queue)
170 g_return_val_if_fail (queue != NULL, NULL);
172 result = g_queue_new ();
174 for (list = queue->head; list != NULL; list = list->next)
175 g_queue_push_tail (result, list->data);
183 * @func: the function to call for each element's data
184 * @user_data: user data to pass to @func
186 * Calls @func for each element in the queue passing @user_data to the
192 g_queue_foreach (GQueue *queue,
198 g_return_if_fail (queue != NULL);
199 g_return_if_fail (func != NULL);
204 GList *next = list->next;
205 func (list->data, user_data);
213 * @data: data to find
215 * Finds the first link in @queue which contains @data.
217 * Return value: The first link in @queue which contains @data.
222 g_queue_find (GQueue *queue,
225 g_return_val_if_fail (queue != NULL, NULL);
227 return g_list_find (queue->head, data);
231 * g_queue_find_custom:
233 * @data: user data passed to @func
234 * @func: a #GCompareFunc to call for each element. It should return 0
235 * when the desired element is found
237 * Finds an element in a #GQueue, using a supplied function to find the
238 * desired element. It iterates over the queue, calling the given function
239 * which should return 0 when the desired element is found. The function
240 * takes two gconstpointer arguments, the #GQueue element's data as the
241 * first argument and the given user data as the second argument.
243 * Return value: The found link, or %NULL if it wasn't found
248 g_queue_find_custom (GQueue *queue,
252 g_return_val_if_fail (queue != NULL, NULL);
253 g_return_val_if_fail (func != NULL, NULL);
255 return g_list_find_custom (queue->head, data, func);
261 * @compare_func: the #GCompareDataFunc used to sort @queue. This function
262 * is passed two elements of the queue and should return 0 if they are
263 * equal, a negative value if the first comes before the second, and
264 * a positive value if the second comes before the first.
265 * @user_data: user data passed to @compare_func
267 * Sorts @queue using @compare_func.
272 g_queue_sort (GQueue *queue,
273 GCompareDataFunc compare_func,
276 g_return_if_fail (queue != NULL);
277 g_return_if_fail (compare_func != NULL);
279 queue->head = g_list_sort_with_data (queue->head, compare_func, user_data);
280 queue->tail = g_list_last (queue->head);
286 * @data: the data for the new element.
288 * Adds a new element at the head of the queue.
291 g_queue_push_head (GQueue *queue,
294 g_return_if_fail (queue != NULL);
296 queue->head = g_list_prepend (queue->head, data);
298 queue->tail = queue->head;
305 * @data: the data for the new element
306 * @n: the position to insert the new element. If @n is negative or
307 * larger than the number of elements in the @queue, the element is
308 * added to the end of the queue.
310 * Inserts a new element into @queue at the given position
315 g_queue_push_nth (GQueue *queue,
319 g_return_if_fail (queue != NULL);
321 if (n < 0 || n >= queue->length)
323 g_queue_push_tail (queue, data);
327 g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
331 * g_queue_push_head_link:
333 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
334 * more than one element.
336 * Adds a new element at the head of the queue.
339 g_queue_push_head_link (GQueue *queue,
342 g_return_if_fail (queue != NULL);
343 g_return_if_fail (link != NULL);
344 g_return_if_fail (link->prev == NULL);
345 g_return_if_fail (link->next == NULL);
347 link->next = queue->head;
349 queue->head->prev = link;
359 * @data: the data for the new element.
361 * Adds a new element at the tail of the queue.
364 g_queue_push_tail (GQueue *queue,
367 g_return_if_fail (queue != NULL);
369 queue->tail = g_list_append (queue->tail, data);
370 if (queue->tail->next)
371 queue->tail = queue->tail->next;
373 queue->head = queue->tail;
378 * g_queue_push_tail_link:
380 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
381 * more than one element.
383 * Adds a new element at the tail of the queue.
386 g_queue_push_tail_link (GQueue *queue,
389 g_return_if_fail (queue != NULL);
390 g_return_if_fail (link != NULL);
391 g_return_if_fail (link->prev == NULL);
392 g_return_if_fail (link->next == NULL);
394 link->prev = queue->tail;
396 queue->tail->next = link;
404 * g_queue_push_nth_link:
406 * @n: the position to insert the link. If this is negative or larger than
407 * the number of elements in @queue, the link is added to the end of
409 * @link_: the link to add to @queue
411 * Inserts @link into @queue at the given position.
416 g_queue_push_nth_link (GQueue *queue,
423 g_return_if_fail (queue != NULL);
424 g_return_if_fail (link_ != NULL);
426 if (n < 0 || n >= queue->length)
428 g_queue_push_tail_link (queue, link_);
432 g_assert (queue->head);
433 g_assert (queue->tail);
435 next = g_queue_peek_nth_link (queue, n);
445 if (queue->head->prev)
446 queue->head = queue->head->prev;
448 if (queue->tail->next)
449 queue->tail = queue->tail->next;
458 * Removes the first element of the queue.
460 * Returns: the data of the first element in the queue, or %NULL if the queue
464 g_queue_pop_head (GQueue *queue)
466 g_return_val_if_fail (queue != NULL, NULL);
470 GList *node = queue->head;
471 gpointer data = node->data;
473 queue->head = node->next;
475 queue->head->prev = NULL;
478 g_list_free_1 (node);
488 * g_queue_pop_head_link:
491 * Removes the first element of the queue.
493 * Returns: the #GList element at the head of the queue, or %NULL if the queue
497 g_queue_pop_head_link (GQueue *queue)
499 g_return_val_if_fail (queue != NULL, NULL);
503 GList *node = queue->head;
505 queue->head = node->next;
508 queue->head->prev = NULL;
522 * g_queue_peek_head_link:
525 * Returns the first link in @queue
527 * Return value: the first link in @queue, or %NULL if @queue is empty
532 g_queue_peek_head_link (GQueue *queue)
534 g_return_val_if_fail (queue != NULL, NULL);
540 * g_queue_peek_tail_link:
543 * Returns the last link @queue.
545 * Return value: the last link in @queue, or %NULL if @queue is empty
550 g_queue_peek_tail_link (GQueue *queue)
552 g_return_val_if_fail (queue != NULL, NULL);
561 * Removes the last element of the queue.
563 * Returns: the data of the last element in the queue, or %NULL if the queue
567 g_queue_pop_tail (GQueue *queue)
569 g_return_val_if_fail (queue != NULL, NULL);
573 GList *node = queue->tail;
574 gpointer data = node->data;
576 queue->tail = node->prev;
578 queue->tail->next = NULL;
582 g_list_free_1 (node);
593 * @n: the position of the element.
595 * Removes the @n'th element of @queue.
597 * Return value: the element's data, or %NULL if @n is off the end of @queue.
602 g_queue_pop_nth (GQueue *queue,
608 g_return_val_if_fail (queue != NULL, NULL);
610 if (n >= queue->length)
613 nth_link = g_queue_peek_nth_link (queue, n);
614 result = nth_link->data;
616 g_queue_delete_link (queue, nth_link);
622 * g_queue_pop_tail_link:
625 * Removes the last element of the queue.
627 * Returns: the #GList element at the tail of the queue, or %NULL if the queue
631 g_queue_pop_tail_link (GQueue *queue)
633 g_return_val_if_fail (queue != NULL, NULL);
637 GList *node = queue->tail;
639 queue->tail = node->prev;
642 queue->tail->next = NULL;
656 * g_queue_pop_nth_link:
658 * @n: the link's position
660 * Removes and returns the link at the given position.
662 * Return value: The @n'th link, or %NULL if @n is off the end of @queue.
667 g_queue_pop_nth_link (GQueue *queue,
672 g_return_val_if_fail (queue != NULL, NULL);
674 if (n >= queue->length)
677 link = g_queue_peek_nth_link (queue, n);
678 g_queue_unlink (queue, link);
684 * g_queue_peek_nth_link:
686 * @n: the position of the link
688 * Returns the link at the given position
690 * Return value: The link at the @n'th position, or %NULL if @n is off the
696 g_queue_peek_nth_link (GQueue *queue,
702 g_return_val_if_fail (queue != NULL, NULL);
704 if (n >= queue->length)
707 if (n > queue->length / 2)
709 n = queue->length - n - 1;
712 for (i = 0; i < n; ++i)
718 for (i = 0; i < n; ++i)
726 * g_queue_link_index:
728 * @link_: A #GList link
730 * Returns the position of @link_ in @queue.
732 * Return value: The position of @link_, or -1 if the link is
738 g_queue_link_index (GQueue *queue,
741 g_return_val_if_fail (queue != NULL, -1);
743 return g_list_position (queue->head, link_);
749 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
751 * Unlinks @link_ so that it will no longer be part of @queue. The link is
754 * @link_ must be part of @queue,
759 g_queue_unlink (GQueue *queue,
762 g_return_if_fail (queue != NULL);
763 g_return_if_fail (link_ != NULL);
765 if (link_ == queue->tail)
766 queue->tail = queue->tail->prev;
768 queue->head = g_list_remove_link (queue->head, link_);
773 * g_queue_delete_link:
775 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
777 * Removes @link_ from @queue and frees it.
779 * @link_ must be part of @queue.
784 g_queue_delete_link (GQueue *queue,
787 g_return_if_fail (queue != NULL);
788 g_return_if_fail (link_ != NULL);
790 g_queue_unlink (queue, link_);
798 * Returns the first element of the queue.
800 * Returns: the data of the first element in the queue, or %NULL if the queue
804 g_queue_peek_head (GQueue *queue)
806 g_return_val_if_fail (queue != NULL, NULL);
808 return queue->head ? queue->head->data : NULL;
815 * Returns the last element of the queue.
817 * Returns: the data of the last element in the queue, or %NULL if the queue
821 g_queue_peek_tail (GQueue *queue)
823 g_return_val_if_fail (queue != NULL, NULL);
825 return queue->tail ? queue->tail->data : NULL;
831 * @n: the position of the element.
833 * Returns the @n'th element of @queue.
835 * Return value: The data for the @n'th element of @queue, or %NULL if @n is
836 * off the end of @queue.
841 g_queue_peek_nth (GQueue *queue,
846 g_return_val_if_fail (queue != NULL, NULL);
848 link = g_queue_peek_nth_link (queue, n);
859 * @data: the data to find.
861 * Returns the position of the first element in @queue which contains @data.
863 * Return value: The position of the first element in @queue which contains @data, or -1 if no element in @queue contains @data.
868 g_queue_index (GQueue *queue,
871 g_return_val_if_fail (queue != NULL, -1);
873 return g_list_index (queue->head, data);
879 * @data: data to remove.
881 * Removes the first element in @queue that contains @data.
886 g_queue_remove (GQueue *queue,
891 g_return_if_fail (queue != NULL);
893 link = g_list_find (queue->head, data);
896 g_queue_delete_link (queue, link);
900 * g_queue_remove_all:
902 * @data: data to remove
904 * Remove all elements whose data equals @data from @queue.
909 g_queue_remove_all (GQueue *queue,
914 g_return_if_fail (queue != NULL);
919 GList *next = list->next;
921 if (list->data == data)
922 g_queue_delete_link (queue, list);
929 * g_queue_insert_before:
931 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
932 * @data: the data to insert
934 * Inserts @data into @queue before @sibling.
936 * @sibling must be part of @queue.
941 g_queue_insert_before (GQueue *queue,
945 g_return_if_fail (queue != NULL);
946 g_return_if_fail (sibling != NULL);
948 queue->head = g_list_insert_before (queue->head, sibling, data);
953 * g_queue_insert_after:
955 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
956 * @data: the data to insert
958 * Inserts @data into @queue after @sibling
960 * @sibling must be part of @queue
965 g_queue_insert_after (GQueue *queue,
969 g_return_if_fail (queue != NULL);
970 g_return_if_fail (sibling != NULL);
972 if (sibling == queue->tail)
973 g_queue_push_tail (queue, data);
975 g_queue_insert_before (queue, sibling->next, data);
979 * g_queue_insert_sorted:
981 * @data: the data to insert
982 * @func: the #GCompareDataFunc used to compare elements in the queue. It is
983 * called with two elements of the @queue and @user_data. It should
984 * return 0 if the elements are equal, a negative value if the first
985 * element comes before the second, and a positive value if the second
986 * element comes before the first.
987 * @user_data: user data passed to @func.
989 * Inserts @data into @queue using @func to determine the new position.
994 g_queue_insert_sorted (GQueue *queue,
996 GCompareDataFunc func,
1001 g_return_if_fail (queue != NULL);
1004 while (list && func (list->data, data, user_data) < 0)
1008 g_queue_insert_before (queue, list, data);
1010 g_queue_push_tail (queue, data);