-
Notifications
You must be signed in to change notification settings - Fork 9
/
avl.h
71 lines (62 loc) · 2.53 KB
/
avl.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
/*
* avl.h - this file is part of Libtree.
* $Id: avl.h 2632 2021-05-03 06:38:18Z soci $
*
* Copyright (C) 2010 Franck Bui-Huu <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; version 2 of the
* License.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
#ifndef AVL_H
#define AVL_H
#include <stddef.h>
#include "attributes.h"
/*
* The definition has been stolen from the Linux kernel.
*/
#ifdef __GNUC__
# define avltree_container_of(node, type, member) ({ \
struct avltree_node *__mptr = (node); \
(type *)( (char *)__mptr - offsetof(type,member) );})
# define cavltree_container_of(node, type, member) ({ \
const struct avltree_node *__mptr = (node); \
(const type *)( (const char *)__mptr - offsetof(type,member) );})
#else
# define avltree_container_of(node, type, member) \
((type *)((char *)(node) - offsetof(type, member)))
# define cavltree_container_of(node, type, member) \
((const type *)((const char *)(node) - offsetof(type, member)))
#endif /* __GNUC__ */
/*
* AVL tree
*/
struct avltree_node {
struct avltree_node *left, *right;
struct avltree_node *parent;
signed char balance; /* balance factor [-2:+2] */
};
typedef FAST_CALL int (*avltree_cmp_fn_t)(const struct avltree_node *, const struct avltree_node *);
typedef void (*avltree_free_fn_t)(struct avltree_node *);
struct avltree {
struct avltree_node *root;
};
static inline void avltree_init(struct avltree *tree) {
tree->root = NULL;
}
extern FAST_CALL struct avltree_node *avltree_lookup(const struct avltree_node *, const struct avltree *, avltree_cmp_fn_t);
extern struct avltree_node *avltree_insert(struct avltree_node *, struct avltree *, avltree_cmp_fn_t);
extern void avltree_remove(struct avltree_node *, struct avltree *);
extern void avltree_destroy(struct avltree *, avltree_free_fn_t);
#endif