File: iv_avl.h

package info (click to toggle)
ivykis 0.30.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 852 kB
  • sloc: ansic: 6,071; makefile: 254
file content (110 lines) | stat: -rw-r--r-- 2,681 bytes parent folder | download | duplicates (8)
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
/*
 * ivykis, an event handling library
 * Copyright (C) 2010 Lennert Buytenhek
 * Dedicated to Marija Kulikova.
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version
 * 2.1 as published by the Free Software Foundation.
 *
 * 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 version 2.1 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License version 2.1 along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef __IV_AVL_H
#define __IV_AVL_H

#ifdef __cplusplus
extern "C" {
#endif

#include <inttypes.h>

struct iv_avl_node {
	struct iv_avl_node	*left;
	struct iv_avl_node	*right;
	struct iv_avl_node	*parent;
	uint8_t			height;
};

struct iv_avl_tree {
	int			(*compare)(struct iv_avl_node *a,
					   struct iv_avl_node *b);

	struct iv_avl_node	*root;
};

#define IV_AVL_TREE_INIT(comp)				\
	{ .compare = comp, .root = NULL }

#define INIT_IV_AVL_TREE(tree, comp)			\
	do {						\
		(tree)->compare = (comp);		\
		(tree)->root = NULL;			\
	} while (0)

int iv_avl_tree_insert(struct iv_avl_tree *tree, struct iv_avl_node *an);
void iv_avl_tree_delete(struct iv_avl_tree *tree, struct iv_avl_node *an);
struct iv_avl_node *iv_avl_tree_next(struct iv_avl_node *an);
struct iv_avl_node *iv_avl_tree_prev(struct iv_avl_node *an);

static inline int iv_avl_tree_empty(struct iv_avl_tree *tree)
{
	return tree->root == NULL;
}

static inline struct iv_avl_node *iv_avl_tree_min(struct iv_avl_tree *tree)
{
	if (tree->root != NULL) {
		struct iv_avl_node *an;

		an = tree->root;
		while (an->left != NULL)
			an = an->left;

		return an;
	}

	return NULL;
}

static inline struct iv_avl_node *iv_avl_tree_max(struct iv_avl_tree *tree)
{
	if (tree->root != NULL) {
		struct iv_avl_node *an;

		an = tree->root;
		while (an->right != NULL)
			an = an->right;

		return an;
	}

	return NULL;
}

#define iv_avl_tree_for_each(an, tree) \
	for (an = iv_avl_tree_min(tree); an != NULL; an = iv_avl_tree_next(an))

static inline struct iv_avl_node *iv_avl_tree_next_safe(struct iv_avl_node *an)
{
	return an != NULL ? iv_avl_tree_next(an) : NULL;
}

#define iv_avl_tree_for_each_safe(an, an2, tree) \
	for (an = iv_avl_tree_min(tree), an2 = iv_avl_tree_next_safe(an); \
	     an != NULL; an = an2, an2 = iv_avl_tree_next_safe(an))

#ifdef __cplusplus
}
#endif


#endif