unicode: Simplify width table generation
[glib.git] / glib / gqueue.c
blobe93886db0c6ed528f92433d1df53e06888ee2d29
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, see <http://www.gnu.org/licenses/>.
22 * MT safe
25 /**
26 * SECTION:queue
27 * @Title: Double-ended Queues
28 * @Short_description: double-ended queue data structure
30 * The #GQueue structure and its associated functions provide a standard
31 * queue data structure. Internally, GQueue uses the same data structure
32 * as #GList to store elements.
34 * The data contained in each element can be either integer values, by
35 * using one of the [Type Conversion Macros][glib-Type-Conversion-Macros],
36 * or simply pointers to any type of data.
38 * To create a new GQueue, use g_queue_new().
40 * To initialize a statically-allocated GQueue, use #G_QUEUE_INIT or
41 * g_queue_init().
43 * To add elements, use g_queue_push_head(), g_queue_push_head_link(),
44 * g_queue_push_tail() and g_queue_push_tail_link().
46 * To remove elements, use g_queue_pop_head() and g_queue_pop_tail().
48 * To free the entire queue, use g_queue_free().
50 #include "config.h"
52 #include "gqueue.h"
54 #include "gtestutils.h"
55 #include "gslice.h"
57 /**
58 * g_queue_new:
60 * Creates a new #GQueue.
62 * Returns: a newly allocated #GQueue
63 **/
64 GQueue *
65 g_queue_new (void)
67 return g_slice_new0 (GQueue);
70 /**
71 * g_queue_free:
72 * @queue: a #GQueue
74 * Frees the memory allocated for the #GQueue. Only call this function
75 * if @queue was created with g_queue_new(). If queue elements contain
76 * dynamically-allocated memory, they should be freed first.
78 * If queue elements contain dynamically-allocated memory, you should
79 * either use g_queue_free_full() or free them manually first.
80 **/
81 void
82 g_queue_free (GQueue *queue)
84 g_return_if_fail (queue != NULL);
86 g_list_free (queue->head);
87 g_slice_free (GQueue, queue);
90 /**
91 * g_queue_free_full:
92 * @queue: a pointer to a #GQueue
93 * @free_func: the function to be called to free each element's data
95 * Convenience method, which frees all the memory used by a #GQueue,
96 * and calls the specified destroy function on every element's data.
98 * Since: 2.32
100 void
101 g_queue_free_full (GQueue *queue,
102 GDestroyNotify free_func)
104 g_queue_foreach (queue, (GFunc) free_func, NULL);
105 g_queue_free (queue);
109 * g_queue_init:
110 * @queue: an uninitialized #GQueue
112 * A statically-allocated #GQueue must be initialized with this function
113 * before it can be used. Alternatively you can initialize it with
114 * #G_QUEUE_INIT. It is not necessary to initialize queues created with
115 * g_queue_new().
117 * Since: 2.14
119 void
120 g_queue_init (GQueue *queue)
122 g_return_if_fail (queue != NULL);
124 queue->head = queue->tail = NULL;
125 queue->length = 0;
129 * g_queue_clear:
130 * @queue: a #GQueue
132 * Removes all the elements in @queue. If queue elements contain
133 * dynamically-allocated memory, they should be freed first.
135 * Since: 2.14
137 void
138 g_queue_clear (GQueue *queue)
140 g_return_if_fail (queue != NULL);
142 g_list_free (queue->head);
143 g_queue_init (queue);
147 * g_queue_is_empty:
148 * @queue: a #GQueue.
150 * Returns %TRUE if the queue is empty.
152 * Returns: %TRUE if the queue is empty
154 gboolean
155 g_queue_is_empty (GQueue *queue)
157 g_return_val_if_fail (queue != NULL, TRUE);
159 return queue->head == NULL;
163 * g_queue_get_length:
164 * @queue: a #GQueue
166 * Returns the number of items in @queue.
168 * Returns: the number of items in @queue
170 * Since: 2.4
172 guint
173 g_queue_get_length (GQueue *queue)
175 g_return_val_if_fail (queue != NULL, 0);
177 return queue->length;
181 * g_queue_reverse:
182 * @queue: a #GQueue
184 * Reverses the order of the items in @queue.
186 * Since: 2.4
188 void
189 g_queue_reverse (GQueue *queue)
191 g_return_if_fail (queue != NULL);
193 queue->tail = queue->head;
194 queue->head = g_list_reverse (queue->head);
198 * g_queue_copy:
199 * @queue: a #GQueue
201 * Copies a @queue. Note that is a shallow copy. If the elements in the
202 * queue consist of pointers to data, the pointers are copied, but the
203 * actual data is not.
205 * Returns: a copy of @queue
207 * Since: 2.4
209 GQueue *
210 g_queue_copy (GQueue *queue)
212 GQueue *result;
213 GList *list;
215 g_return_val_if_fail (queue != NULL, NULL);
217 result = g_queue_new ();
219 for (list = queue->head; list != NULL; list = list->next)
220 g_queue_push_tail (result, list->data);
222 return result;
226 * g_queue_foreach:
227 * @queue: a #GQueue
228 * @func: the function to call for each element's data
229 * @user_data: user data to pass to @func
231 * Calls @func for each element in the queue passing @user_data to the
232 * function.
234 * Since: 2.4
236 void
237 g_queue_foreach (GQueue *queue,
238 GFunc func,
239 gpointer user_data)
241 GList *list;
243 g_return_if_fail (queue != NULL);
244 g_return_if_fail (func != NULL);
246 list = queue->head;
247 while (list)
249 GList *next = list->next;
250 func (list->data, user_data);
251 list = next;
256 * g_queue_find:
257 * @queue: a #GQueue
258 * @data: data to find
260 * Finds the first link in @queue which contains @data.
262 * Returns: the first link in @queue which contains @data
264 * Since: 2.4
266 GList *
267 g_queue_find (GQueue *queue,
268 gconstpointer data)
270 g_return_val_if_fail (queue != NULL, NULL);
272 return g_list_find (queue->head, data);
276 * g_queue_find_custom:
277 * @queue: a #GQueue
278 * @data: user data passed to @func
279 * @func: a #GCompareFunc to call for each element. It should return 0
280 * when the desired element is found
282 * Finds an element in a #GQueue, using a supplied function to find the
283 * desired element. It iterates over the queue, calling the given function
284 * which should return 0 when the desired element is found. The function
285 * takes two gconstpointer arguments, the #GQueue element's data as the
286 * first argument and the given user data as the second argument.
288 * Returns: the found link, or %NULL if it wasn't found
290 * Since: 2.4
292 GList *
293 g_queue_find_custom (GQueue *queue,
294 gconstpointer data,
295 GCompareFunc func)
297 g_return_val_if_fail (queue != NULL, NULL);
298 g_return_val_if_fail (func != NULL, NULL);
300 return g_list_find_custom (queue->head, data, func);
304 * g_queue_sort:
305 * @queue: a #GQueue
306 * @compare_func: the #GCompareDataFunc used to sort @queue. This function
307 * is passed two elements of the queue and should return 0 if they are
308 * equal, a negative value if the first comes before the second, and
309 * a positive value if the second comes before the first.
310 * @user_data: user data passed to @compare_func
312 * Sorts @queue using @compare_func.
314 * Since: 2.4
316 void
317 g_queue_sort (GQueue *queue,
318 GCompareDataFunc compare_func,
319 gpointer user_data)
321 g_return_if_fail (queue != NULL);
322 g_return_if_fail (compare_func != NULL);
324 queue->head = g_list_sort_with_data (queue->head, compare_func, user_data);
325 queue->tail = g_list_last (queue->head);
329 * g_queue_push_head:
330 * @queue: a #GQueue.
331 * @data: the data for the new element.
333 * Adds a new element at the head of the queue.
335 void
336 g_queue_push_head (GQueue *queue,
337 gpointer data)
339 g_return_if_fail (queue != NULL);
341 queue->head = g_list_prepend (queue->head, data);
342 if (!queue->tail)
343 queue->tail = queue->head;
344 queue->length++;
348 * g_queue_push_nth:
349 * @queue: a #GQueue
350 * @data: the data for the new element
351 * @n: the position to insert the new element. If @n is negative or
352 * larger than the number of elements in the @queue, the element is
353 * added to the end of the queue.
355 * Inserts a new element into @queue at the given position.
357 * Since: 2.4
359 void
360 g_queue_push_nth (GQueue *queue,
361 gpointer data,
362 gint n)
364 g_return_if_fail (queue != NULL);
366 if (n < 0 || n >= queue->length)
368 g_queue_push_tail (queue, data);
369 return;
372 g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
376 * g_queue_push_head_link:
377 * @queue: a #GQueue
378 * @link_: a single #GList element, not a list with more than one element
380 * Adds a new element at the head of the queue.
382 void
383 g_queue_push_head_link (GQueue *queue,
384 GList *link)
386 g_return_if_fail (queue != NULL);
387 g_return_if_fail (link != NULL);
388 g_return_if_fail (link->prev == NULL);
389 g_return_if_fail (link->next == NULL);
391 link->next = queue->head;
392 if (queue->head)
393 queue->head->prev = link;
394 else
395 queue->tail = link;
396 queue->head = link;
397 queue->length++;
401 * g_queue_push_tail:
402 * @queue: a #GQueue
403 * @data: the data for the new element
405 * Adds a new element at the tail of the queue.
407 void
408 g_queue_push_tail (GQueue *queue,
409 gpointer data)
411 g_return_if_fail (queue != NULL);
413 queue->tail = g_list_append (queue->tail, data);
414 if (queue->tail->next)
415 queue->tail = queue->tail->next;
416 else
417 queue->head = queue->tail;
418 queue->length++;
422 * g_queue_push_tail_link:
423 * @queue: a #GQueue
424 * @link_: a single #GList element, not a list with more than one element
426 * Adds a new element at the tail of the queue.
428 void
429 g_queue_push_tail_link (GQueue *queue,
430 GList *link)
432 g_return_if_fail (queue != NULL);
433 g_return_if_fail (link != NULL);
434 g_return_if_fail (link->prev == NULL);
435 g_return_if_fail (link->next == NULL);
437 link->prev = queue->tail;
438 if (queue->tail)
439 queue->tail->next = link;
440 else
441 queue->head = link;
442 queue->tail = link;
443 queue->length++;
447 * g_queue_push_nth_link:
448 * @queue: a #GQueue
449 * @n: the position to insert the link. If this is negative or larger than
450 * the number of elements in @queue, the link is added to the end of
451 * @queue.
452 * @link_: the link to add to @queue
454 * Inserts @link into @queue at the given position.
456 * Since: 2.4
458 void
459 g_queue_push_nth_link (GQueue *queue,
460 gint n,
461 GList *link_)
463 GList *next;
464 GList *prev;
466 g_return_if_fail (queue != NULL);
467 g_return_if_fail (link_ != NULL);
469 if (n < 0 || n >= queue->length)
471 g_queue_push_tail_link (queue, link_);
472 return;
475 g_assert (queue->head);
476 g_assert (queue->tail);
478 next = g_queue_peek_nth_link (queue, n);
479 prev = next->prev;
481 if (prev)
482 prev->next = link_;
483 next->prev = link_;
485 link_->next = next;
486 link_->prev = prev;
488 if (queue->head->prev)
489 queue->head = queue->head->prev;
491 if (queue->tail->next)
492 queue->tail = queue->tail->next;
494 queue->length++;
498 * g_queue_pop_head:
499 * @queue: a #GQueue
501 * Removes the first element of the queue and returns its data.
503 * Returns: the data of the first element in the queue, or %NULL
504 * if the queue is empty
506 gpointer
507 g_queue_pop_head (GQueue *queue)
509 g_return_val_if_fail (queue != NULL, NULL);
511 if (queue->head)
513 GList *node = queue->head;
514 gpointer data = node->data;
516 queue->head = node->next;
517 if (queue->head)
518 queue->head->prev = NULL;
519 else
520 queue->tail = NULL;
521 g_list_free_1 (node);
522 queue->length--;
524 return data;
527 return NULL;
531 * g_queue_pop_head_link:
532 * @queue: a #GQueue
534 * Removes and returns the first element of the queue.
536 * Returns: the #GList element at the head of the queue, or %NULL
537 * if the queue is empty
539 GList *
540 g_queue_pop_head_link (GQueue *queue)
542 g_return_val_if_fail (queue != NULL, NULL);
544 if (queue->head)
546 GList *node = queue->head;
548 queue->head = node->next;
549 if (queue->head)
551 queue->head->prev = NULL;
552 node->next = NULL;
554 else
555 queue->tail = NULL;
556 queue->length--;
558 return node;
561 return NULL;
565 * g_queue_peek_head_link:
566 * @queue: a #GQueue
568 * Returns the first link in @queue.
570 * Returns: the first link in @queue, or %NULL if @queue is empty
572 * Since: 2.4
574 GList *
575 g_queue_peek_head_link (GQueue *queue)
577 g_return_val_if_fail (queue != NULL, NULL);
579 return queue->head;
583 * g_queue_peek_tail_link:
584 * @queue: a #GQueue
586 * Returns the last link in @queue.
588 * Returns: the last link in @queue, or %NULL if @queue is empty
590 * Since: 2.4
592 GList *
593 g_queue_peek_tail_link (GQueue *queue)
595 g_return_val_if_fail (queue != NULL, NULL);
597 return queue->tail;
601 * g_queue_pop_tail:
602 * @queue: a #GQueue
604 * Removes the last element of the queue and returns its data.
606 * Returns: the data of the last element in the queue, or %NULL
607 * if the queue is empty
609 gpointer
610 g_queue_pop_tail (GQueue *queue)
612 g_return_val_if_fail (queue != NULL, NULL);
614 if (queue->tail)
616 GList *node = queue->tail;
617 gpointer data = node->data;
619 queue->tail = node->prev;
620 if (queue->tail)
621 queue->tail->next = NULL;
622 else
623 queue->head = NULL;
624 queue->length--;
625 g_list_free_1 (node);
627 return data;
630 return NULL;
634 * g_queue_pop_nth:
635 * @queue: a #GQueue
636 * @n: the position of the element
638 * Removes the @n'th element of @queue and returns its data.
640 * Returns: the element's data, or %NULL if @n is off the end of @queue
642 * Since: 2.4
644 gpointer
645 g_queue_pop_nth (GQueue *queue,
646 guint n)
648 GList *nth_link;
649 gpointer result;
651 g_return_val_if_fail (queue != NULL, NULL);
653 if (n >= queue->length)
654 return NULL;
656 nth_link = g_queue_peek_nth_link (queue, n);
657 result = nth_link->data;
659 g_queue_delete_link (queue, nth_link);
661 return result;
665 * g_queue_pop_tail_link:
666 * @queue: a #GQueue
668 * Removes and returns the last element of the queue.
670 * Returns: the #GList element at the tail of the queue, or %NULL
671 * if the queue is empty
673 GList *
674 g_queue_pop_tail_link (GQueue *queue)
676 g_return_val_if_fail (queue != NULL, NULL);
678 if (queue->tail)
680 GList *node = queue->tail;
682 queue->tail = node->prev;
683 if (queue->tail)
685 queue->tail->next = NULL;
686 node->prev = NULL;
688 else
689 queue->head = NULL;
690 queue->length--;
692 return node;
695 return NULL;
699 * g_queue_pop_nth_link:
700 * @queue: a #GQueue
701 * @n: the link's position
703 * Removes and returns the link at the given position.
705 * Returns: the @n'th link, or %NULL if @n is off the end of @queue
707 * Since: 2.4
709 GList*
710 g_queue_pop_nth_link (GQueue *queue,
711 guint n)
713 GList *link;
715 g_return_val_if_fail (queue != NULL, NULL);
717 if (n >= queue->length)
718 return NULL;
720 link = g_queue_peek_nth_link (queue, n);
721 g_queue_unlink (queue, link);
723 return link;
727 * g_queue_peek_nth_link:
728 * @queue: a #GQueue
729 * @n: the position of the link
731 * Returns the link at the given position
733 * Returns: the link at the @n'th position, or %NULL
734 * if @n is off the end of the list
736 * Since: 2.4
738 GList *
739 g_queue_peek_nth_link (GQueue *queue,
740 guint n)
742 GList *link;
743 gint i;
745 g_return_val_if_fail (queue != NULL, NULL);
747 if (n >= queue->length)
748 return NULL;
750 if (n > queue->length / 2)
752 n = queue->length - n - 1;
754 link = queue->tail;
755 for (i = 0; i < n; ++i)
756 link = link->prev;
758 else
760 link = queue->head;
761 for (i = 0; i < n; ++i)
762 link = link->next;
765 return link;
769 * g_queue_link_index:
770 * @queue: a #GQueue
771 * @link_: a #GList link
773 * Returns the position of @link_ in @queue.
775 * Returns: the position of @link_, or -1 if the link is
776 * not part of @queue
778 * Since: 2.4
780 gint
781 g_queue_link_index (GQueue *queue,
782 GList *link_)
784 g_return_val_if_fail (queue != NULL, -1);
786 return g_list_position (queue->head, link_);
790 * g_queue_unlink:
791 * @queue: a #GQueue
792 * @link_: a #GList link that must be part of @queue
794 * Unlinks @link_ so that it will no longer be part of @queue.
795 * The link is not freed.
797 * @link_ must be part of @queue.
799 * Since: 2.4
801 void
802 g_queue_unlink (GQueue *queue,
803 GList *link_)
805 g_return_if_fail (queue != NULL);
806 g_return_if_fail (link_ != NULL);
808 if (link_ == queue->tail)
809 queue->tail = queue->tail->prev;
811 queue->head = g_list_remove_link (queue->head, link_);
812 queue->length--;
816 * g_queue_delete_link:
817 * @queue: a #GQueue
818 * @link_: a #GList link that must be part of @queue
820 * Removes @link_ from @queue and frees it.
822 * @link_ must be part of @queue.
824 * Since: 2.4
826 void
827 g_queue_delete_link (GQueue *queue,
828 GList *link_)
830 g_return_if_fail (queue != NULL);
831 g_return_if_fail (link_ != NULL);
833 g_queue_unlink (queue, link_);
834 g_list_free (link_);
838 * g_queue_peek_head:
839 * @queue: a #GQueue
841 * Returns the first element of the queue.
843 * Returns: the data of the first element in the queue, or %NULL
844 * if the queue is empty
846 gpointer
847 g_queue_peek_head (GQueue *queue)
849 g_return_val_if_fail (queue != NULL, NULL);
851 return queue->head ? queue->head->data : NULL;
855 * g_queue_peek_tail:
856 * @queue: a #GQueue
858 * Returns the last element of the queue.
860 * Returns: the data of the last element in the queue, or %NULL
861 * if the queue is empty
863 gpointer
864 g_queue_peek_tail (GQueue *queue)
866 g_return_val_if_fail (queue != NULL, NULL);
868 return queue->tail ? queue->tail->data : NULL;
872 * g_queue_peek_nth:
873 * @queue: a #GQueue
874 * @n: the position of the element
876 * Returns the @n'th element of @queue.
878 * Returns: the data for the @n'th element of @queue,
879 * or %NULL if @n is off the end of @queue
881 * Since: 2.4
883 gpointer
884 g_queue_peek_nth (GQueue *queue,
885 guint n)
887 GList *link;
889 g_return_val_if_fail (queue != NULL, NULL);
891 link = g_queue_peek_nth_link (queue, n);
893 if (link)
894 return link->data;
896 return NULL;
900 * g_queue_index:
901 * @queue: a #GQueue
902 * @data: the data to find
904 * Returns the position of the first element in @queue which contains @data.
906 * Returns: the position of the first element in @queue which
907 * contains @data, or -1 if no element in @queue contains @data
909 * Since: 2.4
911 gint
912 g_queue_index (GQueue *queue,
913 gconstpointer data)
915 g_return_val_if_fail (queue != NULL, -1);
917 return g_list_index (queue->head, data);
921 * g_queue_remove:
922 * @queue: a #GQueue
923 * @data: the data to remove
925 * Removes the first element in @queue that contains @data.
927 * Returns: %TRUE if @data was found and removed from @queue
929 * Since: 2.4
931 gboolean
932 g_queue_remove (GQueue *queue,
933 gconstpointer data)
935 GList *link;
937 g_return_val_if_fail (queue != NULL, FALSE);
939 link = g_list_find (queue->head, data);
941 if (link)
942 g_queue_delete_link (queue, link);
944 return (link != NULL);
948 * g_queue_remove_all:
949 * @queue: a #GQueue
950 * @data: the data to remove
952 * Remove all elements whose data equals @data from @queue.
954 * Returns: the number of elements removed from @queue
956 * Since: 2.4
958 guint
959 g_queue_remove_all (GQueue *queue,
960 gconstpointer data)
962 GList *list;
963 guint old_length;
965 g_return_val_if_fail (queue != NULL, 0);
967 old_length = queue->length;
969 list = queue->head;
970 while (list)
972 GList *next = list->next;
974 if (list->data == data)
975 g_queue_delete_link (queue, list);
977 list = next;
980 return (old_length - queue->length);
984 * g_queue_insert_before:
985 * @queue: a #GQueue
986 * @sibling: a #GList link that must be part of @queue
987 * @data: the data to insert
989 * Inserts @data into @queue before @sibling.
991 * @sibling must be part of @queue.
993 * Since: 2.4
995 void
996 g_queue_insert_before (GQueue *queue,
997 GList *sibling,
998 gpointer data)
1000 g_return_if_fail (queue != NULL);
1001 g_return_if_fail (sibling != NULL);
1003 queue->head = g_list_insert_before (queue->head, sibling, data);
1004 queue->length++;
1008 * g_queue_insert_after:
1009 * @queue: a #GQueue
1010 * @sibling: a #GList link that must be part of @queue
1011 * @data: the data to insert
1013 * Inserts @data into @queue after @sibling
1015 * @sibling must be part of @queue
1017 * Since: 2.4
1019 void
1020 g_queue_insert_after (GQueue *queue,
1021 GList *sibling,
1022 gpointer data)
1024 g_return_if_fail (queue != NULL);
1025 g_return_if_fail (sibling != NULL);
1027 if (sibling == queue->tail)
1028 g_queue_push_tail (queue, data);
1029 else
1030 g_queue_insert_before (queue, sibling->next, data);
1034 * g_queue_insert_sorted:
1035 * @queue: a #GQueue
1036 * @data: the data to insert
1037 * @func: the #GCompareDataFunc used to compare elements in the queue. It is
1038 * called with two elements of the @queue and @user_data. It should
1039 * return 0 if the elements are equal, a negative value if the first
1040 * element comes before the second, and a positive value if the second
1041 * element comes before the first.
1042 * @user_data: user data passed to @func
1044 * Inserts @data into @queue using @func to determine the new position.
1046 * Since: 2.4
1048 void
1049 g_queue_insert_sorted (GQueue *queue,
1050 gpointer data,
1051 GCompareDataFunc func,
1052 gpointer user_data)
1054 GList *list;
1056 g_return_if_fail (queue != NULL);
1058 list = queue->head;
1059 while (list && func (list->data, data, user_data) < 0)
1060 list = list->next;
1062 if (list)
1063 g_queue_insert_before (queue, list, data);
1064 else
1065 g_queue_push_tail (queue, data);