diff options
Diffstat (limited to 'openbsd-compat')
-rw-r--r-- | openbsd-compat/fake-queue.h | 584 | ||||
-rw-r--r-- | openbsd-compat/tree.h | 667 |
2 files changed, 0 insertions, 1251 deletions
diff --git a/openbsd-compat/fake-queue.h b/openbsd-compat/fake-queue.h deleted file mode 100644 index 176fe3174..000000000 --- a/openbsd-compat/fake-queue.h +++ /dev/null | |||
@@ -1,584 +0,0 @@ | |||
1 | /* $OpenBSD: queue.h,v 1.22 2001/06/23 04:39:35 angelos Exp $ */ | ||
2 | /* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */ | ||
3 | |||
4 | /* | ||
5 | * Copyright (c) 1991, 1993 | ||
6 | * The Regents of the University of California. All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * 3. All advertising materials mentioning features or use of this software | ||
17 | * must display the following acknowledgement: | ||
18 | * This product includes software developed by the University of | ||
19 | * California, Berkeley and its contributors. | ||
20 | * 4. Neither the name of the University nor the names of its contributors | ||
21 | * may be used to endorse or promote products derived from this software | ||
22 | * without specific prior written permission. | ||
23 | * | ||
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
34 | * SUCH DAMAGE. | ||
35 | * | ||
36 | * @(#)queue.h 8.5 (Berkeley) 8/20/94 | ||
37 | */ | ||
38 | |||
39 | #ifndef _FAKE_QUEUE_H_ | ||
40 | #define _FAKE_QUEUE_H_ | ||
41 | |||
42 | /* | ||
43 | * Ignore all <sys/queue.h> since older platforms have broken/incomplete | ||
44 | * <sys/queue.h> that are too hard to work around. | ||
45 | */ | ||
46 | #undef SLIST_HEAD | ||
47 | #undef SLIST_HEAD_INITIALIZER | ||
48 | #undef SLIST_ENTRY | ||
49 | #undef SLIST_FIRST | ||
50 | #undef SLIST_END | ||
51 | #undef SLIST_EMPTY | ||
52 | #undef SLIST_NEXT | ||
53 | #undef SLIST_FOREACH | ||
54 | #undef SLIST_INIT | ||
55 | #undef SLIST_INSERT_AFTER | ||
56 | #undef SLIST_INSERT_HEAD | ||
57 | #undef SLIST_REMOVE_HEAD | ||
58 | #undef SLIST_REMOVE | ||
59 | #undef LIST_HEAD | ||
60 | #undef LIST_HEAD_INITIALIZER | ||
61 | #undef LIST_ENTRY | ||
62 | #undef LIST_FIRST | ||
63 | #undef LIST_END | ||
64 | #undef LIST_EMPTY | ||
65 | #undef LIST_NEXT | ||
66 | #undef LIST_FOREACH | ||
67 | #undef LIST_INIT | ||
68 | #undef LIST_INSERT_AFTER | ||
69 | #undef LIST_INSERT_BEFORE | ||
70 | #undef LIST_INSERT_HEAD | ||
71 | #undef LIST_REMOVE | ||
72 | #undef LIST_REPLACE | ||
73 | #undef SIMPLEQ_HEAD | ||
74 | #undef SIMPLEQ_HEAD_INITIALIZER | ||
75 | #undef SIMPLEQ_ENTRY | ||
76 | #undef SIMPLEQ_FIRST | ||
77 | #undef SIMPLEQ_END | ||
78 | #undef SIMPLEQ_EMPTY | ||
79 | #undef SIMPLEQ_NEXT | ||
80 | #undef SIMPLEQ_FOREACH | ||
81 | #undef SIMPLEQ_INIT | ||
82 | #undef SIMPLEQ_INSERT_HEAD | ||
83 | #undef SIMPLEQ_INSERT_TAIL | ||
84 | #undef SIMPLEQ_INSERT_AFTER | ||
85 | #undef SIMPLEQ_REMOVE_HEAD | ||
86 | #undef TAILQ_HEAD | ||
87 | #undef TAILQ_HEAD_INITIALIZER | ||
88 | #undef TAILQ_ENTRY | ||
89 | #undef TAILQ_FIRST | ||
90 | #undef TAILQ_END | ||
91 | #undef TAILQ_NEXT | ||
92 | #undef TAILQ_LAST | ||
93 | #undef TAILQ_PREV | ||
94 | #undef TAILQ_EMPTY | ||
95 | #undef TAILQ_FOREACH | ||
96 | #undef TAILQ_FOREACH_REVERSE | ||
97 | #undef TAILQ_INIT | ||
98 | #undef TAILQ_INSERT_HEAD | ||
99 | #undef TAILQ_INSERT_TAIL | ||
100 | #undef TAILQ_INSERT_AFTER | ||
101 | #undef TAILQ_INSERT_BEFORE | ||
102 | #undef TAILQ_REMOVE | ||
103 | #undef TAILQ_REPLACE | ||
104 | #undef CIRCLEQ_HEAD | ||
105 | #undef CIRCLEQ_HEAD_INITIALIZER | ||
106 | #undef CIRCLEQ_ENTRY | ||
107 | #undef CIRCLEQ_FIRST | ||
108 | #undef CIRCLEQ_LAST | ||
109 | #undef CIRCLEQ_END | ||
110 | #undef CIRCLEQ_NEXT | ||
111 | #undef CIRCLEQ_PREV | ||
112 | #undef CIRCLEQ_EMPTY | ||
113 | #undef CIRCLEQ_FOREACH | ||
114 | #undef CIRCLEQ_FOREACH_REVERSE | ||
115 | #undef CIRCLEQ_INIT | ||
116 | #undef CIRCLEQ_INSERT_AFTER | ||
117 | #undef CIRCLEQ_INSERT_BEFORE | ||
118 | #undef CIRCLEQ_INSERT_HEAD | ||
119 | #undef CIRCLEQ_INSERT_TAIL | ||
120 | #undef CIRCLEQ_REMOVE | ||
121 | #undef CIRCLEQ_REPLACE | ||
122 | |||
123 | /* | ||
124 | * This file defines five types of data structures: singly-linked lists, | ||
125 | * lists, simple queues, tail queues, and circular queues. | ||
126 | * | ||
127 | * | ||
128 | * A singly-linked list is headed by a single forward pointer. The elements | ||
129 | * are singly linked for minimum space and pointer manipulation overhead at | ||
130 | * the expense of O(n) removal for arbitrary elements. New elements can be | ||
131 | * added to the list after an existing element or at the head of the list. | ||
132 | * Elements being removed from the head of the list should use the explicit | ||
133 | * macro for this purpose for optimum efficiency. A singly-linked list may | ||
134 | * only be traversed in the forward direction. Singly-linked lists are ideal | ||
135 | * for applications with large datasets and few or no removals or for | ||
136 | * implementing a LIFO queue. | ||
137 | * | ||
138 | * A list is headed by a single forward pointer (or an array of forward | ||
139 | * pointers for a hash table header). The elements are doubly linked | ||
140 | * so that an arbitrary element can be removed without a need to | ||
141 | * traverse the list. New elements can be added to the list before | ||
142 | * or after an existing element or at the head of the list. A list | ||
143 | * may only be traversed in the forward direction. | ||
144 | * | ||
145 | * A simple queue is headed by a pair of pointers, one the head of the | ||
146 | * list and the other to the tail of the list. The elements are singly | ||
147 | * linked to save space, so elements can only be removed from the | ||
148 | * head of the list. New elements can be added to the list before or after | ||
149 | * an existing element, at the head of the list, or at the end of the | ||
150 | * list. A simple queue may only be traversed in the forward direction. | ||
151 | * | ||
152 | * A tail queue is headed by a pair of pointers, one to the head of the | ||
153 | * list and the other to the tail of the list. The elements are doubly | ||
154 | * linked so that an arbitrary element can be removed without a need to | ||
155 | * traverse the list. New elements can be added to the list before or | ||
156 | * after an existing element, at the head of the list, or at the end of | ||
157 | * the list. A tail queue may be traversed in either direction. | ||
158 | * | ||
159 | * A circle queue is headed by a pair of pointers, one to the head of the | ||
160 | * list and the other to the tail of the list. The elements are doubly | ||
161 | * linked so that an arbitrary element can be removed without a need to | ||
162 | * traverse the list. New elements can be added to the list before or after | ||
163 | * an existing element, at the head of the list, or at the end of the list. | ||
164 | * A circle queue may be traversed in either direction, but has a more | ||
165 | * complex end of list detection. | ||
166 | * | ||
167 | * For details on the use of these macros, see the queue(3) manual page. | ||
168 | */ | ||
169 | |||
170 | /* | ||
171 | * Singly-linked List definitions. | ||
172 | */ | ||
173 | #define SLIST_HEAD(name, type) \ | ||
174 | struct name { \ | ||
175 | struct type *slh_first; /* first element */ \ | ||
176 | } | ||
177 | |||
178 | #define SLIST_HEAD_INITIALIZER(head) \ | ||
179 | { NULL } | ||
180 | |||
181 | #define SLIST_ENTRY(type) \ | ||
182 | struct { \ | ||
183 | struct type *sle_next; /* next element */ \ | ||
184 | } | ||
185 | |||
186 | /* | ||
187 | * Singly-linked List access methods. | ||
188 | */ | ||
189 | #define SLIST_FIRST(head) ((head)->slh_first) | ||
190 | #define SLIST_END(head) NULL | ||
191 | #define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head)) | ||
192 | #define SLIST_NEXT(elm, field) ((elm)->field.sle_next) | ||
193 | |||
194 | #define SLIST_FOREACH(var, head, field) \ | ||
195 | for((var) = SLIST_FIRST(head); \ | ||
196 | (var) != SLIST_END(head); \ | ||
197 | (var) = SLIST_NEXT(var, field)) | ||
198 | |||
199 | /* | ||
200 | * Singly-linked List functions. | ||
201 | */ | ||
202 | #define SLIST_INIT(head) { \ | ||
203 | SLIST_FIRST(head) = SLIST_END(head); \ | ||
204 | } | ||
205 | |||
206 | #define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ | ||
207 | (elm)->field.sle_next = (slistelm)->field.sle_next; \ | ||
208 | (slistelm)->field.sle_next = (elm); \ | ||
209 | } while (0) | ||
210 | |||
211 | #define SLIST_INSERT_HEAD(head, elm, field) do { \ | ||
212 | (elm)->field.sle_next = (head)->slh_first; \ | ||
213 | (head)->slh_first = (elm); \ | ||
214 | } while (0) | ||
215 | |||
216 | #define SLIST_REMOVE_HEAD(head, field) do { \ | ||
217 | (head)->slh_first = (head)->slh_first->field.sle_next; \ | ||
218 | } while (0) | ||
219 | |||
220 | #define SLIST_REMOVE(head, elm, type, field) do { \ | ||
221 | if ((head)->slh_first == (elm)) { \ | ||
222 | SLIST_REMOVE_HEAD((head), field); \ | ||
223 | } \ | ||
224 | else { \ | ||
225 | struct type *curelm = (head)->slh_first; \ | ||
226 | while( curelm->field.sle_next != (elm) ) \ | ||
227 | curelm = curelm->field.sle_next; \ | ||
228 | curelm->field.sle_next = \ | ||
229 | curelm->field.sle_next->field.sle_next; \ | ||
230 | } \ | ||
231 | } while (0) | ||
232 | |||
233 | /* | ||
234 | * List definitions. | ||
235 | */ | ||
236 | #define LIST_HEAD(name, type) \ | ||
237 | struct name { \ | ||
238 | struct type *lh_first; /* first element */ \ | ||
239 | } | ||
240 | |||
241 | #define LIST_HEAD_INITIALIZER(head) \ | ||
242 | { NULL } | ||
243 | |||
244 | #define LIST_ENTRY(type) \ | ||
245 | struct { \ | ||
246 | struct type *le_next; /* next element */ \ | ||
247 | struct type **le_prev; /* address of previous next element */ \ | ||
248 | } | ||
249 | |||
250 | /* | ||
251 | * List access methods | ||
252 | */ | ||
253 | #define LIST_FIRST(head) ((head)->lh_first) | ||
254 | #define LIST_END(head) NULL | ||
255 | #define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head)) | ||
256 | #define LIST_NEXT(elm, field) ((elm)->field.le_next) | ||
257 | |||
258 | #define LIST_FOREACH(var, head, field) \ | ||
259 | for((var) = LIST_FIRST(head); \ | ||
260 | (var)!= LIST_END(head); \ | ||
261 | (var) = LIST_NEXT(var, field)) | ||
262 | |||
263 | /* | ||
264 | * List functions. | ||
265 | */ | ||
266 | #define LIST_INIT(head) do { \ | ||
267 | LIST_FIRST(head) = LIST_END(head); \ | ||
268 | } while (0) | ||
269 | |||
270 | #define LIST_INSERT_AFTER(listelm, elm, field) do { \ | ||
271 | if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ | ||
272 | (listelm)->field.le_next->field.le_prev = \ | ||
273 | &(elm)->field.le_next; \ | ||
274 | (listelm)->field.le_next = (elm); \ | ||
275 | (elm)->field.le_prev = &(listelm)->field.le_next; \ | ||
276 | } while (0) | ||
277 | |||
278 | #define LIST_INSERT_BEFORE(listelm, elm, field) do { \ | ||
279 | (elm)->field.le_prev = (listelm)->field.le_prev; \ | ||
280 | (elm)->field.le_next = (listelm); \ | ||
281 | *(listelm)->field.le_prev = (elm); \ | ||
282 | (listelm)->field.le_prev = &(elm)->field.le_next; \ | ||
283 | } while (0) | ||
284 | |||
285 | #define LIST_INSERT_HEAD(head, elm, field) do { \ | ||
286 | if (((elm)->field.le_next = (head)->lh_first) != NULL) \ | ||
287 | (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ | ||
288 | (head)->lh_first = (elm); \ | ||
289 | (elm)->field.le_prev = &(head)->lh_first; \ | ||
290 | } while (0) | ||
291 | |||
292 | #define LIST_REMOVE(elm, field) do { \ | ||
293 | if ((elm)->field.le_next != NULL) \ | ||
294 | (elm)->field.le_next->field.le_prev = \ | ||
295 | (elm)->field.le_prev; \ | ||
296 | *(elm)->field.le_prev = (elm)->field.le_next; \ | ||
297 | } while (0) | ||
298 | |||
299 | #define LIST_REPLACE(elm, elm2, field) do { \ | ||
300 | if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \ | ||
301 | (elm2)->field.le_next->field.le_prev = \ | ||
302 | &(elm2)->field.le_next; \ | ||
303 | (elm2)->field.le_prev = (elm)->field.le_prev; \ | ||
304 | *(elm2)->field.le_prev = (elm2); \ | ||
305 | } while (0) | ||
306 | |||
307 | /* | ||
308 | * Simple queue definitions. | ||
309 | */ | ||
310 | #define SIMPLEQ_HEAD(name, type) \ | ||
311 | struct name { \ | ||
312 | struct type *sqh_first; /* first element */ \ | ||
313 | struct type **sqh_last; /* addr of last next element */ \ | ||
314 | } | ||
315 | |||
316 | #define SIMPLEQ_HEAD_INITIALIZER(head) \ | ||
317 | { NULL, &(head).sqh_first } | ||
318 | |||
319 | #define SIMPLEQ_ENTRY(type) \ | ||
320 | struct { \ | ||
321 | struct type *sqe_next; /* next element */ \ | ||
322 | } | ||
323 | |||
324 | /* | ||
325 | * Simple queue access methods. | ||
326 | */ | ||
327 | #define SIMPLEQ_FIRST(head) ((head)->sqh_first) | ||
328 | #define SIMPLEQ_END(head) NULL | ||
329 | #define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head)) | ||
330 | #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) | ||
331 | |||
332 | #define SIMPLEQ_FOREACH(var, head, field) \ | ||
333 | for((var) = SIMPLEQ_FIRST(head); \ | ||
334 | (var) != SIMPLEQ_END(head); \ | ||
335 | (var) = SIMPLEQ_NEXT(var, field)) | ||
336 | |||
337 | /* | ||
338 | * Simple queue functions. | ||
339 | */ | ||
340 | #define SIMPLEQ_INIT(head) do { \ | ||
341 | (head)->sqh_first = NULL; \ | ||
342 | (head)->sqh_last = &(head)->sqh_first; \ | ||
343 | } while (0) | ||
344 | |||
345 | #define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \ | ||
346 | if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ | ||
347 | (head)->sqh_last = &(elm)->field.sqe_next; \ | ||
348 | (head)->sqh_first = (elm); \ | ||
349 | } while (0) | ||
350 | |||
351 | #define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \ | ||
352 | (elm)->field.sqe_next = NULL; \ | ||
353 | *(head)->sqh_last = (elm); \ | ||
354 | (head)->sqh_last = &(elm)->field.sqe_next; \ | ||
355 | } while (0) | ||
356 | |||
357 | #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ | ||
358 | if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\ | ||
359 | (head)->sqh_last = &(elm)->field.sqe_next; \ | ||
360 | (listelm)->field.sqe_next = (elm); \ | ||
361 | } while (0) | ||
362 | |||
363 | #define SIMPLEQ_REMOVE_HEAD(head, elm, field) do { \ | ||
364 | if (((head)->sqh_first = (elm)->field.sqe_next) == NULL) \ | ||
365 | (head)->sqh_last = &(head)->sqh_first; \ | ||
366 | } while (0) | ||
367 | |||
368 | /* | ||
369 | * Tail queue definitions. | ||
370 | */ | ||
371 | #define TAILQ_HEAD(name, type) \ | ||
372 | struct name { \ | ||
373 | struct type *tqh_first; /* first element */ \ | ||
374 | struct type **tqh_last; /* addr of last next element */ \ | ||
375 | } | ||
376 | |||
377 | #define TAILQ_HEAD_INITIALIZER(head) \ | ||
378 | { NULL, &(head).tqh_first } | ||
379 | |||
380 | #define TAILQ_ENTRY(type) \ | ||
381 | struct { \ | ||
382 | struct type *tqe_next; /* next element */ \ | ||
383 | struct type **tqe_prev; /* address of previous next element */ \ | ||
384 | } | ||
385 | |||
386 | /* | ||
387 | * tail queue access methods | ||
388 | */ | ||
389 | #define TAILQ_FIRST(head) ((head)->tqh_first) | ||
390 | #define TAILQ_END(head) NULL | ||
391 | #define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) | ||
392 | #define TAILQ_LAST(head, headname) \ | ||
393 | (*(((struct headname *)((head)->tqh_last))->tqh_last)) | ||
394 | /* XXX */ | ||
395 | #define TAILQ_PREV(elm, headname, field) \ | ||
396 | (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) | ||
397 | #define TAILQ_EMPTY(head) \ | ||
398 | (TAILQ_FIRST(head) == TAILQ_END(head)) | ||
399 | |||
400 | #define TAILQ_FOREACH(var, head, field) \ | ||
401 | for((var) = TAILQ_FIRST(head); \ | ||
402 | (var) != TAILQ_END(head); \ | ||
403 | (var) = TAILQ_NEXT(var, field)) | ||
404 | |||
405 | #define TAILQ_FOREACH_REVERSE(var, head, field, headname) \ | ||
406 | for((var) = TAILQ_LAST(head, headname); \ | ||
407 | (var) != TAILQ_END(head); \ | ||
408 | (var) = TAILQ_PREV(var, headname, field)) | ||
409 | |||
410 | /* | ||
411 | * Tail queue functions. | ||
412 | */ | ||
413 | #define TAILQ_INIT(head) do { \ | ||
414 | (head)->tqh_first = NULL; \ | ||
415 | (head)->tqh_last = &(head)->tqh_first; \ | ||
416 | } while (0) | ||
417 | |||
418 | #define TAILQ_INSERT_HEAD(head, elm, field) do { \ | ||
419 | if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ | ||
420 | (head)->tqh_first->field.tqe_prev = \ | ||
421 | &(elm)->field.tqe_next; \ | ||
422 | else \ | ||
423 | (head)->tqh_last = &(elm)->field.tqe_next; \ | ||
424 | (head)->tqh_first = (elm); \ | ||
425 | (elm)->field.tqe_prev = &(head)->tqh_first; \ | ||
426 | } while (0) | ||
427 | |||
428 | #define TAILQ_INSERT_TAIL(head, elm, field) do { \ | ||
429 | (elm)->field.tqe_next = NULL; \ | ||
430 | (elm)->field.tqe_prev = (head)->tqh_last; \ | ||
431 | *(head)->tqh_last = (elm); \ | ||
432 | (head)->tqh_last = &(elm)->field.tqe_next; \ | ||
433 | } while (0) | ||
434 | |||
435 | #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ | ||
436 | if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ | ||
437 | (elm)->field.tqe_next->field.tqe_prev = \ | ||
438 | &(elm)->field.tqe_next; \ | ||
439 | else \ | ||
440 | (head)->tqh_last = &(elm)->field.tqe_next; \ | ||
441 | (listelm)->field.tqe_next = (elm); \ | ||
442 | (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ | ||
443 | } while (0) | ||
444 | |||
445 | #define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ | ||
446 | (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ | ||
447 | (elm)->field.tqe_next = (listelm); \ | ||
448 | *(listelm)->field.tqe_prev = (elm); \ | ||
449 | (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ | ||
450 | } while (0) | ||
451 | |||
452 | #define TAILQ_REMOVE(head, elm, field) do { \ | ||
453 | if (((elm)->field.tqe_next) != NULL) \ | ||
454 | (elm)->field.tqe_next->field.tqe_prev = \ | ||
455 | (elm)->field.tqe_prev; \ | ||
456 | else \ | ||
457 | (head)->tqh_last = (elm)->field.tqe_prev; \ | ||
458 | *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ | ||
459 | } while (0) | ||
460 | |||
461 | #define TAILQ_REPLACE(head, elm, elm2, field) do { \ | ||
462 | if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \ | ||
463 | (elm2)->field.tqe_next->field.tqe_prev = \ | ||
464 | &(elm2)->field.tqe_next; \ | ||
465 | else \ | ||
466 | (head)->tqh_last = &(elm2)->field.tqe_next; \ | ||
467 | (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \ | ||
468 | *(elm2)->field.tqe_prev = (elm2); \ | ||
469 | } while (0) | ||
470 | |||
471 | /* | ||
472 | * Circular queue definitions. | ||
473 | */ | ||
474 | #define CIRCLEQ_HEAD(name, type) \ | ||
475 | struct name { \ | ||
476 | struct type *cqh_first; /* first element */ \ | ||
477 | struct type *cqh_last; /* last element */ \ | ||
478 | } | ||
479 | |||
480 | #define CIRCLEQ_HEAD_INITIALIZER(head) \ | ||
481 | { CIRCLEQ_END(&head), CIRCLEQ_END(&head) } | ||
482 | |||
483 | #define CIRCLEQ_ENTRY(type) \ | ||
484 | struct { \ | ||
485 | struct type *cqe_next; /* next element */ \ | ||
486 | struct type *cqe_prev; /* previous element */ \ | ||
487 | } | ||
488 | |||
489 | /* | ||
490 | * Circular queue access methods | ||
491 | */ | ||
492 | #define CIRCLEQ_FIRST(head) ((head)->cqh_first) | ||
493 | #define CIRCLEQ_LAST(head) ((head)->cqh_last) | ||
494 | #define CIRCLEQ_END(head) ((void *)(head)) | ||
495 | #define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next) | ||
496 | #define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev) | ||
497 | #define CIRCLEQ_EMPTY(head) \ | ||
498 | (CIRCLEQ_FIRST(head) == CIRCLEQ_END(head)) | ||
499 | |||
500 | #define CIRCLEQ_FOREACH(var, head, field) \ | ||
501 | for((var) = CIRCLEQ_FIRST(head); \ | ||
502 | (var) != CIRCLEQ_END(head); \ | ||
503 | (var) = CIRCLEQ_NEXT(var, field)) | ||
504 | |||
505 | #define CIRCLEQ_FOREACH_REVERSE(var, head, field) \ | ||
506 | for((var) = CIRCLEQ_LAST(head); \ | ||
507 | (var) != CIRCLEQ_END(head); \ | ||
508 | (var) = CIRCLEQ_PREV(var, field)) | ||
509 | |||
510 | /* | ||
511 | * Circular queue functions. | ||
512 | */ | ||
513 | #define CIRCLEQ_INIT(head) do { \ | ||
514 | (head)->cqh_first = CIRCLEQ_END(head); \ | ||
515 | (head)->cqh_last = CIRCLEQ_END(head); \ | ||
516 | } while (0) | ||
517 | |||
518 | #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ | ||
519 | (elm)->field.cqe_next = (listelm)->field.cqe_next; \ | ||
520 | (elm)->field.cqe_prev = (listelm); \ | ||
521 | if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \ | ||
522 | (head)->cqh_last = (elm); \ | ||
523 | else \ | ||
524 | (listelm)->field.cqe_next->field.cqe_prev = (elm); \ | ||
525 | (listelm)->field.cqe_next = (elm); \ | ||
526 | } while (0) | ||
527 | |||
528 | #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \ | ||
529 | (elm)->field.cqe_next = (listelm); \ | ||
530 | (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ | ||
531 | if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \ | ||
532 | (head)->cqh_first = (elm); \ | ||
533 | else \ | ||
534 | (listelm)->field.cqe_prev->field.cqe_next = (elm); \ | ||
535 | (listelm)->field.cqe_prev = (elm); \ | ||
536 | } while (0) | ||
537 | |||
538 | #define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \ | ||
539 | (elm)->field.cqe_next = (head)->cqh_first; \ | ||
540 | (elm)->field.cqe_prev = CIRCLEQ_END(head); \ | ||
541 | if ((head)->cqh_last == CIRCLEQ_END(head)) \ | ||
542 | (head)->cqh_last = (elm); \ | ||
543 | else \ | ||
544 | (head)->cqh_first->field.cqe_prev = (elm); \ | ||
545 | (head)->cqh_first = (elm); \ | ||
546 | } while (0) | ||
547 | |||
548 | #define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \ | ||
549 | (elm)->field.cqe_next = CIRCLEQ_END(head); \ | ||
550 | (elm)->field.cqe_prev = (head)->cqh_last; \ | ||
551 | if ((head)->cqh_first == CIRCLEQ_END(head)) \ | ||
552 | (head)->cqh_first = (elm); \ | ||
553 | else \ | ||
554 | (head)->cqh_last->field.cqe_next = (elm); \ | ||
555 | (head)->cqh_last = (elm); \ | ||
556 | } while (0) | ||
557 | |||
558 | #define CIRCLEQ_REMOVE(head, elm, field) do { \ | ||
559 | if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \ | ||
560 | (head)->cqh_last = (elm)->field.cqe_prev; \ | ||
561 | else \ | ||
562 | (elm)->field.cqe_next->field.cqe_prev = \ | ||
563 | (elm)->field.cqe_prev; \ | ||
564 | if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \ | ||
565 | (head)->cqh_first = (elm)->field.cqe_next; \ | ||
566 | else \ | ||
567 | (elm)->field.cqe_prev->field.cqe_next = \ | ||
568 | (elm)->field.cqe_next; \ | ||
569 | } while (0) | ||
570 | |||
571 | #define CIRCLEQ_REPLACE(head, elm, elm2, field) do { \ | ||
572 | if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \ | ||
573 | CIRCLEQ_END(head)) \ | ||
574 | (head).cqh_last = (elm2); \ | ||
575 | else \ | ||
576 | (elm2)->field.cqe_next->field.cqe_prev = (elm2); \ | ||
577 | if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \ | ||
578 | CIRCLEQ_END(head)) \ | ||
579 | (head).cqh_first = (elm2); \ | ||
580 | else \ | ||
581 | (elm2)->field.cqe_prev->field.cqe_next = (elm2); \ | ||
582 | } while (0) | ||
583 | |||
584 | #endif /* !_FAKE_QUEUE_H_ */ | ||
diff --git a/openbsd-compat/tree.h b/openbsd-compat/tree.h deleted file mode 100644 index 30b4a8561..000000000 --- a/openbsd-compat/tree.h +++ /dev/null | |||
@@ -1,667 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright 2002 Niels Provos <provos@citi.umich.edu> | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
24 | */ | ||
25 | |||
26 | #ifndef _SYS_TREE_H_ | ||
27 | #define _SYS_TREE_H_ | ||
28 | |||
29 | /* | ||
30 | * This file defines data structures for different types of trees: | ||
31 | * splay trees and red-black trees. | ||
32 | * | ||
33 | * A splay tree is a self-organizing data structure. Every operation | ||
34 | * on the tree causes a splay to happen. The splay moves the requested | ||
35 | * node to the root of the tree and partly rebalances it. | ||
36 | * | ||
37 | * This has the benefit that request locality causes faster lookups as | ||
38 | * the requested nodes move to the top of the tree. On the other hand, | ||
39 | * every lookup causes memory writes. | ||
40 | * | ||
41 | * The Balance Theorem bounds the total access time for m operations | ||
42 | * and n inserts on an initially empty tree as O((m + n)lg n). The | ||
43 | * amortized cost for a sequence of m accesses to a splay tree is O(lg n); | ||
44 | * | ||
45 | * A red-black tree is a binary search tree with the node color as an | ||
46 | * extra attribute. It fulfills a set of conditions: | ||
47 | * - every search path from the root to a leaf consists of the | ||
48 | * same number of black nodes, | ||
49 | * - each red node (except for the root) has a black parent, | ||
50 | * - each leaf node is black. | ||
51 | * | ||
52 | * Every operation on a red-black tree is bounded as O(lg n). | ||
53 | * The maximum height of a red-black tree is 2lg (n+1). | ||
54 | */ | ||
55 | |||
56 | #define SPLAY_HEAD(name, type) \ | ||
57 | struct name { \ | ||
58 | struct type *sph_root; /* root of the tree */ \ | ||
59 | } | ||
60 | |||
61 | #define SPLAY_INITIALIZER(root) \ | ||
62 | { NULL } | ||
63 | |||
64 | #define SPLAY_INIT(root) do { \ | ||
65 | (root)->sph_root = NULL; \ | ||
66 | } while (0) | ||
67 | |||
68 | #define SPLAY_ENTRY(type) \ | ||
69 | struct { \ | ||
70 | struct type *spe_left; /* left element */ \ | ||
71 | struct type *spe_right; /* right element */ \ | ||
72 | } | ||
73 | |||
74 | #define SPLAY_LEFT(elm, field) (elm)->field.spe_left | ||
75 | #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right | ||
76 | #define SPLAY_ROOT(head) (head)->sph_root | ||
77 | #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL) | ||
78 | |||
79 | /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */ | ||
80 | #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \ | ||
81 | SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \ | ||
82 | SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ | ||
83 | (head)->sph_root = tmp; \ | ||
84 | } while (0) | ||
85 | |||
86 | #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \ | ||
87 | SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \ | ||
88 | SPLAY_LEFT(tmp, field) = (head)->sph_root; \ | ||
89 | (head)->sph_root = tmp; \ | ||
90 | } while (0) | ||
91 | |||
92 | #define SPLAY_LINKLEFT(head, tmp, field) do { \ | ||
93 | SPLAY_LEFT(tmp, field) = (head)->sph_root; \ | ||
94 | tmp = (head)->sph_root; \ | ||
95 | (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \ | ||
96 | } while (0) | ||
97 | |||
98 | #define SPLAY_LINKRIGHT(head, tmp, field) do { \ | ||
99 | SPLAY_RIGHT(tmp, field) = (head)->sph_root; \ | ||
100 | tmp = (head)->sph_root; \ | ||
101 | (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \ | ||
102 | } while (0) | ||
103 | |||
104 | #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \ | ||
105 | SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \ | ||
106 | SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\ | ||
107 | SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \ | ||
108 | SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \ | ||
109 | } while (0) | ||
110 | |||
111 | /* Generates prototypes and inline functions */ | ||
112 | |||
113 | #define SPLAY_PROTOTYPE(name, type, field, cmp) \ | ||
114 | void name##_SPLAY(struct name *, struct type *); \ | ||
115 | void name##_SPLAY_MINMAX(struct name *, int); \ | ||
116 | \ | ||
117 | static __inline void \ | ||
118 | name##_SPLAY_INSERT(struct name *head, struct type *elm) \ | ||
119 | { \ | ||
120 | if (SPLAY_EMPTY(head)) { \ | ||
121 | SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \ | ||
122 | } else { \ | ||
123 | int __comp; \ | ||
124 | name##_SPLAY(head, elm); \ | ||
125 | __comp = (cmp)(elm, (head)->sph_root); \ | ||
126 | if(__comp < 0) { \ | ||
127 | SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\ | ||
128 | SPLAY_RIGHT(elm, field) = (head)->sph_root; \ | ||
129 | SPLAY_LEFT((head)->sph_root, field) = NULL; \ | ||
130 | } else if (__comp > 0) { \ | ||
131 | SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\ | ||
132 | SPLAY_LEFT(elm, field) = (head)->sph_root; \ | ||
133 | SPLAY_RIGHT((head)->sph_root, field) = NULL; \ | ||
134 | } else \ | ||
135 | return; \ | ||
136 | } \ | ||
137 | (head)->sph_root = (elm); \ | ||
138 | } \ | ||
139 | \ | ||
140 | static __inline void \ | ||
141 | name##_SPLAY_REMOVE(struct name *head, struct type *elm) \ | ||
142 | { \ | ||
143 | struct type *__tmp; \ | ||
144 | if (SPLAY_EMPTY(head)) \ | ||
145 | return; \ | ||
146 | name##_SPLAY(head, elm); \ | ||
147 | if ((cmp)(elm, (head)->sph_root) == 0) { \ | ||
148 | if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \ | ||
149 | (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\ | ||
150 | } else { \ | ||
151 | __tmp = SPLAY_RIGHT((head)->sph_root, field); \ | ||
152 | (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\ | ||
153 | name##_SPLAY(head, elm); \ | ||
154 | SPLAY_RIGHT((head)->sph_root, field) = __tmp; \ | ||
155 | } \ | ||
156 | } \ | ||
157 | } \ | ||
158 | \ | ||
159 | /* Finds the node with the same key as elm */ \ | ||
160 | static __inline struct type * \ | ||
161 | name##_SPLAY_FIND(struct name *head, struct type *elm) \ | ||
162 | { \ | ||
163 | if (SPLAY_EMPTY(head)) \ | ||
164 | return(NULL); \ | ||
165 | name##_SPLAY(head, elm); \ | ||
166 | if ((cmp)(elm, (head)->sph_root) == 0) \ | ||
167 | return (head->sph_root); \ | ||
168 | return (NULL); \ | ||
169 | } \ | ||
170 | \ | ||
171 | static __inline struct type * \ | ||
172 | name##_SPLAY_NEXT(struct name *head, struct type *elm) \ | ||
173 | { \ | ||
174 | name##_SPLAY(head, elm); \ | ||
175 | if (SPLAY_RIGHT(elm, field) != NULL) { \ | ||
176 | elm = SPLAY_RIGHT(elm, field); \ | ||
177 | while (SPLAY_LEFT(elm, field) != NULL) { \ | ||
178 | elm = SPLAY_LEFT(elm, field); \ | ||
179 | } \ | ||
180 | } else \ | ||
181 | elm = NULL; \ | ||
182 | return (elm); \ | ||
183 | } \ | ||
184 | \ | ||
185 | static __inline struct type * \ | ||
186 | name##_SPLAY_MIN_MAX(struct name *head, int val) \ | ||
187 | { \ | ||
188 | name##_SPLAY_MINMAX(head, val); \ | ||
189 | return (SPLAY_ROOT(head)); \ | ||
190 | } | ||
191 | |||
192 | /* Main splay operation. | ||
193 | * Moves node close to the key of elm to top | ||
194 | */ | ||
195 | #define SPLAY_GENERATE(name, type, field, cmp) \ | ||
196 | void name##_SPLAY(struct name *head, struct type *elm) \ | ||
197 | { \ | ||
198 | struct type __node, *__left, *__right, *__tmp; \ | ||
199 | int __comp; \ | ||
200 | \ | ||
201 | SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ | ||
202 | __left = __right = &__node; \ | ||
203 | \ | ||
204 | while ((__comp = (cmp)(elm, (head)->sph_root))) { \ | ||
205 | if (__comp < 0) { \ | ||
206 | __tmp = SPLAY_LEFT((head)->sph_root, field); \ | ||
207 | if (__tmp == NULL) \ | ||
208 | break; \ | ||
209 | if ((cmp)(elm, __tmp) < 0){ \ | ||
210 | SPLAY_ROTATE_RIGHT(head, __tmp, field); \ | ||
211 | if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ | ||
212 | break; \ | ||
213 | } \ | ||
214 | SPLAY_LINKLEFT(head, __right, field); \ | ||
215 | } else if (__comp > 0) { \ | ||
216 | __tmp = SPLAY_RIGHT((head)->sph_root, field); \ | ||
217 | if (__tmp == NULL) \ | ||
218 | break; \ | ||
219 | if ((cmp)(elm, __tmp) > 0){ \ | ||
220 | SPLAY_ROTATE_LEFT(head, __tmp, field); \ | ||
221 | if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ | ||
222 | break; \ | ||
223 | } \ | ||
224 | SPLAY_LINKRIGHT(head, __left, field); \ | ||
225 | } \ | ||
226 | } \ | ||
227 | SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ | ||
228 | } \ | ||
229 | \ | ||
230 | /* Splay with either the minimum or the maximum element \ | ||
231 | * Used to find minimum or maximum element in tree. \ | ||
232 | */ \ | ||
233 | void name##_SPLAY_MINMAX(struct name *head, int __comp) \ | ||
234 | { \ | ||
235 | struct type __node, *__left, *__right, *__tmp; \ | ||
236 | \ | ||
237 | SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\ | ||
238 | __left = __right = &__node; \ | ||
239 | \ | ||
240 | while (1) { \ | ||
241 | if (__comp < 0) { \ | ||
242 | __tmp = SPLAY_LEFT((head)->sph_root, field); \ | ||
243 | if (__tmp == NULL) \ | ||
244 | break; \ | ||
245 | if (__comp < 0){ \ | ||
246 | SPLAY_ROTATE_RIGHT(head, __tmp, field); \ | ||
247 | if (SPLAY_LEFT((head)->sph_root, field) == NULL)\ | ||
248 | break; \ | ||
249 | } \ | ||
250 | SPLAY_LINKLEFT(head, __right, field); \ | ||
251 | } else if (__comp > 0) { \ | ||
252 | __tmp = SPLAY_RIGHT((head)->sph_root, field); \ | ||
253 | if (__tmp == NULL) \ | ||
254 | break; \ | ||
255 | if (__comp > 0) { \ | ||
256 | SPLAY_ROTATE_LEFT(head, __tmp, field); \ | ||
257 | if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\ | ||
258 | break; \ | ||
259 | } \ | ||
260 | SPLAY_LINKRIGHT(head, __left, field); \ | ||
261 | } \ | ||
262 | } \ | ||
263 | SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \ | ||
264 | } | ||
265 | |||
266 | #define SPLAY_NEGINF -1 | ||
267 | #define SPLAY_INF 1 | ||
268 | |||
269 | #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y) | ||
270 | #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y) | ||
271 | #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y) | ||
272 | #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y) | ||
273 | #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \ | ||
274 | : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF)) | ||
275 | #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \ | ||
276 | : name##_SPLAY_MIN_MAX(x, SPLAY_INF)) | ||
277 | |||
278 | #define SPLAY_FOREACH(x, name, head) \ | ||
279 | for ((x) = SPLAY_MIN(name, head); \ | ||
280 | (x) != NULL; \ | ||
281 | (x) = SPLAY_NEXT(name, head, x)) | ||
282 | |||
283 | /* Macros that define a red-back tree */ | ||
284 | #define RB_HEAD(name, type) \ | ||
285 | struct name { \ | ||
286 | struct type *rbh_root; /* root of the tree */ \ | ||
287 | } | ||
288 | |||
289 | #define RB_INITIALIZER(root) \ | ||
290 | { NULL } | ||
291 | |||
292 | #define RB_INIT(root) do { \ | ||
293 | (root)->rbh_root = NULL; \ | ||
294 | } while (0) | ||
295 | |||
296 | #define RB_BLACK 0 | ||
297 | #define RB_RED 1 | ||
298 | #define RB_ENTRY(type) \ | ||
299 | struct { \ | ||
300 | struct type *rbe_left; /* left element */ \ | ||
301 | struct type *rbe_right; /* right element */ \ | ||
302 | struct type *rbe_parent; /* parent element */ \ | ||
303 | int rbe_color; /* node color */ \ | ||
304 | } | ||
305 | |||
306 | #define RB_LEFT(elm, field) (elm)->field.rbe_left | ||
307 | #define RB_RIGHT(elm, field) (elm)->field.rbe_right | ||
308 | #define RB_PARENT(elm, field) (elm)->field.rbe_parent | ||
309 | #define RB_COLOR(elm, field) (elm)->field.rbe_color | ||
310 | #define RB_ROOT(head) (head)->rbh_root | ||
311 | #define RB_EMPTY(head) (RB_ROOT(head) == NULL) | ||
312 | |||
313 | #define RB_SET(elm, parent, field) do { \ | ||
314 | RB_PARENT(elm, field) = parent; \ | ||
315 | RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \ | ||
316 | RB_COLOR(elm, field) = RB_RED; \ | ||
317 | } while (0) | ||
318 | |||
319 | #define RB_SET_BLACKRED(black, red, field) do { \ | ||
320 | RB_COLOR(black, field) = RB_BLACK; \ | ||
321 | RB_COLOR(red, field) = RB_RED; \ | ||
322 | } while (0) | ||
323 | |||
324 | #ifndef RB_AUGMENT | ||
325 | #define RB_AUGMENT(x) | ||
326 | #endif | ||
327 | |||
328 | #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \ | ||
329 | (tmp) = RB_RIGHT(elm, field); \ | ||
330 | if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \ | ||
331 | RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \ | ||
332 | } \ | ||
333 | RB_AUGMENT(elm); \ | ||
334 | if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \ | ||
335 | if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ | ||
336 | RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ | ||
337 | else \ | ||
338 | RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ | ||
339 | RB_AUGMENT(RB_PARENT(elm, field)); \ | ||
340 | } else \ | ||
341 | (head)->rbh_root = (tmp); \ | ||
342 | RB_LEFT(tmp, field) = (elm); \ | ||
343 | RB_PARENT(elm, field) = (tmp); \ | ||
344 | RB_AUGMENT(tmp); \ | ||
345 | } while (0) | ||
346 | |||
347 | #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \ | ||
348 | (tmp) = RB_LEFT(elm, field); \ | ||
349 | if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \ | ||
350 | RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \ | ||
351 | } \ | ||
352 | RB_AUGMENT(elm); \ | ||
353 | if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \ | ||
354 | if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \ | ||
355 | RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \ | ||
356 | else \ | ||
357 | RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \ | ||
358 | RB_AUGMENT(RB_PARENT(elm, field)); \ | ||
359 | } else \ | ||
360 | (head)->rbh_root = (tmp); \ | ||
361 | RB_RIGHT(tmp, field) = (elm); \ | ||
362 | RB_PARENT(elm, field) = (tmp); \ | ||
363 | RB_AUGMENT(tmp); \ | ||
364 | } while (0) | ||
365 | |||
366 | /* Generates prototypes and inline functions */ | ||
367 | #define RB_PROTOTYPE(name, type, field, cmp) \ | ||
368 | void name##_RB_INSERT_COLOR(struct name *, struct type *); \ | ||
369 | void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\ | ||
370 | void name##_RB_REMOVE(struct name *, struct type *); \ | ||
371 | struct type *name##_RB_INSERT(struct name *, struct type *); \ | ||
372 | struct type *name##_RB_FIND(struct name *, struct type *); \ | ||
373 | struct type *name##_RB_NEXT(struct name *, struct type *); \ | ||
374 | struct type *name##_RB_MINMAX(struct name *, int); \ | ||
375 | \ | ||
376 | |||
377 | /* Main rb operation. | ||
378 | * Moves node close to the key of elm to top | ||
379 | */ | ||
380 | #define RB_GENERATE(name, type, field, cmp) \ | ||
381 | void \ | ||
382 | name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \ | ||
383 | { \ | ||
384 | struct type *parent, *gparent, *tmp; \ | ||
385 | while ((parent = RB_PARENT(elm, field)) && \ | ||
386 | RB_COLOR(parent, field) == RB_RED) { \ | ||
387 | gparent = RB_PARENT(parent, field); \ | ||
388 | if (parent == RB_LEFT(gparent, field)) { \ | ||
389 | tmp = RB_RIGHT(gparent, field); \ | ||
390 | if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ | ||
391 | RB_COLOR(tmp, field) = RB_BLACK; \ | ||
392 | RB_SET_BLACKRED(parent, gparent, field);\ | ||
393 | elm = gparent; \ | ||
394 | continue; \ | ||
395 | } \ | ||
396 | if (RB_RIGHT(parent, field) == elm) { \ | ||
397 | RB_ROTATE_LEFT(head, parent, tmp, field);\ | ||
398 | tmp = parent; \ | ||
399 | parent = elm; \ | ||
400 | elm = tmp; \ | ||
401 | } \ | ||
402 | RB_SET_BLACKRED(parent, gparent, field); \ | ||
403 | RB_ROTATE_RIGHT(head, gparent, tmp, field); \ | ||
404 | } else { \ | ||
405 | tmp = RB_LEFT(gparent, field); \ | ||
406 | if (tmp && RB_COLOR(tmp, field) == RB_RED) { \ | ||
407 | RB_COLOR(tmp, field) = RB_BLACK; \ | ||
408 | RB_SET_BLACKRED(parent, gparent, field);\ | ||
409 | elm = gparent; \ | ||
410 | continue; \ | ||
411 | } \ | ||
412 | if (RB_LEFT(parent, field) == elm) { \ | ||
413 | RB_ROTATE_RIGHT(head, parent, tmp, field);\ | ||
414 | tmp = parent; \ | ||
415 | parent = elm; \ | ||
416 | elm = tmp; \ | ||
417 | } \ | ||
418 | RB_SET_BLACKRED(parent, gparent, field); \ | ||
419 | RB_ROTATE_LEFT(head, gparent, tmp, field); \ | ||
420 | } \ | ||
421 | } \ | ||
422 | RB_COLOR(head->rbh_root, field) = RB_BLACK; \ | ||
423 | } \ | ||
424 | \ | ||
425 | void \ | ||
426 | name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \ | ||
427 | { \ | ||
428 | struct type *tmp; \ | ||
429 | while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \ | ||
430 | elm != RB_ROOT(head)) { \ | ||
431 | if (RB_LEFT(parent, field) == elm) { \ | ||
432 | tmp = RB_RIGHT(parent, field); \ | ||
433 | if (RB_COLOR(tmp, field) == RB_RED) { \ | ||
434 | RB_SET_BLACKRED(tmp, parent, field); \ | ||
435 | RB_ROTATE_LEFT(head, parent, tmp, field);\ | ||
436 | tmp = RB_RIGHT(parent, field); \ | ||
437 | } \ | ||
438 | if ((RB_LEFT(tmp, field) == NULL || \ | ||
439 | RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ | ||
440 | (RB_RIGHT(tmp, field) == NULL || \ | ||
441 | RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ | ||
442 | RB_COLOR(tmp, field) = RB_RED; \ | ||
443 | elm = parent; \ | ||
444 | parent = RB_PARENT(elm, field); \ | ||
445 | } else { \ | ||
446 | if (RB_RIGHT(tmp, field) == NULL || \ | ||
447 | RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\ | ||
448 | struct type *oleft; \ | ||
449 | if ((oleft = RB_LEFT(tmp, field)))\ | ||
450 | RB_COLOR(oleft, field) = RB_BLACK;\ | ||
451 | RB_COLOR(tmp, field) = RB_RED; \ | ||
452 | RB_ROTATE_RIGHT(head, tmp, oleft, field);\ | ||
453 | tmp = RB_RIGHT(parent, field); \ | ||
454 | } \ | ||
455 | RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ | ||
456 | RB_COLOR(parent, field) = RB_BLACK; \ | ||
457 | if (RB_RIGHT(tmp, field)) \ | ||
458 | RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\ | ||
459 | RB_ROTATE_LEFT(head, parent, tmp, field);\ | ||
460 | elm = RB_ROOT(head); \ | ||
461 | break; \ | ||
462 | } \ | ||
463 | } else { \ | ||
464 | tmp = RB_LEFT(parent, field); \ | ||
465 | if (RB_COLOR(tmp, field) == RB_RED) { \ | ||
466 | RB_SET_BLACKRED(tmp, parent, field); \ | ||
467 | RB_ROTATE_RIGHT(head, parent, tmp, field);\ | ||
468 | tmp = RB_LEFT(parent, field); \ | ||
469 | } \ | ||
470 | if ((RB_LEFT(tmp, field) == NULL || \ | ||
471 | RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\ | ||
472 | (RB_RIGHT(tmp, field) == NULL || \ | ||
473 | RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\ | ||
474 | RB_COLOR(tmp, field) = RB_RED; \ | ||
475 | elm = parent; \ | ||
476 | parent = RB_PARENT(elm, field); \ | ||
477 | } else { \ | ||
478 | if (RB_LEFT(tmp, field) == NULL || \ | ||
479 | RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\ | ||
480 | struct type *oright; \ | ||
481 | if ((oright = RB_RIGHT(tmp, field)))\ | ||
482 | RB_COLOR(oright, field) = RB_BLACK;\ | ||
483 | RB_COLOR(tmp, field) = RB_RED; \ | ||
484 | RB_ROTATE_LEFT(head, tmp, oright, field);\ | ||
485 | tmp = RB_LEFT(parent, field); \ | ||
486 | } \ | ||
487 | RB_COLOR(tmp, field) = RB_COLOR(parent, field);\ | ||
488 | RB_COLOR(parent, field) = RB_BLACK; \ | ||
489 | if (RB_LEFT(tmp, field)) \ | ||
490 | RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\ | ||
491 | RB_ROTATE_RIGHT(head, parent, tmp, field);\ | ||
492 | elm = RB_ROOT(head); \ | ||
493 | break; \ | ||
494 | } \ | ||
495 | } \ | ||
496 | } \ | ||
497 | if (elm) \ | ||
498 | RB_COLOR(elm, field) = RB_BLACK; \ | ||
499 | } \ | ||
500 | \ | ||
501 | void \ | ||
502 | name##_RB_REMOVE(struct name *head, struct type *elm) \ | ||
503 | { \ | ||
504 | struct type *child, *parent; \ | ||
505 | int color; \ | ||
506 | if (RB_LEFT(elm, field) == NULL) \ | ||
507 | child = RB_RIGHT(elm, field); \ | ||
508 | else if (RB_RIGHT(elm, field) == NULL) \ | ||
509 | child = RB_LEFT(elm, field); \ | ||
510 | else { \ | ||
511 | struct type *old = elm, *left; \ | ||
512 | elm = RB_RIGHT(elm, field); \ | ||
513 | while ((left = RB_LEFT(elm, field))) \ | ||
514 | elm = left; \ | ||
515 | child = RB_RIGHT(elm, field); \ | ||
516 | parent = RB_PARENT(elm, field); \ | ||
517 | color = RB_COLOR(elm, field); \ | ||
518 | if (child) \ | ||
519 | RB_PARENT(child, field) = parent; \ | ||
520 | if (parent) { \ | ||
521 | if (RB_LEFT(parent, field) == elm) \ | ||
522 | RB_LEFT(parent, field) = child; \ | ||
523 | else \ | ||
524 | RB_RIGHT(parent, field) = child; \ | ||
525 | RB_AUGMENT(parent); \ | ||
526 | } else \ | ||
527 | RB_ROOT(head) = child; \ | ||
528 | if (RB_PARENT(elm, field) == old) \ | ||
529 | parent = elm; \ | ||
530 | (elm)->field = (old)->field; \ | ||
531 | if (RB_PARENT(old, field)) { \ | ||
532 | if (RB_LEFT(RB_PARENT(old, field), field) == old)\ | ||
533 | RB_LEFT(RB_PARENT(old, field), field) = elm;\ | ||
534 | else \ | ||
535 | RB_RIGHT(RB_PARENT(old, field), field) = elm;\ | ||
536 | RB_AUGMENT(RB_PARENT(old, field)); \ | ||
537 | } else \ | ||
538 | RB_ROOT(head) = elm; \ | ||
539 | RB_PARENT(RB_LEFT(old, field), field) = elm; \ | ||
540 | if (RB_RIGHT(old, field)) \ | ||
541 | RB_PARENT(RB_RIGHT(old, field), field) = elm; \ | ||
542 | if (parent) { \ | ||
543 | left = parent; \ | ||
544 | do { \ | ||
545 | RB_AUGMENT(left); \ | ||
546 | } while ((left = RB_PARENT(left, field))); \ | ||
547 | } \ | ||
548 | goto color; \ | ||
549 | } \ | ||
550 | parent = RB_PARENT(elm, field); \ | ||
551 | color = RB_COLOR(elm, field); \ | ||
552 | if (child) \ | ||
553 | RB_PARENT(child, field) = parent; \ | ||
554 | if (parent) { \ | ||
555 | if (RB_LEFT(parent, field) == elm) \ | ||
556 | RB_LEFT(parent, field) = child; \ | ||
557 | else \ | ||
558 | RB_RIGHT(parent, field) = child; \ | ||
559 | RB_AUGMENT(parent); \ | ||
560 | } else \ | ||
561 | RB_ROOT(head) = child; \ | ||
562 | color: \ | ||
563 | if (color == RB_BLACK) \ | ||
564 | name##_RB_REMOVE_COLOR(head, parent, child); \ | ||
565 | } \ | ||
566 | \ | ||
567 | /* Inserts a node into the RB tree */ \ | ||
568 | struct type * \ | ||
569 | name##_RB_INSERT(struct name *head, struct type *elm) \ | ||
570 | { \ | ||
571 | struct type *tmp; \ | ||
572 | struct type *parent = NULL; \ | ||
573 | int comp = 0; \ | ||
574 | tmp = RB_ROOT(head); \ | ||
575 | while (tmp) { \ | ||
576 | parent = tmp; \ | ||
577 | comp = (cmp)(elm, parent); \ | ||
578 | if (comp < 0) \ | ||
579 | tmp = RB_LEFT(tmp, field); \ | ||
580 | else if (comp > 0) \ | ||
581 | tmp = RB_RIGHT(tmp, field); \ | ||
582 | else \ | ||
583 | return (tmp); \ | ||
584 | } \ | ||
585 | RB_SET(elm, parent, field); \ | ||
586 | if (parent != NULL) { \ | ||
587 | if (comp < 0) \ | ||
588 | RB_LEFT(parent, field) = elm; \ | ||
589 | else \ | ||
590 | RB_RIGHT(parent, field) = elm; \ | ||
591 | RB_AUGMENT(parent); \ | ||
592 | } else \ | ||
593 | RB_ROOT(head) = elm; \ | ||
594 | name##_RB_INSERT_COLOR(head, elm); \ | ||
595 | return (NULL); \ | ||
596 | } \ | ||
597 | \ | ||
598 | /* Finds the node with the same key as elm */ \ | ||
599 | struct type * \ | ||
600 | name##_RB_FIND(struct name *head, struct type *elm) \ | ||
601 | { \ | ||
602 | struct type *tmp = RB_ROOT(head); \ | ||
603 | int comp; \ | ||
604 | while (tmp) { \ | ||
605 | comp = cmp(elm, tmp); \ | ||
606 | if (comp < 0) \ | ||
607 | tmp = RB_LEFT(tmp, field); \ | ||
608 | else if (comp > 0) \ | ||
609 | tmp = RB_RIGHT(tmp, field); \ | ||
610 | else \ | ||
611 | return (tmp); \ | ||
612 | } \ | ||
613 | return (NULL); \ | ||
614 | } \ | ||
615 | \ | ||
616 | struct type * \ | ||
617 | name##_RB_NEXT(struct name *head, struct type *elm) \ | ||
618 | { \ | ||
619 | if (RB_RIGHT(elm, field)) { \ | ||
620 | elm = RB_RIGHT(elm, field); \ | ||
621 | while (RB_LEFT(elm, field)) \ | ||
622 | elm = RB_LEFT(elm, field); \ | ||
623 | } else { \ | ||
624 | if (RB_PARENT(elm, field) && \ | ||
625 | (elm == RB_LEFT(RB_PARENT(elm, field), field))) \ | ||
626 | elm = RB_PARENT(elm, field); \ | ||
627 | else { \ | ||
628 | while (RB_PARENT(elm, field) && \ | ||
629 | (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\ | ||
630 | elm = RB_PARENT(elm, field); \ | ||
631 | elm = RB_PARENT(elm, field); \ | ||
632 | } \ | ||
633 | } \ | ||
634 | return (elm); \ | ||
635 | } \ | ||
636 | \ | ||
637 | struct type * \ | ||
638 | name##_RB_MINMAX(struct name *head, int val) \ | ||
639 | { \ | ||
640 | struct type *tmp = RB_ROOT(head); \ | ||
641 | struct type *parent = NULL; \ | ||
642 | while (tmp) { \ | ||
643 | parent = tmp; \ | ||
644 | if (val < 0) \ | ||
645 | tmp = RB_LEFT(tmp, field); \ | ||
646 | else \ | ||
647 | tmp = RB_RIGHT(tmp, field); \ | ||
648 | } \ | ||
649 | return (parent); \ | ||
650 | } | ||
651 | |||
652 | #define RB_NEGINF -1 | ||
653 | #define RB_INF 1 | ||
654 | |||
655 | #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y) | ||
656 | #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y) | ||
657 | #define RB_FIND(name, x, y) name##_RB_FIND(x, y) | ||
658 | #define RB_NEXT(name, x, y) name##_RB_NEXT(x, y) | ||
659 | #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF) | ||
660 | #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF) | ||
661 | |||
662 | #define RB_FOREACH(x, name, head) \ | ||
663 | for ((x) = RB_MIN(name, head); \ | ||
664 | (x) != NULL; \ | ||
665 | (x) = name##_RB_NEXT(head, x)) | ||
666 | |||
667 | #endif /* _SYS_TREE_H_ */ | ||