~ubuntu-branches/ubuntu/precise/libpgm/precise

« back to all changes in this revision

Viewing changes to openpgm/pgm/queue.c

  • Committer: Bazaar Package Importer
  • Author(s): Gabriel de Perthuis
  • Date: 2011-04-07 16:48:52 UTC
  • Revision ID: james.westby@ubuntu.com-20110407164852-8uamem42ojeptj6l
Tags: upstream-5.1.116~dfsg
ImportĀ upstreamĀ versionĀ 5.1.116~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
 
2
 *
 
3
 * portable double-ended queue.
 
4
 *
 
5
 * Copyright (c) 2010-2011 Miru Limited.
 
6
 *
 
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.1 of the License, or (at your option) any later version.
 
11
 *
 
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.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
20
 */
 
21
 
 
22
#include <impl/framework.h>
 
23
 
 
24
 
 
25
//#define QUEUE_DEBUG
 
26
 
 
27
PGM_GNUC_INTERNAL
 
28
bool
 
29
pgm_queue_is_empty (
 
30
        const pgm_queue_t*const queue
 
31
        )
 
32
{
 
33
        pgm_return_val_if_fail (queue != NULL, TRUE);
 
34
 
 
35
        return queue->head == NULL;
 
36
}
 
37
 
 
38
PGM_GNUC_INTERNAL
 
39
void
 
40
pgm_queue_push_head_link (
 
41
        pgm_queue_t* restrict queue,
 
42
        pgm_list_t*  restrict head_link
 
43
        )
 
44
{
 
45
        pgm_return_if_fail (queue != NULL);
 
46
        pgm_return_if_fail (head_link != NULL);
 
47
        pgm_return_if_fail (head_link->prev == NULL);
 
48
        pgm_return_if_fail (head_link->next == NULL);
 
49
 
 
50
        head_link->next = queue->head;
 
51
        if (queue->head)
 
52
                queue->head->prev = head_link;
 
53
        else
 
54
                queue->tail = head_link;
 
55
        queue->head = head_link;
 
56
        queue->length++;
 
57
}
 
58
 
 
59
PGM_GNUC_INTERNAL
 
60
pgm_list_t*
 
61
pgm_queue_pop_tail_link (
 
62
        pgm_queue_t*    queue
 
63
        )
 
64
{
 
65
        pgm_return_val_if_fail (queue != NULL, NULL);
 
66
 
 
67
        if (queue->tail)
 
68
        {
 
69
                pgm_list_t *node = queue->tail;
 
70
 
 
71
                queue->tail = node->prev;
 
72
                if (queue->tail)
 
73
                {
 
74
                        queue->tail->next = NULL;
 
75
                        node->prev = NULL;
 
76
                }
 
77
                else
 
78
                        queue->head = NULL;
 
79
                queue->length--;
 
80
 
 
81
                return node;
 
82
        }
 
83
  
 
84
        return NULL;
 
85
}
 
86
 
 
87
PGM_GNUC_INTERNAL
 
88
pgm_list_t*
 
89
pgm_queue_peek_tail_link (
 
90
        pgm_queue_t*    queue
 
91
        )
 
92
{
 
93
        pgm_return_val_if_fail (queue != NULL, NULL);
 
94
 
 
95
        return queue->tail;
 
96
}
 
97
 
 
98
PGM_GNUC_INTERNAL
 
99
void
 
100
pgm_queue_unlink (
 
101
        pgm_queue_t* restrict queue,
 
102
        pgm_list_t*  restrict target_link
 
103
        )
 
104
{
 
105
        pgm_return_if_fail (queue != NULL);
 
106
        pgm_return_if_fail (target_link != NULL);
 
107
 
 
108
        if (target_link == queue->tail)
 
109
                queue->tail = queue->tail->prev;
 
110
  
 
111
        queue->head = pgm_list_remove_link (queue->head, target_link);
 
112
        queue->length--;
 
113
}
 
114
 
 
115
/* eof */