Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | Red Black Trees |
| 3 | (C) 1999 Andrea Arcangeli <andrea@suse.de> |
| 4 | (C) 2002 David Woodhouse <dwmw2@infradead.org> |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | |
| 20 | linux/lib/rbtree.c |
| 21 | */ |
| 22 | |
| 23 | #include <linux/rbtree.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 24 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 26 | /* |
| 27 | * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree |
| 28 | * |
| 29 | * 1) A node is either red or black |
| 30 | * 2) The root is black |
| 31 | * 3) All leaves (NULL) are black |
| 32 | * 4) Both children of every red node are black |
| 33 | * 5) Every simple path from root to leaves contains the same number |
| 34 | * of black nodes. |
| 35 | * |
| 36 | * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two |
| 37 | * consecutive red nodes in a path and every red node is therefore followed by |
| 38 | * a black. So if B is the number of black nodes on every simple path (as per |
| 39 | * 5), then the longest possible path due to 4 is 2B. |
| 40 | * |
| 41 | * We shall indicate color with case, where black nodes are uppercase and red |
Michel Lespinasse | 6280d23 | 2012-10-08 16:30:57 -0700 | [diff] [blame] | 42 | * nodes will be lowercase. Unknown color nodes shall be drawn as red within |
| 43 | * parentheses and have some accompanying text comment. |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 44 | */ |
| 45 | |
Michel Lespinasse | bf7ad8e | 2012-10-08 16:30:37 -0700 | [diff] [blame] | 46 | #define RB_RED 0 |
| 47 | #define RB_BLACK 1 |
| 48 | |
| 49 | #define rb_color(r) ((r)->__rb_parent_color & 1) |
| 50 | #define rb_is_red(r) (!rb_color(r)) |
| 51 | #define rb_is_black(r) rb_color(r) |
Michel Lespinasse | bf7ad8e | 2012-10-08 16:30:37 -0700 | [diff] [blame] | 52 | |
| 53 | static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p) |
| 54 | { |
| 55 | rb->__rb_parent_color = rb_color(rb) | (unsigned long)p; |
| 56 | } |
Michel Lespinasse | bf7ad8e | 2012-10-08 16:30:37 -0700 | [diff] [blame] | 57 | |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 58 | static inline void rb_set_parent_color(struct rb_node *rb, |
| 59 | struct rb_node *p, int color) |
| 60 | { |
| 61 | rb->__rb_parent_color = (unsigned long)p | color; |
| 62 | } |
| 63 | |
| 64 | static inline struct rb_node *rb_red_parent(struct rb_node *red) |
| 65 | { |
| 66 | return (struct rb_node *)red->__rb_parent_color; |
| 67 | } |
| 68 | |
Michel Lespinasse | 7abc704 | 2012-10-08 16:31:07 -0700 | [diff] [blame^] | 69 | static inline void |
| 70 | __rb_change_child(struct rb_node *old, struct rb_node *new, |
| 71 | struct rb_node *parent, struct rb_root *root) |
| 72 | { |
| 73 | if (parent) { |
| 74 | if (parent->rb_left == old) |
| 75 | parent->rb_left = new; |
| 76 | else |
| 77 | parent->rb_right = new; |
| 78 | } else |
| 79 | root->rb_node = new; |
| 80 | } |
| 81 | |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 82 | /* |
| 83 | * Helper function for rotations: |
| 84 | * - old's parent and color get assigned to new |
| 85 | * - old gets assigned new as a parent and 'color' as a color. |
| 86 | */ |
| 87 | static inline void |
| 88 | __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new, |
| 89 | struct rb_root *root, int color) |
| 90 | { |
| 91 | struct rb_node *parent = rb_parent(old); |
| 92 | new->__rb_parent_color = old->__rb_parent_color; |
| 93 | rb_set_parent_color(old, new, color); |
Michel Lespinasse | 7abc704 | 2012-10-08 16:31:07 -0700 | [diff] [blame^] | 94 | __rb_change_child(old, new, parent, root); |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | void rb_insert_color(struct rb_node *node, struct rb_root *root) |
| 98 | { |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 99 | struct rb_node *parent = rb_red_parent(node), *gparent, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Michel Lespinasse | 6d58452 | 2012-10-08 16:30:44 -0700 | [diff] [blame] | 101 | while (true) { |
| 102 | /* |
| 103 | * Loop invariant: node is red |
| 104 | * |
| 105 | * If there is a black parent, we are done. |
| 106 | * Otherwise, take some corrective action as we don't |
| 107 | * want a red root or two consecutive red nodes. |
| 108 | */ |
Michel Lespinasse | 6d58452 | 2012-10-08 16:30:44 -0700 | [diff] [blame] | 109 | if (!parent) { |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 110 | rb_set_parent_color(node, NULL, RB_BLACK); |
Michel Lespinasse | 6d58452 | 2012-10-08 16:30:44 -0700 | [diff] [blame] | 111 | break; |
| 112 | } else if (rb_is_black(parent)) |
| 113 | break; |
| 114 | |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 115 | gparent = rb_red_parent(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Michel Lespinasse | 59633ab | 2012-10-08 16:31:02 -0700 | [diff] [blame] | 117 | tmp = gparent->rb_right; |
| 118 | if (parent != tmp) { /* parent == gparent->rb_left */ |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 119 | if (tmp && rb_is_red(tmp)) { |
| 120 | /* |
| 121 | * Case 1 - color flips |
| 122 | * |
| 123 | * G g |
| 124 | * / \ / \ |
| 125 | * p u --> P U |
| 126 | * / / |
| 127 | * n N |
| 128 | * |
| 129 | * However, since g's parent might be red, and |
| 130 | * 4) does not allow this, we need to recurse |
| 131 | * at g. |
| 132 | */ |
| 133 | rb_set_parent_color(tmp, gparent, RB_BLACK); |
| 134 | rb_set_parent_color(parent, gparent, RB_BLACK); |
| 135 | node = gparent; |
| 136 | parent = rb_parent(node); |
| 137 | rb_set_parent_color(node, parent, RB_RED); |
| 138 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Michel Lespinasse | 59633ab | 2012-10-08 16:31:02 -0700 | [diff] [blame] | 141 | tmp = parent->rb_right; |
| 142 | if (node == tmp) { |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 143 | /* |
| 144 | * Case 2 - left rotate at parent |
| 145 | * |
| 146 | * G G |
| 147 | * / \ / \ |
| 148 | * p U --> n U |
| 149 | * \ / |
| 150 | * n p |
| 151 | * |
| 152 | * This still leaves us in violation of 4), the |
| 153 | * continuation into Case 3 will fix that. |
| 154 | */ |
| 155 | parent->rb_right = tmp = node->rb_left; |
| 156 | node->rb_left = parent; |
| 157 | if (tmp) |
| 158 | rb_set_parent_color(tmp, parent, |
| 159 | RB_BLACK); |
| 160 | rb_set_parent_color(parent, node, RB_RED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | parent = node; |
Michel Lespinasse | 59633ab | 2012-10-08 16:31:02 -0700 | [diff] [blame] | 162 | tmp = node->rb_right; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 165 | /* |
| 166 | * Case 3 - right rotate at gparent |
| 167 | * |
| 168 | * G P |
| 169 | * / \ / \ |
| 170 | * p U --> n g |
| 171 | * / \ |
| 172 | * n U |
| 173 | */ |
Michel Lespinasse | 59633ab | 2012-10-08 16:31:02 -0700 | [diff] [blame] | 174 | gparent->rb_left = tmp; /* == parent->rb_right */ |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 175 | parent->rb_right = gparent; |
| 176 | if (tmp) |
| 177 | rb_set_parent_color(tmp, gparent, RB_BLACK); |
| 178 | __rb_rotate_set_parents(gparent, parent, root, RB_RED); |
Michel Lespinasse | 1f05286 | 2012-10-08 16:30:42 -0700 | [diff] [blame] | 179 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } else { |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 181 | tmp = gparent->rb_left; |
| 182 | if (tmp && rb_is_red(tmp)) { |
| 183 | /* Case 1 - color flips */ |
| 184 | rb_set_parent_color(tmp, gparent, RB_BLACK); |
| 185 | rb_set_parent_color(parent, gparent, RB_BLACK); |
| 186 | node = gparent; |
| 187 | parent = rb_parent(node); |
| 188 | rb_set_parent_color(node, parent, RB_RED); |
| 189 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Michel Lespinasse | 59633ab | 2012-10-08 16:31:02 -0700 | [diff] [blame] | 192 | tmp = parent->rb_left; |
| 193 | if (node == tmp) { |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 194 | /* Case 2 - right rotate at parent */ |
| 195 | parent->rb_left = tmp = node->rb_right; |
| 196 | node->rb_right = parent; |
| 197 | if (tmp) |
| 198 | rb_set_parent_color(tmp, parent, |
| 199 | RB_BLACK); |
| 200 | rb_set_parent_color(parent, node, RB_RED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | parent = node; |
Michel Lespinasse | 59633ab | 2012-10-08 16:31:02 -0700 | [diff] [blame] | 202 | tmp = node->rb_left; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 205 | /* Case 3 - left rotate at gparent */ |
Michel Lespinasse | 59633ab | 2012-10-08 16:31:02 -0700 | [diff] [blame] | 206 | gparent->rb_right = tmp; /* == parent->rb_left */ |
Michel Lespinasse | 5bc9188 | 2012-10-08 16:30:47 -0700 | [diff] [blame] | 207 | parent->rb_left = gparent; |
| 208 | if (tmp) |
| 209 | rb_set_parent_color(tmp, gparent, RB_BLACK); |
| 210 | __rb_rotate_set_parents(gparent, parent, root, RB_RED); |
Michel Lespinasse | 1f05286 | 2012-10-08 16:30:42 -0700 | [diff] [blame] | 211 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | EXPORT_SYMBOL(rb_insert_color); |
| 216 | |
| 217 | static void __rb_erase_color(struct rb_node *node, struct rb_node *parent, |
| 218 | struct rb_root *root) |
| 219 | { |
Michel Lespinasse | 6280d23 | 2012-10-08 16:30:57 -0700 | [diff] [blame] | 220 | struct rb_node *sibling, *tmp1, *tmp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Michel Lespinasse | d6ff127 | 2012-10-08 16:30:50 -0700 | [diff] [blame] | 222 | while (true) { |
| 223 | /* |
| 224 | * Loop invariant: all leaf paths going through node have a |
| 225 | * black node count that is 1 lower than other leaf paths. |
| 226 | * |
| 227 | * If node is red, we can flip it to black to adjust. |
| 228 | * If node is the root, all leaf paths go through it. |
| 229 | * Otherwise, we need to adjust the tree through color flips |
| 230 | * and tree rotations as per one of the 4 cases below. |
| 231 | */ |
| 232 | if (node && rb_is_red(node)) { |
Michel Lespinasse | 6280d23 | 2012-10-08 16:30:57 -0700 | [diff] [blame] | 233 | rb_set_parent_color(node, parent, RB_BLACK); |
Michel Lespinasse | d6ff127 | 2012-10-08 16:30:50 -0700 | [diff] [blame] | 234 | break; |
| 235 | } else if (!parent) { |
| 236 | break; |
Michel Lespinasse | 59633ab | 2012-10-08 16:31:02 -0700 | [diff] [blame] | 237 | } |
| 238 | sibling = parent->rb_right; |
| 239 | if (node != sibling) { /* node == parent->rb_left */ |
Michel Lespinasse | 6280d23 | 2012-10-08 16:30:57 -0700 | [diff] [blame] | 240 | if (rb_is_red(sibling)) { |
| 241 | /* |
| 242 | * Case 1 - left rotate at parent |
| 243 | * |
| 244 | * P S |
| 245 | * / \ / \ |
| 246 | * N s --> p Sr |
| 247 | * / \ / \ |
| 248 | * Sl Sr N Sl |
| 249 | */ |
| 250 | parent->rb_right = tmp1 = sibling->rb_left; |
| 251 | sibling->rb_left = parent; |
| 252 | rb_set_parent_color(tmp1, parent, RB_BLACK); |
| 253 | __rb_rotate_set_parents(parent, sibling, root, |
| 254 | RB_RED); |
| 255 | sibling = tmp1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
Michel Lespinasse | 6280d23 | 2012-10-08 16:30:57 -0700 | [diff] [blame] | 257 | tmp1 = sibling->rb_right; |
| 258 | if (!tmp1 || rb_is_black(tmp1)) { |
| 259 | tmp2 = sibling->rb_left; |
| 260 | if (!tmp2 || rb_is_black(tmp2)) { |
| 261 | /* |
| 262 | * Case 2 - sibling color flip |
| 263 | * (p could be either color here) |
| 264 | * |
| 265 | * (p) (p) |
| 266 | * / \ / \ |
| 267 | * N S --> N s |
| 268 | * / \ / \ |
| 269 | * Sl Sr Sl Sr |
| 270 | * |
| 271 | * This leaves us violating 5), so |
| 272 | * recurse at p. If p is red, the |
| 273 | * recursion will just flip it to black |
| 274 | * and exit. If coming from Case 1, |
| 275 | * p is known to be red. |
| 276 | */ |
| 277 | rb_set_parent_color(sibling, parent, |
| 278 | RB_RED); |
Michel Lespinasse | e125d14 | 2012-10-08 16:30:54 -0700 | [diff] [blame] | 279 | node = parent; |
| 280 | parent = rb_parent(node); |
| 281 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
Michel Lespinasse | 6280d23 | 2012-10-08 16:30:57 -0700 | [diff] [blame] | 283 | /* |
| 284 | * Case 3 - right rotate at sibling |
| 285 | * (p could be either color here) |
| 286 | * |
| 287 | * (p) (p) |
| 288 | * / \ / \ |
| 289 | * N S --> N Sl |
| 290 | * / \ \ |
| 291 | * sl Sr s |
| 292 | * \ |
| 293 | * Sr |
| 294 | */ |
| 295 | sibling->rb_left = tmp1 = tmp2->rb_right; |
| 296 | tmp2->rb_right = sibling; |
| 297 | parent->rb_right = tmp2; |
| 298 | if (tmp1) |
| 299 | rb_set_parent_color(tmp1, sibling, |
| 300 | RB_BLACK); |
| 301 | tmp1 = sibling; |
| 302 | sibling = tmp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
Michel Lespinasse | 6280d23 | 2012-10-08 16:30:57 -0700 | [diff] [blame] | 304 | /* |
| 305 | * Case 4 - left rotate at parent + color flips |
| 306 | * (p and sl could be either color here. |
| 307 | * After rotation, p becomes black, s acquires |
| 308 | * p's color, and sl keeps its color) |
| 309 | * |
| 310 | * (p) (s) |
| 311 | * / \ / \ |
| 312 | * N S --> P Sr |
| 313 | * / \ / \ |
| 314 | * (sl) sr N (sl) |
| 315 | */ |
| 316 | parent->rb_right = tmp2 = sibling->rb_left; |
| 317 | sibling->rb_left = parent; |
| 318 | rb_set_parent_color(tmp1, sibling, RB_BLACK); |
| 319 | if (tmp2) |
| 320 | rb_set_parent(tmp2, parent); |
| 321 | __rb_rotate_set_parents(parent, sibling, root, |
| 322 | RB_BLACK); |
Michel Lespinasse | e125d14 | 2012-10-08 16:30:54 -0700 | [diff] [blame] | 323 | break; |
Michel Lespinasse | d6ff127 | 2012-10-08 16:30:50 -0700 | [diff] [blame] | 324 | } else { |
Michel Lespinasse | 6280d23 | 2012-10-08 16:30:57 -0700 | [diff] [blame] | 325 | sibling = parent->rb_left; |
| 326 | if (rb_is_red(sibling)) { |
| 327 | /* Case 1 - right rotate at parent */ |
| 328 | parent->rb_left = tmp1 = sibling->rb_right; |
| 329 | sibling->rb_right = parent; |
| 330 | rb_set_parent_color(tmp1, parent, RB_BLACK); |
| 331 | __rb_rotate_set_parents(parent, sibling, root, |
| 332 | RB_RED); |
| 333 | sibling = tmp1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
Michel Lespinasse | 6280d23 | 2012-10-08 16:30:57 -0700 | [diff] [blame] | 335 | tmp1 = sibling->rb_left; |
| 336 | if (!tmp1 || rb_is_black(tmp1)) { |
| 337 | tmp2 = sibling->rb_right; |
| 338 | if (!tmp2 || rb_is_black(tmp2)) { |
| 339 | /* Case 2 - sibling color flip */ |
| 340 | rb_set_parent_color(sibling, parent, |
| 341 | RB_RED); |
Michel Lespinasse | e125d14 | 2012-10-08 16:30:54 -0700 | [diff] [blame] | 342 | node = parent; |
| 343 | parent = rb_parent(node); |
| 344 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
Michel Lespinasse | 6280d23 | 2012-10-08 16:30:57 -0700 | [diff] [blame] | 346 | /* Case 3 - right rotate at sibling */ |
| 347 | sibling->rb_right = tmp1 = tmp2->rb_left; |
| 348 | tmp2->rb_left = sibling; |
| 349 | parent->rb_left = tmp2; |
| 350 | if (tmp1) |
| 351 | rb_set_parent_color(tmp1, sibling, |
| 352 | RB_BLACK); |
| 353 | tmp1 = sibling; |
| 354 | sibling = tmp2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
Michel Lespinasse | 6280d23 | 2012-10-08 16:30:57 -0700 | [diff] [blame] | 356 | /* Case 4 - left rotate at parent + color flips */ |
| 357 | parent->rb_left = tmp2 = sibling->rb_right; |
| 358 | sibling->rb_right = parent; |
| 359 | rb_set_parent_color(tmp1, sibling, RB_BLACK); |
| 360 | if (tmp2) |
| 361 | rb_set_parent(tmp2, parent); |
| 362 | __rb_rotate_set_parents(parent, sibling, root, |
| 363 | RB_BLACK); |
Michel Lespinasse | e125d14 | 2012-10-08 16:30:54 -0700 | [diff] [blame] | 364 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void rb_erase(struct rb_node *node, struct rb_root *root) |
| 370 | { |
| 371 | struct rb_node *child, *parent; |
| 372 | int color; |
| 373 | |
| 374 | if (!node->rb_left) |
| 375 | child = node->rb_right; |
| 376 | else if (!node->rb_right) |
| 377 | child = node->rb_left; |
Michel Lespinasse | 7ce6ff9 | 2012-10-08 16:31:01 -0700 | [diff] [blame] | 378 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | struct rb_node *old = node, *left; |
| 380 | |
| 381 | node = node->rb_right; |
| 382 | while ((left = node->rb_left) != NULL) |
| 383 | node = left; |
Wolfram Strepp | 16c047a | 2009-06-16 15:34:11 -0700 | [diff] [blame] | 384 | |
Michel Lespinasse | 7abc704 | 2012-10-08 16:31:07 -0700 | [diff] [blame^] | 385 | __rb_change_child(old, node, rb_parent(old), root); |
Wolfram Strepp | 16c047a | 2009-06-16 15:34:11 -0700 | [diff] [blame] | 386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | child = node->rb_right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 388 | parent = rb_parent(node); |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 389 | color = rb_color(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 391 | if (parent == old) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | parent = node; |
Wolfram Strepp | 4c60117 | 2009-06-16 15:34:12 -0700 | [diff] [blame] | 393 | } else { |
| 394 | if (child) |
| 395 | rb_set_parent(child, parent); |
David Woodhouse | 1975e59 | 2006-04-21 13:30:36 +0100 | [diff] [blame] | 396 | parent->rb_left = child; |
Wolfram Strepp | 4b32412 | 2009-06-16 15:34:13 -0700 | [diff] [blame] | 397 | |
| 398 | node->rb_right = old->rb_right; |
| 399 | rb_set_parent(old->rb_right, node); |
Wolfram Strepp | 4c60117 | 2009-06-16 15:34:12 -0700 | [diff] [blame] | 400 | } |
David Woodhouse | 1975e59 | 2006-04-21 13:30:36 +0100 | [diff] [blame] | 401 | |
Michel Lespinasse | bf7ad8e | 2012-10-08 16:30:37 -0700 | [diff] [blame] | 402 | node->__rb_parent_color = old->__rb_parent_color; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | node->rb_left = old->rb_left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 404 | rb_set_parent(old->rb_left, node); |
Wolfram Strepp | 4b32412 | 2009-06-16 15:34:13 -0700 | [diff] [blame] | 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | goto color; |
| 407 | } |
| 408 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 409 | parent = rb_parent(node); |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 410 | color = rb_color(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
| 412 | if (child) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 413 | rb_set_parent(child, parent); |
Michel Lespinasse | 7abc704 | 2012-10-08 16:31:07 -0700 | [diff] [blame^] | 414 | __rb_change_child(node, child, parent, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Michel Lespinasse | 7ce6ff9 | 2012-10-08 16:31:01 -0700 | [diff] [blame] | 416 | color: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | if (color == RB_BLACK) |
| 418 | __rb_erase_color(child, parent, root); |
| 419 | } |
| 420 | EXPORT_SYMBOL(rb_erase); |
| 421 | |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 422 | static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data) |
| 423 | { |
| 424 | struct rb_node *parent; |
| 425 | |
| 426 | up: |
| 427 | func(node, data); |
| 428 | parent = rb_parent(node); |
| 429 | if (!parent) |
| 430 | return; |
| 431 | |
| 432 | if (node == parent->rb_left && parent->rb_right) |
| 433 | func(parent->rb_right, data); |
| 434 | else if (parent->rb_left) |
| 435 | func(parent->rb_left, data); |
| 436 | |
| 437 | node = parent; |
| 438 | goto up; |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * after inserting @node into the tree, update the tree to account for |
| 443 | * both the new entry and any damage done by rebalance |
| 444 | */ |
| 445 | void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data) |
| 446 | { |
| 447 | if (node->rb_left) |
| 448 | node = node->rb_left; |
| 449 | else if (node->rb_right) |
| 450 | node = node->rb_right; |
| 451 | |
| 452 | rb_augment_path(node, func, data); |
| 453 | } |
Andreas Gruenbacher | 0b6bb66 | 2011-01-26 15:55:36 +0100 | [diff] [blame] | 454 | EXPORT_SYMBOL(rb_augment_insert); |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 455 | |
| 456 | /* |
| 457 | * before removing the node, find the deepest node on the rebalance path |
| 458 | * that will still be there after @node gets removed |
| 459 | */ |
| 460 | struct rb_node *rb_augment_erase_begin(struct rb_node *node) |
| 461 | { |
| 462 | struct rb_node *deepest; |
| 463 | |
| 464 | if (!node->rb_right && !node->rb_left) |
| 465 | deepest = rb_parent(node); |
| 466 | else if (!node->rb_right) |
| 467 | deepest = node->rb_left; |
| 468 | else if (!node->rb_left) |
| 469 | deepest = node->rb_right; |
| 470 | else { |
| 471 | deepest = rb_next(node); |
| 472 | if (deepest->rb_right) |
| 473 | deepest = deepest->rb_right; |
| 474 | else if (rb_parent(deepest) != node) |
| 475 | deepest = rb_parent(deepest); |
| 476 | } |
| 477 | |
| 478 | return deepest; |
| 479 | } |
Andreas Gruenbacher | 0b6bb66 | 2011-01-26 15:55:36 +0100 | [diff] [blame] | 480 | EXPORT_SYMBOL(rb_augment_erase_begin); |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 481 | |
| 482 | /* |
| 483 | * after removal, update the tree to account for the removed entry |
| 484 | * and any rebalance damage. |
| 485 | */ |
| 486 | void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data) |
| 487 | { |
| 488 | if (node) |
| 489 | rb_augment_path(node, func, data); |
| 490 | } |
Andreas Gruenbacher | 0b6bb66 | 2011-01-26 15:55:36 +0100 | [diff] [blame] | 491 | EXPORT_SYMBOL(rb_augment_erase_end); |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | /* |
| 494 | * This function returns the first node (in sort order) of the tree. |
| 495 | */ |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 496 | struct rb_node *rb_first(const struct rb_root *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | { |
| 498 | struct rb_node *n; |
| 499 | |
| 500 | n = root->rb_node; |
| 501 | if (!n) |
| 502 | return NULL; |
| 503 | while (n->rb_left) |
| 504 | n = n->rb_left; |
| 505 | return n; |
| 506 | } |
| 507 | EXPORT_SYMBOL(rb_first); |
| 508 | |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 509 | struct rb_node *rb_last(const struct rb_root *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | { |
| 511 | struct rb_node *n; |
| 512 | |
| 513 | n = root->rb_node; |
| 514 | if (!n) |
| 515 | return NULL; |
| 516 | while (n->rb_right) |
| 517 | n = n->rb_right; |
| 518 | return n; |
| 519 | } |
| 520 | EXPORT_SYMBOL(rb_last); |
| 521 | |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 522 | struct rb_node *rb_next(const struct rb_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 524 | struct rb_node *parent; |
| 525 | |
Michel Lespinasse | 4c199a9 | 2012-10-08 16:30:32 -0700 | [diff] [blame] | 526 | if (RB_EMPTY_NODE(node)) |
Jens Axboe | 10fd48f | 2006-07-11 21:15:52 +0200 | [diff] [blame] | 527 | return NULL; |
| 528 | |
Michel Lespinasse | 7ce6ff9 | 2012-10-08 16:31:01 -0700 | [diff] [blame] | 529 | /* |
| 530 | * If we have a right-hand child, go down and then left as far |
| 531 | * as we can. |
| 532 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | if (node->rb_right) { |
| 534 | node = node->rb_right; |
| 535 | while (node->rb_left) |
| 536 | node=node->rb_left; |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 537 | return (struct rb_node *)node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Michel Lespinasse | 7ce6ff9 | 2012-10-08 16:31:01 -0700 | [diff] [blame] | 540 | /* |
| 541 | * No right-hand children. Everything down and left is smaller than us, |
| 542 | * so any 'next' node must be in the general direction of our parent. |
| 543 | * Go up the tree; any time the ancestor is a right-hand child of its |
| 544 | * parent, keep going up. First time it's a left-hand child of its |
| 545 | * parent, said parent is our 'next' node. |
| 546 | */ |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 547 | while ((parent = rb_parent(node)) && node == parent->rb_right) |
| 548 | node = parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 550 | return parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
| 552 | EXPORT_SYMBOL(rb_next); |
| 553 | |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 554 | struct rb_node *rb_prev(const struct rb_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 556 | struct rb_node *parent; |
| 557 | |
Michel Lespinasse | 4c199a9 | 2012-10-08 16:30:32 -0700 | [diff] [blame] | 558 | if (RB_EMPTY_NODE(node)) |
Jens Axboe | 10fd48f | 2006-07-11 21:15:52 +0200 | [diff] [blame] | 559 | return NULL; |
| 560 | |
Michel Lespinasse | 7ce6ff9 | 2012-10-08 16:31:01 -0700 | [diff] [blame] | 561 | /* |
| 562 | * If we have a left-hand child, go down and then right as far |
| 563 | * as we can. |
| 564 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | if (node->rb_left) { |
| 566 | node = node->rb_left; |
| 567 | while (node->rb_right) |
| 568 | node=node->rb_right; |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 569 | return (struct rb_node *)node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Michel Lespinasse | 7ce6ff9 | 2012-10-08 16:31:01 -0700 | [diff] [blame] | 572 | /* |
| 573 | * No left-hand children. Go up till we find an ancestor which |
| 574 | * is a right-hand child of its parent. |
| 575 | */ |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 576 | while ((parent = rb_parent(node)) && node == parent->rb_left) |
| 577 | node = parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 579 | return parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | } |
| 581 | EXPORT_SYMBOL(rb_prev); |
| 582 | |
| 583 | void rb_replace_node(struct rb_node *victim, struct rb_node *new, |
| 584 | struct rb_root *root) |
| 585 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 586 | struct rb_node *parent = rb_parent(victim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
| 588 | /* Set the surrounding nodes to point to the replacement */ |
Michel Lespinasse | 7abc704 | 2012-10-08 16:31:07 -0700 | [diff] [blame^] | 589 | __rb_change_child(victim, new, parent, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | if (victim->rb_left) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 591 | rb_set_parent(victim->rb_left, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | if (victim->rb_right) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 593 | rb_set_parent(victim->rb_right, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
| 595 | /* Copy the pointers/colour from the victim to the replacement */ |
| 596 | *new = *victim; |
| 597 | } |
| 598 | EXPORT_SYMBOL(rb_replace_node); |