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