Look in the configured sbin directory for the tincd binary.
[tinc] / lib / avl_tree.c
1 /*
2     avl_tree.c -- avl_ tree and linked list convenience
3     Copyright (C) 1998 Michael H. Buselli
4                   2000-2005 Ivo Timmermans,
5                   2000-2006 Guus Sliepen <guus@tinc-vpn.org>
6                   2000-2005 Wessel Dankers <wsl@tinc-vpn.org>
7
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22     Original AVL tree library by Michael H. Buselli <cosine@cosine.org>.
23
24     Modified 2000-11-28 by Wessel Dankers <wsl@tinc-vpn.org> to use counts
25     instead of depths, to add the ->next and ->prev and to generally obfuscate
26     the code. Mail me if you found a bug.
27
28     Cleaned up and incorporated some of the ideas from the red-black tree
29     library for inclusion into tinc (http://www.tinc-vpn.org/) by
30     Guus Sliepen <guus@tinc-vpn.org>.
31
32     $Id$
33 */
34
35 #include "system.h"
36
37 #include "avl_tree.h"
38 #include "xalloc.h"
39
40 #ifdef AVL_COUNT
41 #define AVL_NODE_COUNT(n)  ((n) ? (n)->count : 0)
42 #define AVL_L_COUNT(n)     (AVL_NODE_COUNT((n)->left))
43 #define AVL_R_COUNT(n)     (AVL_NODE_COUNT((n)->right))
44 #define AVL_CALC_COUNT(n)  (AVL_L_COUNT(n) + AVL_R_COUNT(n) + 1)
45 #endif
46
47 #ifdef AVL_DEPTH
48 #define AVL_NODE_DEPTH(n)  ((n) ? (n)->depth : 0)
49 #define L_AVL_DEPTH(n)     (AVL_NODE_DEPTH((n)->left))
50 #define R_AVL_DEPTH(n)     (AVL_NODE_DEPTH((n)->right))
51 #define AVL_CALC_DEPTH(n)  ((L_AVL_DEPTH(n)>R_AVL_DEPTH(n)?L_AVL_DEPTH(n):R_AVL_DEPTH(n)) + 1)
52 #endif
53
54 #ifndef AVL_DEPTH
55 static int lg(unsigned int u) __attribute__ ((__const__));
56
57 static int lg(unsigned int u) {
58         int r = 1;
59
60         if(!u)
61                 return 0;
62
63         if(u & 0xffff0000) {
64                 u >>= 16;
65                 r += 16;
66         }
67
68         if(u & 0x0000ff00) {
69                 u >>= 8;
70                 r += 8;
71         }
72
73         if(u & 0x000000f0) {
74                 u >>= 4;
75                 r += 4;
76         }
77
78         if(u & 0x0000000c) {
79                 u >>= 2;
80                 r += 2;
81         }
82
83         if(u & 0x00000002)
84                 r++;
85
86         return r;
87 }
88 #endif
89
90 /* Internal helper functions */
91
92 static int avl_check_balance(const avl_node_t *node) {
93 #ifdef AVL_DEPTH
94         int d;
95
96         d = R_AVL_DEPTH(node) - L_AVL_DEPTH(node);
97
98         return d < -1 ? -1 : d > 1 ? 1 : 0;
99 #else
100 /*      int d;
101  *      d = lg(AVL_R_COUNT(node)) - lg(AVL_L_COUNT(node));
102  *      d = d<-1?-1:d>1?1:0;
103  */
104         int pl, r;
105
106         pl = lg(AVL_L_COUNT(node));
107         r = AVL_R_COUNT(node);
108
109         if(r >> pl + 1)
110                 return 1;
111
112         if(pl < 2 || r >> pl - 2)
113                 return 0;
114
115         return -1;
116 #endif
117 }
118
119 static void avl_rebalance(avl_tree_t *tree, avl_node_t *node) {
120         avl_node_t *child;
121         avl_node_t *gchild;
122         avl_node_t *parent;
123         avl_node_t **superparent;
124
125         parent = node;
126
127         while(node) {
128                 parent = node->parent;
129
130                 superparent =
131                         parent ? node ==
132                         parent->left ? &parent->left : &parent->right : &tree->root;
133
134                 switch (avl_check_balance(node)) {
135                         case -1:
136                                 child = node->left;
137 #ifdef AVL_DEPTH
138                                 if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
139 #else
140                                 if(AVL_L_COUNT(child) >= AVL_R_COUNT(child)) {
141 #endif
142                                         node->left = child->right;
143                                         if(node->left)
144                                                 node->left->parent = node;
145
146                                         child->right = node;
147                                         node->parent = child;
148                                         *superparent = child;
149                                         child->parent = parent;
150 #ifdef AVL_COUNT
151                                         node->count = AVL_CALC_COUNT(node);
152                                         child->count = AVL_CALC_COUNT(child);
153 #endif
154 #ifdef AVL_DEPTH
155                                         node->depth = AVL_CALC_DEPTH(node);
156                                         child->depth = AVL_CALC_DEPTH(child);
157 #endif
158                                 } else {
159                                         gchild = child->right;
160                                         node->left = gchild->right;
161
162                                         if(node->left)
163                                                 node->left->parent = node;
164                                         child->right = gchild->left;
165
166                                         if(child->right)
167                                                 child->right->parent = child;
168                                         gchild->right = node;
169
170                                         if(gchild->right)
171                                                 gchild->right->parent = gchild;
172                                         gchild->left = child;
173
174                                         if(gchild->left)
175                                                 gchild->left->parent = gchild;
176                                         *superparent = gchild;
177
178                                         gchild->parent = parent;
179 #ifdef AVL_COUNT
180                                         node->count = AVL_CALC_COUNT(node);
181                                         child->count = AVL_CALC_COUNT(child);
182                                         gchild->count = AVL_CALC_COUNT(gchild);
183 #endif
184 #ifdef AVL_DEPTH
185                                         node->depth = AVL_CALC_DEPTH(node);
186                                         child->depth = AVL_CALC_DEPTH(child);
187                                         gchild->depth = AVL_CALC_DEPTH(gchild);
188 #endif
189                                 }
190                                 break;
191
192                         case 1:
193                                 child = node->right;
194 #ifdef AVL_DEPTH
195                                 if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
196 #else
197                                 if(AVL_R_COUNT(child) >= AVL_L_COUNT(child)) {
198 #endif
199                                         node->right = child->left;
200                                         if(node->right)
201                                                 node->right->parent = node;
202                                         child->left = node;
203                                         node->parent = child;
204                                         *superparent = child;
205                                         child->parent = parent;
206 #ifdef AVL_COUNT
207                                         node->count = AVL_CALC_COUNT(node);
208                                         child->count = AVL_CALC_COUNT(child);
209 #endif
210 #ifdef AVL_DEPTH
211                                         node->depth = AVL_CALC_DEPTH(node);
212                                         child->depth = AVL_CALC_DEPTH(child);
213 #endif
214                                 } else {
215                                         gchild = child->left;
216                                         node->right = gchild->left;
217
218                                         if(node->right)
219                                                 node->right->parent = node;
220                                         child->left = gchild->right;
221
222                                         if(child->left)
223                                                 child->left->parent = child;
224                                         gchild->left = node;
225
226                                         if(gchild->left)
227                                                 gchild->left->parent = gchild;
228                                         gchild->right = child;
229
230                                         if(gchild->right)
231                                                 gchild->right->parent = gchild;
232
233                                         *superparent = gchild;
234                                         gchild->parent = parent;
235 #ifdef AVL_COUNT
236                                         node->count = AVL_CALC_COUNT(node);
237                                         child->count = AVL_CALC_COUNT(child);
238                                         gchild->count = AVL_CALC_COUNT(gchild);
239 #endif
240 #ifdef AVL_DEPTH
241                                         node->depth = AVL_CALC_DEPTH(node);
242                                         child->depth = AVL_CALC_DEPTH(child);
243                                         gchild->depth = AVL_CALC_DEPTH(gchild);
244 #endif
245                                 }
246                                 break;
247
248                         default:
249 #ifdef AVL_COUNT
250                                 node->count = AVL_CALC_COUNT(node);
251 #endif
252 #ifdef AVL_DEPTH
253                                 node->depth = AVL_CALC_DEPTH(node);
254 #endif
255                 }
256                 node = parent;
257         }
258 }
259
260 /* (De)constructors */
261
262 avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete) {
263         avl_tree_t *tree;
264
265         tree = xmalloc_and_zero(sizeof(avl_tree_t));
266         tree->compare = compare;
267         tree->delete = delete;
268
269         return tree;
270 }
271
272 void avl_free_tree(avl_tree_t *tree) {
273         free(tree);
274 }
275
276 avl_node_t *avl_alloc_node(void) {
277         return xmalloc_and_zero(sizeof(avl_node_t));
278 }
279
280 void avl_free_node(avl_tree_t *tree, avl_node_t *node) {
281         if(node->data && tree->delete)
282                 tree->delete(node->data);
283
284         free(node);
285 }
286
287 /* Searching */
288
289 void *avl_search(const avl_tree_t *tree, const void *data) {
290         avl_node_t *node;
291
292         node = avl_search_node(tree, data);
293
294         return node ? node->data : NULL;
295 }
296
297 void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result) {
298         avl_node_t *node;
299
300         node = avl_search_closest_node(tree, data, result);
301
302         return node ? node->data : NULL;
303 }
304
305 void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data) {
306         avl_node_t *node;
307
308         node = avl_search_closest_smaller_node(tree, data);
309
310         return node ? node->data : NULL;
311 }
312
313 void *avl_search_closest_greater(const avl_tree_t *tree, const void *data) {
314         avl_node_t *node;
315
316         node = avl_search_closest_greater_node(tree, data);
317
318         return node ? node->data : NULL;
319 }
320
321 avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data) {
322         avl_node_t *node;
323         int result;
324
325         node = avl_search_closest_node(tree, data, &result);
326
327         return result ? NULL : node;
328 }
329
330 avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data,
331                                                                         int *result) {
332         avl_node_t *node;
333         int c;
334
335         node = tree->root;
336
337         if(!node) {
338                 if(result)
339                         *result = 0;
340                 return NULL;
341         }
342
343         for(;;) {
344                 c = tree->compare(data, node->data);
345
346                 if(c < 0) {
347                         if(node->left)
348                                 node = node->left;
349                         else {
350                                 if(result)
351                                         *result = -1;
352                                 break;
353                         }
354                 } else if(c > 0) {
355                         if(node->right)
356                                 node = node->right;
357                         else {
358                                 if(result)
359                                         *result = 1;
360                                 break;
361                         }
362                 } else {
363                         if(result)
364                                 *result = 0;
365                         break;
366                 }
367         }
368
369         return node;
370 }
371
372 avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree,
373                                                                                         const void *data) {
374         avl_node_t *node;
375         int result;
376
377         node = avl_search_closest_node(tree, data, &result);
378
379         if(result < 0)
380                 node = node->prev;
381
382         return node;
383 }
384
385 avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree,
386                                                                                         const void *data) {
387         avl_node_t *node;
388         int result;
389
390         node = avl_search_closest_node(tree, data, &result);
391
392         if(result > 0)
393                 node = node->next;
394
395         return node;
396 }
397
398 /* Insertion and deletion */
399
400 avl_node_t *avl_insert(avl_tree_t *tree, void *data) {
401         avl_node_t *closest, *new;
402         int result;
403
404         if(!tree->root) {
405                 new = avl_alloc_node();
406                 new->data = data;
407                 avl_insert_top(tree, new);
408         } else {
409                 closest = avl_search_closest_node(tree, data, &result);
410
411                 switch (result) {
412                         case -1:
413                                 new = avl_alloc_node();
414                                 new->data = data;
415                                 avl_insert_before(tree, closest, new);
416                                 break;
417
418                         case 1:
419                                 new = avl_alloc_node();
420                                 new->data = data;
421                                 avl_insert_after(tree, closest, new);
422                                 break;
423
424                         default:
425                                 return NULL;
426                 }
427         }
428
429 #ifdef AVL_COUNT
430         new->count = 1;
431 #endif
432 #ifdef AVL_DEPTH
433         new->depth = 1;
434 #endif
435
436         return new;
437 }
438
439 avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node) {
440         avl_node_t *closest;
441         int result;
442
443         if(!tree->root)
444                 avl_insert_top(tree, node);
445         else {
446                 closest = avl_search_closest_node(tree, node->data, &result);
447
448                 switch (result) {
449                         case -1:
450                                 avl_insert_before(tree, closest, node);
451                                 break;
452
453                         case 1:
454                                 avl_insert_after(tree, closest, node);
455                                 break;
456
457                         case 0:
458                                 return NULL;
459                 }
460         }
461
462 #ifdef AVL_COUNT
463         node->count = 1;
464 #endif
465 #ifdef AVL_DEPTH
466         node->depth = 1;
467 #endif
468
469         return node;
470 }
471
472 void avl_insert_top(avl_tree_t *tree, avl_node_t *node) {
473         node->prev = node->next = node->parent = NULL;
474         tree->head = tree->tail = tree->root = node;
475 }
476
477 void avl_insert_before(avl_tree_t *tree, avl_node_t *before,
478                                            avl_node_t *node) {
479         if(!before) {
480                 if(tree->tail)
481                         avl_insert_after(tree, tree->tail, node);
482                 else
483                         avl_insert_top(tree, node);
484                 return;
485         }
486
487         node->next = before;
488         node->parent = before;
489         node->prev = before->prev;
490
491         if(before->left) {
492                 avl_insert_after(tree, before->prev, node);
493                 return;
494         }
495
496         if(before->prev)
497                 before->prev->next = node;
498         else
499                 tree->head = node;
500
501         before->prev = node;
502         before->left = node;
503
504         avl_rebalance(tree, before);
505 }
506
507 void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node) {
508         if(!after) {
509                 if(tree->head)
510                         avl_insert_before(tree, tree->head, node);
511                 else
512                         avl_insert_top(tree, node);
513                 return;
514         }
515
516         if(after->right) {
517                 avl_insert_before(tree, after->next, node);
518                 return;
519         }
520
521         node->prev = after;
522         node->parent = after;
523         node->next = after->next;
524
525         if(after->next)
526                 after->next->prev = node;
527         else
528                 tree->tail = node;
529
530         after->next = node;
531         after->right = node;
532
533         avl_rebalance(tree, after);
534 }
535
536 avl_node_t *avl_unlink(avl_tree_t *tree, void *data) {
537         avl_node_t *node;
538
539         node = avl_search_node(tree, data);
540
541         if(node)
542                 avl_unlink_node(tree, node);
543
544         return node;
545 }
546
547 void avl_unlink_node(avl_tree_t *tree, avl_node_t *node) {
548         avl_node_t *parent;
549         avl_node_t **superparent;
550         avl_node_t *subst, *left, *right;
551         avl_node_t *balnode;
552
553         if(node->prev)
554                 node->prev->next = node->next;
555         else
556                 tree->head = node->next;
557         if(node->next)
558                 node->next->prev = node->prev;
559         else
560                 tree->tail = node->prev;
561
562         parent = node->parent;
563
564         superparent =
565                 parent ? node ==
566                 parent->left ? &parent->left : &parent->right : &tree->root;
567
568         left = node->left;
569         right = node->right;
570         if(!left) {
571                 *superparent = right;
572
573                 if(right)
574                         right->parent = parent;
575
576                 balnode = parent;
577         } else if(!right) {
578                 *superparent = left;
579                 left->parent = parent;
580                 balnode = parent;
581         } else {
582                 subst = node->prev;
583
584                 if(subst == left) {
585                         balnode = subst;
586                 } else {
587                         balnode = subst->parent;
588                         balnode->right = subst->left;
589
590                         if(balnode->right)
591                                 balnode->right->parent = balnode;
592
593                         subst->left = left;
594                         left->parent = subst;
595                 }
596
597                 subst->right = right;
598                 subst->parent = parent;
599                 right->parent = subst;
600                 *superparent = subst;
601         }
602
603         avl_rebalance(tree, balnode);
604
605         node->next = node->prev = node->parent = node->left = node->right = NULL;
606
607 #ifdef AVL_COUNT
608         node->count = 0;
609 #endif
610 #ifdef AVL_DEPTH
611         node->depth = 0;
612 #endif
613 }
614
615 void avl_delete_node(avl_tree_t *tree, avl_node_t *node) {
616         avl_unlink_node(tree, node);
617         avl_free_node(tree, node);
618 }
619
620 void avl_delete(avl_tree_t *tree, void *data) {
621         avl_node_t *node;
622
623         node = avl_search_node(tree, data);
624
625         if(node)
626                 avl_delete_node(tree, node);
627 }
628
629 /* Fast tree cleanup */
630
631 void avl_delete_tree(avl_tree_t *tree) {
632         avl_node_t *node, *next;
633
634         for(node = tree->head; node; node = next) {
635                 next = node->next;
636                 avl_free_node(tree, node);
637         }
638
639         avl_free_tree(tree);
640 }
641
642 /* Tree walking */
643
644 void avl_foreach(const avl_tree_t *tree, avl_action_t action) {
645         avl_node_t *node, *next;
646
647         for(node = tree->head; node; node = next) {
648                 next = node->next;
649                 action(node->data);
650         }
651 }
652
653 void avl_foreach_node(const avl_tree_t *tree, avl_action_t action) {
654         avl_node_t *node, *next;
655
656         for(node = tree->head; node; node = next) {
657                 next = node->next;
658                 action(node);
659         }
660 }
661
662 /* Indexing */
663
664 #ifdef AVL_COUNT
665 unsigned int avl_count(const avl_tree_t *tree) {
666         return AVL_NODE_COUNT(tree->root);
667 }
668
669 avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index) {
670         avl_node_t *node;
671         unsigned int c;
672
673         node = tree->root;
674
675         while(node) {
676                 c = AVL_L_COUNT(node);
677
678                 if(index < c) {
679                         node = node->left;
680                 } else if(index > c) {
681                         node = node->right;
682                         index -= c + 1;
683                 } else {
684                         return node;
685                 }
686         }
687
688         return NULL;
689 }
690
691 unsigned int avl_index(const avl_node_t *node) {
692         avl_node_t *next;
693         unsigned int index;
694
695         index = AVL_L_COUNT(node);
696
697         while((next = node->parent)) {
698                 if(node == next->right)
699                         index += AVL_L_COUNT(next) + 1;
700                 node = next;
701         }
702
703         return index;
704 }
705 #endif
706 #ifdef AVL_DEPTH
707 unsigned int avl_depth(const avl_tree_t *tree) {
708         return AVL_NODE_DEPTH(tree->root);
709 }
710 #endif