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 24*9c079addSMichel 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 * / / 1045bc9188aSMichel Lespinasse * 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 197*9c079addSMichel Lespinasse __always_inline void 19814b94af0SMichel Lespinasse __rb_erase_color(struct rb_node *parent, struct rb_root *root, 199*9c079addSMichel Lespinasse void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) 2001da177e4SLinus Torvalds { 20146b6135aSMichel Lespinasse struct rb_node *node = NULL, *sibling, *tmp1, *tmp2; 2021da177e4SLinus Torvalds 203d6ff1273SMichel Lespinasse while (true) { 204d6ff1273SMichel Lespinasse /* 20546b6135aSMichel Lespinasse * Loop invariants: 20646b6135aSMichel Lespinasse * - node is black (or NULL on first iteration) 20746b6135aSMichel Lespinasse * - node is not the root (parent is not NULL) 20846b6135aSMichel Lespinasse * - All leaf paths going through parent and node have a 209d6ff1273SMichel Lespinasse * black node count that is 1 lower than other leaf paths. 210d6ff1273SMichel Lespinasse */ 2116280d235SMichel Lespinasse sibling = parent->rb_right; 21259633abfSMichel Lespinasse if (node != sibling) { /* node == parent->rb_left */ 2136280d235SMichel Lespinasse if (rb_is_red(sibling)) { 2146280d235SMichel Lespinasse /* 2156280d235SMichel Lespinasse * Case 1 - left rotate at parent 2166280d235SMichel Lespinasse * 2176280d235SMichel Lespinasse * P S 2186280d235SMichel Lespinasse * / \ / \ 2196280d235SMichel Lespinasse * N s --> p Sr 2206280d235SMichel Lespinasse * / \ / \ 2216280d235SMichel Lespinasse * Sl Sr N Sl 2226280d235SMichel Lespinasse */ 2236280d235SMichel Lespinasse parent->rb_right = tmp1 = sibling->rb_left; 2246280d235SMichel Lespinasse sibling->rb_left = parent; 2256280d235SMichel Lespinasse rb_set_parent_color(tmp1, parent, RB_BLACK); 2266280d235SMichel Lespinasse __rb_rotate_set_parents(parent, sibling, root, 2276280d235SMichel Lespinasse RB_RED); 228*9c079addSMichel Lespinasse augment_rotate(parent, sibling); 2296280d235SMichel Lespinasse sibling = tmp1; 2301da177e4SLinus Torvalds } 2316280d235SMichel Lespinasse tmp1 = sibling->rb_right; 2326280d235SMichel Lespinasse if (!tmp1 || rb_is_black(tmp1)) { 2336280d235SMichel Lespinasse tmp2 = sibling->rb_left; 2346280d235SMichel Lespinasse if (!tmp2 || rb_is_black(tmp2)) { 2356280d235SMichel Lespinasse /* 2366280d235SMichel Lespinasse * Case 2 - sibling color flip 2376280d235SMichel Lespinasse * (p could be either color here) 2386280d235SMichel Lespinasse * 2396280d235SMichel Lespinasse * (p) (p) 2406280d235SMichel Lespinasse * / \ / \ 2416280d235SMichel Lespinasse * N S --> N s 2426280d235SMichel Lespinasse * / \ / \ 2436280d235SMichel Lespinasse * Sl Sr Sl Sr 2446280d235SMichel Lespinasse * 24546b6135aSMichel Lespinasse * This leaves us violating 5) which 24646b6135aSMichel Lespinasse * can be fixed by flipping p to black 24746b6135aSMichel Lespinasse * if it was red, or by recursing at p. 24846b6135aSMichel Lespinasse * p is red when coming from Case 1. 2496280d235SMichel Lespinasse */ 2506280d235SMichel Lespinasse rb_set_parent_color(sibling, parent, 2516280d235SMichel Lespinasse RB_RED); 25246b6135aSMichel Lespinasse if (rb_is_red(parent)) 25346b6135aSMichel Lespinasse rb_set_black(parent); 25446b6135aSMichel Lespinasse else { 2551da177e4SLinus Torvalds node = parent; 25655a98102SDavid Woodhouse parent = rb_parent(node); 25746b6135aSMichel Lespinasse if (parent) 258e125d147SMichel Lespinasse continue; 2591da177e4SLinus Torvalds } 26046b6135aSMichel Lespinasse break; 26146b6135aSMichel Lespinasse } 2626280d235SMichel Lespinasse /* 2636280d235SMichel Lespinasse * Case 3 - right rotate at sibling 2646280d235SMichel Lespinasse * (p could be either color here) 2656280d235SMichel Lespinasse * 2666280d235SMichel Lespinasse * (p) (p) 2676280d235SMichel Lespinasse * / \ / \ 2686280d235SMichel Lespinasse * N S --> N Sl 2696280d235SMichel Lespinasse * / \ \ 2706280d235SMichel Lespinasse * sl Sr s 2716280d235SMichel Lespinasse * \ 2726280d235SMichel Lespinasse * Sr 2736280d235SMichel Lespinasse */ 2746280d235SMichel Lespinasse sibling->rb_left = tmp1 = tmp2->rb_right; 2756280d235SMichel Lespinasse tmp2->rb_right = sibling; 2766280d235SMichel Lespinasse parent->rb_right = tmp2; 2776280d235SMichel Lespinasse if (tmp1) 2786280d235SMichel Lespinasse rb_set_parent_color(tmp1, sibling, 2796280d235SMichel Lespinasse RB_BLACK); 280*9c079addSMichel Lespinasse augment_rotate(sibling, tmp2); 2816280d235SMichel Lespinasse tmp1 = sibling; 2826280d235SMichel Lespinasse sibling = tmp2; 2831da177e4SLinus Torvalds } 2846280d235SMichel Lespinasse /* 2856280d235SMichel Lespinasse * Case 4 - left rotate at parent + color flips 2866280d235SMichel Lespinasse * (p and sl could be either color here. 2876280d235SMichel Lespinasse * After rotation, p becomes black, s acquires 2886280d235SMichel Lespinasse * p's color, and sl keeps its color) 2896280d235SMichel Lespinasse * 2906280d235SMichel Lespinasse * (p) (s) 2916280d235SMichel Lespinasse * / \ / \ 2926280d235SMichel Lespinasse * N S --> P Sr 2936280d235SMichel Lespinasse * / \ / \ 2946280d235SMichel Lespinasse * (sl) sr N (sl) 2956280d235SMichel Lespinasse */ 2966280d235SMichel Lespinasse parent->rb_right = tmp2 = sibling->rb_left; 2976280d235SMichel Lespinasse sibling->rb_left = parent; 2986280d235SMichel Lespinasse rb_set_parent_color(tmp1, sibling, RB_BLACK); 2996280d235SMichel Lespinasse if (tmp2) 3006280d235SMichel Lespinasse rb_set_parent(tmp2, parent); 3016280d235SMichel Lespinasse __rb_rotate_set_parents(parent, sibling, root, 3026280d235SMichel Lespinasse RB_BLACK); 303*9c079addSMichel Lespinasse augment_rotate(parent, sibling); 3041da177e4SLinus Torvalds break; 305d6ff1273SMichel Lespinasse } else { 3066280d235SMichel Lespinasse sibling = parent->rb_left; 3076280d235SMichel Lespinasse if (rb_is_red(sibling)) { 3086280d235SMichel Lespinasse /* Case 1 - right rotate at parent */ 3096280d235SMichel Lespinasse parent->rb_left = tmp1 = sibling->rb_right; 3106280d235SMichel Lespinasse sibling->rb_right = parent; 3116280d235SMichel Lespinasse rb_set_parent_color(tmp1, parent, RB_BLACK); 3126280d235SMichel Lespinasse __rb_rotate_set_parents(parent, sibling, root, 3136280d235SMichel Lespinasse RB_RED); 314*9c079addSMichel Lespinasse augment_rotate(parent, sibling); 3156280d235SMichel Lespinasse sibling = tmp1; 3161da177e4SLinus Torvalds } 3176280d235SMichel Lespinasse tmp1 = sibling->rb_left; 3186280d235SMichel Lespinasse if (!tmp1 || rb_is_black(tmp1)) { 3196280d235SMichel Lespinasse tmp2 = sibling->rb_right; 3206280d235SMichel Lespinasse if (!tmp2 || rb_is_black(tmp2)) { 3216280d235SMichel Lespinasse /* Case 2 - sibling color flip */ 3226280d235SMichel Lespinasse rb_set_parent_color(sibling, parent, 3236280d235SMichel Lespinasse RB_RED); 32446b6135aSMichel Lespinasse if (rb_is_red(parent)) 32546b6135aSMichel Lespinasse rb_set_black(parent); 32646b6135aSMichel Lespinasse else { 3271da177e4SLinus Torvalds node = parent; 32855a98102SDavid Woodhouse parent = rb_parent(node); 32946b6135aSMichel Lespinasse if (parent) 330e125d147SMichel Lespinasse continue; 3311da177e4SLinus Torvalds } 33246b6135aSMichel Lespinasse break; 33346b6135aSMichel Lespinasse } 3346280d235SMichel Lespinasse /* Case 3 - right rotate at sibling */ 3356280d235SMichel Lespinasse sibling->rb_right = tmp1 = tmp2->rb_left; 3366280d235SMichel Lespinasse tmp2->rb_left = sibling; 3376280d235SMichel Lespinasse parent->rb_left = tmp2; 3386280d235SMichel Lespinasse if (tmp1) 3396280d235SMichel Lespinasse rb_set_parent_color(tmp1, sibling, 3406280d235SMichel Lespinasse RB_BLACK); 341*9c079addSMichel Lespinasse augment_rotate(sibling, tmp2); 3426280d235SMichel Lespinasse tmp1 = sibling; 3436280d235SMichel Lespinasse sibling = tmp2; 3441da177e4SLinus Torvalds } 3456280d235SMichel Lespinasse /* Case 4 - left rotate at parent + color flips */ 3466280d235SMichel Lespinasse parent->rb_left = tmp2 = sibling->rb_right; 3476280d235SMichel Lespinasse sibling->rb_right = parent; 3486280d235SMichel Lespinasse rb_set_parent_color(tmp1, sibling, RB_BLACK); 3496280d235SMichel Lespinasse if (tmp2) 3506280d235SMichel Lespinasse rb_set_parent(tmp2, parent); 3516280d235SMichel Lespinasse __rb_rotate_set_parents(parent, sibling, root, 3526280d235SMichel Lespinasse RB_BLACK); 353*9c079addSMichel Lespinasse augment_rotate(parent, sibling); 3541da177e4SLinus Torvalds break; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds } 358*9c079addSMichel Lespinasse EXPORT_SYMBOL(__rb_erase_color); 35914b94af0SMichel Lespinasse 36014b94af0SMichel Lespinasse /* 36114b94af0SMichel Lespinasse * Non-augmented rbtree manipulation functions. 36214b94af0SMichel Lespinasse * 36314b94af0SMichel Lespinasse * We use dummy augmented callbacks here, and have the compiler optimize them 36414b94af0SMichel Lespinasse * out of the rb_insert_color() and rb_erase() function definitions. 36514b94af0SMichel Lespinasse */ 36614b94af0SMichel Lespinasse 36714b94af0SMichel Lespinasse static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {} 36814b94af0SMichel Lespinasse static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {} 36914b94af0SMichel Lespinasse static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {} 37014b94af0SMichel Lespinasse 37114b94af0SMichel Lespinasse static const struct rb_augment_callbacks dummy_callbacks = { 37214b94af0SMichel Lespinasse dummy_propagate, dummy_copy, dummy_rotate 37314b94af0SMichel Lespinasse }; 37414b94af0SMichel Lespinasse 37514b94af0SMichel Lespinasse void rb_insert_color(struct rb_node *node, struct rb_root *root) 37614b94af0SMichel Lespinasse { 37714b94af0SMichel Lespinasse __rb_insert(node, root, dummy_rotate); 37814b94af0SMichel Lespinasse } 37914b94af0SMichel Lespinasse EXPORT_SYMBOL(rb_insert_color); 38014b94af0SMichel Lespinasse 38114b94af0SMichel Lespinasse void rb_erase(struct rb_node *node, struct rb_root *root) 38214b94af0SMichel Lespinasse { 383*9c079addSMichel Lespinasse rb_erase_augmented(node, root, &dummy_callbacks); 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds EXPORT_SYMBOL(rb_erase); 3861da177e4SLinus Torvalds 38714b94af0SMichel Lespinasse /* 38814b94af0SMichel Lespinasse * Augmented rbtree manipulation functions. 38914b94af0SMichel Lespinasse * 39014b94af0SMichel Lespinasse * This instantiates the same __always_inline functions as in the non-augmented 39114b94af0SMichel Lespinasse * case, but this time with user-defined callbacks. 39214b94af0SMichel Lespinasse */ 39314b94af0SMichel Lespinasse 39414b94af0SMichel Lespinasse void __rb_insert_augmented(struct rb_node *node, struct rb_root *root, 39514b94af0SMichel Lespinasse void (*augment_rotate)(struct rb_node *old, struct rb_node *new)) 39614b94af0SMichel Lespinasse { 39714b94af0SMichel Lespinasse __rb_insert(node, root, augment_rotate); 39814b94af0SMichel Lespinasse } 39914b94af0SMichel Lespinasse EXPORT_SYMBOL(__rb_insert_augmented); 40014b94af0SMichel Lespinasse 4011da177e4SLinus Torvalds /* 4021da177e4SLinus Torvalds * This function returns the first node (in sort order) of the tree. 4031da177e4SLinus Torvalds */ 404f4b477c4SArtem Bityutskiy struct rb_node *rb_first(const struct rb_root *root) 4051da177e4SLinus Torvalds { 4061da177e4SLinus Torvalds struct rb_node *n; 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds n = root->rb_node; 4091da177e4SLinus Torvalds if (!n) 4101da177e4SLinus Torvalds return NULL; 4111da177e4SLinus Torvalds while (n->rb_left) 4121da177e4SLinus Torvalds n = n->rb_left; 4131da177e4SLinus Torvalds return n; 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds EXPORT_SYMBOL(rb_first); 4161da177e4SLinus Torvalds 417f4b477c4SArtem Bityutskiy struct rb_node *rb_last(const struct rb_root *root) 4181da177e4SLinus Torvalds { 4191da177e4SLinus Torvalds struct rb_node *n; 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds n = root->rb_node; 4221da177e4SLinus Torvalds if (!n) 4231da177e4SLinus Torvalds return NULL; 4241da177e4SLinus Torvalds while (n->rb_right) 4251da177e4SLinus Torvalds n = n->rb_right; 4261da177e4SLinus Torvalds return n; 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds EXPORT_SYMBOL(rb_last); 4291da177e4SLinus Torvalds 430f4b477c4SArtem Bityutskiy struct rb_node *rb_next(const struct rb_node *node) 4311da177e4SLinus Torvalds { 43255a98102SDavid Woodhouse struct rb_node *parent; 43355a98102SDavid Woodhouse 4344c199a93SMichel Lespinasse if (RB_EMPTY_NODE(node)) 43510fd48f2SJens Axboe return NULL; 43610fd48f2SJens Axboe 4377ce6ff9eSMichel Lespinasse /* 4387ce6ff9eSMichel Lespinasse * If we have a right-hand child, go down and then left as far 4397ce6ff9eSMichel Lespinasse * as we can. 4407ce6ff9eSMichel Lespinasse */ 4411da177e4SLinus Torvalds if (node->rb_right) { 4421da177e4SLinus Torvalds node = node->rb_right; 4431da177e4SLinus Torvalds while (node->rb_left) 4441da177e4SLinus Torvalds node=node->rb_left; 445f4b477c4SArtem Bityutskiy return (struct rb_node *)node; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4487ce6ff9eSMichel Lespinasse /* 4497ce6ff9eSMichel Lespinasse * No right-hand children. Everything down and left is smaller than us, 4507ce6ff9eSMichel Lespinasse * so any 'next' node must be in the general direction of our parent. 4517ce6ff9eSMichel Lespinasse * Go up the tree; any time the ancestor is a right-hand child of its 4527ce6ff9eSMichel Lespinasse * parent, keep going up. First time it's a left-hand child of its 4537ce6ff9eSMichel Lespinasse * parent, said parent is our 'next' node. 4547ce6ff9eSMichel Lespinasse */ 45555a98102SDavid Woodhouse while ((parent = rb_parent(node)) && node == parent->rb_right) 45655a98102SDavid Woodhouse node = parent; 4571da177e4SLinus Torvalds 45855a98102SDavid Woodhouse return parent; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds EXPORT_SYMBOL(rb_next); 4611da177e4SLinus Torvalds 462f4b477c4SArtem Bityutskiy struct rb_node *rb_prev(const struct rb_node *node) 4631da177e4SLinus Torvalds { 46455a98102SDavid Woodhouse struct rb_node *parent; 46555a98102SDavid Woodhouse 4664c199a93SMichel Lespinasse if (RB_EMPTY_NODE(node)) 46710fd48f2SJens Axboe return NULL; 46810fd48f2SJens Axboe 4697ce6ff9eSMichel Lespinasse /* 4707ce6ff9eSMichel Lespinasse * If we have a left-hand child, go down and then right as far 4717ce6ff9eSMichel Lespinasse * as we can. 4727ce6ff9eSMichel Lespinasse */ 4731da177e4SLinus Torvalds if (node->rb_left) { 4741da177e4SLinus Torvalds node = node->rb_left; 4751da177e4SLinus Torvalds while (node->rb_right) 4761da177e4SLinus Torvalds node=node->rb_right; 477f4b477c4SArtem Bityutskiy return (struct rb_node *)node; 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds 4807ce6ff9eSMichel Lespinasse /* 4817ce6ff9eSMichel Lespinasse * No left-hand children. Go up till we find an ancestor which 4827ce6ff9eSMichel Lespinasse * is a right-hand child of its parent. 4837ce6ff9eSMichel Lespinasse */ 48455a98102SDavid Woodhouse while ((parent = rb_parent(node)) && node == parent->rb_left) 48555a98102SDavid Woodhouse node = parent; 4861da177e4SLinus Torvalds 48755a98102SDavid Woodhouse return parent; 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds EXPORT_SYMBOL(rb_prev); 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds void rb_replace_node(struct rb_node *victim, struct rb_node *new, 4921da177e4SLinus Torvalds struct rb_root *root) 4931da177e4SLinus Torvalds { 49455a98102SDavid Woodhouse struct rb_node *parent = rb_parent(victim); 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds /* Set the surrounding nodes to point to the replacement */ 4977abc704aSMichel Lespinasse __rb_change_child(victim, new, parent, root); 4981da177e4SLinus Torvalds if (victim->rb_left) 49955a98102SDavid Woodhouse rb_set_parent(victim->rb_left, new); 5001da177e4SLinus Torvalds if (victim->rb_right) 50155a98102SDavid Woodhouse rb_set_parent(victim->rb_right, new); 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds /* Copy the pointers/colour from the victim to the replacement */ 5041da177e4SLinus Torvalds *new = *victim; 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds EXPORT_SYMBOL(rb_replace_node); 507