-
Notifications
You must be signed in to change notification settings - Fork 1
/
bostree.h
156 lines (127 loc) · 4.22 KB
/
bostree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Self-Balancing order statistic tree
Implements an AVL tree with two additional methods,
Select(i), which finds the i'th smallest element, and
Rank(x), which returns the rank of a given element,
which both run in O(log n).
The tree is implemented with map semantics, that is, there are separete key
and value pointers.
Copyright (c) 2017, Phillip Berndt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BOSTREE_H
#define BOSTREE_H
/* Opaque tree structure */
typedef struct _BOSTree BOSTree;
/* Node structure */
struct _BOSNode {
unsigned int left_child_count;
unsigned int right_child_count;
unsigned int depth;
struct _BOSNode *left_child_node;
struct _BOSNode *right_child_node;
struct _BOSNode *parent_node;
void *key;
void *data;
unsigned char weak_ref_count;
unsigned char weak_ref_node_valid;
};
typedef struct _BOSNode BOSNode;
/*
Public interface
*/
/**
* Key comparison function.
*
* Should return a positive value if the second argument is larger than the
* first one, a negative value if the first is larger, and zero exactly if both
* are equal.
*/
typedef int (*BOSTree_cmp_function)(const void *, const void *);
/**
* Free function for deleted nodes.
*
* This function should free the key and data members of a node. The node
* structure itself is free()d internally by BOSZTree.
*/
typedef void (*BOSTree_free_function)(BOSNode *node);
/**
* Create a new tree.
*
* The cmp_function is mandatory, but for the free function, you may supply a
* NULL pointer if you do not have any data that needs to be free()d in
* ->key and ->data.
*/
BOSTree *bostree_new(BOSTree_cmp_function cmp_function, BOSTree_free_function free_function);
/**
* Destroy a tree and all its members.
*/
void bostree_destroy(BOSTree *tree);
/**
* Return the number of nodes in a tree
*/
unsigned int bostree_node_count(BOSTree *tree);
/**
* Insert a new member into the tree and return the associated node.
*/
BOSNode *bostree_insert(BOSTree *tree, void *key, void *data);
/**
* Remove a given node from a tree.
*/
void bostree_remove(BOSTree *tree, BOSNode *node);
/**
* Weak reference management for nodes.
*
* Nodes have an internal reference counter. They are only free()d after the
* last weak reference has been removed. Calling bostree_node_weak_unref() on a
* node which has been removed from the tree results in the weak reference
* count being decreased, the node being possibly free()d if this has been the
* last weak reference, and a NULL pointer being returned.
*/
BOSNode *bostree_node_weak_ref(BOSNode *node);
/**
* Weak reference management for nodes.
* See bostree_node_weak_ref()
*/
BOSNode *bostree_node_weak_unref(BOSTree *tree, BOSNode *node);
/**
* Return a node given a key. NULL is returned if a key is not present in the
* tree.
*/
BOSNode *bostree_lookup(BOSTree *tree, const void *key);
/**
* Return a node given an index in in-order traversal. Indexing starts at 0.
*/
BOSNode *bostree_select(BOSTree *tree, unsigned int index);
/**
* Return the next node in in-order traversal, or NULL is node was the last
* node in the tree.
*/
BOSNode *bostree_next_node(BOSNode *node);
/**
* Return the previous node in in-order traversal, or NULL is node was the first
* node in the tree.
*/
BOSNode *bostree_previous_node(BOSNode *node);
/**
* Return the rank of a node within it's owning tree.
*
* bostree_select(bostree_rank(node)) == node is always true.
*/
unsigned int bostree_rank(BOSNode *node);
#if !defined(NDEBUG) && (_BSD_SOURCE || _XOPEN_SOURCE || _POSIX_C_SOURCE >= 200112L)
void bostree_print(BOSTree *tree);
#define bostree_debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define bostree_debug(...) void
#endif
#endif