summaryrefslogtreecommitdiffstats
path: root/3rdparty/openpgm-svn-r1135/pgm/queue.c
blob: 351c7efcd02a3c90f2fd3768b40ef1b103d1bcf5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
 *
 * portable double-ended queue.
 *
 * Copyright (c) 2010 Miru Limited.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <impl/framework.h>


//#define QUEUE_DEBUG

bool
pgm_queue_is_empty (
	const pgm_queue_t*const queue
	)
{
	pgm_return_val_if_fail (queue != NULL, TRUE);

	return queue->head == NULL;
}

void
pgm_queue_push_head_link (
	pgm_queue_t* restrict queue,
	pgm_list_t*  restrict head_link
	)
{
	pgm_return_if_fail (queue != NULL);
	pgm_return_if_fail (head_link != NULL);
	pgm_return_if_fail (head_link->prev == NULL);
	pgm_return_if_fail (head_link->next == NULL);

	head_link->next = queue->head;
	if (queue->head)
		queue->head->prev = head_link;
	else
		queue->tail = head_link;
	queue->head = head_link;
	queue->length++;
}

pgm_list_t*
pgm_queue_pop_tail_link (
	pgm_queue_t*	queue
	)
{
	pgm_return_val_if_fail (queue != NULL, NULL);

	if (queue->tail)
	{
		pgm_list_t *node = queue->tail;

		queue->tail = node->prev;
		if (queue->tail)
		{
			queue->tail->next = NULL;
			node->prev = NULL;
		}
		else
			queue->head = NULL;
		queue->length--;

		return node;
	}
  
	return NULL;
}

pgm_list_t*
pgm_queue_peek_tail_link (
	pgm_queue_t*	queue
	)
{
	pgm_return_val_if_fail (queue != NULL, NULL);

	return queue->tail;
}

void
pgm_queue_unlink (
	pgm_queue_t* restrict queue,
	pgm_list_t*  restrict target_link
	)
{
	pgm_return_if_fail (queue != NULL);
	pgm_return_if_fail (target_link != NULL);

	if (target_link == queue->tail)
		queue->tail = queue->tail->prev;
  
	queue->head = pgm_list_remove_link (queue->head, target_link);
	queue->length--;
}

/* eof */