Don't send MTU probes to nodes we can't reach directly.
[tinc] / src / splay_tree.c
1 /*
2     splay_tree.c -- splay tree and linked list convenience
3     Copyright (C) 2004-2013 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "splay_tree.h"
23 #include "xalloc.h"
24
25 /* Splay operation */
26
27 static splay_node_t *splay_top_down(splay_tree_t *tree, const void *data, int *result) {
28         splay_node_t left = {NULL}, right = {NULL};
29         splay_node_t *leftbottom = &left, *rightbottom = &right, *child, *grandchild;
30         splay_node_t *root = tree->root;
31         int c;
32
33         if(!root) {
34                 if(result)
35                         *result = 0;
36                 return NULL;
37         }
38
39         while((c = tree->compare(data, root->data))) {
40                 if(c < 0 && (child = root->left)) {
41                         c = tree->compare(data, child->data);
42
43                         if(c < 0 && (grandchild = child->left)) {
44                                 rightbottom->left = child;
45                                 child->parent = rightbottom;
46                                 rightbottom = child;
47
48                                 if((root->left = child->right))
49                                         child->right->parent = root;
50
51                                 child->right = root;
52                                 root->parent = child;
53
54                                 child->left = NULL;
55                                 grandchild->parent = NULL;
56
57                                 root = grandchild;
58                         } else if (c > 0 && (grandchild = child->right)) {
59                                 leftbottom->right = child;
60                                 child->parent = leftbottom;
61                                 leftbottom = child;
62
63                                 child->right = NULL;
64                                 grandchild->parent = NULL;
65
66                                 rightbottom->left = root;
67                                 root->parent = rightbottom;
68                                 rightbottom = root;
69
70                                 root->left = NULL;
71
72                                 root = grandchild;
73                         } else {
74                                 rightbottom->left = root;
75                                 root->parent = rightbottom;
76                                 rightbottom = root;
77
78                                 root->left = NULL;
79                                 child->parent = NULL;
80
81                                 root = child;
82                                 break;
83                         }
84                 } else if(c > 0 && (child = root->right)) {
85                         c = tree->compare(data, child->data);
86
87                         if(c > 0 && (grandchild = child->right)) {
88                                 leftbottom->right = child;
89                                 child->parent = leftbottom;
90                                 leftbottom = child;
91
92                                 if((root->right = child->left))
93                                         child->left->parent = root;
94
95                                 child->left = root;
96                                 root->parent = child;
97
98                                 child->right = NULL;
99                                 grandchild->parent = NULL;
100
101                                 root = grandchild;
102                         } else if (c < 0 && (grandchild = child->left)) {
103                                 rightbottom->left = child;
104                                 child->parent = rightbottom;
105                                 rightbottom = child;
106
107                                 child->left = NULL;
108                                 grandchild->parent = NULL;
109
110                                 leftbottom->right = root;
111                                 root->parent = leftbottom;
112                                 leftbottom = root;
113
114                                 root->right = NULL;
115
116                                 root = grandchild;
117                         } else {
118                                 leftbottom->right = root;
119                                 root->parent = leftbottom;
120                                 leftbottom = root;
121
122                                 root->right = NULL;
123                                 child->parent = NULL;
124
125                                 root = child;
126                                 break;
127                         }
128                 } else {
129                         break;
130                 }
131         }
132
133         /* Merge trees */
134
135         if(left.right) {
136                 if(root->left) {
137                         leftbottom->right = root->left;
138                         root->left->parent = leftbottom;
139                 }
140                 root->left = left.right;
141                 left.right->parent = root;
142         }
143
144         if(right.left) {
145                 if(root->right) {
146                         rightbottom->left = root->right;
147                         root->right->parent = rightbottom;
148                 }
149                 root->right = right.left;
150                 right.left->parent = root;
151         }
152
153         /* Return result */
154
155         tree->root = root;
156         if(result)
157                 *result = c;
158
159         return tree->root;
160 }
161
162 static void splay_bottom_up(splay_tree_t *tree, splay_node_t *node) {
163         splay_node_t *parent, *grandparent, *greatgrandparent;
164
165         while((parent = node->parent)) {
166                 if(!(grandparent = parent->parent)) { /* zig */
167                         if(node == parent->left) {
168                                 if((parent->left = node->right))
169                                         parent->left->parent = parent;
170                                 node->right = parent;
171                         } else {
172                                 if((parent->right = node->left))
173                                         parent->right->parent = parent;
174                                 node->left = parent;
175                         }
176
177                         parent->parent = node;
178                         node->parent = NULL;
179                 } else {
180                         greatgrandparent = grandparent->parent;
181
182                         if(node == parent->left && parent == grandparent->left) { /* left zig-zig */
183                                 if((grandparent->left = parent->right))
184                                         grandparent->left->parent = grandparent;
185                                 parent->right = grandparent;
186                                 grandparent->parent = parent;
187
188                                 if((parent->left = node->right))
189                                         parent->left->parent = parent;
190                                 node->right = parent;
191                                 parent->parent = node;
192                         } else if(node == parent->right && parent == grandparent->right) { /* right zig-zig */
193                                 if((grandparent->right = parent->left))
194                                         grandparent->right->parent = grandparent;
195                                 parent->left = grandparent;
196                                 grandparent->parent = parent;
197
198                                 if((parent->right = node->left))
199                                         parent->right->parent = parent;
200                                 node->left = parent;
201                                 parent->parent = node;
202                         } else if(node == parent->right && parent == grandparent->left) { /* left-right zig-zag */
203                                 if((parent->right = node->left))
204                                         parent->right->parent = parent;
205                                 node->left = parent;
206                                 parent->parent = node;
207
208                                 if((grandparent->left = node->right))
209                                         grandparent->left->parent = grandparent;
210                                 node->right = grandparent;
211                                 grandparent->parent = node;
212                         } else { /* right-left zig-zag */
213                                 if((parent->left = node->right))
214                                         parent->left->parent = parent;
215                                 node->right = parent;
216                                 parent->parent = node;
217
218                                 if((grandparent->right = node->left))
219                                         grandparent->right->parent = grandparent;
220                                 node->left = grandparent;
221                                 grandparent->parent = node;
222                         }
223
224                         if((node->parent = greatgrandparent)) {
225                                 if(grandparent == greatgrandparent->left)
226                                         greatgrandparent->left = node;
227                                 else
228                                         greatgrandparent->right = node;
229                         }
230                 }
231         }
232
233         tree->root = node;
234 }
235
236 /* (De)constructors */
237
238 splay_tree_t *splay_alloc_tree(splay_compare_t compare, splay_action_t delete) {
239         splay_tree_t *tree;
240
241         tree = xzalloc(sizeof(splay_tree_t));
242         tree->compare = compare;
243         tree->delete = delete;
244
245         return tree;
246 }
247
248 void splay_free_tree(splay_tree_t *tree) {
249         free(tree);
250 }
251
252 splay_node_t *splay_alloc_node(void) {
253         return xzalloc(sizeof(splay_node_t));
254 }
255
256 void splay_free_node(splay_tree_t *tree, splay_node_t *node) {
257         if(node->data && tree->delete)
258                 tree->delete(node->data);
259
260         free(node);
261 }
262
263 /* Searching */
264
265 void *splay_search(splay_tree_t *tree, const void *data) {
266         splay_node_t *node;
267
268         node = splay_search_node(tree, data);
269
270         return node ? node->data : NULL;
271 }
272
273 void *splay_search_closest(splay_tree_t *tree, const void *data, int *result) {
274         splay_node_t *node;
275
276         node = splay_search_closest_node(tree, data, result);
277
278         return node ? node->data : NULL;
279 }
280
281 void *splay_search_closest_smaller(splay_tree_t *tree, const void *data) {
282         splay_node_t *node;
283
284         node = splay_search_closest_smaller_node(tree, data);
285
286         return node ? node->data : NULL;
287 }
288
289 void *splay_search_closest_greater(splay_tree_t *tree, const void *data) {
290         splay_node_t *node;
291
292         node = splay_search_closest_greater_node(tree, data);
293
294         return node ? node->data : NULL;
295 }
296
297 splay_node_t *splay_search_node(splay_tree_t *tree, const void *data) {
298         splay_node_t *node;
299         int result;
300
301         node = splay_search_closest_node(tree, data, &result);
302
303         return result ? NULL : node;
304 }
305
306 splay_node_t *splay_search_closest_node_nosplay(const splay_tree_t *tree, const void *data, int *result) {
307         splay_node_t *node;
308         int c;
309
310         node = tree->root;
311
312         if(!node) {
313                 if(result)
314                         *result = 0;
315                 return NULL;
316         }
317
318         for(;;) {
319                 c = tree->compare(data, node->data);
320
321                 if(c < 0) {
322                         if(node->left)
323                                 node = node->left;
324                         else
325                                 break;
326                 } else if(c > 0) {
327                         if(node->right)
328                                 node = node->right;
329                         else
330                                 break;
331                 } else {
332                         break;
333                 }
334         }
335
336         if(result)
337                 *result = c;
338         return node;
339 }
340
341 splay_node_t *splay_search_closest_node(splay_tree_t *tree, const void *data, int *result) {
342         return splay_top_down(tree, data, result);
343 }
344
345 splay_node_t *splay_search_closest_smaller_node(splay_tree_t *tree, const void *data) {
346         splay_node_t *node;
347         int result;
348
349         node = splay_search_closest_node(tree, data, &result);
350
351         if(result < 0)
352                 node = node->prev;
353
354         return node;
355 }
356
357 splay_node_t *splay_search_closest_greater_node(splay_tree_t *tree, const void *data) {
358         splay_node_t *node;
359         int result;
360
361         node = splay_search_closest_node(tree, data, &result);
362
363         if(result > 0)
364                 node = node->next;
365
366         return node;
367 }
368
369 /* Insertion and deletion */
370
371 splay_node_t *splay_insert(splay_tree_t *tree, void *data) {
372         splay_node_t *closest, *new;
373         int result;
374
375         if(!tree->root) {
376                 new = splay_alloc_node();
377                 new->data = data;
378                 splay_insert_top(tree, new);
379         } else {
380                 closest = splay_search_closest_node(tree, data, &result);
381
382                 if(!result)
383                         return NULL;
384
385                 new = splay_alloc_node();
386                 new->data = data;
387
388                 if(result < 0)
389                         splay_insert_before(tree, closest, new);
390                 else
391                         splay_insert_after(tree, closest, new);
392         }
393
394         return new;
395 }
396
397 splay_node_t *splay_insert_node(splay_tree_t *tree, splay_node_t *node) {
398         splay_node_t *closest;
399         int result;
400
401         node->left = node->right = node->parent = node->next = node->prev = NULL;
402
403         if(!tree->root)
404                 splay_insert_top(tree, node);
405         else {
406                 closest = splay_search_closest_node(tree, node->data, &result);
407
408                 if(!result)
409                         return NULL;
410
411                 if(result < 0)
412                         splay_insert_before(tree, closest, node);
413                 else
414                         splay_insert_after(tree, closest, node);
415         }
416
417         return node;
418 }
419
420 void splay_insert_top(splay_tree_t *tree, splay_node_t *node) {
421         node->prev = node->next = node->left = node->right = node->parent = NULL;
422         tree->head = tree->tail = tree->root = node;
423         tree->count++;
424 }
425
426 void splay_insert_before(splay_tree_t *tree, splay_node_t *before, splay_node_t *node) {
427         if(!before) {
428                 if(tree->tail)
429                         splay_insert_after(tree, tree->tail, node);
430                 else
431                         splay_insert_top(tree, node);
432                 return;
433         }
434
435         node->next = before;
436         if((node->prev = before->prev))
437                 before->prev->next = node;
438         else
439                 tree->head = node;
440         before->prev = node;
441
442         splay_bottom_up(tree, before);
443
444         node->right = before;
445         before->parent = node;
446         if((node->left = before->left))
447                 before->left->parent = node;
448         before->left = NULL;
449
450         node->parent = NULL;
451         tree->root = node;
452         tree->count++;
453 }
454
455 void splay_insert_after(splay_tree_t *tree, splay_node_t *after, splay_node_t *node) {
456         if(!after) {
457                 if(tree->head)
458                         splay_insert_before(tree, tree->head, node);
459                 else
460                         splay_insert_top(tree, node);
461                 return;
462         }
463
464         node->prev = after;
465         if((node->next = after->next))
466                 after->next->prev = node;
467         else
468                 tree->tail = node;
469         after->next = node;
470
471         splay_bottom_up(tree, after);
472
473         node->left = after;
474         after->parent = node;
475         if((node->right = after->right))
476                 after->right->parent = node;
477         after->right = NULL;
478
479         node->parent = NULL;
480         tree->root = node;
481         tree->count++;
482 }
483
484 splay_node_t *splay_unlink(splay_tree_t *tree, void *data) {
485         splay_node_t *node;
486
487         node = splay_search_node(tree, data);
488
489         if(node)
490                 splay_unlink_node(tree, node);
491
492         return node;
493 }
494
495 void splay_unlink_node(splay_tree_t *tree, splay_node_t *node) {
496         if(node->prev)
497                 node->prev->next = node->next;
498         else
499                 tree->head = node->next;
500
501         if(node->next)
502                 node->next->prev = node->prev;
503         else
504                 tree->tail = node->prev;
505
506         splay_bottom_up(tree, node);
507
508         if(node->prev) {
509                 node->left->parent = NULL;
510                 tree->root = node->left;
511                 if((node->prev->right = node->right))
512                         node->right->parent = node->prev;
513         } else if(node->next) {
514                 tree->root = node->right;
515                 node->right->parent = NULL;
516         } else {
517                 tree->root = NULL;
518         }
519
520         tree->count--;
521 }
522
523 void splay_delete_node(splay_tree_t *tree, splay_node_t *node) {
524         splay_unlink_node(tree, node);
525         splay_free_node(tree, node);
526 }
527
528 void splay_delete(splay_tree_t *tree, void *data) {
529         splay_node_t *node;
530
531         node = splay_search_node(tree, data);
532
533         if(node)
534                 splay_delete_node(tree, node);
535 }
536
537 /* Fast tree cleanup */
538
539 void splay_delete_tree(splay_tree_t *tree) {
540         for(splay_node_t *node = tree->head, *next; node; node = next) {
541                 next = node->next;
542                 splay_free_node(tree, node);
543         }
544
545         splay_free_tree(tree);
546 }
547
548 /* Tree walking */
549
550 void splay_foreach(const splay_tree_t *tree, splay_action_t action) {
551         for(splay_node_t *node = tree->head, *next; node; node = next) {
552                 next = node->next;
553                 action(node->data);
554         }
555 }
556
557 void splay_foreach_node(const splay_tree_t *tree, splay_action_t action) {
558         for(splay_node_t *node = tree->head, *next; node; node = next) {
559                 next = node->next;
560                 action(node);
561         }
562 }