Handle multiple user names with the same UID better. (#319535, Laszlo
[glib.git] / glib / gqueue.c
blobf381db3803af4f59642a596549c04c16c65d1287
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.
24 * MT safe
27 #include "config.h"
29 #include "glib.h"
30 #include "galias.h"
32 /**
33 * g_queue_new:
35 * Creates a new #GQueue.
37 * Returns: a new #GQueue.
38 **/
39 GQueue*
40 g_queue_new (void)
42 return g_slice_new0 (GQueue);
45 /**
46 * g_queue_free:
47 * @queue: a #GQueue.
49 * Frees the memory allocated for the #GQueue.
50 **/
51 void
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);
60 /**
61 * g_queue_is_empty:
62 * @queue: a #GQueue.
64 * Returns %TRUE if the queue is empty.
66 * Returns: %TRUE if the queue is empty.
67 **/
68 gboolean
69 g_queue_is_empty (GQueue *queue)
71 g_return_val_if_fail (queue != NULL, TRUE);
73 return queue->head == NULL;
76 /**
77 * g_queue_get_length:
78 * @queue: a #GQueue
80 * Returns the number of items in @queue.
82 * Return value: The number of items in @queue.
84 * Since: 2.4
85 **/
86 guint
87 g_queue_get_length (GQueue *queue)
89 g_return_val_if_fail (queue != NULL, 0);
91 return queue->length;
94 /**
95 * g_queue_reverse:
96 * @queue: a #GQueue
98 * Reverses the order of the items in @queue.
100 * Since: 2.4
102 void
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);
112 * g_queue_copy:
113 * @queue: a #GQueue
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
121 * Since: 2.4
123 GQueue *
124 g_queue_copy (GQueue *queue)
126 GQueue *result;
127 GList *list;
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);
136 return result;
140 * g_queue_foreach:
141 * @queue: a #GQueue
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
146 * function.
148 * Since: 2.4
150 void
151 g_queue_foreach (GQueue *queue,
152 GFunc func,
153 gpointer user_data)
155 GList *list;
157 g_return_if_fail (queue != NULL);
158 g_return_if_fail (func != NULL);
160 list = queue->head;
161 while (list)
163 GList *next = list->next;
164 func (list->data, user_data);
165 list = next;
170 * g_queue_find:
171 * @queue: a #GQueue
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.
178 * Since: 2.4
180 GList *
181 g_queue_find (GQueue *queue,
182 gconstpointer data)
184 g_return_val_if_fail (queue != NULL, NULL);
186 return g_list_find (queue->head, data);
190 * g_queue_find_custom:
191 * @queue: a #GQueue
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
204 * Since: 2.4
206 GList *
207 g_queue_find_custom (GQueue *queue,
208 gconstpointer data,
209 GCompareFunc func)
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);
218 * g_queue_sort:
219 * @queue: a #GQueue
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.
228 * Since: 2.4
230 void
231 g_queue_sort (GQueue *queue,
232 GCompareDataFunc compare_func,
233 gpointer user_data)
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);
243 * g_queue_push_head:
244 * @queue: a #GQueue.
245 * @data: the data for the new element.
247 * Adds a new element at the head of the queue.
249 void
250 g_queue_push_head (GQueue *queue,
251 gpointer data)
253 g_return_if_fail (queue != NULL);
255 queue->head = g_list_prepend (queue->head, data);
256 if (!queue->tail)
257 queue->tail = queue->head;
258 queue->length++;
262 * g_queue_push_nth:
263 * @queue: a #GQueue
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
271 * Since: 2.4
273 void
274 g_queue_push_nth (GQueue *queue,
275 gpointer data,
276 gint n)
278 g_return_if_fail (queue != NULL);
280 if (n < 0 || n >= queue->length)
282 g_queue_push_tail (queue, data);
283 return;
286 g_queue_insert_before (queue, g_queue_peek_nth_link (queue, n), data);
290 * g_queue_push_head_link:
291 * @queue: a #GQueue.
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.
297 void
298 g_queue_push_head_link (GQueue *queue,
299 GList *link)
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;
307 if (queue->head)
308 queue->head->prev = link;
309 else
310 queue->tail = link;
311 queue->head = link;
312 queue->length++;
316 * g_queue_push_tail:
317 * @queue: a #GQueue.
318 * @data: the data for the new element.
320 * Adds a new element at the tail of the queue.
322 void
323 g_queue_push_tail (GQueue *queue,
324 gpointer data)
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;
331 else
332 queue->head = queue->tail;
333 queue->length++;
337 * g_queue_push_tail_link:
338 * @queue: a #GQueue.
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.
344 void
345 g_queue_push_tail_link (GQueue *queue,
346 GList *link)
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;
354 if (queue->tail)
355 queue->tail->next = link;
356 else
357 queue->head = link;
358 queue->tail = link;
359 queue->length++;
363 * g_queue_push_nth_link:
364 * @queue: a #GQueue
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
367 * @queue.
368 * @link_: the link to add to @queue
370 * Inserts @link into @queue at the given position.
372 * Since: 2.4
374 void
375 g_queue_push_nth_link (GQueue *queue,
376 gint n,
377 GList *link_)
379 GList *next;
380 GList *prev;
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_);
388 return;
391 g_assert (queue->head);
392 g_assert (queue->tail);
394 next = g_queue_peek_nth_link (queue, n);
395 prev = next->prev;
397 if (prev)
398 prev->next = link_;
399 next->prev = link_;
401 link_->next = next;
402 link_->prev = prev;
404 if (queue->head->prev)
405 queue->head = queue->head->prev;
407 if (queue->tail->next)
408 queue->tail = queue->tail->next;
410 queue->length++;
414 * g_queue_pop_head:
415 * @queue: a #GQueue.
417 * Removes the first element of the queue.
419 * Returns: the data of the first element in the queue, or %NULL if the queue
420 * is empty.
422 gpointer
423 g_queue_pop_head (GQueue *queue)
425 g_return_val_if_fail (queue != NULL, NULL);
427 if (queue->head)
429 GList *node = queue->head;
430 gpointer data = node->data;
432 queue->head = node->next;
433 if (queue->head)
434 queue->head->prev = NULL;
435 else
436 queue->tail = NULL;
437 g_list_free_1 (node);
438 queue->length--;
440 return data;
443 return NULL;
447 * g_queue_pop_head_link:
448 * @queue: a #GQueue.
450 * Removes the first element of the queue.
452 * Returns: the #GList element at the head of the queue, or %NULL if the queue
453 * is empty.
455 GList*
456 g_queue_pop_head_link (GQueue *queue)
458 g_return_val_if_fail (queue != NULL, NULL);
460 if (queue->head)
462 GList *node = queue->head;
464 queue->head = node->next;
465 if (queue->head)
467 queue->head->prev = NULL;
468 node->next = NULL;
470 else
471 queue->tail = NULL;
472 queue->length--;
474 return node;
477 return NULL;
481 * g_queue_peek_head_link:
482 * @queue: a #GQueue
484 * Returns the first link in @queue
486 * Return value: the first link in @queue, or %NULL if @queue is empty
488 * Since: 2.4
490 GList*
491 g_queue_peek_head_link (GQueue *queue)
493 g_return_val_if_fail (queue != NULL, NULL);
495 return queue->head;
499 * g_queue_peek_tail_link:
500 * @queue: a #GQueue
502 * Returns the last link @queue.
504 * Return value: the last link in @queue, or %NULL if @queue is empty
506 * Since: 2.4
508 GList*
509 g_queue_peek_tail_link (GQueue *queue)
511 g_return_val_if_fail (queue != NULL, NULL);
513 return queue->tail;
517 * g_queue_pop_tail:
518 * @queue: a #GQueue.
520 * Removes the last element of the queue.
522 * Returns: the data of the last element in the queue, or %NULL if the queue
523 * is empty.
525 gpointer
526 g_queue_pop_tail (GQueue *queue)
528 g_return_val_if_fail (queue != NULL, NULL);
530 if (queue->tail)
532 GList *node = queue->tail;
533 gpointer data = node->data;
535 queue->tail = node->prev;
536 if (queue->tail)
537 queue->tail->next = NULL;
538 else
539 queue->head = NULL;
540 queue->length--;
541 g_list_free_1 (node);
543 return data;
546 return NULL;
550 * g_queue_pop_nth:
551 * @queue: a #GQueue
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.
558 * Since: 2.4
560 gpointer
561 g_queue_pop_nth (GQueue *queue,
562 guint n)
564 GList *nth_link;
565 gpointer result;
567 g_return_val_if_fail (queue != NULL, NULL);
569 if (n >= queue->length)
570 return NULL;
572 nth_link = g_queue_peek_nth_link (queue, n);
573 result = nth_link->data;
575 g_queue_delete_link (queue, nth_link);
577 return result;
581 * g_queue_pop_tail_link:
582 * @queue: a #GQueue.
584 * Removes the last element of the queue.
586 * Returns: the #GList element at the tail of the queue, or %NULL if the queue
587 * is empty.
589 GList*
590 g_queue_pop_tail_link (GQueue *queue)
592 g_return_val_if_fail (queue != NULL, NULL);
594 if (queue->tail)
596 GList *node = queue->tail;
598 queue->tail = node->prev;
599 if (queue->tail)
601 queue->tail->next = NULL;
602 node->prev = NULL;
604 else
605 queue->head = NULL;
606 queue->length--;
608 return node;
611 return NULL;
615 * g_queue_pop_nth_link:
616 * @queue: a #GQueue
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.
623 * Since: 2.4
625 GList*
626 g_queue_pop_nth_link (GQueue *queue,
627 guint n)
629 GList *link;
631 g_return_val_if_fail (queue != NULL, NULL);
633 if (n >= queue->length)
634 return NULL;
636 link = g_queue_peek_nth_link (queue, n);
637 g_queue_unlink (queue, link);
639 return link;
643 * g_queue_peek_nth_link:
644 * @queue: a #GQueue
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
650 * end of the list
652 * Since: 2.4
654 GList *
655 g_queue_peek_nth_link (GQueue *queue,
656 guint n)
658 GList *link;
659 gint i;
661 g_return_val_if_fail (queue != NULL, NULL);
663 if (n >= queue->length)
664 return NULL;
666 if (n > queue->length / 2)
668 n = queue->length - n - 1;
670 link = queue->tail;
671 for (i = 0; i < n; ++i)
672 link = link->prev;
674 else
676 link = queue->head;
677 for (i = 0; i < n; ++i)
678 link = link->next;
681 return link;
685 * g_queue_link_index:
686 * @queue: a #Gqueue
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
692 * not part of @queue
694 * Since: 2.4
696 gint
697 g_queue_link_index (GQueue *queue,
698 GList *link_)
700 g_return_val_if_fail (queue != NULL, -1);
702 return g_list_position (queue->head, link_);
706 * g_queue_unlink
707 * @queue: a #GQueue
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
711 * not freed.
713 * @link_ must be part of @queue,
715 * Since: 2.4
717 void
718 g_queue_unlink (GQueue *queue,
719 GList *link_)
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_);
728 queue->length--;
732 * g_queue_delete_link:
733 * @queue: a #GQueue
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.
740 * Since: 2.4
742 void
743 g_queue_delete_link (GQueue *queue,
744 GList *link_)
746 g_return_if_fail (queue != NULL);
747 g_return_if_fail (link_ != NULL);
749 g_queue_unlink (queue, link_);
750 g_list_free (link_);
754 * g_queue_peek_head:
755 * @queue: a #GQueue.
757 * Returns the first element of the queue.
759 * Returns: the data of the first element in the queue, or %NULL if the queue
760 * is empty.
762 gpointer
763 g_queue_peek_head (GQueue *queue)
765 g_return_val_if_fail (queue != NULL, NULL);
767 return queue->head ? queue->head->data : NULL;
771 * g_queue_peek_tail:
772 * @queue: a #GQueue.
774 * Returns the last element of the queue.
776 * Returns: the data of the last element in the queue, or %NULL if the queue
777 * is empty.
779 gpointer
780 g_queue_peek_tail (GQueue *queue)
782 g_return_val_if_fail (queue != NULL, NULL);
784 return queue->tail ? queue->tail->data : NULL;
788 * g_queue_peek_nth:
789 * @queue: a #GQueue
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.
797 * Since: 2.4
799 gpointer
800 g_queue_peek_nth (GQueue *queue,
801 guint n)
803 GList *link;
805 g_return_val_if_fail (queue != NULL, NULL);
807 link = g_queue_peek_nth_link (queue, n);
809 if (link)
810 return link->data;
812 return NULL;
816 * g_queue_index:
817 * @queue: a #GQueue
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.
824 * Since: 2.4
826 gint
827 g_queue_index (GQueue *queue,
828 gconstpointer data)
830 g_return_val_if_fail (queue != NULL, -1);
832 return g_list_index (queue->head, data);
836 * g_queue_remove:
837 * @queue: a #GQueue
838 * @data: data to remove.
840 * Removes the first element in @queue that contains @data.
842 * Since: 2.4
844 void
845 g_queue_remove (GQueue *queue,
846 gconstpointer data)
848 GList *link;
850 g_return_if_fail (queue != NULL);
852 link = g_list_find (queue->head, data);
854 if (link)
855 g_queue_delete_link (queue, link);
859 * g_queue_remove_all:
860 * @queue: a #GQueue
861 * @data: data to remove
863 * Remove all elemeents in @queue which contains @data.
865 * Since: 2.4
867 void
868 g_queue_remove_all (GQueue *queue,
869 gconstpointer data)
871 GList *list;
873 g_return_if_fail (queue != NULL);
875 list = queue->head;
876 while (list)
878 GList *next = list->next;
880 if (list->data == data)
881 g_queue_delete_link (queue, list);
883 list = next;
888 * g_queue_insert_before:
889 * @queue: a #GQueue
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.
897 * Since: 2.4
899 void
900 g_queue_insert_before (GQueue *queue,
901 GList *sibling,
902 gpointer data)
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);
908 queue->length++;
912 * g_queue_insert_after:
913 * @queue: a #GQueue
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
921 * Since: 2.4
923 void
924 g_queue_insert_after (GQueue *queue,
925 GList *sibling,
926 gpointer data)
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);
933 else
934 g_queue_insert_before (queue, sibling->next, data);
938 * g_queue_insert_sorted:
939 * @queue: a #GQueue
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.
950 * Since: 2.4
952 void
953 g_queue_insert_sorted (GQueue *queue,
954 gpointer data,
955 GCompareDataFunc func,
956 gpointer user_data)
958 GList *list;
960 g_return_if_fail (queue != NULL);
962 list = queue->head;
963 while (list && func (list->data, data, user_data) < 0)
964 list = list->next;
966 if (list)
967 g_queue_insert_before (queue, list, data);
968 else
969 g_queue_push_tail (queue, data);
972 #define __G_QUEUE_C__
973 #include "galiasdef.c"