2 splay_tree.h -- header file for splay_tree.c
3 Copyright (C) 2004-2006 Guus Sliepen <guus@tinc-vpn.org>
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.
21 #ifndef __SPLAY_TREE_H__
22 #define __SPLAY_TREE_H__
24 typedef struct splay_node_t {
26 /* Linked list part */
28 struct splay_node_t *next;
29 struct splay_node_t *prev;
33 struct splay_node_t *parent;
34 struct splay_node_t *left;
35 struct splay_node_t *right;
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 *);
47 typedef struct splay_tree_t {
49 /* Linked list part */
58 splay_compare_t compare;
59 splay_action_t delete;
63 /* (De)constructors */
65 extern splay_tree_t *splay_alloc_tree(splay_compare_t, splay_action_t);
66 extern void splay_free_tree(splay_tree_t *);
68 extern splay_node_t *splay_alloc_node(void);
69 extern void splay_free_node(splay_tree_t *tree, splay_node_t *);
71 /* Insertion and deletion */
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 *);
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 *);
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 *);
85 /* Fast tree cleanup */
87 extern void splay_delete_tree(splay_tree_t *);
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 *);
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 *);
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);
107 #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)