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.
32 G_LOCK_DEFINE_STATIC (queue_memchunk);
33 static GMemChunk *queue_memchunk = NULL;
34 static GTrashStack *free_queue_nodes = NULL;
39 * Creates a new #GQueue.
41 * Returns: a new #GQueue.
48 G_LOCK (queue_memchunk);
49 queue = g_trash_stack_pop (&free_queue_nodes);
54 queue_memchunk = g_mem_chunk_new ("GLib GQueue chunk",
58 queue = g_chunk_new (GQueue, queue_memchunk);
60 G_UNLOCK (queue_memchunk);
73 * Frees the memory allocated for the #GQueue.
76 g_queue_free (GQueue *queue)
78 g_return_if_fail (queue != NULL);
80 g_list_free (queue->head);
82 #ifdef ENABLE_GC_FRIENDLY
85 #endif /* ENABLE_GC_FRIENDLY */
87 G_LOCK (queue_memchunk);
88 g_trash_stack_push (&free_queue_nodes, queue);
89 G_UNLOCK (queue_memchunk);
96 * Returns %TRUE if the queue is empty.
98 * Returns: %TRUE if the queue is empty.
101 g_queue_is_empty (GQueue *queue)
103 g_return_val_if_fail (queue != NULL, TRUE);
105 return queue->head == NULL;
109 * g_queue_get_length:
112 * Returns the number of items in @queue.
114 * Return value: The number of items in @queue.
119 g_queue_get_length (GQueue *queue)
121 g_return_val_if_fail (queue != NULL, 0);
123 return queue->length;
130 * Reverses the order of the items in @queue.
135 g_queue_reverse (GQueue *queue)
137 g_return_if_fail (queue != NULL);
139 queue->tail = queue->head;
140 queue->head = g_list_reverse (queue->head);
147 * Copies a @queue. Note that is a shallow copy. If the elements in the
148 * queue consist of pointers to data, the pointers are copied, but the
149 * actual data is not.
151 * Return value: A copy of @queue
156 g_queue_copy (GQueue *queue)
161 g_return_val_if_fail (queue != NULL, NULL);
163 result = g_queue_new ();
165 for (list = queue->head; list != NULL; list = list->next)
166 g_queue_push_tail (result, list->data);
174 * @func: the function to call for each element's data
175 * @user_data: user data to pass to @func
177 * Calls @func for each element in the queue passing @user_data to the
183 g_queue_foreach (GQueue *queue,
189 g_return_if_fail (queue != NULL);
190 g_return_if_fail (func != NULL);
195 GList *next = list->next;
196 func (list->data, user_data);
204 * @data: data to find
206 * Finds the first link in @queue which contains @data.
208 * Return value: The first link in @queue which contains @data.
213 g_queue_find (GQueue *queue,
216 g_return_val_if_fail (queue != NULL, NULL);
218 return g_list_find (queue->head, data);
222 * g_queue_find_custom:
224 * @data: user data passed to @func
225 * @func: a #GCompareFunc to call for each element. It should return 0
226 * when the desired element is found
228 * Finds an element in a #GQueue, using a supplied function to find the
229 * desired element. It iterates over the queue, calling the given function
230 * which should return 0 when the desired element is found. The function
231 * takes two gconstpointer arguments, the #GQueue element's data and the
234 * Return value: The found link, or %NULL if it wasn't found
239 g_queue_find_custom (GQueue *queue,
243 g_return_val_if_fail (queue != NULL, NULL);
244 g_return_val_if_fail (func != NULL, NULL);
246 return g_list_find_custom (queue->head, data, func);
252 * @compare_func: the #GCompareDataFunc used to sort @queue. This function
253 * is passed two elements of the queue and should return 0 if they are
254 * equal, a negative value if the first comes before the second, and
255 * a positive value if the second comes before the first.
256 * @user_data: user data passed to @compare_func
258 * Sorts @queue using @compare_func.
263 g_queue_sort (GQueue *queue,
264 GCompareDataFunc compare_func,
267 g_return_if_fail (queue != NULL);
268 g_return_if_fail (compare_func != NULL);
270 queue->head = g_list_sort_with_data (queue->head, compare_func, user_data);
271 queue->tail = g_list_last (queue->head);
277 * @data: the data for the new element.
279 * Adds a new element at the head of the queue.
282 g_queue_push_head (GQueue *queue,
285 g_return_if_fail (queue != NULL);
287 queue->head = g_list_prepend (queue->head, data);
289 queue->tail = queue->head;
296 * @data: the data for the new element
297 * @n: the position to insert the new element. If @n is negative or
298 * larger than the number of elements in the @queue, the element is
299 * added to the end of the queue.
301 * Inserts a new element into @queue at the given position
306 g_queue_push_nth (GQueue *queue,
310 g_return_if_fail (queue != NULL);
312 if (n < 0 || n >= queue->length)
314 g_queue_push_tail (queue, data);
318 g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
322 * g_queue_push_head_link:
324 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
325 * more than one element.
327 * Adds a new element at the head of the queue.
330 g_queue_push_head_link (GQueue *queue,
333 g_return_if_fail (queue != NULL);
334 g_return_if_fail (link != NULL);
335 g_return_if_fail (link->prev == NULL);
336 g_return_if_fail (link->next == NULL);
338 link->next = queue->head;
340 queue->head->prev = link;
350 * @data: the data for the new element.
352 * Adds a new element at the tail of the queue.
355 g_queue_push_tail (GQueue *queue,
358 g_return_if_fail (queue != NULL);
360 queue->tail = g_list_append (queue->tail, data);
361 if (queue->tail->next)
362 queue->tail = queue->tail->next;
364 queue->head = queue->tail;
369 * g_queue_push_tail_link:
371 * @link_: a single #GList element, <emphasis>not</emphasis> a list with
372 * more than one element.
374 * Adds a new element at the tail of the queue.
377 g_queue_push_tail_link (GQueue *queue,
380 g_return_if_fail (queue != NULL);
381 g_return_if_fail (link != NULL);
382 g_return_if_fail (link->prev == NULL);
383 g_return_if_fail (link->next == NULL);
385 link->prev = queue->tail;
387 queue->tail->next = link;
395 * g_queue_push_nth_link:
397 * @n: the position to insert the link. If this is negative or larger than
398 * the number of elements in @queue, the link is added to the end of
400 * @link_: the link to add to @queue
402 * Inserts @link into @queue at the given position.
407 g_queue_push_nth_link (GQueue *queue,
414 g_return_if_fail (queue != NULL);
415 g_return_if_fail (link_ != NULL);
417 if (n < 0 || n >= queue->length)
419 g_queue_push_tail_link (queue, link_);
423 g_assert (queue->head);
424 g_assert (queue->tail);
426 next = g_queue_peek_nth_link (queue, n);
436 if (queue->head->prev)
437 queue->head = queue->head->prev;
439 if (queue->tail->next)
440 queue->tail = queue->tail->next;
449 * Removes the first element of the queue.
451 * Returns: the data of the first element in the queue, or %NULL if the queue
455 g_queue_pop_head (GQueue *queue)
457 g_return_val_if_fail (queue != NULL, NULL);
461 GList *node = queue->head;
462 gpointer data = node->data;
464 queue->head = node->next;
466 queue->head->prev = NULL;
469 g_list_free_1 (node);
479 * g_queue_pop_head_link:
482 * Removes the first element of the queue.
484 * Returns: the #GList element at the head of the queue, or %NULL if the queue
488 g_queue_pop_head_link (GQueue *queue)
490 g_return_val_if_fail (queue != NULL, NULL);
494 GList *node = queue->head;
496 queue->head = node->next;
499 queue->head->prev = NULL;
513 * g_queue_peek_head_link:
516 * Returns the first link in @queue
518 * Return value: the first link in @queue, or %NULL if @queue is empty
523 g_queue_peek_head_link (GQueue *queue)
525 g_return_val_if_fail (queue != NULL, NULL);
531 * g_queue_peek_tail_link:
534 * Returns the last link @queue.
536 * Return value: the last link in @queue, or %NULL if @queue is empty
541 g_queue_peek_tail_link (GQueue *queue)
543 g_return_val_if_fail (queue != NULL, NULL);
552 * Removes the last element of the queue.
554 * Returns: the data of the last element in the queue, or %NULL if the queue
558 g_queue_pop_tail (GQueue *queue)
560 g_return_val_if_fail (queue != NULL, NULL);
564 GList *node = queue->tail;
565 gpointer data = node->data;
567 queue->tail = node->prev;
569 queue->tail->next = NULL;
573 g_list_free_1 (node);
584 * @n: the position of the element.
586 * Removes the @n'th element of @queue.
588 * Return value: the element's data, or %NULL if @n is off the end of @queue.
593 g_queue_pop_nth (GQueue *queue,
599 g_return_val_if_fail (queue != NULL, NULL);
601 if (n >= queue->length)
604 nth_link = g_queue_peek_nth_link (queue, n);
605 result = nth_link->data;
607 g_queue_delete_link (queue, nth_link);
613 * g_queue_pop_tail_link:
616 * Removes the last element of the queue.
618 * Returns: the #GList element at the tail of the queue, or %NULL if the queue
622 g_queue_pop_tail_link (GQueue *queue)
624 g_return_val_if_fail (queue != NULL, NULL);
628 GList *node = queue->tail;
630 queue->tail = node->prev;
633 queue->tail->next = NULL;
647 * g_queue_pop_nth_link:
649 * @n: the link's position
651 * Removes and returns the link at the given position.
653 * Return value: The @n'th link, or %NULL if @n is off the end of @queue.
658 g_queue_pop_nth_link (GQueue *queue,
663 g_return_val_if_fail (queue != NULL, NULL);
665 if (n > queue->length)
668 link = g_queue_peek_nth_link (queue, n);
669 g_queue_unlink (queue, link);
675 * g_queue_peek_nth_link:
677 * @n: the position of the link
679 * Returns the link at the given position
681 * Return value: The link at the @n'th position, or %NULL if @n is off the
687 g_queue_peek_nth_link (GQueue *queue,
693 g_return_val_if_fail (queue != NULL, NULL);
695 if (n >= queue->length)
698 if (n > queue->length / 2)
700 n = queue->length - n - 1;
703 for (i = 0; i < n; ++i)
709 for (i = 0; i < n; ++i)
717 * g_queue_link_index:
719 * @link_: A #GList link
721 * Returns the position of @link_ in @queue.
723 * Return value: The position of @link_, or -1 if the link is
729 g_queue_link_index (GQueue *queue,
732 g_return_val_if_fail (queue != NULL, 0);
734 return g_list_position (queue->head, link_);
740 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
742 * Unlinks @link_ so that it will no longer be part of @queue. The link is
745 * @link_ must be part of @queue,
750 g_queue_unlink (GQueue *queue,
753 g_return_if_fail (queue != NULL);
754 g_return_if_fail (link_ != NULL);
756 if (link_ == queue->tail)
757 queue->tail = queue->tail->prev;
759 queue->head = g_list_remove_link (queue->head, link_);
764 * g_queue_delete_link:
766 * @link_: a #GList link that <emphasis>must</emphasis> be part of @queue
768 * Removes @link_ from @queue and frees it.
770 * @link_ must be part of @queue.
775 g_queue_delete_link (GQueue *queue,
778 g_return_if_fail (queue != NULL);
779 g_return_if_fail (link_ != NULL);
781 g_queue_unlink (queue, link_);
789 * Returns the first element of the queue.
791 * Returns: the data of the first element in the queue, or %NULL if the queue
795 g_queue_peek_head (GQueue *queue)
797 g_return_val_if_fail (queue != NULL, NULL);
799 return queue->head ? queue->head->data : NULL;
806 * Returns the last element of the queue.
808 * Returns: the data of the last element in the queue, or %NULL if the queue
812 g_queue_peek_tail (GQueue *queue)
814 g_return_val_if_fail (queue != NULL, NULL);
816 return queue->tail ? queue->tail->data : NULL;
822 * @n: the position of the element.
824 * Returns the @n'th element of @queue.
826 * Return value: The data for the @n'th element of @queue, or %NULL if @n is
827 * off the end of @queue.
832 g_queue_peek_nth (GQueue *queue,
837 g_return_val_if_fail (queue != NULL, NULL);
839 link = g_queue_peek_nth_link (queue, n);
850 * @data: the data to find.
852 * Returns the position of the first element in @queue which contains @data.
854 * Return value: The position of the first element in @queue which contains @data, or -1 if no element in @queue contains @data.
859 g_queue_index (GQueue *queue,
862 g_return_val_if_fail (queue != NULL, -1);
864 return g_list_index (queue->head, data);
870 * @data: data to remove.
872 * Removes the first element in @queue that contains @data.
877 g_queue_remove (GQueue *queue,
882 g_return_if_fail (queue != NULL);
884 link = g_list_find (queue->head, data);
887 g_queue_delete_link (queue, link);
891 * g_queue_remove_all:
893 * @data: data to remove
895 * Remove all elemeents in @queue which contains @data.
900 g_queue_remove_all (GQueue *queue,
905 g_return_if_fail (queue != NULL);
910 GList *next = list->next;
912 if (list->data == data)
913 g_queue_delete_link (queue, list);
920 * g_queue_insert_before:
922 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
923 * @data: the data to insert
925 * Inserts @data into @queue before @sibling.
927 * @sibling must be part of @queue.
932 g_queue_insert_before (GQueue *queue,
936 g_return_if_fail (queue != NULL);
937 g_return_if_fail (sibling != NULL);
939 queue->head = g_list_insert_before (queue->head, sibling, data);
944 * g_queue_insert_after:
946 * @sibling: a #GList link that <emphasis>must</emphasis> be part of @queue
947 * @data: the data to insert
949 * Inserts @data into @queue after @sibling
951 * @sibling must be part of @queue
956 g_queue_insert_after (GQueue *queue,
960 g_return_if_fail (queue != NULL);
961 g_return_if_fail (sibling != NULL);
963 if (sibling == queue->tail)
964 g_queue_push_tail (queue, data);
966 g_queue_insert_before (queue, sibling->next, data);
970 * g_queue_insert_sorted:
972 * @data: the data to insert
973 * @func: the #GCompareDataFunc used to compare elements in the queue. It is
974 * called with two elements of the @queue and @user_data. It should
975 * return 0 if the elements are equal, a negative value if the first
976 * element comes before the second, and a positive value if the second
977 * element comes after the first.
978 * @user_data: user data passed to @func.
980 * Inserts @data into @queue using @func to determine the new position.
985 g_queue_insert_sorted (GQueue *queue,
987 GCompareDataFunc func,
992 g_return_if_fail (queue != NULL);
995 while (list && func (list->data, data, user_data) < 0)
999 g_queue_insert_before (queue, list, data);
1001 g_queue_push_tail (queue, data);