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.
34 * Creates a new #GQueue.
36 * Returns: a new #GQueue.
41 return g_slice_new0 (GQueue);
48 * Frees the memory allocated for the #GQueue. Only call this function if
49 * @queue was created with g_queue_new(). If queue elements contain
50 * dynamically-allocated memory, they should be freed first.
53 g_queue_free (GQueue *queue)
55 g_return_if_fail (queue != NULL);
57 g_list_free (queue->head);
58 g_slice_free (GQueue, queue);
63 * @queue: an uninitialized #GQueue
65 * A statically-allocated #GQueue must be initialized with this function
66 * before it can be used. Alternatively you can initialize it with
67 * #G_QUEUE_INIT. It is not necessary to initialize queues created with
73 g_queue_init (GQueue *queue)
75 g_return_if_fail (queue != NULL);
77 queue->head = queue->tail = NULL;
85 * Removes all the elements in @queue. If queue elements contain
86 * dynamically-allocated memory, they should be freed first.
91 g_queue_clear (GQueue *queue)
93 g_return_if_fail (queue != NULL);
95 g_list_free (queue->head);
103 * Returns %TRUE if the queue is empty.
105 * Returns: %TRUE if the queue is empty.
108 g_queue_is_empty (GQueue *queue)
110 g_return_val_if_fail (queue != NULL, TRUE);
112 return queue->head == NULL;
116 * g_queue_get_length:
119 * Returns the number of items in @queue.
121 * Return value: The number of items in @queue.
126 g_queue_get_length (GQueue *queue)
128 g_return_val_if_fail (queue != NULL, 0);
130 return queue->length;
137 * Reverses the order of the items in @queue.
142 g_queue_reverse (GQueue *queue)
144 g_return_if_fail (queue != NULL);
146 queue->tail = queue->head;
147 queue->head = g_list_reverse (queue->head);
154 * Copies a @queue. Note that is a shallow copy. If the elements in the
155 * queue consist of pointers to data, the pointers are copied, but the
156 * actual data is not.
158 * Return value: A copy of @queue
163 g_queue_copy (GQueue *queue)
168 g_return_val_if_fail (queue != NULL, NULL);
170 result = g_queue_new ();
172 for (list = queue->head; list != NULL; list = list->next)
173 g_queue_push_tail (result, list->data);
181 * @func: the function to call for each element's data
182 * @user_data: user data to pass to @func
184 * Calls @func for each element in the queue passing @user_data to the
190 g_queue_foreach (GQueue *queue,
196 g_return_if_fail (queue != NULL);
197 g_return_if_fail (func != NULL);
202 GList *next = list->next;
203 func (list->data, user_data);
211 * @data: data to find
213 * Finds the first link in @queue which contains @data.
215 * Return value: The first link in @queue which contains @data.
220 g_queue_find (GQueue *queue,
223 g_return_val_if_fail (queue != NULL, NULL);
225 return g_list_find (queue->head, data);
229 * g_queue_find_custom:
231 * @data: user data passed to @func
232 * @func: a #GCompareFunc to call for each element. It should return 0
233 * when the desired element is found
235 * Finds an element in a #GQueue, using a supplied function to find the
236 * desired element. It iterates over the queue, calling the given function
237 * which should return 0 when the desired element is found. The function
238 * takes two gconstpointer arguments, the #GQueue element's data as the
239 * first argument and the given user data as the second argument.
241 * Return value: The found link, or %NULL if it wasn't found
246 g_queue_find_custom (GQueue *queue,
250 g_return_val_if_fail (queue != NULL, NULL);
251 g_return_val_if_fail (func != NULL, NULL);
253 return g_list_find_custom (queue->head, data, func);
259 * @compare_func: the #GCompareDataFunc used to sort @queue. This function
260 * is passed two elements of the queue and should return 0 if they are
261 * equal, a negative value if the first comes before the second, and
262 * a positive value if the second comes before the first.
263 * @user_data: user data passed to @compare_func
265 * Sorts @queue using @compare_func.
270 g_queue_sort (GQueue *queue,
271 GCompareDataFunc compare_func,
274 g_return_if_fail (queue != NULL);
275 g_return_if_fail (compare_func != NULL);
277 queue->head = g_list_sort_with_data (queue->head, compare_func, user_data);
278 queue->tail = g_list_last (queue->head);
284 * @data: the data for the new element.
286 * Adds a new element at the head of the queue.
289 g_queue_push_head (GQueue *queue,
292 g_return_if_fail (queue != NULL);
294 queue->head = g_list_prepend (queue->head, data);
296 queue->tail = queue->head;
303 * @data: the data for the new element
304 * @n: the position to insert the new element. If @n is negative or
305 * larger than the number of elements in the @queue, the element is
306 * added to the end of the queue.
308 * Inserts a new element into @queue at the given position
313 g_queue_push_nth (GQueue *queue,
317 g_return_if_fail (queue != NULL);
319 if (n < 0 || n >= queue->length)
321 g_queue_push_tail (queue, data);
325 g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
329 * g_queue_push_head_link:
331 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
332 * more than one element.
334 * Adds a new element at the head of the queue.
337 g_queue_push_head_link (GQueue *queue,
340 g_return_if_fail (queue != NULL);
341 g_return_if_fail (link != NULL);
342 g_return_if_fail (link->prev == NULL);
343 g_return_if_fail (link->next == NULL);
345 link->next = queue->head;
347 queue->head->prev = link;
357 * @data: the data for the new element.
359 * Adds a new element at the tail of the queue.
362 g_queue_push_tail (GQueue *queue,
365 g_return_if_fail (queue != NULL);
367 queue->tail = g_list_append (queue->tail, data);
368 if (queue->tail->next)
369 queue->tail = queue->tail->next;
371 queue->head = queue->tail;
376 * g_queue_push_tail_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 tail of the queue.
384 g_queue_push_tail_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->prev = queue->tail;
394 queue->tail->next = link;
402 * g_queue_push_nth_link:
404 * @n: the position to insert the link. If this is negative or larger than
405 * the number of elements in @queue, the link is added to the end of
407 * @link_: the link to add to @queue
409 * Inserts @link into @queue at the given position.
414 g_queue_push_nth_link (GQueue *queue,
421 g_return_if_fail (queue != NULL);
422 g_return_if_fail (link_ != NULL);
424 if (n < 0 || n >= queue->length)
426 g_queue_push_tail_link (queue, link_);
430 g_assert (queue->head);
431 g_assert (queue->tail);
433 next = g_queue_peek_nth_link (queue, n);
443 if (queue->head->prev)
444 queue->head = queue->head->prev;
446 if (queue->tail->next)
447 queue->tail = queue->tail->next;
456 * Removes the first element of the queue.
458 * Returns: the data of the first element in the queue, or %NULL if the queue
462 g_queue_pop_head (GQueue *queue)
464 g_return_val_if_fail (queue != NULL, NULL);
468 GList *node = queue->head;
469 gpointer data = node->data;
471 queue->head = node->next;
473 queue->head->prev = NULL;
476 g_list_free_1 (node);
486 * g_queue_pop_head_link:
489 * Removes the first element of the queue.
491 * Returns: the #GList element at the head of the queue, or %NULL if the queue
495 g_queue_pop_head_link (GQueue *queue)
497 g_return_val_if_fail (queue != NULL, NULL);
501 GList *node = queue->head;
503 queue->head = node->next;
506 queue->head->prev = NULL;
520 * g_queue_peek_head_link:
523 * Returns the first link in @queue
525 * Return value: the first link in @queue, or %NULL if @queue is empty
530 g_queue_peek_head_link (GQueue *queue)
532 g_return_val_if_fail (queue != NULL, NULL);
538 * g_queue_peek_tail_link:
541 * Returns the last link @queue.
543 * Return value: the last link in @queue, or %NULL if @queue is empty
548 g_queue_peek_tail_link (GQueue *queue)
550 g_return_val_if_fail (queue != NULL, NULL);
559 * Removes the last element of the queue.
561 * Returns: the data of the last element in the queue, or %NULL if the queue
565 g_queue_pop_tail (GQueue *queue)
567 g_return_val_if_fail (queue != NULL, NULL);
571 GList *node = queue->tail;
572 gpointer data = node->data;
574 queue->tail = node->prev;
576 queue->tail->next = NULL;
580 g_list_free_1 (node);
591 * @n: the position of the element.
593 * Removes the @n'th element of @queue.
595 * Return value: the element's data, or %NULL if @n is off the end of @queue.
600 g_queue_pop_nth (GQueue *queue,
606 g_return_val_if_fail (queue != NULL, NULL);
608 if (n >= queue->length)
611 nth_link = g_queue_peek_nth_link (queue, n);
612 result = nth_link->data;
614 g_queue_delete_link (queue, nth_link);
620 * g_queue_pop_tail_link:
623 * Removes the last element of the queue.
625 * Returns: the #GList element at the tail of the queue, or %NULL if the queue
629 g_queue_pop_tail_link (GQueue *queue)
631 g_return_val_if_fail (queue != NULL, NULL);
635 GList *node = queue->tail;
637 queue->tail = node->prev;
640 queue->tail->next = NULL;
654 * g_queue_pop_nth_link:
656 * @n: the link's position
658 * Removes and returns the link at the given position.
660 * Return value: The @n'th link, or %NULL if @n is off the end of @queue.
665 g_queue_pop_nth_link (GQueue *queue,
670 g_return_val_if_fail (queue != NULL, NULL);
672 if (n >= queue->length)
675 link = g_queue_peek_nth_link (queue, n);
676 g_queue_unlink (queue, link);
682 * g_queue_peek_nth_link:
684 * @n: the position of the link
686 * Returns the link at the given position
688 * Return value: The link at the @n'th position, or %NULL if @n is off the
694 g_queue_peek_nth_link (GQueue *queue,
700 g_return_val_if_fail (queue != NULL, NULL);
702 if (n >= queue->length)
705 if (n > queue->length / 2)
707 n = queue->length - n - 1;
710 for (i = 0; i < n; ++i)
716 for (i = 0; i < n; ++i)
724 * g_queue_link_index:
726 * @link_: A #GList link
728 * Returns the position of @link_ in @queue.
730 * Return value: The position of @link_, or -1 if the link is
736 g_queue_link_index (GQueue *queue,
739 g_return_val_if_fail (queue != NULL, -1);
741 return g_list_position (queue->head, link_);
747 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
749 * Unlinks @link_ so that it will no longer be part of @queue. The link is
752 * @link_ must be part of @queue,
757 g_queue_unlink (GQueue *queue,
760 g_return_if_fail (queue != NULL);
761 g_return_if_fail (link_ != NULL);
763 if (link_ == queue->tail)
764 queue->tail = queue->tail->prev;
766 queue->head = g_list_remove_link (queue->head, link_);
771 * g_queue_delete_link:
773 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
775 * Removes @link_ from @queue and frees it.
777 * @link_ must be part of @queue.
782 g_queue_delete_link (GQueue *queue,
785 g_return_if_fail (queue != NULL);
786 g_return_if_fail (link_ != NULL);
788 g_queue_unlink (queue, link_);
796 * Returns the first element of the queue.
798 * Returns: the data of the first element in the queue, or %NULL if the queue
802 g_queue_peek_head (GQueue *queue)
804 g_return_val_if_fail (queue != NULL, NULL);
806 return queue->head ? queue->head->data : NULL;
813 * Returns the last element of the queue.
815 * Returns: the data of the last element in the queue, or %NULL if the queue
819 g_queue_peek_tail (GQueue *queue)
821 g_return_val_if_fail (queue != NULL, NULL);
823 return queue->tail ? queue->tail->data : NULL;
829 * @n: the position of the element.
831 * Returns the @n'th element of @queue.
833 * Return value: The data for the @n'th element of @queue, or %NULL if @n is
834 * off the end of @queue.
839 g_queue_peek_nth (GQueue *queue,
844 g_return_val_if_fail (queue != NULL, NULL);
846 link = g_queue_peek_nth_link (queue, n);
857 * @data: the data to find.
859 * Returns the position of the first element in @queue which contains @data.
861 * Return value: The position of the first element in @queue which contains @data, or -1 if no element in @queue contains @data.
866 g_queue_index (GQueue *queue,
869 g_return_val_if_fail (queue != NULL, -1);
871 return g_list_index (queue->head, data);
877 * @data: data to remove.
879 * Removes the first element in @queue that contains @data.
884 g_queue_remove (GQueue *queue,
889 g_return_if_fail (queue != NULL);
891 link = g_list_find (queue->head, data);
894 g_queue_delete_link (queue, link);
898 * g_queue_remove_all:
900 * @data: data to remove
902 * Remove all elemeents in @queue which contains @data.
907 g_queue_remove_all (GQueue *queue,
912 g_return_if_fail (queue != NULL);
917 GList *next = list->next;
919 if (list->data == data)
920 g_queue_delete_link (queue, list);
927 * g_queue_insert_before:
929 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
930 * @data: the data to insert
932 * Inserts @data into @queue before @sibling.
934 * @sibling must be part of @queue.
939 g_queue_insert_before (GQueue *queue,
943 g_return_if_fail (queue != NULL);
944 g_return_if_fail (sibling != NULL);
946 queue->head = g_list_insert_before (queue->head, sibling, data);
951 * g_queue_insert_after:
953 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
954 * @data: the data to insert
956 * Inserts @data into @queue after @sibling
958 * @sibling must be part of @queue
963 g_queue_insert_after (GQueue *queue,
967 g_return_if_fail (queue != NULL);
968 g_return_if_fail (sibling != NULL);
970 if (sibling == queue->tail)
971 g_queue_push_tail (queue, data);
973 g_queue_insert_before (queue, sibling->next, data);
977 * g_queue_insert_sorted:
979 * @data: the data to insert
980 * @func: the #GCompareDataFunc used to compare elements in the queue. It is
981 * called with two elements of the @queue and @user_data. It should
982 * return 0 if the elements are equal, a negative value if the first
983 * element comes before the second, and a positive value if the second
984 * element comes before the first.
985 * @user_data: user data passed to @func.
987 * Inserts @data into @queue using @func to determine the new position.
992 g_queue_insert_sorted (GQueue *queue,
994 GCompareDataFunc func,
999 g_return_if_fail (queue != NULL);
1002 while (list && func (list->data, data, user_data) < 0)
1006 g_queue_insert_before (queue, list, data);
1008 g_queue_push_tail (queue, data);