]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Generic linked list routine. |
2 | * Copyright (C) 1997, 2000 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
3a5c3bcb | 22 | #include <stdlib.h> |
718e3744 | 23 | |
24 | #include "linklist.h" | |
25 | #include "memory.h" | |
6b0655a2 | 26 | |
4a1ab8e4 DL |
27 | DEFINE_MTYPE_STATIC(LIB, LINK_LIST, "Link List") |
28 | DEFINE_MTYPE_STATIC(LIB, LINK_NODE, "Link Node") | |
29 | ||
d62a17ae | 30 | struct list *list_new(void) |
718e3744 | 31 | { |
d62a17ae | 32 | return XCALLOC(MTYPE_LINK_LIST, sizeof(struct list)); |
718e3744 | 33 | } |
34 | ||
35 | /* Free list. */ | |
acdf5e25 | 36 | static void list_free_internal(struct list *l) |
718e3744 | 37 | { |
d62a17ae | 38 | XFREE(MTYPE_LINK_LIST, l); |
718e3744 | 39 | } |
40 | ||
41 | /* Allocate new listnode. Internal use only. */ | |
d62a17ae | 42 | static struct listnode *listnode_new(void) |
718e3744 | 43 | { |
d62a17ae | 44 | return XCALLOC(MTYPE_LINK_NODE, sizeof(struct listnode)); |
718e3744 | 45 | } |
46 | ||
47 | /* Free listnode. */ | |
d62a17ae | 48 | static void listnode_free(struct listnode *node) |
718e3744 | 49 | { |
d62a17ae | 50 | XFREE(MTYPE_LINK_NODE, node); |
718e3744 | 51 | } |
6b0655a2 | 52 | |
d62a17ae | 53 | void listnode_add(struct list *list, void *val) |
718e3744 | 54 | { |
d62a17ae | 55 | struct listnode *node; |
56 | ||
57 | assert(val != NULL); | |
58 | ||
59 | node = listnode_new(); | |
60 | ||
61 | node->prev = list->tail; | |
62 | node->data = val; | |
63 | ||
64 | if (list->head == NULL) | |
65 | list->head = node; | |
66 | else | |
67 | list->tail->next = node; | |
68 | list->tail = node; | |
69 | ||
70 | list->count++; | |
718e3744 | 71 | } |
72 | ||
9ea82f28 RW |
73 | void listnode_add_head(struct list *list, void *val) |
74 | { | |
75 | struct listnode *node; | |
76 | ||
77 | assert(val != NULL); | |
78 | ||
79 | node = listnode_new(); | |
80 | ||
81 | node->next = list->head; | |
82 | node->data = val; | |
83 | ||
84 | if (list->head == NULL) | |
85 | list->head = node; | |
86 | else | |
87 | list->head->prev = node; | |
88 | list->head = node; | |
89 | ||
90 | list->count++; | |
91 | } | |
92 | ||
d62a17ae | 93 | void listnode_add_sort(struct list *list, void *val) |
718e3744 | 94 | { |
d62a17ae | 95 | struct listnode *n; |
96 | struct listnode *new; | |
97 | ||
98 | assert(val != NULL); | |
99 | ||
100 | new = listnode_new(); | |
101 | new->data = val; | |
102 | ||
103 | if (list->cmp) { | |
104 | for (n = list->head; n; n = n->next) { | |
105 | if ((*list->cmp)(val, n->data) < 0) { | |
106 | new->next = n; | |
107 | new->prev = n->prev; | |
108 | ||
109 | if (n->prev) | |
110 | n->prev->next = new; | |
111 | else | |
112 | list->head = new; | |
113 | n->prev = new; | |
114 | list->count++; | |
115 | return; | |
116 | } | |
117 | } | |
718e3744 | 118 | } |
718e3744 | 119 | |
d62a17ae | 120 | new->prev = list->tail; |
718e3744 | 121 | |
d62a17ae | 122 | if (list->tail) |
123 | list->tail->next = new; | |
124 | else | |
125 | list->head = new; | |
718e3744 | 126 | |
d62a17ae | 127 | list->tail = new; |
128 | list->count++; | |
718e3744 | 129 | } |
130 | ||
d62a17ae | 131 | struct listnode *listnode_add_after(struct list *list, struct listnode *pp, |
132 | void *val) | |
718e3744 | 133 | { |
d62a17ae | 134 | struct listnode *nn; |
135 | ||
136 | assert(val != NULL); | |
137 | ||
138 | nn = listnode_new(); | |
139 | nn->data = val; | |
140 | ||
141 | if (pp == NULL) { | |
142 | if (list->head) | |
143 | list->head->prev = nn; | |
144 | else | |
145 | list->tail = nn; | |
146 | ||
147 | nn->next = list->head; | |
148 | nn->prev = pp; | |
149 | ||
150 | list->head = nn; | |
151 | } else { | |
152 | if (pp->next) | |
153 | pp->next->prev = nn; | |
154 | else | |
155 | list->tail = nn; | |
156 | ||
157 | nn->next = pp->next; | |
158 | nn->prev = pp; | |
159 | ||
160 | pp->next = nn; | |
161 | } | |
162 | list->count++; | |
163 | return nn; | |
718e3744 | 164 | } |
165 | ||
d62a17ae | 166 | struct listnode *listnode_add_before(struct list *list, struct listnode *pp, |
167 | void *val) | |
b21e9619 | 168 | { |
d62a17ae | 169 | struct listnode *nn; |
170 | ||
171 | assert(val != NULL); | |
172 | ||
173 | nn = listnode_new(); | |
174 | nn->data = val; | |
175 | ||
176 | if (pp == NULL) { | |
177 | if (list->tail) | |
178 | list->tail->next = nn; | |
179 | else | |
180 | list->head = nn; | |
181 | ||
182 | nn->prev = list->tail; | |
183 | nn->next = pp; | |
184 | ||
185 | list->tail = nn; | |
186 | } else { | |
187 | if (pp->prev) | |
188 | pp->prev->next = nn; | |
189 | else | |
190 | list->head = nn; | |
191 | ||
192 | nn->prev = pp->prev; | |
193 | nn->next = pp; | |
194 | ||
195 | pp->prev = nn; | |
196 | } | |
197 | list->count++; | |
198 | return nn; | |
b21e9619 DL |
199 | } |
200 | ||
d62a17ae | 201 | void listnode_move_to_tail(struct list *l, struct listnode *n) |
93559b99 | 202 | { |
d62a17ae | 203 | LISTNODE_DETACH(l, n); |
204 | LISTNODE_ATTACH(l, n); | |
93559b99 | 205 | } |
718e3744 | 206 | |
d62a17ae | 207 | void listnode_delete(struct list *list, void *val) |
718e3744 | 208 | { |
0866cdaf | 209 | struct listnode *node = listnode_lookup(list, val); |
d62a17ae | 210 | |
0866cdaf A |
211 | if (node) |
212 | list_delete_node(list, node); | |
718e3744 | 213 | } |
214 | ||
d62a17ae | 215 | void *listnode_head(struct list *list) |
718e3744 | 216 | { |
d62a17ae | 217 | struct listnode *node; |
718e3744 | 218 | |
d62a17ae | 219 | assert(list); |
220 | node = list->head; | |
718e3744 | 221 | |
d62a17ae | 222 | if (node) |
223 | return node->data; | |
224 | return NULL; | |
718e3744 | 225 | } |
226 | ||
d62a17ae | 227 | void list_delete_all_node(struct list *list) |
718e3744 | 228 | { |
d62a17ae | 229 | struct listnode *node; |
230 | struct listnode *next; | |
231 | ||
232 | assert(list); | |
233 | for (node = list->head; node; node = next) { | |
234 | next = node->next; | |
affe9e99 | 235 | if (*list->del) |
d62a17ae | 236 | (*list->del)(node->data); |
237 | listnode_free(node); | |
238 | } | |
239 | list->head = list->tail = NULL; | |
240 | list->count = 0; | |
718e3744 | 241 | } |
242 | ||
6a154c88 | 243 | void list_delete(struct list **list) |
718e3744 | 244 | { |
affe9e99 DS |
245 | assert(*list); |
246 | list_delete_all_node(*list); | |
acdf5e25 | 247 | list_free_internal(*list); |
affe9e99 DS |
248 | *list = NULL; |
249 | } | |
250 | ||
d62a17ae | 251 | struct listnode *listnode_lookup(struct list *list, void *data) |
718e3744 | 252 | { |
d62a17ae | 253 | struct listnode *node; |
718e3744 | 254 | |
d62a17ae | 255 | assert(list); |
256 | for (node = listhead(list); node; node = listnextnode(node)) | |
257 | if (data == listgetdata(node)) | |
258 | return node; | |
259 | return NULL; | |
718e3744 | 260 | } |
6b0655a2 | 261 | |
d62a17ae | 262 | void list_delete_node(struct list *list, struct listnode *node) |
718e3744 | 263 | { |
d62a17ae | 264 | if (node->prev) |
265 | node->prev->next = node->next; | |
266 | else | |
267 | list->head = node->next; | |
268 | if (node->next) | |
269 | node->next->prev = node->prev; | |
270 | else | |
271 | list->tail = node->prev; | |
272 | list->count--; | |
273 | listnode_free(node); | |
718e3744 | 274 | } |
6b0655a2 | 275 | |
6fd8c487 | 276 | void list_add_list(struct list *list, struct list *add) |
718e3744 | 277 | { |
d62a17ae | 278 | struct listnode *n; |
718e3744 | 279 | |
6fd8c487 QY |
280 | for (n = listhead(add); n; n = listnextnode(n)) |
281 | listnode_add(list, n->data); | |
718e3744 | 282 | } |
3a5c3bcb | 283 | |
6fd8c487 | 284 | struct list *list_dup(struct list *list) |
3a5c3bcb QY |
285 | { |
286 | struct list *new = list_new(); | |
287 | struct listnode *ln; | |
288 | void *data; | |
289 | ||
6fd8c487 QY |
290 | new->cmp = list->cmp; |
291 | new->del = list->del; | |
3a5c3bcb | 292 | |
6fd8c487 | 293 | for (ALL_LIST_ELEMENTS_RO(list, ln, data)) |
3a5c3bcb QY |
294 | listnode_add(new, data); |
295 | ||
296 | return new; | |
297 | } | |
298 | ||
299 | void list_sort(struct list *list, int (*cmp)(const void **, const void **)) | |
300 | { | |
301 | struct listnode *ln, *nn; | |
302 | int i = -1; | |
303 | void *data; | |
304 | size_t n = list->count; | |
305 | void **items = XCALLOC(MTYPE_TMP, (sizeof(void *)) * n); | |
306 | int (*realcmp)(const void *, const void *) = | |
307 | (int (*)(const void *, const void *))cmp; | |
308 | ||
309 | for (ALL_LIST_ELEMENTS(list, ln, nn, data)) { | |
310 | items[++i] = data; | |
311 | list_delete_node(list, ln); | |
312 | } | |
313 | ||
314 | qsort(items, n, sizeof(void *), realcmp); | |
315 | ||
c683bd44 A |
316 | for (unsigned int j = 0; j < n; ++j) |
317 | listnode_add(list, items[j]); | |
3a5c3bcb QY |
318 | |
319 | XFREE(MTYPE_TMP, items); | |
320 | } |