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#ifndef _TOOLS_LINUX_RBTREE_AUGMENTED_H
28#define _TOOLS_LINUX_RBTREE_AUGMENTED_H
29
30#include <linux/compiler.h>
31#include <linux/rbtree.h>
32
33
34
35
36
37
38
39
40
41struct rb_augment_callbacks {
42 void (*propagate)(struct rb_node *node, struct rb_node *stop);
43 void (*copy)(struct rb_node *old, struct rb_node *new);
44 void (*rotate)(struct rb_node *old, struct rb_node *new);
45};
46
47extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
48 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
49
50
51
52
53
54
55
56
57
58
59static inline void
60rb_insert_augmented(struct rb_node *node, struct rb_root *root,
61 const struct rb_augment_callbacks *augment)
62{
63 __rb_insert_augmented(node, root, augment->rotate);
64}
65
66#define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \
67 rbtype, rbaugmented, rbcompute) \
68static inline void \
69rbname ## _propagate(struct rb_node *rb, struct rb_node *stop) \
70{ \
71 while (rb != stop) { \
72 rbstruct *node = rb_entry(rb, rbstruct, rbfield); \
73 rbtype augmented = rbcompute(node); \
74 if (node->rbaugmented == augmented) \
75 break; \
76 node->rbaugmented = augmented; \
77 rb = rb_parent(&node->rbfield); \
78 } \
79} \
80static inline void \
81rbname ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
82{ \
83 rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
84 rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
85 new->rbaugmented = old->rbaugmented; \
86} \
87static void \
88rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
89{ \
90 rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
91 rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
92 new->rbaugmented = old->rbaugmented; \
93 old->rbaugmented = rbcompute(old); \
94} \
95rbstatic const struct rb_augment_callbacks rbname = { \
96 rbname ## _propagate, rbname ## _copy, rbname ## _rotate \
97};
98
99
100#define RB_RED 0
101#define RB_BLACK 1
102
103#define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
104
105#define __rb_color(pc) ((pc) & 1)
106#define __rb_is_black(pc) __rb_color(pc)
107#define __rb_is_red(pc) (!__rb_color(pc))
108#define rb_color(rb) __rb_color((rb)->__rb_parent_color)
109#define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
110#define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
111
112static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
113{
114 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
115}
116
117static inline void rb_set_parent_color(struct rb_node *rb,
118 struct rb_node *p, int color)
119{
120 rb->__rb_parent_color = (unsigned long)p | color;
121}
122
123static inline void
124__rb_change_child(struct rb_node *old, struct rb_node *new,
125 struct rb_node *parent, struct rb_root *root)
126{
127 if (parent) {
128 if (parent->rb_left == old)
129 parent->rb_left = new;
130 else
131 parent->rb_right = new;
132 } else
133 root->rb_node = new;
134}
135
136extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
137 void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
138
139static __always_inline struct rb_node *
140__rb_erase_augmented(struct rb_node *node, struct rb_root *root,
141 const struct rb_augment_callbacks *augment)
142{
143 struct rb_node *child = node->rb_right, *tmp = node->rb_left;
144 struct rb_node *parent, *rebalance;
145 unsigned long pc;
146
147 if (!tmp) {
148
149
150
151
152
153
154
155 pc = node->__rb_parent_color;
156 parent = __rb_parent(pc);
157 __rb_change_child(node, child, parent, root);
158 if (child) {
159 child->__rb_parent_color = pc;
160 rebalance = NULL;
161 } else
162 rebalance = __rb_is_black(pc) ? parent : NULL;
163 tmp = parent;
164 } else if (!child) {
165
166 tmp->__rb_parent_color = pc = node->__rb_parent_color;
167 parent = __rb_parent(pc);
168 __rb_change_child(node, tmp, parent, root);
169 rebalance = NULL;
170 tmp = parent;
171 } else {
172 struct rb_node *successor = child, *child2;
173 tmp = child->rb_left;
174 if (!tmp) {
175
176
177
178
179
180
181
182
183
184 parent = successor;
185 child2 = successor->rb_right;
186 augment->copy(node, successor);
187 } else {
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 do {
203 parent = successor;
204 successor = tmp;
205 tmp = tmp->rb_left;
206 } while (tmp);
207 parent->rb_left = child2 = successor->rb_right;
208 successor->rb_right = child;
209 rb_set_parent(child, successor);
210 augment->copy(node, successor);
211 augment->propagate(parent, successor);
212 }
213
214 successor->rb_left = tmp = node->rb_left;
215 rb_set_parent(tmp, successor);
216
217 pc = node->__rb_parent_color;
218 tmp = __rb_parent(pc);
219 __rb_change_child(node, successor, tmp, root);
220 if (child2) {
221 successor->__rb_parent_color = pc;
222 rb_set_parent_color(child2, parent, RB_BLACK);
223 rebalance = NULL;
224 } else {
225 unsigned long pc2 = successor->__rb_parent_color;
226 successor->__rb_parent_color = pc;
227 rebalance = __rb_is_black(pc2) ? parent : NULL;
228 }
229 tmp = successor;
230 }
231
232 augment->propagate(tmp, NULL);
233 return rebalance;
234}
235
236static __always_inline void
237rb_erase_augmented(struct rb_node *node, struct rb_root *root,
238 const struct rb_augment_callbacks *augment)
239{
240 struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
241 if (rebalance)
242 __rb_erase_color(rebalance, root, augment->rotate);
243}
244
245#endif
246