Merge branch 'master' into 1.1
[tinc] / lib / splay_tree.h
1 /*
2     splay_tree.h -- header file for splay_tree.c
3     Copyright (C) 2004-2006 Guus Sliepen <guus@tinc-vpn.org>
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
21 #ifndef __SPLAY_TREE_H__
22 #define __SPLAY_TREE_H__
23
24 typedef struct splay_node_t {
25
26         /* Linked list part */
27
28         struct splay_node_t *next;
29         struct splay_node_t *prev;
30
31         /* Tree part */
32
33         struct splay_node_t *parent;
34         struct splay_node_t *left;
35         struct splay_node_t *right;
36
37         /* Payload */
38
39         void *data;
40
41 } splay_node_t;
42
43 typedef int (*splay_compare_t)(const void *, const void *);
44 typedef void (*splay_action_t)(const void *);
45 typedef void (*splay_action_node_t)(const splay_node_t *);
46
47 typedef struct splay_tree_t {
48
49         /* Linked list part */
50
51         splay_node_t *head;
52         splay_node_t *tail;
53
54         /* Tree part */
55
56         splay_node_t *root;
57
58         splay_compare_t compare;
59         splay_action_t delete;
60
61 } splay_tree_t;
62
63 /* (De)constructors */
64
65 extern splay_tree_t *splay_alloc_tree(splay_compare_t, splay_action_t);
66 extern void splay_free_tree(splay_tree_t *);
67
68 extern splay_node_t *splay_alloc_node(void);
69 extern void splay_free_node(splay_tree_t *tree, splay_node_t *);
70
71 /* Insertion and deletion */
72
73 extern splay_node_t *splay_insert(splay_tree_t *, void *);
74 extern splay_node_t *splay_insert_node(splay_tree_t *, splay_node_t *);
75
76 extern void splay_insert_top(splay_tree_t *, splay_node_t *);
77 extern void splay_insert_before(splay_tree_t *, splay_node_t *, splay_node_t *);
78 extern void splay_insert_after(splay_tree_t *, splay_node_t *, splay_node_t *);
79
80 extern splay_node_t *splay_unlink(splay_tree_t *, void *);
81 extern void splay_unlink_node(splay_tree_t *tree, splay_node_t *);
82 extern void splay_delete(splay_tree_t *, void *);
83 extern void splay_delete_node(splay_tree_t *, splay_node_t *);
84
85 /* Fast tree cleanup */
86
87 extern void splay_delete_tree(splay_tree_t *);
88
89 /* Searching */
90
91 extern void *splay_search(splay_tree_t *, const void *);
92 extern void *splay_search_closest(splay_tree_t *, const void *, int *);
93 extern void *splay_search_closest_smaller(splay_tree_t *, const void *);
94 extern void *splay_search_closest_greater(splay_tree_t *, const void *);
95
96 extern splay_node_t *splay_search_node(splay_tree_t *, const void *);
97 extern splay_node_t *splay_search_closest_node(splay_tree_t *, const void *, int *);
98 extern splay_node_t *splay_search_closest_node_nosplay(const splay_tree_t *, const void *, int *);
99 extern splay_node_t *splay_search_closest_smaller_node(splay_tree_t *, const void *);
100 extern splay_node_t *splay_search_closest_greater_node(splay_tree_t *, const void *);
101
102 /* Tree walking */
103
104 extern void splay_foreach(const splay_tree_t *, splay_action_t);
105 extern void splay_foreach_node(const splay_tree_t *, splay_action_t);
106
107 #endif