Fix UBSAN warnings about conversions and overflows.
[tinc] / src / splay_tree.h
1 #ifndef TINC_SPLAY_TREE_H
2 #define TINC_SPLAY_TREE_H
3
4 /*
5     splay_tree.h -- header file for splay_tree.c
6     Copyright (C) 2004-2013 Guus Sliepen <guus@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 along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 typedef struct splay_node_t {
24
25         /* Linked list part */
26
27         struct splay_node_t *next;
28         struct splay_node_t *prev;
29
30         /* Tree part */
31
32         struct splay_node_t *parent;
33         struct splay_node_t *left;
34         struct splay_node_t *right;
35
36         /* Payload */
37
38         void *data;
39
40 } splay_node_t;
41
42 typedef int (*splay_compare_t)(const void *data1, const void *data2);
43 typedef void (*splay_action_t)(const void *data);
44
45 typedef struct splay_tree_t {
46
47         /* Linked list part */
48
49         splay_node_t *head;
50         splay_node_t *tail;
51
52         /* Tree part */
53
54         splay_node_t *root;
55
56         splay_compare_t compare;
57         splay_action_t delete;
58
59         unsigned int count;
60         unsigned int generation;
61
62 } splay_tree_t;
63
64 /* (De)constructors */
65
66 extern splay_tree_t *splay_alloc_tree(splay_compare_t compare, splay_action_t delete) __attribute__((__malloc__));
67 extern void splay_free_tree(splay_tree_t *tree);
68
69 extern splay_node_t *splay_alloc_node(void) __attribute__((__malloc__));
70 extern void splay_free_node(splay_tree_t *tree, splay_node_t *node);
71
72 /* Insertion and deletion */
73
74 extern splay_node_t *splay_insert(splay_tree_t *tree, void *data);
75 extern splay_node_t *splay_insert_node(splay_tree_t *tree, splay_node_t *node);
76
77 extern void splay_insert_top(splay_tree_t *tree, splay_node_t *node);
78 extern void splay_insert_before(splay_tree_t *tree, splay_node_t *before, splay_node_t *node);
79 extern void splay_insert_after(splay_tree_t *tree, splay_node_t *after, splay_node_t *node);
80
81 extern splay_node_t *splay_unlink(splay_tree_t *tree, void *data);
82 extern void splay_unlink_node(splay_tree_t *tree, splay_node_t *node);
83 extern void splay_delete(splay_tree_t *tree, void *data);
84 extern void splay_delete_node(splay_tree_t *tree, splay_node_t *node);
85
86 /* Fast tree cleanup */
87
88 extern void splay_empty_tree(splay_tree_t *tree);
89 extern void splay_delete_tree(splay_tree_t *tree);
90
91 /* Searching */
92
93 extern void *splay_search(splay_tree_t *tree, const void *data);
94 extern void *splay_search_closest(splay_tree_t *tree, const void *data, int *result);
95 extern void *splay_search_closest_smaller(splay_tree_t *tree, const void *data);
96 extern void *splay_search_closest_greater(splay_tree_t *tree, const void *data);
97
98 extern splay_node_t *splay_search_node(splay_tree_t *tree, const void *data);
99 extern splay_node_t *splay_search_closest_node(splay_tree_t *tree, const void *data, int *result);
100 extern splay_node_t *splay_search_closest_node_nosplay(const splay_tree_t *tree, const void *data, int *result);
101 extern splay_node_t *splay_search_closest_smaller_node(splay_tree_t *tree, const void *data);
102 extern splay_node_t *splay_search_closest_greater_node(splay_tree_t *tree, const void *data);
103
104 /* Tree walking */
105
106 extern void splay_foreach(const splay_tree_t *tree, splay_action_t action);
107 extern void splay_foreach_node(const splay_tree_t *tree, splay_action_t action);
108
109 /*
110    Iterates over a tree.
111
112    CAUTION: while this construct supports deleting the current item,
113    it does *not* support deleting *other* nodes while iterating on the tree.
114  */
115 #define splay_each(type, item, tree) (type *item = (type *)1; item; item = NULL) for(splay_node_t *node = (tree)->head, *next; item = node ? node->data : NULL, next = node ? node->next : NULL, node; node = next)
116
117 #endif