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 splay_node_t *splay_alloc_node(void) {
281 return xzalloc(sizeof(splay_node_t));
284 void splay_free_node(splay_tree_t *tree, splay_node_t *node) {
285 if(node->data && tree->delete) {
286 tree->delete(node->data);
294 void *splay_search(splay_tree_t *tree, const void *data) {
297 node = splay_search_node(tree, data);
299 return node ? node->data : NULL;
302 void *splay_search_closest(splay_tree_t *tree, const void *data, int *result) {
305 node = splay_search_closest_node(tree, data, result);
307 return node ? node->data : NULL;
310 void *splay_search_closest_smaller(splay_tree_t *tree, const void *data) {
313 node = splay_search_closest_smaller_node(tree, data);
315 return node ? node->data : NULL;
318 void *splay_search_closest_greater(splay_tree_t *tree, const void *data) {
321 node = splay_search_closest_greater_node(tree, data);
323 return node ? node->data : NULL;
326 splay_node_t *splay_search_node(splay_tree_t *tree, const void *data) {
330 node = splay_search_closest_node(tree, data, &result);
332 return result ? NULL : node;
335 splay_node_t *splay_search_closest_node_nosplay(const splay_tree_t *tree, const void *data, int *result) {
350 c = tree->compare(data, node->data);
376 splay_node_t *splay_search_closest_node(splay_tree_t *tree, const void *data, int *result) {
377 return splay_top_down(tree, data, result);
380 splay_node_t *splay_search_closest_smaller_node(splay_tree_t *tree, const void *data) {
384 node = splay_search_closest_node(tree, data, &result);
393 splay_node_t *splay_search_closest_greater_node(splay_tree_t *tree, const void *data) {
397 node = splay_search_closest_node(tree, data, &result);
406 /* Insertion and deletion */
408 static void splay_insert_top(splay_tree_t *tree, splay_node_t *node) {
409 node->prev = node->next = node->left = node->right = node->parent = NULL;
410 tree->head = tree->tail = tree->root = node;
414 static void splay_insert_after(splay_tree_t *tree, splay_node_t *after, splay_node_t *node);
416 static void splay_insert_before(splay_tree_t *tree, splay_node_t *before, splay_node_t *node) {
419 splay_insert_after(tree, tree->tail, node);
421 splay_insert_top(tree, node);
429 if((node->prev = before->prev)) {
430 before->prev->next = node;
437 splay_bottom_up(tree, before);
439 node->right = before;
440 before->parent = node;
442 if((node->left = before->left)) {
443 before->left->parent = node;
453 static void splay_insert_after(splay_tree_t *tree, splay_node_t *after, splay_node_t *node) {
456 splay_insert_before(tree, tree->head, node);
458 splay_insert_top(tree, node);
466 if((node->next = after->next)) {
467 after->next->prev = node;
474 splay_bottom_up(tree, after);
477 after->parent = node;
479 if((node->right = after->right)) {
480 after->right->parent = node;
490 splay_node_t *splay_insert(splay_tree_t *tree, void *data) {
491 splay_node_t *closest, *new;
495 new = splay_alloc_node();
497 splay_insert_top(tree, new);
499 closest = splay_search_closest_node(tree, data, &result);
505 new = splay_alloc_node();
509 splay_insert_before(tree, closest, new);
511 splay_insert_after(tree, closest, new);
518 splay_node_t *splay_insert_node(splay_tree_t *tree, splay_node_t *node) {
519 splay_node_t *closest;
522 node->left = node->right = node->parent = node->next = node->prev = NULL;
525 splay_insert_top(tree, node);
527 closest = splay_search_closest_node(tree, node->data, &result);
534 splay_insert_before(tree, closest, node);
536 splay_insert_after(tree, closest, node);
543 splay_node_t *splay_unlink(splay_tree_t *tree, void *data) {
546 node = splay_search_node(tree, data);
549 splay_unlink_node(tree, node);
555 void splay_unlink_node(splay_tree_t *tree, splay_node_t *node) {
557 assert(node->prev || tree->head == node);
558 assert(node->next || tree->tail == node);
561 node->prev->next = node->next;
563 tree->head = node->next;
567 node->next->prev = node->prev;
569 tree->tail = node->prev;
572 splay_bottom_up(tree, node);
575 node->left->parent = NULL;
576 tree->root = node->left;
578 if((node->prev->right = node->right)) {
579 node->right->parent = node->prev;
581 } else if(node->next) {
582 tree->root = node->right;
583 node->right->parent = NULL;
591 void splay_delete_node(splay_tree_t *tree, splay_node_t *node) {
592 splay_unlink_node(tree, node);
593 splay_free_node(tree, node);
596 void splay_delete(splay_tree_t *tree, void *data) {
599 node = splay_search_node(tree, data);
602 splay_delete_node(tree, node);
606 /* Fast tree cleanup */
608 void splay_delete_tree(splay_tree_t *tree) {
609 for(splay_node_t *node = tree->head, *next; node; node = next) {
611 splay_free_node(tree, node);
615 assert(!tree->count);
621 void splay_foreach(const splay_tree_t *tree, splay_action_t action) {
622 for(splay_node_t *node = tree->head, *next; node; node = next) {
628 void splay_foreach_node(const splay_tree_t *tree, splay_action_t action) {
629 for(splay_node_t *node = tree->head, *next; node; node = next) {