summaryrefslogtreecommitdiff
path: root/cesar/lib/list.h
blob: 042236ac65c02cfd600d34c82c4d284fc0b294b1 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef lib_list_h
#define lib_list_h
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    lib/list.h
 * \brief   Double linked lists.
 * \ingroup lib
 */

/** Double linked list node. */
struct list_node_t
{
    /** Pointer to next node. */
    struct list_node_t *next;
    /** Pointer to previous node. */
    struct list_node_t *prev;
};
typedef struct list_node_t list_node_t;

/** Double linked list. */
struct list_t
{
    /** List NIL element. */
    list_node_t nil;
};
typedef struct list_t list_t;

BEGIN_DECLS

/**
 * Initialise a double linked list.
 * \param  list  the list to initialise
 */
void
list_init (list_t *list);

/**
 * Initialise a list node.
 * \param  node  the node to initialise
 */
void
list_init_node (list_node_t *node);

/**
 * Return the first node.
 * \param  list  the list
 * \return  the first node
 */
list_node_t *
list_begin (list_t *list);

/**
 * Return past the last node.
 * \param  list  the list
 * \return  the node after the last
 */
list_node_t *
list_end (list_t *list);

/**
 * Return last node to backward iteration.
 * \param  list  the list
 * \return  the last node
 */
list_node_t *
list_rbegin (list_t *list);

/**
 * Return before the first node.
 * \param  list  the list
 * \return  the node before the first
 */
list_node_t *
list_rend (list_t *list);

/**
 * Return the next node.
 * \param  node  the current node
 * \return  the node after the current one
 */
list_node_t *
list_next (list_node_t *node);

/**
 * Return the previous node.
 * \param  node  the current node
 * \return  the node before the current one
 */
list_node_t *
list_prev (list_node_t *node);

/**
 * Is the list empty?
 * \param  list  the list
 * \return  true if empty
 */
bool
list_empty (list_t *list);

/**
 * Push a node to the end of a list.
 * \param  list  the list
 * \param  node  the node to push
 */
void
list_push (list_t *list, list_node_t *node);

/**
 * Unshift a node to the begin of a list.
 * \param  list  the list
 * \param  node  the node to unshift
 */
void
list_unshift (list_t *list, list_node_t *node);

/**
 * Pop a node from the end of a list.
 * \param  list  the list
 * \return  the popped node
 */
list_node_t *
list_pop (list_t *list);

/**
 * Shift a node from the begin of a list.
 * \param  list  the list
 * \return  the shifted node
 */
list_node_t *
list_shift (list_t *list);

/**
 * Remove a node from a list.
 * \param  list  the list
 * \param  node  the node to remove
 */
void
list_remove (list_t *list, list_node_t *node);

/**
 * Insert a node into a list.
 * \param  list  the list
 * \param  before  node before which to insert
 * \param  node  node to insert
 */
void
list_insert (list_t *list, list_node_t *before, list_node_t *node);

/**
 * Push a range [first, last) to a end of a list.
 * \param  to  list to push to
 * \param  from  list to remove from
 * \param  first  first node to push
 * \param  last  node after the last one to push
 */
void
list_push_range (list_t *to, list_t *from,
                 list_node_t *first, list_node_t *last);

/**
 * Unshift a range [first, last) to a begin of a list.
 * \param  to  list to unshift to
 * \param  from  list to remove from
 * \param  first  first node to unshift
 * \param  last  node after the last one to unshift
 */
void
list_unshift_range (list_t *to, list_t *from,
                    list_node_t *first, list_node_t *last);

/**
 * Insert a range [first, last) to a list
 * \param  to  list to insert to
 * \param  from  list to remove from
 * \param  before  node before which to insert
 * \param  first  first node to insert
 * \param  last  node after the last one to insert
 */
void
list_insert_range (list_t *to, list_t *from, list_node_t *before,
                   list_node_t *first, list_node_t *last);

END_DECLS

#endif /* lib_list_h */