2010-04-06 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / eglib / src / gqueue.c
blob2eedbf6f9f3d4973fdff90942610df59b2808e29
1 /*
2 * gqueue.c: Queue
4 * Author:
5 * Duncan Mak (duncan@novell.com)
6 * Gonzalo Paniagua Javier (gonzalo@novell.com)
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * Copyright (c) 2006-2009 Novell, Inc.
31 #include <stdio.h>
32 #include <glib.h>
34 gpointer
35 g_queue_pop_head (GQueue *queue)
37 gpointer result;
38 GList *old_head;
40 if (!queue || queue->length == 0)
41 return NULL;
43 result = queue->head->data;
44 old_head = queue->head;
45 queue->head = old_head->next;
46 g_list_free_1 (old_head);
48 if (--queue->length)
49 queue->head->prev = NULL;
50 else
51 queue->tail = NULL;
53 return result;
56 gboolean
57 g_queue_is_empty (GQueue *queue)
59 if (!queue)
60 return TRUE;
62 return queue->length == 0;
65 void
66 g_queue_push_head (GQueue *queue, gpointer head)
68 if (!queue)
69 return;
71 queue->head = g_list_prepend (queue->head, head);
73 if (!queue->tail)
74 queue->tail = queue->head;
76 queue->length ++;
79 void
80 g_queue_push_tail (GQueue *queue, gpointer data)
82 if (!queue)
83 return;
85 queue->tail = g_list_append (queue->tail, data);
86 if (queue->head == NULL)
87 queue->head = queue->tail;
88 else
89 queue->tail = queue->tail->next;
90 queue->length++;
93 GQueue *
94 g_queue_new (void)
96 return g_new0 (GQueue, 1);
99 void
100 g_queue_free (GQueue *queue)
102 if (!queue)
103 return;
105 g_list_free (queue->head);
106 g_free (queue);