11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds Red Black Trees 31da177e4SLinus Torvalds (C) 1999 Andrea Arcangeli <andrea@suse.de> 41da177e4SLinus Torvalds (C) 2002 David Woodhouse <dwmw2@infradead.org> 546b6135aSMichel Lespinasse (C) 2012 Michel Lespinasse <walken@google.com> 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds This program is free software; you can redistribute it and/or modify 81da177e4SLinus Torvalds it under the terms of the GNU General Public License as published by 91da177e4SLinus Torvalds the Free Software Foundation; either version 2 of the License, or 101da177e4SLinus Torvalds (at your option) any later version. 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds This program is distributed in the hope that it will be useful, 131da177e4SLinus Torvalds but WITHOUT ANY WARRANTY; without even the implied warranty of 141da177e4SLinus Torvalds MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 151da177e4SLinus Torvalds GNU General Public License for more details. 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds You should have received a copy of the GNU General Public License 181da177e4SLinus Torvalds along with this program; if not, write to the Free Software 191da177e4SLinus Torvalds Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds linux/lib/rbtree.c 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 249c079addSMichel Lespinasse #include <linux/rbtree_augmented.h> 258bc3bcc9SPaul Gortmaker #include <linux/export.h> 261da177e4SLinus Torvalds 275bc9188aSMichel Lespinasse /* 285bc9188aSMichel Lespinasse * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree 295bc9188aSMichel Lespinasse * 305bc9188aSMichel Lespinasse * 1) A node is either red or black 315bc9188aSMichel Lespinasse * 2) The root is black 325bc9188aSMichel Lespinasse * 3) All leaves (NULL) are black 335bc9188aSMichel Lespinasse * 4) Both children of every red node are black 345bc9188aSMichel Lespinasse * 5) Every simple path from root to leaves contains the same number 355bc9188aSMichel Lespinasse * of black nodes. 365bc9188aSMichel Lespinasse * 375bc9188aSMichel Lespinasse * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two 385bc9188aSMichel Lespinasse * consecutive red nodes in a path and every red node is therefore followed by 395bc9188aSMichel Lespinasse * a black. So if B is the number of black nodes on every simple path (as per 405bc9188aSMichel Lespinasse * 5), then the longest possible path due to 4 is 2B. 415bc9188aSMichel Lespinasse * 425bc9188aSMichel Lespinasse * We shall indicate color with case, where black nodes are uppercase and red 436280d235SMichel Lespinasse * nodes will be lowercase. Unknown color nodes shall be drawn as red within 446280d235SMichel Lespinasse * parentheses and have some accompanying text comment. 455bc9188aSMichel Lespinasse */ 465bc9188aSMichel Lespinasse 4746b6135aSMichel Lespinasse static inline void rb_set_black(struct rb_node *rb) 4846b6135aSMichel Lespinasse { 4946b6135aSMichel Lespinasse rb->__rb_parent_color |= RB_BLACK; 5046b6135aSMichel Lespinasse } 5146b6135aSMichel Lespinasse 525bc9188aSMichel Lespinasse static inline struct rb_node *rb_red_parent(struct rb_node *red) 535bc9188aSMichel Lespinasse { 545bc9188aSMichel Lespinasse return (struct rb_node *)red->__rb_parent_color; 555bc9188aSMichel Lespinasse } 565bc9188aSMichel Lespinasse 575bc9188aSMichel Lespinasse /* 585bc9188aSMichel Lespinasse * Helper function for rotations: 595bc9188aSMichel Lespinasse * - old's parent and color get assigned to new 605bc9188aSMichel Lespinasse * - old gets assigned new as a parent and 'color' as a color. 615bc9188aSMichel Lespinasse */ 625bc9188aSMichel Lespinasse static inline void 635bc9188aSMichel Lespinasse __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new, 645bc9188aSMichel Lespinasse struct rb_root *root, int color) 655bc9188aSMichel Lespinasse { 665bc9188aSMichel Lespinasse struct rb_node *parent = rb_parent(old); 675bc9188aSMichel Lespinasse new->__rb_parent_color = old->__rb_parent_color; 685bc9188aSMichel Lespinasse rb_set_parent_color(old, new, color); 697abc704aSMichel Lespinasse __rb_change_child(old, new, parent, root); 705bc9188aSMichel Lespinasse } 715bc9188aSMichel Lespinasse 7214b94af0SMichel Lespinasse static __always_inline void 7314b94af0SMichel Lespinasse __rb_insert(struct rb_node *node, struct rb_root *root, 7414b94af0SMichel Lespinasse void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) 751da177e4SLinus Torvalds { 765bc9188aSMichel Lespinasse struct rb_node *parent = rb_red_parent(node), *gparent, *tmp; 771da177e4SLinus Torvalds 786d58452dSMichel Lespinasse while (true) { 796d58452dSMichel Lespinasse /* 806d58452dSMichel Lespinasse * Loop invariant: node is red 816d58452dSMichel Lespinasse * 826d58452dSMichel Lespinasse * If there is a black parent, we are done. 836d58452dSMichel Lespinasse * Otherwise, take some corrective action as we don't 846d58452dSMichel Lespinasse * want a red root or two consecutive red nodes. 856d58452dSMichel Lespinasse */ 866d58452dSMichel Lespinasse if (!parent) { 875bc9188aSMichel Lespinasse rb_set_parent_color(node, NULL, RB_BLACK); 886d58452dSMichel Lespinasse break; 896d58452dSMichel Lespinasse } else if (rb_is_black(parent)) 906d58452dSMichel Lespinasse break; 916d58452dSMichel Lespinasse 925bc9188aSMichel Lespinasse gparent = rb_red_parent(parent); 931da177e4SLinus Torvalds 945bc9188aSMichel Lespinasse tmp = gparent->rb_right; 9559633abfSMichel Lespinasse if (parent != tmp) { /* parent == gparent->rb_left */ 965bc9188aSMichel Lespinasse if (tmp && rb_is_red(tmp)) { 975bc9188aSMichel Lespinasse /* 985bc9188aSMichel Lespinasse * Case 1 - color flips 995bc9188aSMichel Lespinasse * 1005bc9188aSMichel Lespinasse * G g 1015bc9188aSMichel Lespinasse * / \ / \ 1025bc9188aSMichel Lespinasse * p u --> P U 1035bc9188aSMichel Lespinasse * / / 104*1b9c53e8SWei Yang * n n 1055bc9188aSMichel Lespinasse * 1065bc9188aSMichel Lespinasse * However, since g's parent might be red, and 1075bc9188aSMichel Lespinasse * 4) does not allow this, we need to recurse 1085bc9188aSMichel Lespinasse * at g. 1095bc9188aSMichel Lespinasse */ 1105bc9188aSMichel Lespinasse rb_set_parent_color(tmp, gparent, RB_BLACK); 1115bc9188aSMichel Lespinasse rb_set_parent_color(parent, gparent, RB_BLACK); 1121da177e4SLinus Torvalds node = gparent; 1135bc9188aSMichel Lespinasse parent = rb_parent(node); 1145bc9188aSMichel Lespinasse rb_set_parent_color(node, parent, RB_RED); 1151da177e4SLinus Torvalds continue; 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 11859633abfSMichel Lespinasse tmp = parent->rb_right; 11959633abfSMichel Lespinasse if (node == tmp) { 1205bc9188aSMichel Lespinasse /* 1215bc9188aSMichel Lespinasse * Case 2 - left rotate at parent 1225bc9188aSMichel Lespinasse * 1235bc9188aSMichel Lespinasse * G G 1245bc9188aSMichel Lespinasse * / \ / \ 1255bc9188aSMichel Lespinasse * p U --> n U 1265bc9188aSMichel Lespinasse * \ / 1275bc9188aSMichel Lespinasse * n p 1285bc9188aSMichel Lespinasse * 1295bc9188aSMichel Lespinasse * This still leaves us in violation of 4), the 1305bc9188aSMichel Lespinasse * continuation into Case 3 will fix that. 1315bc9188aSMichel Lespinasse */ 1325bc9188aSMichel Lespinasse parent->rb_right = tmp = node->rb_left; 1335bc9188aSMichel Lespinasse node->rb_left = parent; 1345bc9188aSMichel Lespinasse if (tmp) 1355bc9188aSMichel Lespinasse rb_set_parent_color(tmp, parent, 1365bc9188aSMichel Lespinasse RB_BLACK); 1375bc9188aSMichel Lespinasse rb_set_parent_color(parent, node, RB_RED); 13814b94af0SMichel Lespinasse augment_rotate(parent, node); 1391da177e4SLinus Torvalds parent = node; 14059633abfSMichel Lespinasse tmp = node->rb_right; 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 1435bc9188aSMichel Lespinasse /* 1445bc9188aSMichel Lespinasse * Case 3 - right rotate at gparent 1455bc9188aSMichel Lespinasse * 1465bc9188aSMichel Lespinasse * G P 1475bc9188aSMichel Lespinasse * / \ / \ 1485bc9188aSMichel Lespinasse * p U --> n g 1495bc9188aSMichel Lespinasse * / \ 1505bc9188aSMichel Lespinasse * n U 1515bc9188aSMichel Lespinasse */ 15259633abfSMichel Lespinasse gparent->rb_left = tmp; /* == parent->rb_right */ 1535bc9188aSMichel Lespinasse parent->rb_right = gparent; 1545bc9188aSMichel Lespinasse if (tmp) 1555bc9188aSMichel Lespinasse rb_set_parent_color(tmp, gparent, RB_BLACK); 1565bc9188aSMichel Lespinasse __rb_rotate_set_parents(gparent, parent, root, RB_RED); 15714b94af0SMichel Lespinasse augment_rotate(gparent, parent); 1581f052865SMichel Lespinasse break; 1591da177e4SLinus Torvalds } else { 1605bc9188aSMichel Lespinasse tmp = gparent->rb_left; 1615bc9188aSMichel Lespinasse if (tmp && rb_is_red(tmp)) { 1625bc9188aSMichel Lespinasse /* Case 1 - color flips */ 1635bc9188aSMichel Lespinasse rb_set_parent_color(tmp, gparent, RB_BLACK); 1645bc9188aSMichel Lespinasse rb_set_parent_color(parent, gparent, RB_BLACK); 1651da177e4SLinus Torvalds node = gparent; 1665bc9188aSMichel Lespinasse parent = rb_parent(node); 1675bc9188aSMichel Lespinasse rb_set_parent_color(node, parent, RB_RED); 1681da177e4SLinus Torvalds continue; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds 17159633abfSMichel Lespinasse tmp = parent->rb_left; 17259633abfSMichel Lespinasse if (node == tmp) { 1735bc9188aSMichel Lespinasse /* Case 2 - right rotate at parent */ 1745bc9188aSMichel Lespinasse parent->rb_left = tmp = node->rb_right; 1755bc9188aSMichel Lespinasse node->rb_right = parent; 1765bc9188aSMichel Lespinasse if (tmp) 1775bc9188aSMichel Lespinasse rb_set_parent_color(tmp, parent, 1785bc9188aSMichel Lespinasse RB_BLACK); 1795bc9188aSMichel Lespinasse rb_set_parent_color(parent, node, RB_RED); 18014b94af0SMichel Lespinasse augment_rotate(parent, node); 1811da177e4SLinus Torvalds parent = node; 18259633abfSMichel Lespinasse tmp = node->rb_left; 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1855bc9188aSMichel Lespinasse /* Case 3 - left rotate at gparent */ 18659633abfSMichel Lespinasse gparent->rb_right = tmp; /* == parent->rb_left */ 1875bc9188aSMichel Lespinasse parent->rb_left = gparent; 1885bc9188aSMichel Lespinasse if (tmp) 1895bc9188aSMichel Lespinasse rb_set_parent_color(tmp, gparent, RB_BLACK); 1905bc9188aSMichel Lespinasse __rb_rotate_set_parents(gparent, parent, root, RB_RED); 19114b94af0SMichel Lespinasse augment_rotate(gparent, parent); 1921f052865SMichel Lespinasse break; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 1973cb7a563SMichel Lespinasse /* 1983cb7a563SMichel Lespinasse * Inline version for rb_erase() use - we want to be able to inline 1993cb7a563SMichel Lespinasse * and eliminate the dummy_rotate callback there 2003cb7a563SMichel Lespinasse */ 2013cb7a563SMichel Lespinasse static __always_inline void 2023cb7a563SMichel Lespinasse ____rb_erase_color(struct rb_node *parent, struct rb_root *root, 2039c079addSMichel Lespinasse void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) 2041da177e4SLinus Torvalds { 20546b6135aSMichel Lespinasse struct rb_node *node = NULL, *sibling, *tmp1, *tmp2; 2061da177e4SLinus Torvalds 207d6ff1273SMichel Lespinasse while (true) { 208d6ff1273SMichel Lespinasse /* 20946b6135aSMichel Lespinasse * Loop invariants: 21046b6135aSMichel Lespinasse * - node is black (or NULL on first iteration) 21146b6135aSMichel Lespinasse * - node is not the root (parent is not NULL) 21246b6135aSMichel Lespinasse * - All leaf paths going through parent and node have a 213d6ff1273SMichel Lespinasse * black node count that is 1 lower than other leaf paths. 214d6ff1273SMichel Lespinasse */ 2156280d235SMichel Lespinasse sibling = parent->rb_right; 21659633abfSMichel Lespinasse if (node != sibling) { /* node == parent->rb_left */ 2176280d235SMichel Lespinasse if (rb_is_red(sibling)) { 2186280d235SMichel Lespinasse /* 2196280d235SMichel Lespinasse * Case 1 - left rotate at parent 2206280d235SMichel Lespinasse * 2216280d235SMichel Lespinasse * P S 2226280d235SMichel Lespinasse * / \ / \ 2236280d235SMichel Lespinasse * N s --> p Sr 2246280d235SMichel Lespinasse * / \ / \ 2256280d235SMichel Lespinasse * Sl Sr N Sl 2266280d235SMichel Lespinasse */ 2276280d235SMichel Lespinasse parent->rb_right = tmp1 = sibling->rb_left; 2286280d235SMichel Lespinasse sibling->rb_left = parent; 2296280d235SMichel Lespinasse rb_set_parent_color(tmp1, parent, RB_BLACK); 2306280d235SMichel Lespinasse __rb_rotate_set_parents(parent, sibling, root, 2316280d235SMichel Lespinasse RB_RED); 2329c079addSMichel Lespinasse augment_rotate(parent, sibling); 2336280d235SMichel Lespinasse sibling = tmp1; 2341da177e4SLinus Torvalds } 2356280d235SMichel Lespinasse tmp1 = sibling->rb_right; 2366280d235SMichel Lespinasse if (!tmp1 || rb_is_black(tmp1)) { 2376280d235SMichel Lespinasse tmp2 = sibling->rb_left; 2386280d235SMichel Lespinasse if (!tmp2 || rb_is_black(tmp2)) { 2396280d235SMichel Lespinasse /* 2406280d235SMichel Lespinasse * Case 2 - sibling color flip 2416280d235SMichel Lespinasse * (p could be either color here) 2426280d235SMichel Lespinasse * 2436280d235SMichel Lespinasse * (p) (p) 2446280d235SMichel Lespinasse * / \ / \ 2456280d235SMichel Lespinasse * N S --> N s 2466280d235SMichel Lespinasse * / \ / \ 2476280d235SMichel Lespinasse * Sl Sr Sl Sr 2486280d235SMichel Lespinasse * 24946b6135aSMichel Lespinasse * This leaves us violating 5) which 25046b6135aSMichel Lespinasse * can be fixed by flipping p to black 25146b6135aSMichel Lespinasse * if it was red, or by recursing at p. 25246b6135aSMichel Lespinasse * p is red when coming from Case 1. 2536280d235SMichel Lespinasse */ 2546280d235SMichel Lespinasse rb_set_parent_color(sibling, parent, 2556280d235SMichel Lespinasse RB_RED); 25646b6135aSMichel Lespinasse if (rb_is_red(parent)) 25746b6135aSMichel Lespinasse rb_set_black(parent); 25846b6135aSMichel Lespinasse else { 2591da177e4SLinus Torvalds node = parent; 26055a98102SDavid Woodhouse parent = rb_parent(node); 26146b6135aSMichel Lespinasse if (parent) 262e125d147SMichel Lespinasse continue; 2631da177e4SLinus Torvalds } 26446b6135aSMichel Lespinasse break; 26546b6135aSMichel Lespinasse } 2666280d235SMichel Lespinasse /* 2676280d235SMichel Lespinasse * Case 3 - right rotate at sibling 2686280d235SMichel Lespinasse * (p could be either color here) 2696280d235SMichel Lespinasse * 2706280d235SMichel Lespinasse * (p) (p) 2716280d235SMichel Lespinasse * / \ / \ 2726280d235SMichel Lespinasse * N S --> N Sl 2736280d235SMichel Lespinasse * / \ \ 2746280d235SMichel Lespinasse * sl Sr s 2756280d235SMichel Lespinasse * \ 2766280d235SMichel Lespinasse * Sr 2776280d235SMichel Lespinasse */ 2786280d235SMichel Lespinasse sibling->rb_left = tmp1 = tmp2->rb_right; 2796280d235SMichel Lespinasse tmp2->rb_right = sibling; 2806280d235SMichel Lespinasse parent->rb_right = tmp2; 2816280d235SMichel Lespinasse if (tmp1) 2826280d235SMichel Lespinasse rb_set_parent_color(tmp1, sibling, 2836280d235SMichel Lespinasse RB_BLACK); 2849c079addSMichel Lespinasse augment_rotate(sibling, tmp2); 2856280d235SMichel Lespinasse tmp1 = sibling; 2866280d235SMichel Lespinasse sibling = tmp2; 2871da177e4SLinus Torvalds } 2886280d235SMichel Lespinasse /* 2896280d235SMichel Lespinasse * Case 4 - left rotate at parent + color flips 2906280d235SMichel Lespinasse * (p and sl could be either color here. 2916280d235SMichel Lespinasse * After rotation, p becomes black, s acquires 2926280d235SMichel Lespinasse * p's color, and sl keeps its color) 2936280d235SMichel Lespinasse * 2946280d235SMichel Lespinasse * (p) (s) 2956280d235SMichel Lespinasse * / \ / \ 2966280d235SMichel Lespinasse * N S --> P Sr 2976280d235SMichel Lespinasse * / \ / \ 2986280d235SMichel Lespinasse * (sl) sr N (sl) 2996280d235SMichel Lespinasse */ 3006280d235SMichel Lespinasse parent->rb_right = tmp2 = sibling->rb_left; 3016280d235SMichel Lespinasse sibling->rb_left = parent; 3026280d235SMichel Lespinasse rb_set_parent_color(tmp1, sibling, RB_BLACK); 3036280d235SMichel Lespinasse if (tmp2) 3046280d235SMichel Lespinasse rb_set_parent(tmp2, parent); 3056280d235SMichel Lespinasse __rb_rotate_set_parents(parent, sibling, root, 3066280d235SMichel Lespinasse RB_BLACK); 3079c079addSMichel Lespinasse augment_rotate(parent, sibling); 3081da177e4SLinus Torvalds break; 309d6ff1273SMichel Lespinasse } else { 3106280d235SMichel Lespinasse sibling = parent->rb_left; 3116280d235SMichel Lespinasse if (rb_is_red(sibling)) { 3126280d235SMichel Lespinasse /* Case 1 - right rotate at parent */ 3136280d235SMichel Lespinasse parent->rb_left = tmp1 = sibling->rb_right; 3146280d235SMichel Lespinasse sibling->rb_right = parent; 3156280d235SMichel Lespinasse rb_set_parent_color(tmp1, parent, RB_BLACK); 3166280d235SMichel Lespinasse __rb_rotate_set_parents(parent, sibling, root, 3176280d235SMichel Lespinasse RB_RED); 3189c079addSMichel Lespinasse augment_rotate(parent, sibling); 3196280d235SMichel Lespinasse sibling = tmp1; 3201da177e4SLinus Torvalds } 3216280d235SMichel Lespinasse tmp1 = sibling->rb_left; 3226280d235SMichel Lespinasse if (!tmp1 || rb_is_black(tmp1)) { 3236280d235SMichel Lespinasse tmp2 = sibling->rb_right; 3246280d235SMichel Lespinasse if (!tmp2 || rb_is_black(tmp2)) { 3256280d235SMichel Lespinasse /* Case 2 - sibling color flip */ 3266280d235SMichel Lespinasse rb_set_parent_color(sibling, parent, 3276280d235SMichel Lespinasse RB_RED); 32846b6135aSMichel Lespinasse if (rb_is_red(parent)) 32946b6135aSMichel Lespinasse rb_set_black(parent); 33046b6135aSMichel Lespinasse else { 3311da177e4SLinus Torvalds node = parent; 33255a98102SDavid Woodhouse parent = rb_parent(node); 33346b6135aSMichel Lespinasse if (parent) 334e125d147SMichel Lespinasse continue; 3351da177e4SLinus Torvalds } 33646b6135aSMichel Lespinasse break; 33746b6135aSMichel Lespinasse } 3386280d235SMichel Lespinasse /* Case 3 - right rotate at sibling */ 3396280d235SMichel Lespinasse sibling->rb_right = tmp1 = tmp2->rb_left; 3406280d235SMichel Lespinasse tmp2->rb_left = sibling; 3416280d235SMichel Lespinasse parent->rb_left = tmp2; 3426280d235SMichel Lespinasse if (tmp1) 3436280d235SMichel Lespinasse rb_set_parent_color(tmp1, sibling, 3446280d235SMichel Lespinasse RB_BLACK); 3459c079addSMichel Lespinasse augment_rotate(sibling, tmp2); 3466280d235SMichel Lespinasse tmp1 = sibling; 3476280d235SMichel Lespinasse sibling = tmp2; 3481da177e4SLinus Torvalds } 3496280d235SMichel Lespinasse /* Case 4 - left rotate at parent + color flips */ 3506280d235SMichel Lespinasse parent->rb_left = tmp2 = sibling->rb_right; 3516280d235SMichel Lespinasse sibling->rb_right = parent; 3526280d235SMichel Lespinasse rb_set_parent_color(tmp1, sibling, RB_BLACK); 3536280d235SMichel Lespinasse if (tmp2) 3546280d235SMichel Lespinasse rb_set_parent(tmp2, parent); 3556280d235SMichel Lespinasse __rb_rotate_set_parents(parent, sibling, root, 3566280d235SMichel Lespinasse RB_BLACK); 3579c079addSMichel Lespinasse augment_rotate(parent, sibling); 3581da177e4SLinus Torvalds break; 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds } 3623cb7a563SMichel Lespinasse 3633cb7a563SMichel Lespinasse /* Non-inline version for rb_erase_augmented() use */ 3643cb7a563SMichel Lespinasse void __rb_erase_color(struct rb_node *parent, struct rb_root *root, 3653cb7a563SMichel Lespinasse void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) 3663cb7a563SMichel Lespinasse { 3673cb7a563SMichel Lespinasse ____rb_erase_color(parent, root, augment_rotate); 3683cb7a563SMichel Lespinasse } 3699c079addSMichel Lespinasse EXPORT_SYMBOL(__rb_erase_color); 37014b94af0SMichel Lespinasse 37114b94af0SMichel Lespinasse /* 37214b94af0SMichel Lespinasse * Non-augmented rbtree manipulation functions. 37314b94af0SMichel Lespinasse * 37414b94af0SMichel Lespinasse * We use dummy augmented callbacks here, and have the compiler optimize them 37514b94af0SMichel Lespinasse * out of the rb_insert_color() and rb_erase() function definitions. 37614b94af0SMichel Lespinasse */ 37714b94af0SMichel Lespinasse 37814b94af0SMichel Lespinasse static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {} 37914b94af0SMichel Lespinasse static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {} 38014b94af0SMichel Lespinasse static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {} 38114b94af0SMichel Lespinasse 38214b94af0SMichel Lespinasse static const struct rb_augment_callbacks dummy_callbacks = { 38314b94af0SMichel Lespinasse dummy_propagate, dummy_copy, dummy_rotate 38414b94af0SMichel Lespinasse }; 38514b94af0SMichel Lespinasse 38614b94af0SMichel Lespinasse void rb_insert_color(struct rb_node *node, struct rb_root *root) 38714b94af0SMichel Lespinasse { 38814b94af0SMichel Lespinasse __rb_insert(node, root, dummy_rotate); 38914b94af0SMichel Lespinasse } 39014b94af0SMichel Lespinasse EXPORT_SYMBOL(rb_insert_color); 39114b94af0SMichel Lespinasse 39214b94af0SMichel Lespinasse void rb_erase(struct rb_node *node, struct rb_root *root) 39314b94af0SMichel Lespinasse { 3943cb7a563SMichel Lespinasse struct rb_node *rebalance; 3953cb7a563SMichel Lespinasse rebalance = __rb_erase_augmented(node, root, &dummy_callbacks); 3963cb7a563SMichel Lespinasse if (rebalance) 3973cb7a563SMichel Lespinasse ____rb_erase_color(rebalance, root, dummy_rotate); 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds EXPORT_SYMBOL(rb_erase); 4001da177e4SLinus Torvalds 40114b94af0SMichel Lespinasse /* 40214b94af0SMichel Lespinasse * Augmented rbtree manipulation functions. 40314b94af0SMichel Lespinasse * 40414b94af0SMichel Lespinasse * This instantiates the same __always_inline functions as in the non-augmented 40514b94af0SMichel Lespinasse * case, but this time with user-defined callbacks. 40614b94af0SMichel Lespinasse */ 40714b94af0SMichel Lespinasse 40814b94af0SMichel Lespinasse void __rb_insert_augmented(struct rb_node *node, struct rb_root *root, 40914b94af0SMichel Lespinasse void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) 41014b94af0SMichel Lespinasse { 41114b94af0SMichel Lespinasse __rb_insert(node, root, augment_rotate); 41214b94af0SMichel Lespinasse } 41314b94af0SMichel Lespinasse EXPORT_SYMBOL(__rb_insert_augmented); 41414b94af0SMichel Lespinasse 4151da177e4SLinus Torvalds /* 4161da177e4SLinus Torvalds * This function returns the first node (in sort order) of the tree. 4171da177e4SLinus Torvalds */ 418f4b477c4SArtem Bityutskiy struct rb_node *rb_first(const struct rb_root *root) 4191da177e4SLinus Torvalds { 4201da177e4SLinus Torvalds struct rb_node *n; 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds n = root->rb_node; 4231da177e4SLinus Torvalds if (!n) 4241da177e4SLinus Torvalds return NULL; 4251da177e4SLinus Torvalds while (n->rb_left) 4261da177e4SLinus Torvalds n = n->rb_left; 4271da177e4SLinus Torvalds return n; 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds EXPORT_SYMBOL(rb_first); 4301da177e4SLinus Torvalds 431f4b477c4SArtem Bityutskiy struct rb_node *rb_last(const struct rb_root *root) 4321da177e4SLinus Torvalds { 4331da177e4SLinus Torvalds struct rb_node *n; 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds n = root->rb_node; 4361da177e4SLinus Torvalds if (!n) 4371da177e4SLinus Torvalds return NULL; 4381da177e4SLinus Torvalds while (n->rb_right) 4391da177e4SLinus Torvalds n = n->rb_right; 4401da177e4SLinus Torvalds return n; 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds EXPORT_SYMBOL(rb_last); 4431da177e4SLinus Torvalds 444f4b477c4SArtem Bityutskiy struct rb_node *rb_next(const struct rb_node *node) 4451da177e4SLinus Torvalds { 44655a98102SDavid Woodhouse struct rb_node *parent; 44755a98102SDavid Woodhouse 4484c199a93SMichel Lespinasse if (RB_EMPTY_NODE(node)) 44910fd48f2SJens Axboe return NULL; 45010fd48f2SJens Axboe 4517ce6ff9eSMichel Lespinasse /* 4527ce6ff9eSMichel Lespinasse * If we have a right-hand child, go down and then left as far 4537ce6ff9eSMichel Lespinasse * as we can. 4547ce6ff9eSMichel Lespinasse */ 4551da177e4SLinus Torvalds if (node->rb_right) { 4561da177e4SLinus Torvalds node = node->rb_right; 4571da177e4SLinus Torvalds while (node->rb_left) 4581da177e4SLinus Torvalds node=node->rb_left; 459f4b477c4SArtem Bityutskiy return (struct rb_node *)node; 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 4627ce6ff9eSMichel Lespinasse /* 4637ce6ff9eSMichel Lespinasse * No right-hand children. Everything down and left is smaller than us, 4647ce6ff9eSMichel Lespinasse * so any 'next' node must be in the general direction of our parent. 4657ce6ff9eSMichel Lespinasse * Go up the tree; any time the ancestor is a right-hand child of its 4667ce6ff9eSMichel Lespinasse * parent, keep going up. First time it's a left-hand child of its 4677ce6ff9eSMichel Lespinasse * parent, said parent is our 'next' node. 4687ce6ff9eSMichel Lespinasse */ 46955a98102SDavid Woodhouse while ((parent = rb_parent(node)) && node == parent->rb_right) 47055a98102SDavid Woodhouse node = parent; 4711da177e4SLinus Torvalds 47255a98102SDavid Woodhouse return parent; 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds EXPORT_SYMBOL(rb_next); 4751da177e4SLinus Torvalds 476f4b477c4SArtem Bityutskiy struct rb_node *rb_prev(const struct rb_node *node) 4771da177e4SLinus Torvalds { 47855a98102SDavid Woodhouse struct rb_node *parent; 47955a98102SDavid Woodhouse 4804c199a93SMichel Lespinasse if (RB_EMPTY_NODE(node)) 48110fd48f2SJens Axboe return NULL; 48210fd48f2SJens Axboe 4837ce6ff9eSMichel Lespinasse /* 4847ce6ff9eSMichel Lespinasse * If we have a left-hand child, go down and then right as far 4857ce6ff9eSMichel Lespinasse * as we can. 4867ce6ff9eSMichel Lespinasse */ 4871da177e4SLinus Torvalds if (node->rb_left) { 4881da177e4SLinus Torvalds node = node->rb_left; 4891da177e4SLinus Torvalds while (node->rb_right) 4901da177e4SLinus Torvalds node=node->rb_right; 491f4b477c4SArtem Bityutskiy return (struct rb_node *)node; 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 4947ce6ff9eSMichel Lespinasse /* 4957ce6ff9eSMichel Lespinasse * No left-hand children. Go up till we find an ancestor which 4967ce6ff9eSMichel Lespinasse * is a right-hand child of its parent. 4977ce6ff9eSMichel Lespinasse */ 49855a98102SDavid Woodhouse while ((parent = rb_parent(node)) && node == parent->rb_left) 49955a98102SDavid Woodhouse node = parent; 5001da177e4SLinus Torvalds 50155a98102SDavid Woodhouse return parent; 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds EXPORT_SYMBOL(rb_prev); 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds void rb_replace_node(struct rb_node *victim, struct rb_node *new, 5061da177e4SLinus Torvalds struct rb_root *root) 5071da177e4SLinus Torvalds { 50855a98102SDavid Woodhouse struct rb_node *parent = rb_parent(victim); 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds /* Set the surrounding nodes to point to the replacement */ 5117abc704aSMichel Lespinasse __rb_change_child(victim, new, parent, root); 5121da177e4SLinus Torvalds if (victim->rb_left) 51355a98102SDavid Woodhouse rb_set_parent(victim->rb_left, new); 5141da177e4SLinus Torvalds if (victim->rb_right) 51555a98102SDavid Woodhouse rb_set_parent(victim->rb_right, new); 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds /* Copy the pointers/colour from the victim to the replacement */ 5181da177e4SLinus Torvalds *new = *victim; 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds EXPORT_SYMBOL(rb_replace_node); 5219dee5c51SCody P Schafer 5229dee5c51SCody P Schafer static struct rb_node *rb_left_deepest_node(const struct rb_node *node) 5239dee5c51SCody P Schafer { 5249dee5c51SCody P Schafer for (;;) { 5259dee5c51SCody P Schafer if (node->rb_left) 5269dee5c51SCody P Schafer node = node->rb_left; 5279dee5c51SCody P Schafer else if (node->rb_right) 5289dee5c51SCody P Schafer node = node->rb_right; 5299dee5c51SCody P Schafer else 5309dee5c51SCody P Schafer return (struct rb_node *)node; 5319dee5c51SCody P Schafer } 5329dee5c51SCody P Schafer } 5339dee5c51SCody P Schafer 5349dee5c51SCody P Schafer struct rb_node *rb_next_postorder(const struct rb_node *node) 5359dee5c51SCody P Schafer { 5369dee5c51SCody P Schafer const struct rb_node *parent; 5379dee5c51SCody P Schafer if (!node) 5389dee5c51SCody P Schafer return NULL; 5399dee5c51SCody P Schafer parent = rb_parent(node); 5409dee5c51SCody P Schafer 5419dee5c51SCody P Schafer /* If we're sitting on node, we've already seen our children */ 5429dee5c51SCody P Schafer if (parent && node == parent->rb_left && parent->rb_right) { 5439dee5c51SCody P Schafer /* If we are the parent's left node, go to the parent's right 5449dee5c51SCody P Schafer * node then all the way down to the left */ 5459dee5c51SCody P Schafer return rb_left_deepest_node(parent->rb_right); 5469dee5c51SCody P Schafer } else 5479dee5c51SCody P Schafer /* Otherwise we are the parent's right node, and the parent 5489dee5c51SCody P Schafer * should be next */ 5499dee5c51SCody P Schafer return (struct rb_node *)parent; 5509dee5c51SCody P Schafer } 5519dee5c51SCody P Schafer EXPORT_SYMBOL(rb_next_postorder); 5529dee5c51SCody P Schafer 5539dee5c51SCody P Schafer struct rb_node *rb_first_postorder(const struct rb_root *root) 5549dee5c51SCody P Schafer { 5559dee5c51SCody P Schafer if (!root->rb_node) 5569dee5c51SCody P Schafer return NULL; 5579dee5c51SCody P Schafer 5589dee5c51SCody P Schafer return rb_left_deepest_node(root->rb_node); 5599dee5c51SCody P Schafer } 5609dee5c51SCody P Schafer EXPORT_SYMBOL(rb_first_postorder); 561