xref: /kvmtool/include/linux/rbtree.h (revision c09373009946b81f8db97a34b7aabf02ddd1d1e3)
1*c0937300SAndre Przywara /*
2*c0937300SAndre Przywara   Red Black Trees
3*c0937300SAndre Przywara   (C) 1999  Andrea Arcangeli <andrea@suse.de>
4*c0937300SAndre Przywara 
5*c0937300SAndre Przywara   This program is free software; you can redistribute it and/or modify
6*c0937300SAndre Przywara   it under the terms of the GNU General Public License as published by
7*c0937300SAndre Przywara   the Free Software Foundation; either version 2 of the License, or
8*c0937300SAndre Przywara   (at your option) any later version.
9*c0937300SAndre Przywara 
10*c0937300SAndre Przywara   This program is distributed in the hope that it will be useful,
11*c0937300SAndre Przywara   but WITHOUT ANY WARRANTY; without even the implied warranty of
12*c0937300SAndre Przywara   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*c0937300SAndre Przywara   GNU General Public License for more details.
14*c0937300SAndre Przywara 
15*c0937300SAndre Przywara   You should have received a copy of the GNU General Public License
16*c0937300SAndre Przywara   along with this program; if not, write to the Free Software
17*c0937300SAndre Przywara   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18*c0937300SAndre Przywara 
19*c0937300SAndre Przywara   linux/include/linux/rbtree.h
20*c0937300SAndre Przywara 
21*c0937300SAndre Przywara   To use rbtrees you'll have to implement your own insert and search cores.
22*c0937300SAndre Przywara   This will avoid us to use callbacks and to drop drammatically performances.
23*c0937300SAndre Przywara   I know it's not the cleaner way,  but in C (not in C++) to get
24*c0937300SAndre Przywara   performances and genericity...
25*c0937300SAndre Przywara 
26*c0937300SAndre Przywara   See Documentation/rbtree.txt for documentation and samples.
27*c0937300SAndre Przywara */
28*c0937300SAndre Przywara 
29*c0937300SAndre Przywara #ifndef	_LINUX_RBTREE_H
30*c0937300SAndre Przywara #define	_LINUX_RBTREE_H
31*c0937300SAndre Przywara 
32*c0937300SAndre Przywara #include <linux/kernel.h>
33*c0937300SAndre Przywara #include <linux/stddef.h>
34*c0937300SAndre Przywara 
35*c0937300SAndre Przywara struct rb_node {
36*c0937300SAndre Przywara 	unsigned long  __rb_parent_color;
37*c0937300SAndre Przywara 	struct rb_node *rb_right;
38*c0937300SAndre Przywara 	struct rb_node *rb_left;
39*c0937300SAndre Przywara } __attribute__((aligned(sizeof(long))));
40*c0937300SAndre Przywara     /* The alignment might seem pointless, but allegedly CRIS needs it */
41*c0937300SAndre Przywara 
42*c0937300SAndre Przywara struct rb_root {
43*c0937300SAndre Przywara 	struct rb_node *rb_node;
44*c0937300SAndre Przywara };
45*c0937300SAndre Przywara 
46*c0937300SAndre Przywara 
47*c0937300SAndre Przywara #define rb_parent(r)   ((struct rb_node *)((r)->__rb_parent_color & ~3))
48*c0937300SAndre Przywara 
49*c0937300SAndre Przywara #define RB_ROOT	(struct rb_root) { NULL, }
50*c0937300SAndre Przywara #define	rb_entry(ptr, type, member) container_of(ptr, type, member)
51*c0937300SAndre Przywara 
52*c0937300SAndre Przywara #define RB_EMPTY_ROOT(root)  ((root)->rb_node == NULL)
53*c0937300SAndre Przywara 
54*c0937300SAndre Przywara /* 'empty' nodes are nodes that are known not to be inserted in an rbtree */
55*c0937300SAndre Przywara #define RB_EMPTY_NODE(node)  \
56*c0937300SAndre Przywara 	((node)->__rb_parent_color == (unsigned long)(node))
57*c0937300SAndre Przywara #define RB_CLEAR_NODE(node)  \
58*c0937300SAndre Przywara 	((node)->__rb_parent_color = (unsigned long)(node))
59*c0937300SAndre Przywara 
60*c0937300SAndre Przywara 
61*c0937300SAndre Przywara extern void rb_insert_color(struct rb_node *, struct rb_root *);
62*c0937300SAndre Przywara extern void rb_erase(struct rb_node *, struct rb_root *);
63*c0937300SAndre Przywara 
64*c0937300SAndre Przywara 
65*c0937300SAndre Przywara /* Find logical next and previous nodes in a tree */
66*c0937300SAndre Przywara extern struct rb_node *rb_next(const struct rb_node *);
67*c0937300SAndre Przywara extern struct rb_node *rb_prev(const struct rb_node *);
68*c0937300SAndre Przywara extern struct rb_node *rb_first(const struct rb_root *);
69*c0937300SAndre Przywara extern struct rb_node *rb_last(const struct rb_root *);
70*c0937300SAndre Przywara 
71*c0937300SAndre Przywara /* Postorder iteration - always visit the parent after its children */
72*c0937300SAndre Przywara extern struct rb_node *rb_first_postorder(const struct rb_root *);
73*c0937300SAndre Przywara extern struct rb_node *rb_next_postorder(const struct rb_node *);
74*c0937300SAndre Przywara 
75*c0937300SAndre Przywara /* Fast replacement of a single node without remove/rebalance/add/rebalance */
76*c0937300SAndre Przywara extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
77*c0937300SAndre Przywara 			    struct rb_root *root);
78*c0937300SAndre Przywara 
79*c0937300SAndre Przywara static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
80*c0937300SAndre Przywara 				struct rb_node ** rb_link)
81*c0937300SAndre Przywara {
82*c0937300SAndre Przywara 	node->__rb_parent_color = (unsigned long)parent;
83*c0937300SAndre Przywara 	node->rb_left = node->rb_right = NULL;
84*c0937300SAndre Przywara 
85*c0937300SAndre Przywara 	*rb_link = node;
86*c0937300SAndre Przywara }
87*c0937300SAndre Przywara 
88*c0937300SAndre Przywara #define rb_entry_safe(ptr, type, member) \
89*c0937300SAndre Przywara 	({ typeof(ptr) ____ptr = (ptr); \
90*c0937300SAndre Przywara 	   ____ptr ? rb_entry(____ptr, type, member) : NULL; \
91*c0937300SAndre Przywara 	})
92*c0937300SAndre Przywara 
93*c0937300SAndre Przywara /**
94*c0937300SAndre Przywara  * rbtree_postorder_for_each_entry_safe - iterate over rb_root in post order of
95*c0937300SAndre Przywara  * given type safe against removal of rb_node entry
96*c0937300SAndre Przywara  *
97*c0937300SAndre Przywara  * @pos:	the 'type *' to use as a loop cursor.
98*c0937300SAndre Przywara  * @n:		another 'type *' to use as temporary storage
99*c0937300SAndre Przywara  * @root:	'rb_root *' of the rbtree.
100*c0937300SAndre Przywara  * @field:	the name of the rb_node field within 'type'.
101*c0937300SAndre Przywara  */
102*c0937300SAndre Przywara #define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
103*c0937300SAndre Przywara 	for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \
104*c0937300SAndre Przywara 	     pos && ({ n = rb_entry_safe(rb_next_postorder(&pos->field), \
105*c0937300SAndre Przywara 			typeof(*pos), field); 1; }); \
106*c0937300SAndre Przywara 	     pos = n)
107*c0937300SAndre Przywara 
108*c0937300SAndre Przywara #endif	/* _LINUX_RBTREE_H */
109