]> git.meshlink.io Git - meshlink/blob - src/splay_tree.h
Wake up the MeshLink thread if framed channel data is pending to be flushed.
[meshlink] / src / splay_tree.h
1 #ifndef MESHLINK_SPLAY_TREE_H
2 #define MESHLINK_SPLAY_TREE_H
3
4 /*
5     splay_tree.h -- header file for splay_tree.c
6     Copyright (C) 2014, 2017 Guus Sliepen <guus@meshlink.io>
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 *, const void *);
43 typedef void (*splay_action_t)(const void *);
44 typedef void (*splay_action_node_t)(const splay_node_t *);
45
46 typedef struct splay_tree_t {
47
48         /* Linked list part */
49
50         splay_node_t *head;
51         splay_node_t *tail;
52
53         /* Tree part */
54
55         splay_node_t *root;
56
57         splay_compare_t compare;
58         splay_action_t delete;
59
60         unsigned int count;
61
62 } splay_tree_t;
63
64 /* (De)constructors */
65
66 splay_tree_t *splay_alloc_tree(splay_compare_t, splay_action_t) __attribute__((__malloc__));
67 void splay_delete_tree(splay_tree_t *);
68
69 splay_node_t *splay_alloc_node(void) __attribute__((__malloc__));
70 void splay_free_node(splay_tree_t *tree, splay_node_t *);
71
72 /* Insertion and deletion */
73
74 splay_node_t *splay_insert(splay_tree_t *, void *);
75 splay_node_t *splay_insert_node(splay_tree_t *, splay_node_t *);
76
77 splay_node_t *splay_unlink(splay_tree_t *, void *);
78 void splay_unlink_node(splay_tree_t *tree, splay_node_t *);
79 void splay_delete(splay_tree_t *, void *);
80 void splay_delete_node(splay_tree_t *, splay_node_t *);
81
82 /* Searching */
83
84 void *splay_search(splay_tree_t *, const void *);
85 void *splay_search_closest(splay_tree_t *, const void *, int *);
86 void *splay_search_closest_smaller(splay_tree_t *, const void *);
87 void *splay_search_closest_greater(splay_tree_t *, const void *);
88
89 splay_node_t *splay_search_node(splay_tree_t *, const void *);
90 splay_node_t *splay_search_closest_node(splay_tree_t *, const void *, int *);
91 splay_node_t *splay_search_closest_node_nosplay(const splay_tree_t *, const void *, int *);
92 splay_node_t *splay_search_closest_smaller_node(splay_tree_t *, const void *);
93 splay_node_t *splay_search_closest_greater_node(splay_tree_t *, const void *);
94
95 /* Tree walking */
96
97 void splay_foreach(const splay_tree_t *, splay_action_t);
98 void splay_foreach_node(const splay_tree_t *, splay_action_t);
99
100 #define splay_each(type, item, tree) (type *item = (type *)1; item; item = NULL) for(splay_node_t *splay_node = (tree)->head, *splay_next; item = splay_node ? splay_node->data : NULL, splay_next = splay_node ? splay_node->next : NULL, splay_node; splay_node = splay_next)
101 #define inner_splay_each(type, item, tree) (type *item = (type *)1; item; item = NULL) for(splay_node_t *inner_splay_node = (tree)->head, *inner_splay_next; item = inner_splay_node ? inner_splay_node->data : NULL, inner_splay_next = inner_splay_node ? inner_splay_node->next : NULL, inner_splay_node; inner_splay_node = inner_splay_next)
102
103 #endif