1 /* SPDX-License-Identifier: GPL-2.0-or-later */
4 (C) 1999 Andrea Arcangeli <andrea@suse.de>
5 (C) 2002 David Woodhouse <dwmw2@infradead.org>
6 (C) 2012 Michel Lespinasse <walken@google.com>
9 tools/linux/include/linux/rbtree_augmented.h
12 linux/include/linux/rbtree_augmented.h
15 #ifndef _TOOLS_LINUX_RBTREE_AUGMENTED_H
16 #define _TOOLS_LINUX_RBTREE_AUGMENTED_H
18 #include <linux/compiler.h>
19 #include <linux/rbtree.h>
22 * Please note - only struct rb_augment_callbacks and the prototypes for
23 * rb_insert_augmented() and rb_erase_augmented() are intended to be public.
24 * The rest are implementation details you are not expected to depend on.
26 * See Documentation/core-api/rbtree.rst for documentation and samples.
29 struct rb_augment_callbacks
{
30 void (*propagate
)(struct rb_node
*node
, struct rb_node
*stop
);
31 void (*copy
)(struct rb_node
*old
, struct rb_node
*new);
32 void (*rotate
)(struct rb_node
*old
, struct rb_node
*new);
35 extern void __rb_insert_augmented(struct rb_node
*node
, struct rb_root
*root
,
36 void (*augment_rotate
)(struct rb_node
*old
, struct rb_node
*new));
39 * Fixup the rbtree and update the augmented information when rebalancing.
41 * On insertion, the user must update the augmented information on the path
42 * leading to the inserted node, then call rb_link_node() as usual and
43 * rb_insert_augmented() instead of the usual rb_insert_color() call.
44 * If rb_insert_augmented() rebalances the rbtree, it will callback into
45 * a user provided function to update the augmented information on the
49 rb_insert_augmented(struct rb_node
*node
, struct rb_root
*root
,
50 const struct rb_augment_callbacks
*augment
)
52 __rb_insert_augmented(node
, root
, augment
->rotate
);
56 rb_insert_augmented_cached(struct rb_node
*node
,
57 struct rb_root_cached
*root
, bool newleft
,
58 const struct rb_augment_callbacks
*augment
)
61 root
->rb_leftmost
= node
;
62 rb_insert_augmented(node
, &root
->rb_root
, augment
);
66 * Template for declaring augmented rbtree callbacks (generic case)
68 * RBSTATIC: 'static' or empty
69 * RBNAME: name of the rb_augment_callbacks structure
70 * RBSTRUCT: struct type of the tree nodes
71 * RBFIELD: name of struct rb_node field within RBSTRUCT
72 * RBAUGMENTED: name of field within RBSTRUCT holding data for subtree
73 * RBCOMPUTE: name of function that recomputes the RBAUGMENTED data
76 #define RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
77 RBSTRUCT, RBFIELD, RBAUGMENTED, RBCOMPUTE) \
79 RBNAME ## _propagate(struct rb_node *rb, struct rb_node *stop) \
81 while (rb != stop) { \
82 RBSTRUCT *node = rb_entry(rb, RBSTRUCT, RBFIELD); \
83 if (RBCOMPUTE(node, true)) \
85 rb = rb_parent(&node->RBFIELD); \
89 RBNAME ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
91 RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
92 RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
93 new->RBAUGMENTED = old->RBAUGMENTED; \
96 RBNAME ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
98 RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
99 RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
100 new->RBAUGMENTED = old->RBAUGMENTED; \
101 RBCOMPUTE(old, false); \
103 RBSTATIC const struct rb_augment_callbacks RBNAME = { \
104 .propagate = RBNAME ## _propagate, \
105 .copy = RBNAME ## _copy, \
106 .rotate = RBNAME ## _rotate \
110 * Template for declaring augmented rbtree callbacks,
111 * computing RBAUGMENTED scalar as max(RBCOMPUTE(node)) for all subtree nodes.
113 * RBSTATIC: 'static' or empty
114 * RBNAME: name of the rb_augment_callbacks structure
115 * RBSTRUCT: struct type of the tree nodes
116 * RBFIELD: name of struct rb_node field within RBSTRUCT
117 * RBTYPE: type of the RBAUGMENTED field
118 * RBAUGMENTED: name of RBTYPE field within RBSTRUCT holding data for subtree
119 * RBCOMPUTE: name of function that returns the per-node RBTYPE scalar
122 #define RB_DECLARE_CALLBACKS_MAX(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
123 RBTYPE, RBAUGMENTED, RBCOMPUTE) \
124 static inline bool RBNAME ## _compute_max(RBSTRUCT *node, bool exit) \
127 RBTYPE max = RBCOMPUTE(node); \
128 if (node->RBFIELD.rb_left) { \
129 child = rb_entry(node->RBFIELD.rb_left, RBSTRUCT, RBFIELD); \
130 if (child->RBAUGMENTED > max) \
131 max = child->RBAUGMENTED; \
133 if (node->RBFIELD.rb_right) { \
134 child = rb_entry(node->RBFIELD.rb_right, RBSTRUCT, RBFIELD); \
135 if (child->RBAUGMENTED > max) \
136 max = child->RBAUGMENTED; \
138 if (exit && node->RBAUGMENTED == max) \
140 node->RBAUGMENTED = max; \
143 RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
144 RBSTRUCT, RBFIELD, RBAUGMENTED, RBNAME ## _compute_max)
150 #define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
152 #define __rb_color(pc) ((pc) & 1)
153 #define __rb_is_black(pc) __rb_color(pc)
154 #define __rb_is_red(pc) (!__rb_color(pc))
155 #define rb_color(rb) __rb_color((rb)->__rb_parent_color)
156 #define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
157 #define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
159 static inline void rb_set_parent(struct rb_node
*rb
, struct rb_node
*p
)
161 rb
->__rb_parent_color
= rb_color(rb
) + (unsigned long)p
;
164 static inline void rb_set_parent_color(struct rb_node
*rb
,
165 struct rb_node
*p
, int color
)
167 rb
->__rb_parent_color
= (unsigned long)p
+ color
;
171 __rb_change_child(struct rb_node
*old
, struct rb_node
*new,
172 struct rb_node
*parent
, struct rb_root
*root
)
175 if (parent
->rb_left
== old
)
176 WRITE_ONCE(parent
->rb_left
, new);
178 WRITE_ONCE(parent
->rb_right
, new);
180 WRITE_ONCE(root
->rb_node
, new);
183 extern void __rb_erase_color(struct rb_node
*parent
, struct rb_root
*root
,
184 void (*augment_rotate
)(struct rb_node
*old
, struct rb_node
*new));
186 static __always_inline
struct rb_node
*
187 __rb_erase_augmented(struct rb_node
*node
, struct rb_root
*root
,
188 const struct rb_augment_callbacks
*augment
)
190 struct rb_node
*child
= node
->rb_right
;
191 struct rb_node
*tmp
= node
->rb_left
;
192 struct rb_node
*parent
, *rebalance
;
197 * Case 1: node to erase has no more than 1 child (easy!)
199 * Note that if there is one child it must be red due to 5)
200 * and node must be black due to 4). We adjust colors locally
201 * so as to bypass __rb_erase_color() later on.
203 pc
= node
->__rb_parent_color
;
204 parent
= __rb_parent(pc
);
205 __rb_change_child(node
, child
, parent
, root
);
207 child
->__rb_parent_color
= pc
;
210 rebalance
= __rb_is_black(pc
) ? parent
: NULL
;
213 /* Still case 1, but this time the child is node->rb_left */
214 tmp
->__rb_parent_color
= pc
= node
->__rb_parent_color
;
215 parent
= __rb_parent(pc
);
216 __rb_change_child(node
, tmp
, parent
, root
);
220 struct rb_node
*successor
= child
, *child2
;
222 tmp
= child
->rb_left
;
225 * Case 2: node's successor is its right child
234 child2
= successor
->rb_right
;
236 augment
->copy(node
, successor
);
239 * Case 3: node's successor is leftmost under
240 * node's right child subtree
257 child2
= successor
->rb_right
;
258 WRITE_ONCE(parent
->rb_left
, child2
);
259 WRITE_ONCE(successor
->rb_right
, child
);
260 rb_set_parent(child
, successor
);
262 augment
->copy(node
, successor
);
263 augment
->propagate(parent
, successor
);
267 WRITE_ONCE(successor
->rb_left
, tmp
);
268 rb_set_parent(tmp
, successor
);
270 pc
= node
->__rb_parent_color
;
271 tmp
= __rb_parent(pc
);
272 __rb_change_child(node
, successor
, tmp
, root
);
275 successor
->__rb_parent_color
= pc
;
276 rb_set_parent_color(child2
, parent
, RB_BLACK
);
279 unsigned long pc2
= successor
->__rb_parent_color
;
280 successor
->__rb_parent_color
= pc
;
281 rebalance
= __rb_is_black(pc2
) ? parent
: NULL
;
286 augment
->propagate(tmp
, NULL
);
290 static __always_inline
void
291 rb_erase_augmented(struct rb_node
*node
, struct rb_root
*root
,
292 const struct rb_augment_callbacks
*augment
)
294 struct rb_node
*rebalance
= __rb_erase_augmented(node
, root
, augment
);
296 __rb_erase_color(rebalance
, root
, augment
->rotate
);
299 static __always_inline
void
300 rb_erase_augmented_cached(struct rb_node
*node
, struct rb_root_cached
*root
,
301 const struct rb_augment_callbacks
*augment
)
303 if (root
->rb_leftmost
== node
)
304 root
->rb_leftmost
= rb_next(node
);
305 rb_erase_augmented(node
, &root
->rb_root
, augment
);
308 #endif /* _TOOLS_LINUX_RBTREE_AUGMENTED_H */