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