I am the GQ(ueue) man.
[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                 head = queue->head->data;
40                 queue->head = queue->head->next;
41                 queue->length--;                
42         }
43
44         return head;
45 }
46
47 gboolean
48 g_queue_is_empty (GQueue *queue)
49 {
50         if (!queue)
51                 return TRUE;
52         
53         return queue->length == 0;
54 }
55
56 void
57 g_queue_push_head (GQueue *queue, gpointer head)
58 {
59         if (queue)
60                 queue->head = g_list_prepend (queue->head, head);
61         
62         if (!queue->tail)
63                 queue->tail = queue->head;
64
65         queue->length ++;
66 }
67
68 GQueue *
69 g_queue_new (void)
70 {
71         GQueue *queue = g_new0 (GQueue, 1);
72         queue->length = 0;
73
74         return queue;
75 }
76
77 void
78 g_queue_free (GQueue *queue)
79 {
80         if (!queue)
81                 return NULL;
82         
83         g_list_free (queue->head);
84         g_free (queue);
85 }