bloodhound  0.2.0
AVL self-balancing binary search tree with intrusive nodes.
bloodhound.h
Go to the documentation of this file.
1 /* MIT License
2  *
3  * Copyright (c) 2019 Gregory Meyer
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including
14  * the next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24  * IN THE SOFTWARE.
25  */
26 
27 #ifndef BLOODHOUND_H
28 #define BLOODHOUND_H
29 
37 #include <stddef.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
53 typedef struct AvlTree AvlTree;
54 
98 typedef struct AvlNode AvlNode;
99 
100 /* int compare(const AvlNode *lhs, const AvlNode *rhs, void *arg); */
101 typedef int (*AvlComparator)(const AvlNode*, const AvlNode*, void*);
102 
103 /* int compare(const void *lhs, const AvlNode *rhs, void *arg); */
104 typedef int (*AvlHetComparator)(const void*, const AvlNode*, void*);
105 
106 /* void traverse(void *context, const AvlNode *node); */
107 typedef void (*AvlTraverseCb)(void*, const AvlNode*);
108 
109 /* void traverse_mut(void *context, AvlNode *node); */
110 typedef void (*AvlTraverseMutCb)(void*, AvlNode*);
111 
112 /* void delete(AvlNode *node, void *arg); */
113 typedef void (*AvlDeleter)(AvlNode*, void*);
114 
127 void AvlTree_new(AvlTree *self, AvlComparator compare, void *compare_arg,
128  AvlDeleter deleter, void *deleter_arg);
129 
137 void AvlTree_drop(AvlTree *self);
138 
148 const AvlNode* AvlTree_get(const AvlTree *self, const void *key,
149  AvlHetComparator compare, void *arg);
150 
160 AvlNode* AvlTree_get_mut(AvlTree *self, const void *key, AvlHetComparator compare, void *arg);
161 
170 AvlNode* AvlTree_insert(AvlTree *self, AvlNode *node);
171 
190 AvlNode* AvlTree_get_or_insert(AvlTree *self, const void *key, AvlHetComparator compare,
191  void *compare_arg, AvlNode* (*insert)(const void*, void*),
192  void *insert_arg, int *inserted);
193 
204 AvlNode* AvlTree_remove(AvlTree *self, const void *key, AvlHetComparator compare, void *arg);
205 
211 void AvlTree_clear(AvlTree *self);
212 
223 struct AvlTree {
224  AvlNode *root;
225  size_t len;
226  AvlComparator compare;
227  void *compare_arg;
228  AvlDeleter deleter;
229  void *deleter_arg;
230 };
231 
275 struct AvlNode {
276  AvlNode *left;
277  AvlNode *right;
278  signed char balance_factor; /* one of {-2, -1, 0, 1, -2} */
279 };
280 
281 #ifdef __cplusplus
282 } // extern "C"
283 #endif
284 
285 #endif
const AvlNode * AvlTree_get(const AvlTree *self, const void *key, AvlHetComparator compare, void *arg)
void AvlTree_new(AvlTree *self, AvlComparator compare, void *compare_arg, AvlDeleter deleter, void *deleter_arg)
Initializes an empty AvlTree.
void AvlTree_drop(AvlTree *self)
Drops an AvlTree, removing all members.
AvlNode * AvlTree_insert(AvlTree *self, AvlNode *node)
Inserts an element into an AvlTree.
AVL self-balancing binary search tree.
Definition: bloodhound.h:223
AvlNode * AvlTree_remove(AvlTree *self, const void *key, AvlHetComparator compare, void *arg)
Removes the node that compares equal to a key.
AvlNode * AvlTree_get_or_insert(AvlTree *self, const void *key, AvlHetComparator compare, void *compare_arg, AvlNode *(*insert)(const void *, void *), void *insert_arg, int *inserted)
Inserts an element into an AvlTree if no element with a matching key is found.
void AvlTree_clear(AvlTree *self)
Clears the tree, removing all members.
Intrusive AVL tree node.
Definition: bloodhound.h:275
AvlNode * AvlTree_get_mut(AvlTree *self, const void *key, AvlHetComparator compare, void *arg)