2008-06-05 Andreas Faerber <andreas.faerber@web.de>
[mono.git] / eglib / src / gqueue.c
1 /*
2  * gqueue.c: Queue
3  *
4  * Author:
5  *   Duncan Mak (duncan@novell.com)
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  * 
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  * (C) 2006 Novell, Inc.
27  *
28  */
29
30 #include <stdio.h>
31 #include <glib.h>
32
33 gpointer
34 g_queue_pop_head (GQueue *queue)
35 {
36         gpointer head = NULL;
37
38         if (queue->length != 0){
39                 GList *old_head;
40
41                 old_head = queue->head;
42                 head = old_head->data;
43                 queue->head = old_head->next;
44                 queue->length--;
45                 g_list_free_1 (old_head);
46         }
47
48         return head;
49 }
50
51 gboolean
52 g_queue_is_empty (GQueue *queue)
53 {
54         if (!queue)
55                 return TRUE;
56         
57         return queue->length == 0;
58 }
59
60 void
61 g_queue_push_head (GQueue *queue, gpointer head)
62 {
63         if (!queue)
64                 return;
65         
66         queue->head = g_list_prepend (queue->head, head);
67         
68         if (!queue->tail)
69                 queue->tail = queue->head;
70
71         queue->length ++;
72 }
73
74 GQueue *
75 g_queue_new (void)
76 {
77         GQueue *queue = g_new0 (GQueue, 1);
78         queue->length = 0;
79
80         return queue;
81 }
82
83 void
84 g_queue_free (GQueue *queue)
85 {
86         if (!queue)
87                 return;
88         
89         g_list_free (queue->head);
90         g_free (queue);
91 }