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.
35 * Creates a new #GQueue.
37 * Returns: a new #GQueue.
42 return g_slice_new0 (GQueue);
49 * Frees the memory allocated for the #GQueue.
52 g_queue_free (GQueue *queue)
54 g_return_if_fail (queue != NULL);
56 g_list_free (queue->head);
57 g_slice_free (GQueue, queue);
64 * Returns %TRUE if the queue is empty.
66 * Returns: %TRUE if the queue is empty.
69 g_queue_is_empty (GQueue *queue)
71 g_return_val_if_fail (queue != NULL, TRUE);
73 return queue->head == NULL;
80 * Returns the number of items in @queue.
82 * Return value: The number of items in @queue.
87 g_queue_get_length (GQueue *queue)
89 g_return_val_if_fail (queue != NULL, 0);
98 * Reverses the order of the items in @queue.
103 g_queue_reverse (GQueue *queue)
105 g_return_if_fail (queue != NULL);
107 queue->tail = queue->head;
108 queue->head = g_list_reverse (queue->head);
115 * Copies a @queue. Note that is a shallow copy. If the elements in the
116 * queue consist of pointers to data, the pointers are copied, but the
117 * actual data is not.
119 * Return value: A copy of @queue
124 g_queue_copy (GQueue *queue)
129 g_return_val_if_fail (queue != NULL, NULL);
131 result = g_queue_new ();
133 for (list = queue->head; list != NULL; list = list->next)
134 g_queue_push_tail (result, list->data);
142 * @func: the function to call for each element's data
143 * @user_data: user data to pass to @func
145 * Calls @func for each element in the queue passing @user_data to the
151 g_queue_foreach (GQueue *queue,
157 g_return_if_fail (queue != NULL);
158 g_return_if_fail (func != NULL);
163 GList *next = list->next;
164 func (list->data, user_data);
172 * @data: data to find
174 * Finds the first link in @queue which contains @data.
176 * Return value: The first link in @queue which contains @data.
181 g_queue_find (GQueue *queue,
184 g_return_val_if_fail (queue != NULL, NULL);
186 return g_list_find (queue->head, data);
190 * g_queue_find_custom:
192 * @data: user data passed to @func
193 * @func: a #GCompareFunc to call for each element. It should return 0
194 * when the desired element is found
196 * Finds an element in a #GQueue, using a supplied function to find the
197 * desired element. It iterates over the queue, calling the given function
198 * which should return 0 when the desired element is found. The function
199 * takes two gconstpointer arguments, the #GQueue element's data as the
200 * first argument and the given user data as the second argument.
202 * Return value: The found link, or %NULL if it wasn't found
207 g_queue_find_custom (GQueue *queue,
211 g_return_val_if_fail (queue != NULL, NULL);
212 g_return_val_if_fail (func != NULL, NULL);
214 return g_list_find_custom (queue->head, data, func);
220 * @compare_func: the #GCompareDataFunc used to sort @queue. This function
221 * is passed two elements of the queue and should return 0 if they are
222 * equal, a negative value if the first comes before the second, and
223 * a positive value if the second comes before the first.
224 * @user_data: user data passed to @compare_func
226 * Sorts @queue using @compare_func.
231 g_queue_sort (GQueue *queue,
232 GCompareDataFunc compare_func,
235 g_return_if_fail (queue != NULL);
236 g_return_if_fail (compare_func != NULL);
238 queue->head = g_list_sort_with_data (queue->head, compare_func, user_data);
239 queue->tail = g_list_last (queue->head);
245 * @data: the data for the new element.
247 * Adds a new element at the head of the queue.
250 g_queue_push_head (GQueue *queue,
253 g_return_if_fail (queue != NULL);
255 queue->head = g_list_prepend (queue->head, data);
257 queue->tail = queue->head;
264 * @data: the data for the new element
265 * @n: the position to insert the new element. If @n is negative or
266 * larger than the number of elements in the @queue, the element is
267 * added to the end of the queue.
269 * Inserts a new element into @queue at the given position
274 g_queue_push_nth (GQueue *queue,
278 g_return_if_fail (queue != NULL);
280 if (n < 0 || n >= queue->length)
282 g_queue_push_tail (queue, data);
286 g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
290 * g_queue_push_head_link:
292 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
293 * more than one element.
295 * Adds a new element at the head of the queue.
298 g_queue_push_head_link (GQueue *queue,
301 g_return_if_fail (queue != NULL);
302 g_return_if_fail (link != NULL);
303 g_return_if_fail (link->prev == NULL);
304 g_return_if_fail (link->next == NULL);
306 link->next = queue->head;
308 queue->head->prev = link;
318 * @data: the data for the new element.
320 * Adds a new element at the tail of the queue.
323 g_queue_push_tail (GQueue *queue,
326 g_return_if_fail (queue != NULL);
328 queue->tail = g_list_append (queue->tail, data);
329 if (queue->tail->next)
330 queue->tail = queue->tail->next;
332 queue->head = queue->tail;
337 * g_queue_push_tail_link:
339 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
340 * more than one element.
342 * Adds a new element at the tail of the queue.
345 g_queue_push_tail_link (GQueue *queue,
348 g_return_if_fail (queue != NULL);
349 g_return_if_fail (link != NULL);
350 g_return_if_fail (link->prev == NULL);
351 g_return_if_fail (link->next == NULL);
353 link->prev = queue->tail;
355 queue->tail->next = link;
363 * g_queue_push_nth_link:
365 * @n: the position to insert the link. If this is negative or larger than
366 * the number of elements in @queue, the link is added to the end of
368 * @link_: the link to add to @queue
370 * Inserts @link into @queue at the given position.
375 g_queue_push_nth_link (GQueue *queue,
382 g_return_if_fail (queue != NULL);
383 g_return_if_fail (link_ != NULL);
385 if (n < 0 || n >= queue->length)
387 g_queue_push_tail_link (queue, link_);
391 g_assert (queue->head);
392 g_assert (queue->tail);
394 next = g_queue_peek_nth_link (queue, n);
404 if (queue->head->prev)
405 queue->head = queue->head->prev;
407 if (queue->tail->next)
408 queue->tail = queue->tail->next;
417 * Removes the first element of the queue.
419 * Returns: the data of the first element in the queue, or %NULL if the queue
423 g_queue_pop_head (GQueue *queue)
425 g_return_val_if_fail (queue != NULL, NULL);
429 GList *node = queue->head;
430 gpointer data = node->data;
432 queue->head = node->next;
434 queue->head->prev = NULL;
437 g_list_free_1 (node);
447 * g_queue_pop_head_link:
450 * Removes the first element of the queue.
452 * Returns: the #GList element at the head of the queue, or %NULL if the queue
456 g_queue_pop_head_link (GQueue *queue)
458 g_return_val_if_fail (queue != NULL, NULL);
462 GList *node = queue->head;
464 queue->head = node->next;
467 queue->head->prev = NULL;
481 * g_queue_peek_head_link:
484 * Returns the first link in @queue
486 * Return value: the first link in @queue, or %NULL if @queue is empty
491 g_queue_peek_head_link (GQueue *queue)
493 g_return_val_if_fail (queue != NULL, NULL);
499 * g_queue_peek_tail_link:
502 * Returns the last link @queue.
504 * Return value: the last link in @queue, or %NULL if @queue is empty
509 g_queue_peek_tail_link (GQueue *queue)
511 g_return_val_if_fail (queue != NULL, NULL);
520 * Removes the last element of the queue.
522 * Returns: the data of the last element in the queue, or %NULL if the queue
526 g_queue_pop_tail (GQueue *queue)
528 g_return_val_if_fail (queue != NULL, NULL);
532 GList *node = queue->tail;
533 gpointer data = node->data;
535 queue->tail = node->prev;
537 queue->tail->next = NULL;
541 g_list_free_1 (node);
552 * @n: the position of the element.
554 * Removes the @n'th element of @queue.
556 * Return value: the element's data, or %NULL if @n is off the end of @queue.
561 g_queue_pop_nth (GQueue *queue,
567 g_return_val_if_fail (queue != NULL, NULL);
569 if (n >= queue->length)
572 nth_link = g_queue_peek_nth_link (queue, n);
573 result = nth_link->data;
575 g_queue_delete_link (queue, nth_link);
581 * g_queue_pop_tail_link:
584 * Removes the last element of the queue.
586 * Returns: the #GList element at the tail of the queue, or %NULL if the queue
590 g_queue_pop_tail_link (GQueue *queue)
592 g_return_val_if_fail (queue != NULL, NULL);
596 GList *node = queue->tail;
598 queue->tail = node->prev;
601 queue->tail->next = NULL;
615 * g_queue_pop_nth_link:
617 * @n: the link's position
619 * Removes and returns the link at the given position.
621 * Return value: The @n'th link, or %NULL if @n is off the end of @queue.
626 g_queue_pop_nth_link (GQueue *queue,
631 g_return_val_if_fail (queue != NULL, NULL);
633 if (n >= queue->length)
636 link = g_queue_peek_nth_link (queue, n);
637 g_queue_unlink (queue, link);
643 * g_queue_peek_nth_link:
645 * @n: the position of the link
647 * Returns the link at the given position
649 * Return value: The link at the @n'th position, or %NULL if @n is off the
655 g_queue_peek_nth_link (GQueue *queue,
661 g_return_val_if_fail (queue != NULL, NULL);
663 if (n >= queue->length)
666 if (n > queue->length / 2)
668 n = queue->length - n - 1;
671 for (i = 0; i < n; ++i)
677 for (i = 0; i < n; ++i)
685 * g_queue_link_index:
687 * @link_: A #GList link
689 * Returns the position of @link_ in @queue.
691 * Return value: The position of @link_, or -1 if the link is
697 g_queue_link_index (GQueue *queue,
700 g_return_val_if_fail (queue != NULL, -1);
702 return g_list_position (queue->head, link_);
708 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
710 * Unlinks @link_ so that it will no longer be part of @queue. The link is
713 * @link_ must be part of @queue,
718 g_queue_unlink (GQueue *queue,
721 g_return_if_fail (queue != NULL);
722 g_return_if_fail (link_ != NULL);
724 if (link_ == queue->tail)
725 queue->tail = queue->tail->prev;
727 queue->head = g_list_remove_link (queue->head, link_);
732 * g_queue_delete_link:
734 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
736 * Removes @link_ from @queue and frees it.
738 * @link_ must be part of @queue.
743 g_queue_delete_link (GQueue *queue,
746 g_return_if_fail (queue != NULL);
747 g_return_if_fail (link_ != NULL);
749 g_queue_unlink (queue, link_);
757 * Returns the first element of the queue.
759 * Returns: the data of the first element in the queue, or %NULL if the queue
763 g_queue_peek_head (GQueue *queue)
765 g_return_val_if_fail (queue != NULL, NULL);
767 return queue->head ? queue->head->data : NULL;
774 * Returns the last element of the queue.
776 * Returns: the data of the last element in the queue, or %NULL if the queue
780 g_queue_peek_tail (GQueue *queue)
782 g_return_val_if_fail (queue != NULL, NULL);
784 return queue->tail ? queue->tail->data : NULL;
790 * @n: the position of the element.
792 * Returns the @n'th element of @queue.
794 * Return value: The data for the @n'th element of @queue, or %NULL if @n is
795 * off the end of @queue.
800 g_queue_peek_nth (GQueue *queue,
805 g_return_val_if_fail (queue != NULL, NULL);
807 link = g_queue_peek_nth_link (queue, n);
818 * @data: the data to find.
820 * Returns the position of the first element in @queue which contains @data.
822 * Return value: The position of the first element in @queue which contains @data, or -1 if no element in @queue contains @data.
827 g_queue_index (GQueue *queue,
830 g_return_val_if_fail (queue != NULL, -1);
832 return g_list_index (queue->head, data);
838 * @data: data to remove.
840 * Removes the first element in @queue that contains @data.
845 g_queue_remove (GQueue *queue,
850 g_return_if_fail (queue != NULL);
852 link = g_list_find (queue->head, data);
855 g_queue_delete_link (queue, link);
859 * g_queue_remove_all:
861 * @data: data to remove
863 * Remove all elemeents in @queue which contains @data.
868 g_queue_remove_all (GQueue *queue,
873 g_return_if_fail (queue != NULL);
878 GList *next = list->next;
880 if (list->data == data)
881 g_queue_delete_link (queue, list);
888 * g_queue_insert_before:
890 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
891 * @data: the data to insert
893 * Inserts @data into @queue before @sibling.
895 * @sibling must be part of @queue.
900 g_queue_insert_before (GQueue *queue,
904 g_return_if_fail (queue != NULL);
905 g_return_if_fail (sibling != NULL);
907 queue->head = g_list_insert_before (queue->head, sibling, data);
912 * g_queue_insert_after:
914 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
915 * @data: the data to insert
917 * Inserts @data into @queue after @sibling
919 * @sibling must be part of @queue
924 g_queue_insert_after (GQueue *queue,
928 g_return_if_fail (queue != NULL);
929 g_return_if_fail (sibling != NULL);
931 if (sibling == queue->tail)
932 g_queue_push_tail (queue, data);
934 g_queue_insert_before (queue, sibling->next, data);
938 * g_queue_insert_sorted:
940 * @data: the data to insert
941 * @func: the #GCompareDataFunc used to compare elements in the queue. It is
942 * called with two elements of the @queue and @user_data. It should
943 * return 0 if the elements are equal, a negative value if the first
944 * element comes before the second, and a positive value if the second
945 * element comes before the first.
946 * @user_data: user data passed to @func.
948 * Inserts @data into @queue using @func to determine the new position.
953 g_queue_insert_sorted (GQueue *queue,
955 GCompareDataFunc func,
960 g_return_if_fail (queue != NULL);
963 while (list && func (list->data, data, user_data) < 0)
967 g_queue_insert_before (queue, list, data);
969 g_queue_push_tail (queue, data);
972 #define __G_QUEUE_C__
973 #include "galiasdef.c"