2 splay_tree.c -- splay tree and linked list convenience
3 Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
22 #include "splay_tree.h"
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;
33 memset(&left, 0, sizeof(left));
34 memset(&right, 0, sizeof(right));
44 while((c = tree->compare(data, root->data))) {
45 if(c < 0 && (child = root->left)) {
46 c = tree->compare(data, child->data);
48 if(c < 0 && (grandchild = child->left)) {
49 rightbottom->left = child;
50 child->parent = rightbottom;
53 if((root->left = child->right)) {
54 child->right->parent = root;
61 grandchild->parent = NULL;
64 } else if(c > 0 && (grandchild = child->right)) {
65 leftbottom->right = child;
66 child->parent = leftbottom;
70 grandchild->parent = NULL;
72 rightbottom->left = root;
73 root->parent = rightbottom;
80 rightbottom->left = root;
81 root->parent = rightbottom;
90 } else if(c > 0 && (child = root->right)) {
91 c = tree->compare(data, child->data);
93 if(c > 0 && (grandchild = child->right)) {
94 leftbottom->right = child;
95 child->parent = leftbottom;
98 if((root->right = child->left)) {
99 child->left->parent = root;
103 root->parent = child;
106 grandchild->parent = NULL;
109 } else if(c < 0 && (grandchild = child->left)) {
110 rightbottom->left = child;
111 child->parent = rightbottom;
115 grandchild->parent = NULL;
117 leftbottom->right = root;
118 root->parent = leftbottom;
125 leftbottom->right = root;
126 root->parent = leftbottom;
130 child->parent = NULL;
144 leftbottom->right = root->left;
145 root->left->parent = leftbottom;
148 root->left = left.right;
149 left.right->parent = root;
154 rightbottom->left = root->right;
155 root->right->parent = rightbottom;
158 root->right = right.left;
159 right.left->parent = root;
173 static void splay_bottom_up(splay_tree_t *tree, splay_node_t *node) {
174 splay_node_t *parent, *grandparent, *greatgrandparent;
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;
183 node->right = parent;
185 if((parent->right = node->left)) {
186 parent->right->parent = parent;
192 parent->parent = node;
195 greatgrandparent = grandparent->parent;
197 if(node == parent->left && parent == grandparent->left) { /* left zig-zig */
198 if((grandparent->left = parent->right)) {
199 grandparent->left->parent = grandparent;
202 parent->right = grandparent;
203 grandparent->parent = parent;
205 if((parent->left = node->right)) {
206 parent->left->parent = parent;
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;
216 parent->left = grandparent;
217 grandparent->parent = parent;
219 if((parent->right = node->left)) {
220 parent->right->parent = 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;
231 parent->parent = node;
233 if((grandparent->left = node->right)) {
234 grandparent->left->parent = grandparent;
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;
244 node->right = parent;
245 parent->parent = node;
247 if((grandparent->right = node->left)) {
248 grandparent->right->parent = grandparent;
251 node->left = grandparent;
252 grandparent->parent = node;
255 if((node->parent = greatgrandparent)) {
256 if(grandparent == greatgrandparent->left) {
257 greatgrandparent->left = node;
259 greatgrandparent->right = node;
268 /* (De)constructors */
270 splay_tree_t *splay_alloc_tree(splay_compare_t compare, splay_action_t delete) {
273 tree = xzalloc(sizeof(splay_tree_t));
274 tree->compare = compare;
275 tree->delete = delete;
280 void splay_free_tree(splay_tree_t *tree) {
284 splay_node_t *splay_alloc_node(void) {
285 return xzalloc(sizeof(splay_node_t));
288 void splay_free_node(splay_tree_t *tree, splay_node_t *node) {
289 if(node->data && tree->delete) {
290 tree->delete(node->data);
298 void *splay_search(splay_tree_t *tree, const void *data) {
301 node = splay_search_node(tree, data);
303 return node ? node->data : NULL;
306 void *splay_search_closest(splay_tree_t *tree, const void *data, int *result) {
309 node = splay_search_closest_node(tree, data, result);
311 return node ? node->data : NULL;
314 void *splay_search_closest_smaller(splay_tree_t *tree, const void *data) {
317 node = splay_search_closest_smaller_node(tree, data);
319 return node ? node->data : NULL;
322 void *splay_search_closest_greater(splay_tree_t *tree, const void *data) {
325 node = splay_search_closest_greater_node(tree, data);
327 return node ? node->data : NULL;
330 splay_node_t *splay_search_node(splay_tree_t *tree, const void *data) {
334 node = splay_search_closest_node(tree, data, &result);
336 return result ? NULL : node;
339 splay_node_t *splay_search_closest_node_nosplay(const splay_tree_t *tree, const void *data, int *result) {
354 c = tree->compare(data, node->data);
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);
384 splay_node_t *splay_search_closest_smaller_node(splay_tree_t *tree, const void *data) {
388 node = splay_search_closest_node(tree, data, &result);
397 splay_node_t *splay_search_closest_greater_node(splay_tree_t *tree, const void *data) {
401 node = splay_search_closest_node(tree, data, &result);
410 /* Insertion and deletion */
412 splay_node_t *splay_insert(splay_tree_t *tree, void *data) {
413 splay_node_t *closest, *new;
417 new = splay_alloc_node();
419 splay_insert_top(tree, new);
421 closest = splay_search_closest_node(tree, data, &result);
427 new = splay_alloc_node();
431 splay_insert_before(tree, closest, new);
433 splay_insert_after(tree, closest, new);
440 splay_node_t *splay_insert_node(splay_tree_t *tree, splay_node_t *node) {
441 splay_node_t *closest;
444 node->left = node->right = node->parent = node->next = node->prev = NULL;
447 splay_insert_top(tree, node);
449 closest = splay_search_closest_node(tree, node->data, &result);
456 splay_insert_before(tree, closest, node);
458 splay_insert_after(tree, closest, node);
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;
471 void splay_insert_before(splay_tree_t *tree, splay_node_t *before, splay_node_t *node) {
474 splay_insert_after(tree, tree->tail, node);
476 splay_insert_top(tree, node);
484 if((node->prev = before->prev)) {
485 before->prev->next = node;
492 splay_bottom_up(tree, before);
494 node->right = before;
495 before->parent = node;
497 if((node->left = before->left)) {
498 before->left->parent = node;
508 void splay_insert_after(splay_tree_t *tree, splay_node_t *after, splay_node_t *node) {
511 splay_insert_before(tree, tree->head, node);
513 splay_insert_top(tree, node);
521 if((node->next = after->next)) {
522 after->next->prev = node;
529 splay_bottom_up(tree, after);
532 after->parent = node;
534 if((node->right = after->right)) {
535 after->right->parent = node;
545 splay_node_t *splay_unlink(splay_tree_t *tree, void *data) {
548 node = splay_search_node(tree, data);
551 splay_unlink_node(tree, node);
557 void splay_unlink_node(splay_tree_t *tree, splay_node_t *node) {
559 node->prev->next = node->next;
561 tree->head = node->next;
565 node->next->prev = node->prev;
567 tree->tail = node->prev;
570 splay_bottom_up(tree, node);
573 node->left->parent = NULL;
574 tree->root = node->left;
576 if((node->prev->right = node->right)) {
577 node->right->parent = node->prev;
579 } else if(node->next) {
580 tree->root = node->right;
581 node->right->parent = NULL;
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);
594 void splay_delete(splay_tree_t *tree, void *data) {
597 node = splay_search_node(tree, data);
600 splay_delete_node(tree, node);
604 /* Fast tree cleanup */
606 void splay_delete_tree(splay_tree_t *tree) {
607 for(splay_node_t *node = tree->head, *next; node; node = next) {
609 splay_free_node(tree, node);
612 splay_free_tree(tree);
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) {
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) {