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.
33 G_LOCK_DEFINE_STATIC (queue_memchunk);
34 static GMemChunk *queue_memchunk = NULL;
35 static GTrashStack *free_queue_nodes = NULL;
40 * Creates a new #GQueue.
42 * Returns: a new #GQueue.
49 G_LOCK (queue_memchunk);
50 queue = g_trash_stack_pop (&free_queue_nodes);
55 queue_memchunk = g_mem_chunk_new ("GLib GQueue chunk",
57 sizeof (GQueue) * 128,
59 queue = g_chunk_new (GQueue, queue_memchunk);
61 G_UNLOCK (queue_memchunk);
74 * Frees the memory allocated for the #GQueue.
77 g_queue_free (GQueue *queue)
79 g_return_if_fail (queue != NULL);
81 g_list_free (queue->head);
83 #ifdef ENABLE_GC_FRIENDLY
86 #endif /* ENABLE_GC_FRIENDLY */
88 G_LOCK (queue_memchunk);
89 g_trash_stack_push (&free_queue_nodes, queue);
90 G_UNLOCK (queue_memchunk);
97 * Returns %TRUE if the queue is empty.
99 * Returns: %TRUE if the queue is empty.
102 g_queue_is_empty (GQueue *queue)
104 g_return_val_if_fail (queue != NULL, TRUE);
106 return queue->head == NULL;
110 * g_queue_get_length:
113 * Returns the number of items in @queue.
115 * Return value: The number of items in @queue.
120 g_queue_get_length (GQueue *queue)
122 g_return_val_if_fail (queue != NULL, 0);
124 return queue->length;
131 * Reverses the order of the items in @queue.
136 g_queue_reverse (GQueue *queue)
138 g_return_if_fail (queue != NULL);
140 queue->tail = queue->head;
141 queue->head = g_list_reverse (queue->head);
148 * Copies a @queue. Note that is a shallow copy. If the elements in the
149 * queue consist of pointers to data, the pointers are copied, but the
150 * actual data is not.
152 * Return value: A copy of @queue
157 g_queue_copy (GQueue *queue)
162 g_return_val_if_fail (queue != NULL, NULL);
164 result = g_queue_new ();
166 for (list = queue->head; list != NULL; list = list->next)
167 g_queue_push_tail (result, list->data);
175 * @func: the function to call for each element's data
176 * @user_data: user data to pass to @func
178 * Calls @func for each element in the queue passing @user_data to the
184 g_queue_foreach (GQueue *queue,
190 g_return_if_fail (queue != NULL);
191 g_return_if_fail (func != NULL);
196 GList *next = list->next;
197 func (list->data, user_data);
205 * @data: data to find
207 * Finds the first link in @queue which contains @data.
209 * Return value: The first link in @queue which contains @data.
214 g_queue_find (GQueue *queue,
217 g_return_val_if_fail (queue != NULL, NULL);
219 return g_list_find (queue->head, data);
223 * g_queue_find_custom:
225 * @data: user data passed to @func
226 * @func: a #GCompareFunc to call for each element. It should return 0
227 * when the desired element is found
229 * Finds an element in a #GQueue, using a supplied function to find the
230 * desired element. It iterates over the queue, calling the given function
231 * which should return 0 when the desired element is found. The function
232 * takes two gconstpointer arguments, the #GQueue element's data and the
235 * Return value: The found link, or %NULL if it wasn't found
240 g_queue_find_custom (GQueue *queue,
244 g_return_val_if_fail (queue != NULL, NULL);
245 g_return_val_if_fail (func != NULL, NULL);
247 return g_list_find_custom (queue->head, data, func);
253 * @compare_func: the #GCompareDataFunc used to sort @queue. This function
254 * is passed two elements of the queue and should return 0 if they are
255 * equal, a negative value if the first comes before the second, and
256 * a positive value if the second comes before the first.
257 * @user_data: user data passed to @compare_func
259 * Sorts @queue using @compare_func.
264 g_queue_sort (GQueue *queue,
265 GCompareDataFunc compare_func,
268 g_return_if_fail (queue != NULL);
269 g_return_if_fail (compare_func != NULL);
271 queue->head = g_list_sort_with_data (queue->head, compare_func, user_data);
272 queue->tail = g_list_last (queue->head);
278 * @data: the data for the new element.
280 * Adds a new element at the head of the queue.
283 g_queue_push_head (GQueue *queue,
286 g_return_if_fail (queue != NULL);
288 queue->head = g_list_prepend (queue->head, data);
290 queue->tail = queue->head;
297 * @data: the data for the new element
298 * @n: the position to insert the new element. If @n is negative or
299 * larger than the number of elements in the @queue, the element is
300 * added to the end of the queue.
302 * Inserts a new element into @queue at the given position
307 g_queue_push_nth (GQueue *queue,
311 g_return_if_fail (queue != NULL);
313 if (n < 0 || n >= queue->length)
315 g_queue_push_tail (queue, data);
319 g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
323 * g_queue_push_head_link:
325 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
326 * more than one element.
328 * Adds a new element at the head of the queue.
331 g_queue_push_head_link (GQueue *queue,
334 g_return_if_fail (queue != NULL);
335 g_return_if_fail (link != NULL);
336 g_return_if_fail (link->prev == NULL);
337 g_return_if_fail (link->next == NULL);
339 link->next = queue->head;
341 queue->head->prev = link;
351 * @data: the data for the new element.
353 * Adds a new element at the tail of the queue.
356 g_queue_push_tail (GQueue *queue,
359 g_return_if_fail (queue != NULL);
361 queue->tail = g_list_append (queue->tail, data);
362 if (queue->tail->next)
363 queue->tail = queue->tail->next;
365 queue->head = queue->tail;
370 * g_queue_push_tail_link:
372 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
373 * more than one element.
375 * Adds a new element at the tail of the queue.
378 g_queue_push_tail_link (GQueue *queue,
381 g_return_if_fail (queue != NULL);
382 g_return_if_fail (link != NULL);
383 g_return_if_fail (link->prev == NULL);
384 g_return_if_fail (link->next == NULL);
386 link->prev = queue->tail;
388 queue->tail->next = link;
396 * g_queue_push_nth_link:
398 * @n: the position to insert the link. If this is negative or larger than
399 * the number of elements in @queue, the link is added to the end of
401 * @link_: the link to add to @queue
403 * Inserts @link into @queue at the given position.
408 g_queue_push_nth_link (GQueue *queue,
415 g_return_if_fail (queue != NULL);
416 g_return_if_fail (link_ != NULL);
418 if (n < 0 || n >= queue->length)
420 g_queue_push_tail_link (queue, link_);
424 g_assert (queue->head);
425 g_assert (queue->tail);
427 next = g_queue_peek_nth_link (queue, n);
437 if (queue->head->prev)
438 queue->head = queue->head->prev;
440 if (queue->tail->next)
441 queue->tail = queue->tail->next;
450 * Removes the first element of the queue.
452 * Returns: the data of the first element in the queue, or %NULL if the queue
456 g_queue_pop_head (GQueue *queue)
458 g_return_val_if_fail (queue != NULL, NULL);
462 GList *node = queue->head;
463 gpointer data = node->data;
465 queue->head = node->next;
467 queue->head->prev = NULL;
470 g_list_free_1 (node);
480 * g_queue_pop_head_link:
483 * Removes the first element of the queue.
485 * Returns: the #GList element at the head of the queue, or %NULL if the queue
489 g_queue_pop_head_link (GQueue *queue)
491 g_return_val_if_fail (queue != NULL, NULL);
495 GList *node = queue->head;
497 queue->head = node->next;
500 queue->head->prev = NULL;
514 * g_queue_peek_head_link:
517 * Returns the first link in @queue
519 * Return value: the first link in @queue, or %NULL if @queue is empty
524 g_queue_peek_head_link (GQueue *queue)
526 g_return_val_if_fail (queue != NULL, NULL);
532 * g_queue_peek_tail_link:
535 * Returns the last link @queue.
537 * Return value: the last link in @queue, or %NULL if @queue is empty
542 g_queue_peek_tail_link (GQueue *queue)
544 g_return_val_if_fail (queue != NULL, NULL);
553 * Removes the last element of the queue.
555 * Returns: the data of the last element in the queue, or %NULL if the queue
559 g_queue_pop_tail (GQueue *queue)
561 g_return_val_if_fail (queue != NULL, NULL);
565 GList *node = queue->tail;
566 gpointer data = node->data;
568 queue->tail = node->prev;
570 queue->tail->next = NULL;
574 g_list_free_1 (node);
585 * @n: the position of the element.
587 * Removes the @n'th element of @queue.
589 * Return value: the element's data, or %NULL if @n is off the end of @queue.
594 g_queue_pop_nth (GQueue *queue,
600 g_return_val_if_fail (queue != NULL, NULL);
602 if (n >= queue->length)
605 nth_link = g_queue_peek_nth_link (queue, n);
606 result = nth_link->data;
608 g_queue_delete_link (queue, nth_link);
614 * g_queue_pop_tail_link:
617 * Removes the last element of the queue.
619 * Returns: the #GList element at the tail of the queue, or %NULL if the queue
623 g_queue_pop_tail_link (GQueue *queue)
625 g_return_val_if_fail (queue != NULL, NULL);
629 GList *node = queue->tail;
631 queue->tail = node->prev;
634 queue->tail->next = NULL;
648 * g_queue_pop_nth_link:
650 * @n: the link's position
652 * Removes and returns the link at the given position.
654 * Return value: The @n'th link, or %NULL if @n is off the end of @queue.
659 g_queue_pop_nth_link (GQueue *queue,
664 g_return_val_if_fail (queue != NULL, NULL);
666 if (n >= queue->length)
669 link = g_queue_peek_nth_link (queue, n);
670 g_queue_unlink (queue, link);
676 * g_queue_peek_nth_link:
678 * @n: the position of the link
680 * Returns the link at the given position
682 * Return value: The link at the @n'th position, or %NULL if @n is off the
688 g_queue_peek_nth_link (GQueue *queue,
694 g_return_val_if_fail (queue != NULL, NULL);
696 if (n >= queue->length)
699 if (n > queue->length / 2)
701 n = queue->length - n - 1;
704 for (i = 0; i < n; ++i)
710 for (i = 0; i < n; ++i)
718 * g_queue_link_index:
720 * @link_: A #GList link
722 * Returns the position of @link_ in @queue.
724 * Return value: The position of @link_, or -1 if the link is
730 g_queue_link_index (GQueue *queue,
733 g_return_val_if_fail (queue != NULL, -1);
735 return g_list_position (queue->head, link_);
741 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
743 * Unlinks @link_ so that it will no longer be part of @queue. The link is
746 * @link_ must be part of @queue,
751 g_queue_unlink (GQueue *queue,
754 g_return_if_fail (queue != NULL);
755 g_return_if_fail (link_ != NULL);
757 if (link_ == queue->tail)
758 queue->tail = queue->tail->prev;
760 queue->head = g_list_remove_link (queue->head, link_);
765 * g_queue_delete_link:
767 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
769 * Removes @link_ from @queue and frees it.
771 * @link_ must be part of @queue.
776 g_queue_delete_link (GQueue *queue,
779 g_return_if_fail (queue != NULL);
780 g_return_if_fail (link_ != NULL);
782 g_queue_unlink (queue, link_);
790 * Returns the first element of the queue.
792 * Returns: the data of the first element in the queue, or %NULL if the queue
796 g_queue_peek_head (GQueue *queue)
798 g_return_val_if_fail (queue != NULL, NULL);
800 return queue->head ? queue->head->data : NULL;
807 * Returns the last element of the queue.
809 * Returns: the data of the last element in the queue, or %NULL if the queue
813 g_queue_peek_tail (GQueue *queue)
815 g_return_val_if_fail (queue != NULL, NULL);
817 return queue->tail ? queue->tail->data : NULL;
823 * @n: the position of the element.
825 * Returns the @n'th element of @queue.
827 * Return value: The data for the @n'th element of @queue, or %NULL if @n is
828 * off the end of @queue.
833 g_queue_peek_nth (GQueue *queue,
838 g_return_val_if_fail (queue != NULL, NULL);
840 link = g_queue_peek_nth_link (queue, n);
851 * @data: the data to find.
853 * Returns the position of the first element in @queue which contains @data.
855 * Return value: The position of the first element in @queue which contains @data, or -1 if no element in @queue contains @data.
860 g_queue_index (GQueue *queue,
863 g_return_val_if_fail (queue != NULL, -1);
865 return g_list_index (queue->head, data);
871 * @data: data to remove.
873 * Removes the first element in @queue that contains @data.
878 g_queue_remove (GQueue *queue,
883 g_return_if_fail (queue != NULL);
885 link = g_list_find (queue->head, data);
888 g_queue_delete_link (queue, link);
892 * g_queue_remove_all:
894 * @data: data to remove
896 * Remove all elemeents in @queue which contains @data.
901 g_queue_remove_all (GQueue *queue,
906 g_return_if_fail (queue != NULL);
911 GList *next = list->next;
913 if (list->data == data)
914 g_queue_delete_link (queue, list);
921 * g_queue_insert_before:
923 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
924 * @data: the data to insert
926 * Inserts @data into @queue before @sibling.
928 * @sibling must be part of @queue.
933 g_queue_insert_before (GQueue *queue,
937 g_return_if_fail (queue != NULL);
938 g_return_if_fail (sibling != NULL);
940 queue->head = g_list_insert_before (queue->head, sibling, data);
945 * g_queue_insert_after:
947 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
948 * @data: the data to insert
950 * Inserts @data into @queue after @sibling
952 * @sibling must be part of @queue
957 g_queue_insert_after (GQueue *queue,
961 g_return_if_fail (queue != NULL);
962 g_return_if_fail (sibling != NULL);
964 if (sibling == queue->tail)
965 g_queue_push_tail (queue, data);
967 g_queue_insert_before (queue, sibling->next, data);
971 * g_queue_insert_sorted:
973 * @data: the data to insert
974 * @func: the #GCompareDataFunc used to compare elements in the queue. It is
975 * called with two elements of the @queue and @user_data. It should
976 * return 0 if the elements are equal, a negative value if the first
977 * element comes before the second, and a positive value if the second
978 * element comes after the first.
979 * @user_data: user data passed to @func.
981 * Inserts @data into @queue using @func to determine the new position.
986 g_queue_insert_sorted (GQueue *queue,
988 GCompareDataFunc func,
993 g_return_if_fail (queue != NULL);
996 while (list && func (list->data, data, user_data) < 0)
1000 g_queue_insert_before (queue, list, data);
1002 g_queue_push_tail (queue, data);
1005 #define __G_QUEUE_C__
1006 #include "galiasdef.c"