2 splay_tree.h -- header file for splay_tree.c
3 Copyright (C) 2004-2012 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;
65 /* (De)constructors */
67 extern splay_tree_t *splay_alloc_tree(splay_compare_t, splay_action_t) __attribute__ ((__malloc__));
68 extern void splay_free_tree(splay_tree_t *);
70 extern splay_node_t *splay_alloc_node(void) __attribute__ ((__malloc__));
71 extern void splay_free_node(splay_tree_t *tree, splay_node_t *);
73 /* Insertion and deletion */
75 extern splay_node_t *splay_insert(splay_tree_t *, void *);
76 extern splay_node_t *splay_insert_node(splay_tree_t *, splay_node_t *);
78 extern void splay_insert_top(splay_tree_t *, splay_node_t *);
79 extern void splay_insert_before(splay_tree_t *, splay_node_t *, splay_node_t *);
80 extern void splay_insert_after(splay_tree_t *, splay_node_t *, splay_node_t *);
82 extern splay_node_t *splay_unlink(splay_tree_t *, void *);
83 extern void splay_unlink_node(splay_tree_t *tree, splay_node_t *);
84 extern void splay_delete(splay_tree_t *, void *);
85 extern void splay_delete_node(splay_tree_t *, splay_node_t *);
87 /* Fast tree cleanup */
89 extern void splay_delete_tree(splay_tree_t *);
93 extern void *splay_search(splay_tree_t *, const void *);
94 extern void *splay_search_closest(splay_tree_t *, const void *, int *);
95 extern void *splay_search_closest_smaller(splay_tree_t *, const void *);
96 extern void *splay_search_closest_greater(splay_tree_t *, const void *);
98 extern splay_node_t *splay_search_node(splay_tree_t *, const void *);
99 extern splay_node_t *splay_search_closest_node(splay_tree_t *, const void *, int *);
100 extern splay_node_t *splay_search_closest_node_nosplay(const splay_tree_t *, const void *, int *);
101 extern splay_node_t *splay_search_closest_smaller_node(splay_tree_t *, const void *);
102 extern splay_node_t *splay_search_closest_greater_node(splay_tree_t *, const void *);
106 extern void splay_foreach(const splay_tree_t *, splay_action_t);
107 extern void splay_foreach_node(const splay_tree_t *, splay_action_t);
109 #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)