]> git.meshlink.io Git - meshlink/blob - src/splay_tree.c
Speed up initial autoconnect after joining a mesh.
[meshlink] / src / splay_tree.c
1 /*
2     splay_tree.c -- splay tree and linked list convenience
3     Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
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, right;
29         splay_node_t *leftbottom = &left, *rightbottom = &right, *child, *grandchild;
30         splay_node_t *root = tree->root;
31         int c;
32
33         memset(&left, 0, sizeof(left));
34         memset(&right, 0, sizeof(right));
35
36         if(!root) {
37                 if(result) {
38                         *result = 0;
39                 }
40
41                 return NULL;
42         }
43
44         while((c = tree->compare(data, root->data))) {
45                 if(c < 0 && (child = root->left)) {
46                         c = tree->compare(data, child->data);
47
48                         if(c < 0 && (grandchild = child->left)) {
49                                 rightbottom->left = child;
50                                 child->parent = rightbottom;
51                                 rightbottom = child;
52
53                                 if((root->left = child->right)) {
54                                         child->right->parent = root;
55                                 }
56
57                                 child->right = root;
58                                 root->parent = child;
59
60                                 child->left = NULL;
61                                 grandchild->parent = NULL;
62
63                                 root = grandchild;
64                         } else if(c > 0 && (grandchild = child->right)) {
65                                 leftbottom->right = child;
66                                 child->parent = leftbottom;
67                                 leftbottom = child;
68
69                                 child->right = NULL;
70                                 grandchild->parent = NULL;
71
72                                 rightbottom->left = root;
73                                 root->parent = rightbottom;
74                                 rightbottom = root;
75
76                                 root->left = NULL;
77
78                                 root = grandchild;
79                         } else {
80                                 rightbottom->left = root;
81                                 root->parent = rightbottom;
82                                 rightbottom = root;
83
84                                 root->left = NULL;
85                                 child->parent = NULL;
86
87                                 root = child;
88                                 break;
89                         }
90                 } else if(c > 0 && (child = root->right)) {
91                         c = tree->compare(data, child->data);
92
93                         if(c > 0 && (grandchild = child->right)) {
94                                 leftbottom->right = child;
95                                 child->parent = leftbottom;
96                                 leftbottom = child;
97
98                                 if((root->right = child->left)) {
99                                         child->left->parent = root;
100                                 }
101
102                                 child->left = root;
103                                 root->parent = child;
104
105                                 child->right = NULL;
106                                 grandchild->parent = NULL;
107
108                                 root = grandchild;
109                         } else if(c < 0 && (grandchild = child->left)) {
110                                 rightbottom->left = child;
111                                 child->parent = rightbottom;
112                                 rightbottom = child;
113
114                                 child->left = NULL;
115                                 grandchild->parent = NULL;
116
117                                 leftbottom->right = root;
118                                 root->parent = leftbottom;
119                                 leftbottom = root;
120
121                                 root->right = NULL;
122
123                                 root = grandchild;
124                         } else {
125                                 leftbottom->right = root;
126                                 root->parent = leftbottom;
127                                 leftbottom = root;
128
129                                 root->right = NULL;
130                                 child->parent = NULL;
131
132                                 root = child;
133                                 break;
134                         }
135                 } else {
136                         break;
137                 }
138         }
139
140         /* Merge trees */
141
142         if(left.right) {
143                 if(root->left) {
144                         leftbottom->right = root->left;
145                         root->left->parent = leftbottom;
146                 }
147
148                 root->left = left.right;
149                 left.right->parent = root;
150         }
151
152         if(right.left) {
153                 if(root->right) {
154                         rightbottom->left = root->right;
155                         root->right->parent = rightbottom;
156                 }
157
158                 root->right = right.left;
159                 right.left->parent = root;
160         }
161
162         /* Return result */
163
164         tree->root = root;
165
166         if(result) {
167                 *result = c;
168         }
169
170         return tree->root;
171 }
172
173 static void splay_bottom_up(splay_tree_t *tree, splay_node_t *node) {
174         splay_node_t *parent, *grandparent, *greatgrandparent;
175
176         while((parent = node->parent)) {
177                 if(!(grandparent = parent->parent)) { /* zig */
178                         if(node == parent->left) {
179                                 if((parent->left = node->right)) {
180                                         parent->left->parent = parent;
181                                 }
182
183                                 node->right = parent;
184                         } else {
185                                 if((parent->right = node->left)) {
186                                         parent->right->parent = parent;
187                                 }
188
189                                 node->left = parent;
190                         }
191
192                         parent->parent = node;
193                         node->parent = NULL;
194                 } else {
195                         greatgrandparent = grandparent->parent;
196
197                         if(node == parent->left && parent == grandparent->left) { /* left zig-zig */
198                                 if((grandparent->left = parent->right)) {
199                                         grandparent->left->parent = grandparent;
200                                 }
201
202                                 parent->right = grandparent;
203                                 grandparent->parent = parent;
204
205                                 if((parent->left = node->right)) {
206                                         parent->left->parent = parent;
207                                 }
208
209                                 node->right = parent;
210                                 parent->parent = node;
211                         } else if(node == parent->right && parent == grandparent->right) { /* right zig-zig */
212                                 if((grandparent->right = parent->left)) {
213                                         grandparent->right->parent = grandparent;
214                                 }
215
216                                 parent->left = grandparent;
217                                 grandparent->parent = parent;
218
219                                 if((parent->right = node->left)) {
220                                         parent->right->parent = parent;
221                                 }
222
223                                 node->left = parent;
224                                 parent->parent = node;
225                         } else if(node == parent->right && parent == grandparent->left) { /* left-right zig-zag */
226                                 if((parent->right = node->left)) {
227                                         parent->right->parent = parent;
228                                 }
229
230                                 node->left = parent;
231                                 parent->parent = node;
232
233                                 if((grandparent->left = node->right)) {
234                                         grandparent->left->parent = grandparent;
235                                 }
236
237                                 node->right = grandparent;
238                                 grandparent->parent = node;
239                         } else { /* right-left zig-zag */
240                                 if((parent->left = node->right)) {
241                                         parent->left->parent = parent;
242                                 }
243
244                                 node->right = parent;
245                                 parent->parent = node;
246
247                                 if((grandparent->right = node->left)) {
248                                         grandparent->right->parent = grandparent;
249                                 }
250
251                                 node->left = grandparent;
252                                 grandparent->parent = node;
253                         }
254
255                         if((node->parent = greatgrandparent)) {
256                                 if(grandparent == greatgrandparent->left) {
257                                         greatgrandparent->left = node;
258                                 } else {
259                                         greatgrandparent->right = node;
260                                 }
261                         }
262                 }
263         }
264
265         tree->root = node;
266 }
267
268 /* (De)constructors */
269
270 splay_tree_t *splay_alloc_tree(splay_compare_t compare, splay_action_t delete) {
271         splay_tree_t *tree;
272
273         tree = xzalloc(sizeof(splay_tree_t));
274         tree->compare = compare;
275         tree->delete = delete;
276
277         return tree;
278 }
279
280 void splay_free_tree(splay_tree_t *tree) {
281         free(tree);
282 }
283
284 splay_node_t *splay_alloc_node(void) {
285         return xzalloc(sizeof(splay_node_t));
286 }
287
288 void splay_free_node(splay_tree_t *tree, splay_node_t *node) {
289         if(node->data && tree->delete) {
290                 tree->delete(node->data);
291         }
292
293         free(node);
294 }
295
296 /* Searching */
297
298 void *splay_search(splay_tree_t *tree, const void *data) {
299         splay_node_t *node;
300
301         node = splay_search_node(tree, data);
302
303         return node ? node->data : NULL;
304 }
305
306 void *splay_search_closest(splay_tree_t *tree, const void *data, int *result) {
307         splay_node_t *node;
308
309         node = splay_search_closest_node(tree, data, result);
310
311         return node ? node->data : NULL;
312 }
313
314 void *splay_search_closest_smaller(splay_tree_t *tree, const void *data) {
315         splay_node_t *node;
316
317         node = splay_search_closest_smaller_node(tree, data);
318
319         return node ? node->data : NULL;
320 }
321
322 void *splay_search_closest_greater(splay_tree_t *tree, const void *data) {
323         splay_node_t *node;
324
325         node = splay_search_closest_greater_node(tree, data);
326
327         return node ? node->data : NULL;
328 }
329
330 splay_node_t *splay_search_node(splay_tree_t *tree, const void *data) {
331         splay_node_t *node;
332         int result;
333
334         node = splay_search_closest_node(tree, data, &result);
335
336         return result ? NULL : node;
337 }
338
339 splay_node_t *splay_search_closest_node_nosplay(const splay_tree_t *tree, const void *data, int *result) {
340         splay_node_t *node;
341         int c;
342
343         node = tree->root;
344
345         if(!node) {
346                 if(result) {
347                         *result = 0;
348                 }
349
350                 return NULL;
351         }
352
353         for(;;) {
354                 c = tree->compare(data, node->data);
355
356                 if(c < 0) {
357                         if(node->left) {
358                                 node = node->left;
359                         } else {
360                                 break;
361                         }
362                 } else if(c > 0) {
363                         if(node->right) {
364                                 node = node->right;
365                         } else {
366                                 break;
367                         }
368                 } else {
369                         break;
370                 }
371         }
372
373         if(result) {
374                 *result = c;
375         }
376
377         return node;
378 }
379
380 splay_node_t *splay_search_closest_node(splay_tree_t *tree, const void *data, int *result) {
381         return splay_top_down(tree, data, result);
382 }
383
384 splay_node_t *splay_search_closest_smaller_node(splay_tree_t *tree, const void *data) {
385         splay_node_t *node;
386         int result;
387
388         node = splay_search_closest_node(tree, data, &result);
389
390         if(result < 0) {
391                 node = node->prev;
392         }
393
394         return node;
395 }
396
397 splay_node_t *splay_search_closest_greater_node(splay_tree_t *tree, const void *data) {
398         splay_node_t *node;
399         int result;
400
401         node = splay_search_closest_node(tree, data, &result);
402
403         if(result > 0) {
404                 node = node->next;
405         }
406
407         return node;
408 }
409
410 /* Insertion and deletion */
411
412 splay_node_t *splay_insert(splay_tree_t *tree, void *data) {
413         splay_node_t *closest, *new;
414         int result;
415
416         if(!tree->root) {
417                 new = splay_alloc_node();
418                 new->data = data;
419                 splay_insert_top(tree, new);
420         } else {
421                 closest = splay_search_closest_node(tree, data, &result);
422
423                 if(!result) {
424                         return NULL;
425                 }
426
427                 new = splay_alloc_node();
428                 new->data = data;
429
430                 if(result < 0) {
431                         splay_insert_before(tree, closest, new);
432                 } else {
433                         splay_insert_after(tree, closest, new);
434                 }
435         }
436
437         return new;
438 }
439
440 splay_node_t *splay_insert_node(splay_tree_t *tree, splay_node_t *node) {
441         splay_node_t *closest;
442         int result;
443
444         node->left = node->right = node->parent = node->next = node->prev = NULL;
445
446         if(!tree->root) {
447                 splay_insert_top(tree, node);
448         } else {
449                 closest = splay_search_closest_node(tree, node->data, &result);
450
451                 if(!result) {
452                         return NULL;
453                 }
454
455                 if(result < 0) {
456                         splay_insert_before(tree, closest, node);
457                 } else {
458                         splay_insert_after(tree, closest, node);
459                 }
460         }
461
462         return node;
463 }
464
465 void splay_insert_top(splay_tree_t *tree, splay_node_t *node) {
466         node->prev = node->next = node->left = node->right = node->parent = NULL;
467         tree->head = tree->tail = tree->root = node;
468         tree->count++;
469 }
470
471 void splay_insert_before(splay_tree_t *tree, splay_node_t *before, splay_node_t *node) {
472         if(!before) {
473                 if(tree->tail) {
474                         splay_insert_after(tree, tree->tail, node);
475                 } else {
476                         splay_insert_top(tree, node);
477                 }
478
479                 return;
480         }
481
482         node->next = before;
483
484         if((node->prev = before->prev)) {
485                 before->prev->next = node;
486         } else {
487                 tree->head = node;
488         }
489
490         before->prev = node;
491
492         splay_bottom_up(tree, before);
493
494         node->right = before;
495         before->parent = node;
496
497         if((node->left = before->left)) {
498                 before->left->parent = node;
499         }
500
501         before->left = NULL;
502
503         node->parent = NULL;
504         tree->root = node;
505         tree->count++;
506 }
507
508 void splay_insert_after(splay_tree_t *tree, splay_node_t *after, splay_node_t *node) {
509         if(!after) {
510                 if(tree->head) {
511                         splay_insert_before(tree, tree->head, node);
512                 } else {
513                         splay_insert_top(tree, node);
514                 }
515
516                 return;
517         }
518
519         node->prev = after;
520
521         if((node->next = after->next)) {
522                 after->next->prev = node;
523         } else {
524                 tree->tail = node;
525         }
526
527         after->next = node;
528
529         splay_bottom_up(tree, after);
530
531         node->left = after;
532         after->parent = node;
533
534         if((node->right = after->right)) {
535                 after->right->parent = node;
536         }
537
538         after->right = NULL;
539
540         node->parent = NULL;
541         tree->root = node;
542         tree->count++;
543 }
544
545 splay_node_t *splay_unlink(splay_tree_t *tree, void *data) {
546         splay_node_t *node;
547
548         node = splay_search_node(tree, data);
549
550         if(node) {
551                 splay_unlink_node(tree, node);
552         }
553
554         return node;
555 }
556
557 void splay_unlink_node(splay_tree_t *tree, splay_node_t *node) {
558         if(node->prev) {
559                 node->prev->next = node->next;
560         } else {
561                 tree->head = node->next;
562         }
563
564         if(node->next) {
565                 node->next->prev = node->prev;
566         } else {
567                 tree->tail = node->prev;
568         }
569
570         splay_bottom_up(tree, node);
571
572         if(node->prev) {
573                 node->left->parent = NULL;
574                 tree->root = node->left;
575
576                 if((node->prev->right = node->right)) {
577                         node->right->parent = node->prev;
578                 }
579         } else if(node->next) {
580                 tree->root = node->right;
581                 node->right->parent = NULL;
582         } else {
583                 tree->root = NULL;
584         }
585
586         tree->count--;
587 }
588
589 void splay_delete_node(splay_tree_t *tree, splay_node_t *node) {
590         splay_unlink_node(tree, node);
591         splay_free_node(tree, node);
592 }
593
594 void splay_delete(splay_tree_t *tree, void *data) {
595         splay_node_t *node;
596
597         node = splay_search_node(tree, data);
598
599         if(node) {
600                 splay_delete_node(tree, node);
601         }
602 }
603
604 /* Fast tree cleanup */
605
606 void splay_delete_tree(splay_tree_t *tree) {
607         for(splay_node_t *node = tree->head, *next; node; node = next) {
608                 next = node->next;
609                 splay_free_node(tree, node);
610         }
611
612         splay_free_tree(tree);
613 }
614
615 /* Tree walking */
616
617 void splay_foreach(const splay_tree_t *tree, splay_action_t action) {
618         for(splay_node_t *node = tree->head, *next; node; node = next) {
619                 next = node->next;
620                 action(node->data);
621         }
622 }
623
624 void splay_foreach_node(const splay_tree_t *tree, splay_action_t action) {
625         for(splay_node_t *node = tree->head, *next; node; node = next) {
626                 next = node->next;
627                 action(node);
628         }
629 }