1 |
pippijn |
1.1 |
/* |
2 |
|
|
* dlink.C: Linked lists. |
3 |
pippijn |
1.2 |
* Rights to this code are documented in doc/pod/license.pod. |
4 |
pippijn |
1.1 |
* |
5 |
pippijn |
1.4 |
* Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
6 |
pippijn |
1.1 |
*/ |
7 |
|
|
|
8 |
pippijn |
1.4 |
static char const rcsid[] = "$Id: dlink.C,v 1.3 2007-07-21 13:23:21 pippijn Exp $"; |
9 |
pippijn |
1.1 |
|
10 |
|
|
#include "atheme.h" |
11 |
|
|
#include "internal.h" |
12 |
|
|
|
13 |
|
|
void |
14 |
|
|
init_dlink_nodes (void) |
15 |
|
|
{ |
16 |
pippijn |
1.4 |
#if 0 |
17 |
pippijn |
1.1 |
node_heap = BlockHeapCreate (sizeof (node_t), HEAP_NODE); |
18 |
pippijn |
1.4 |
#endif |
19 |
pippijn |
1.1 |
} |
20 |
|
|
|
21 |
|
|
/* creates a new node */ |
22 |
|
|
node_t * |
23 |
|
|
node_create (void) |
24 |
|
|
{ |
25 |
|
|
node_t *n; |
26 |
|
|
|
27 |
|
|
/* allocate it */ |
28 |
pippijn |
1.4 |
n = new node_t; |
29 |
pippijn |
1.1 |
|
30 |
|
|
/* initialize */ |
31 |
|
|
n->data = n->next = n->prev = NULL; |
32 |
|
|
|
33 |
|
|
/* up the count */ |
34 |
pippijn |
1.4 |
system_state.node++; |
35 |
pippijn |
1.1 |
|
36 |
|
|
/* return a pointer to the new node */ |
37 |
|
|
return n; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
/* frees a node */ |
41 |
|
|
void |
42 |
|
|
node_free (node_t *n) |
43 |
|
|
{ |
44 |
|
|
return_if_fail (n != NULL); |
45 |
|
|
|
46 |
|
|
/* free it */ |
47 |
pippijn |
1.4 |
delete n; |
48 |
pippijn |
1.1 |
|
49 |
|
|
/* down the count */ |
50 |
pippijn |
1.4 |
system_state.node--; |
51 |
pippijn |
1.1 |
} |
52 |
|
|
|
53 |
|
|
/* adds a node to the end of a list */ |
54 |
|
|
void |
55 |
|
|
node_add (void *data, node_t *n, list_t *l) |
56 |
|
|
{ |
57 |
|
|
node_t *tn; |
58 |
|
|
|
59 |
|
|
return_if_fail (n != NULL); |
60 |
|
|
return_if_fail (l != NULL); |
61 |
|
|
|
62 |
|
|
n->next = n->prev = NULL; |
63 |
|
|
n->data = data; |
64 |
|
|
|
65 |
|
|
/* first node? */ |
66 |
|
|
if (!l->head) |
67 |
|
|
{ |
68 |
|
|
l->head = n; |
69 |
|
|
l->tail = n; |
70 |
|
|
l->count++; |
71 |
|
|
return; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
/* Speed increase. */ |
75 |
|
|
tn = l->tail; |
76 |
|
|
|
77 |
|
|
/* set the our `prev' to the last node */ |
78 |
|
|
n->prev = tn; |
79 |
|
|
|
80 |
|
|
/* set the last node's `next' to us */ |
81 |
|
|
n->prev->next = n; |
82 |
|
|
|
83 |
|
|
/* set the list's `tail' to us */ |
84 |
|
|
l->tail = n; |
85 |
|
|
|
86 |
|
|
/* up the count */ |
87 |
|
|
l->count++; |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
/* adds a node to the head of a list */ |
91 |
|
|
void |
92 |
|
|
node_add_head (void *data, node_t *n, list_t *l) |
93 |
|
|
{ |
94 |
|
|
node_t *tn; |
95 |
|
|
|
96 |
|
|
return_if_fail (n != NULL); |
97 |
|
|
return_if_fail (l != NULL); |
98 |
|
|
|
99 |
|
|
n->next = n->prev = NULL; |
100 |
|
|
n->data = data; |
101 |
|
|
|
102 |
|
|
/* first node? */ |
103 |
|
|
if (!l->head) |
104 |
|
|
{ |
105 |
|
|
l->head = n; |
106 |
|
|
l->tail = n; |
107 |
|
|
l->count++; |
108 |
|
|
return; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
tn = l->head; |
112 |
|
|
n->next = tn; |
113 |
|
|
tn->prev = n; |
114 |
|
|
l->head = n; |
115 |
|
|
l->count++; |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
/* adds a node to a list before another node, or to the end */ |
119 |
|
|
void |
120 |
|
|
node_add_before (void *data, node_t *n, list_t *l, node_t *before) |
121 |
|
|
{ |
122 |
|
|
return_if_fail (n != NULL); |
123 |
|
|
return_if_fail (l != NULL); |
124 |
|
|
|
125 |
|
|
if (before == NULL) |
126 |
|
|
node_add (data, n, l); |
127 |
|
|
else if (before == l->head) |
128 |
|
|
node_add_head (data, n, l); |
129 |
|
|
else |
130 |
|
|
{ |
131 |
|
|
n->data = data; |
132 |
|
|
n->prev = before->prev; |
133 |
|
|
n->next = before; |
134 |
|
|
before->prev = n; |
135 |
|
|
l->count++; |
136 |
|
|
} |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
void |
140 |
|
|
node_del (node_t *n, list_t *l) |
141 |
|
|
{ |
142 |
|
|
return_if_fail (n != NULL); |
143 |
|
|
return_if_fail (l != NULL); |
144 |
|
|
|
145 |
|
|
/* are we the head? */ |
146 |
|
|
if (!n->prev) |
147 |
|
|
l->head = n->next; |
148 |
|
|
else |
149 |
|
|
n->prev->next = n->next; |
150 |
|
|
|
151 |
|
|
/* are we the tail? */ |
152 |
|
|
if (!n->next) |
153 |
|
|
l->tail = n->prev; |
154 |
|
|
else |
155 |
|
|
n->next->prev = n->prev; |
156 |
|
|
|
157 |
|
|
/* down the count */ |
158 |
|
|
l->count--; |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
/* finds a node by `data' */ |
162 |
|
|
node_t * |
163 |
|
|
node_find (void *data, list_t *l) |
164 |
|
|
{ |
165 |
|
|
node_t *n; |
166 |
|
|
|
167 |
|
|
return_val_if_fail (l != NULL, NULL); |
168 |
|
|
|
169 |
pippijn |
1.4 |
LIST_FOREACH (n, l->head) |
170 |
|
|
if (n->data == data) |
171 |
|
|
return n; |
172 |
pippijn |
1.1 |
|
173 |
|
|
return NULL; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
void |
177 |
|
|
node_move (node_t *m, list_t *oldlist, list_t *newlist) |
178 |
|
|
{ |
179 |
|
|
return_if_fail (m != NULL); |
180 |
|
|
return_if_fail (oldlist != NULL); |
181 |
|
|
return_if_fail (newlist != NULL); |
182 |
|
|
|
183 |
|
|
/* Assumption: If m->next == NULL, then list->tail == m |
184 |
|
|
* and: If m->prev == NULL, then list->head == m |
185 |
|
|
*/ |
186 |
|
|
if (m->next) |
187 |
|
|
m->next->prev = m->prev; |
188 |
|
|
else |
189 |
|
|
oldlist->tail = m->prev; |
190 |
|
|
|
191 |
|
|
if (m->prev) |
192 |
|
|
m->prev->next = m->next; |
193 |
|
|
else |
194 |
|
|
oldlist->head = m->next; |
195 |
|
|
|
196 |
|
|
m->prev = NULL; |
197 |
|
|
m->next = newlist->head; |
198 |
|
|
if (newlist->head != NULL) |
199 |
|
|
newlist->head->prev = m; |
200 |
|
|
else if (newlist->tail == NULL) |
201 |
|
|
newlist->tail = m; |
202 |
|
|
newlist->head = m; |
203 |
|
|
|
204 |
|
|
oldlist->count--; |
205 |
|
|
newlist->count++; |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
209 |
|
|
* vim:ts=8 |
210 |
|
|
* vim:sw=8 |
211 |
|
|
* vim:noexpandtab |
212 |
|
|
*/ |