Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | Red Black Trees |
| 3 | (C) 1999 Andrea Arcangeli <andrea@suse.de> |
| 4 | (C) 2002 David Woodhouse <dwmw2@infradead.org> |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | |
| 20 | linux/lib/rbtree.c |
| 21 | */ |
| 22 | |
| 23 | #include <linux/rbtree.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 24 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Michel Lespinasse | bf7ad8e | 2012-10-08 16:30:37 -0700 | [diff] [blame] | 26 | #define RB_RED 0 |
| 27 | #define RB_BLACK 1 |
| 28 | |
| 29 | #define rb_color(r) ((r)->__rb_parent_color & 1) |
| 30 | #define rb_is_red(r) (!rb_color(r)) |
| 31 | #define rb_is_black(r) rb_color(r) |
| 32 | #define rb_set_red(r) do { (r)->__rb_parent_color &= ~1; } while (0) |
| 33 | #define rb_set_black(r) do { (r)->__rb_parent_color |= 1; } while (0) |
| 34 | |
| 35 | static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p) |
| 36 | { |
| 37 | rb->__rb_parent_color = rb_color(rb) | (unsigned long)p; |
| 38 | } |
| 39 | static inline void rb_set_color(struct rb_node *rb, int color) |
| 40 | { |
| 41 | rb->__rb_parent_color = (rb->__rb_parent_color & ~1) | color; |
| 42 | } |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | static void __rb_rotate_left(struct rb_node *node, struct rb_root *root) |
| 45 | { |
| 46 | struct rb_node *right = node->rb_right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 47 | struct rb_node *parent = rb_parent(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | if ((node->rb_right = right->rb_left)) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 50 | rb_set_parent(right->rb_left, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | right->rb_left = node; |
| 52 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 53 | rb_set_parent(right, parent); |
| 54 | |
| 55 | if (parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 57 | if (node == parent->rb_left) |
| 58 | parent->rb_left = right; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | else |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 60 | parent->rb_right = right; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | else |
| 63 | root->rb_node = right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 64 | rb_set_parent(node, right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static void __rb_rotate_right(struct rb_node *node, struct rb_root *root) |
| 68 | { |
| 69 | struct rb_node *left = node->rb_left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 70 | struct rb_node *parent = rb_parent(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | if ((node->rb_left = left->rb_right)) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 73 | rb_set_parent(left->rb_right, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | left->rb_right = node; |
| 75 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 76 | rb_set_parent(left, parent); |
| 77 | |
| 78 | if (parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 80 | if (node == parent->rb_right) |
| 81 | parent->rb_right = left; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | else |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 83 | parent->rb_left = left; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | else |
| 86 | root->rb_node = left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 87 | rb_set_parent(node, left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void rb_insert_color(struct rb_node *node, struct rb_root *root) |
| 91 | { |
| 92 | struct rb_node *parent, *gparent; |
| 93 | |
Michel Lespinasse | 6d58452 | 2012-10-08 16:30:44 -0700 | [diff] [blame^] | 94 | while (true) { |
| 95 | /* |
| 96 | * Loop invariant: node is red |
| 97 | * |
| 98 | * If there is a black parent, we are done. |
| 99 | * Otherwise, take some corrective action as we don't |
| 100 | * want a red root or two consecutive red nodes. |
| 101 | */ |
| 102 | parent = rb_parent(node); |
| 103 | if (!parent) { |
| 104 | rb_set_black(node); |
| 105 | break; |
| 106 | } else if (rb_is_black(parent)) |
| 107 | break; |
| 108 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 109 | gparent = rb_parent(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
| 111 | if (parent == gparent->rb_left) |
| 112 | { |
| 113 | { |
| 114 | register struct rb_node *uncle = gparent->rb_right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 115 | if (uncle && rb_is_red(uncle)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 117 | rb_set_black(uncle); |
| 118 | rb_set_black(parent); |
| 119 | rb_set_red(gparent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | node = gparent; |
| 121 | continue; |
| 122 | } |
| 123 | } |
| 124 | |
Michel Lespinasse | 1f05286 | 2012-10-08 16:30:42 -0700 | [diff] [blame] | 125 | if (parent->rb_right == node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | __rb_rotate_left(parent, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | parent = node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 130 | rb_set_black(parent); |
| 131 | rb_set_red(gparent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | __rb_rotate_right(gparent, root); |
Michel Lespinasse | 1f05286 | 2012-10-08 16:30:42 -0700 | [diff] [blame] | 133 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } else { |
| 135 | { |
| 136 | register struct rb_node *uncle = gparent->rb_left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 137 | if (uncle && rb_is_red(uncle)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 139 | rb_set_black(uncle); |
| 140 | rb_set_black(parent); |
| 141 | rb_set_red(gparent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | node = gparent; |
| 143 | continue; |
| 144 | } |
| 145 | } |
| 146 | |
Michel Lespinasse | 1f05286 | 2012-10-08 16:30:42 -0700 | [diff] [blame] | 147 | if (parent->rb_left == node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | __rb_rotate_right(parent, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | parent = node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 152 | rb_set_black(parent); |
| 153 | rb_set_red(gparent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | __rb_rotate_left(gparent, root); |
Michel Lespinasse | 1f05286 | 2012-10-08 16:30:42 -0700 | [diff] [blame] | 155 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | EXPORT_SYMBOL(rb_insert_color); |
| 160 | |
| 161 | static void __rb_erase_color(struct rb_node *node, struct rb_node *parent, |
| 162 | struct rb_root *root) |
| 163 | { |
| 164 | struct rb_node *other; |
| 165 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 166 | while ((!node || rb_is_black(node)) && node != root->rb_node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
| 168 | if (parent->rb_left == node) |
| 169 | { |
| 170 | other = parent->rb_right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 171 | if (rb_is_red(other)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 173 | rb_set_black(other); |
| 174 | rb_set_red(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | __rb_rotate_left(parent, root); |
| 176 | other = parent->rb_right; |
| 177 | } |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 178 | if ((!other->rb_left || rb_is_black(other->rb_left)) && |
| 179 | (!other->rb_right || rb_is_black(other->rb_right))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 181 | rb_set_red(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | node = parent; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 183 | parent = rb_parent(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | else |
| 186 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 187 | if (!other->rb_right || rb_is_black(other->rb_right)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
Wolfram Strepp | 55a6399 | 2009-03-31 15:23:45 -0700 | [diff] [blame] | 189 | rb_set_black(other->rb_left); |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 190 | rb_set_red(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | __rb_rotate_right(other, root); |
| 192 | other = parent->rb_right; |
| 193 | } |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 194 | rb_set_color(other, rb_color(parent)); |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 195 | rb_set_black(parent); |
Wolfram Strepp | 55a6399 | 2009-03-31 15:23:45 -0700 | [diff] [blame] | 196 | rb_set_black(other->rb_right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | __rb_rotate_left(parent, root); |
| 198 | node = root->rb_node; |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | other = parent->rb_left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 205 | if (rb_is_red(other)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 207 | rb_set_black(other); |
| 208 | rb_set_red(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | __rb_rotate_right(parent, root); |
| 210 | other = parent->rb_left; |
| 211 | } |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 212 | if ((!other->rb_left || rb_is_black(other->rb_left)) && |
| 213 | (!other->rb_right || rb_is_black(other->rb_right))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 215 | rb_set_red(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | node = parent; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 217 | parent = rb_parent(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | else |
| 220 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 221 | if (!other->rb_left || rb_is_black(other->rb_left)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Wolfram Strepp | 55a6399 | 2009-03-31 15:23:45 -0700 | [diff] [blame] | 223 | rb_set_black(other->rb_right); |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 224 | rb_set_red(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | __rb_rotate_left(other, root); |
| 226 | other = parent->rb_left; |
| 227 | } |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 228 | rb_set_color(other, rb_color(parent)); |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 229 | rb_set_black(parent); |
Wolfram Strepp | 55a6399 | 2009-03-31 15:23:45 -0700 | [diff] [blame] | 230 | rb_set_black(other->rb_left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | __rb_rotate_right(parent, root); |
| 232 | node = root->rb_node; |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | if (node) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 238 | rb_set_black(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void rb_erase(struct rb_node *node, struct rb_root *root) |
| 242 | { |
| 243 | struct rb_node *child, *parent; |
| 244 | int color; |
| 245 | |
| 246 | if (!node->rb_left) |
| 247 | child = node->rb_right; |
| 248 | else if (!node->rb_right) |
| 249 | child = node->rb_left; |
| 250 | else |
| 251 | { |
| 252 | struct rb_node *old = node, *left; |
| 253 | |
| 254 | node = node->rb_right; |
| 255 | while ((left = node->rb_left) != NULL) |
| 256 | node = left; |
Wolfram Strepp | 16c047a | 2009-06-16 15:34:11 -0700 | [diff] [blame] | 257 | |
| 258 | if (rb_parent(old)) { |
| 259 | if (rb_parent(old)->rb_left == old) |
| 260 | rb_parent(old)->rb_left = node; |
| 261 | else |
| 262 | rb_parent(old)->rb_right = node; |
| 263 | } else |
| 264 | root->rb_node = node; |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | child = node->rb_right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 267 | parent = rb_parent(node); |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 268 | color = rb_color(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 270 | if (parent == old) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | parent = node; |
Wolfram Strepp | 4c60117 | 2009-06-16 15:34:12 -0700 | [diff] [blame] | 272 | } else { |
| 273 | if (child) |
| 274 | rb_set_parent(child, parent); |
David Woodhouse | 1975e59 | 2006-04-21 13:30:36 +0100 | [diff] [blame] | 275 | parent->rb_left = child; |
Wolfram Strepp | 4b32412 | 2009-06-16 15:34:13 -0700 | [diff] [blame] | 276 | |
| 277 | node->rb_right = old->rb_right; |
| 278 | rb_set_parent(old->rb_right, node); |
Wolfram Strepp | 4c60117 | 2009-06-16 15:34:12 -0700 | [diff] [blame] | 279 | } |
David Woodhouse | 1975e59 | 2006-04-21 13:30:36 +0100 | [diff] [blame] | 280 | |
Michel Lespinasse | bf7ad8e | 2012-10-08 16:30:37 -0700 | [diff] [blame] | 281 | node->__rb_parent_color = old->__rb_parent_color; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | node->rb_left = old->rb_left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 283 | rb_set_parent(old->rb_left, node); |
Wolfram Strepp | 4b32412 | 2009-06-16 15:34:13 -0700 | [diff] [blame] | 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | goto color; |
| 286 | } |
| 287 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 288 | parent = rb_parent(node); |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 289 | color = rb_color(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | if (child) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 292 | rb_set_parent(child, parent); |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 293 | if (parent) |
| 294 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (parent->rb_left == node) |
| 296 | parent->rb_left = child; |
| 297 | else |
| 298 | parent->rb_right = child; |
Pallipadi, Venkatesh | 17d9ddc | 2010-02-10 15:23:44 -0800 | [diff] [blame] | 299 | } |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 300 | else |
| 301 | root->rb_node = child; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | color: |
| 304 | if (color == RB_BLACK) |
| 305 | __rb_erase_color(child, parent, root); |
| 306 | } |
| 307 | EXPORT_SYMBOL(rb_erase); |
| 308 | |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 309 | static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data) |
| 310 | { |
| 311 | struct rb_node *parent; |
| 312 | |
| 313 | up: |
| 314 | func(node, data); |
| 315 | parent = rb_parent(node); |
| 316 | if (!parent) |
| 317 | return; |
| 318 | |
| 319 | if (node == parent->rb_left && parent->rb_right) |
| 320 | func(parent->rb_right, data); |
| 321 | else if (parent->rb_left) |
| 322 | func(parent->rb_left, data); |
| 323 | |
| 324 | node = parent; |
| 325 | goto up; |
| 326 | } |
| 327 | |
| 328 | /* |
| 329 | * after inserting @node into the tree, update the tree to account for |
| 330 | * both the new entry and any damage done by rebalance |
| 331 | */ |
| 332 | void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data) |
| 333 | { |
| 334 | if (node->rb_left) |
| 335 | node = node->rb_left; |
| 336 | else if (node->rb_right) |
| 337 | node = node->rb_right; |
| 338 | |
| 339 | rb_augment_path(node, func, data); |
| 340 | } |
Andreas Gruenbacher | 0b6bb66 | 2011-01-26 15:55:36 +0100 | [diff] [blame] | 341 | EXPORT_SYMBOL(rb_augment_insert); |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 342 | |
| 343 | /* |
| 344 | * before removing the node, find the deepest node on the rebalance path |
| 345 | * that will still be there after @node gets removed |
| 346 | */ |
| 347 | struct rb_node *rb_augment_erase_begin(struct rb_node *node) |
| 348 | { |
| 349 | struct rb_node *deepest; |
| 350 | |
| 351 | if (!node->rb_right && !node->rb_left) |
| 352 | deepest = rb_parent(node); |
| 353 | else if (!node->rb_right) |
| 354 | deepest = node->rb_left; |
| 355 | else if (!node->rb_left) |
| 356 | deepest = node->rb_right; |
| 357 | else { |
| 358 | deepest = rb_next(node); |
| 359 | if (deepest->rb_right) |
| 360 | deepest = deepest->rb_right; |
| 361 | else if (rb_parent(deepest) != node) |
| 362 | deepest = rb_parent(deepest); |
| 363 | } |
| 364 | |
| 365 | return deepest; |
| 366 | } |
Andreas Gruenbacher | 0b6bb66 | 2011-01-26 15:55:36 +0100 | [diff] [blame] | 367 | EXPORT_SYMBOL(rb_augment_erase_begin); |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 368 | |
| 369 | /* |
| 370 | * after removal, update the tree to account for the removed entry |
| 371 | * and any rebalance damage. |
| 372 | */ |
| 373 | void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data) |
| 374 | { |
| 375 | if (node) |
| 376 | rb_augment_path(node, func, data); |
| 377 | } |
Andreas Gruenbacher | 0b6bb66 | 2011-01-26 15:55:36 +0100 | [diff] [blame] | 378 | EXPORT_SYMBOL(rb_augment_erase_end); |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /* |
| 381 | * This function returns the first node (in sort order) of the tree. |
| 382 | */ |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 383 | struct rb_node *rb_first(const struct rb_root *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
| 385 | struct rb_node *n; |
| 386 | |
| 387 | n = root->rb_node; |
| 388 | if (!n) |
| 389 | return NULL; |
| 390 | while (n->rb_left) |
| 391 | n = n->rb_left; |
| 392 | return n; |
| 393 | } |
| 394 | EXPORT_SYMBOL(rb_first); |
| 395 | |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 396 | struct rb_node *rb_last(const struct rb_root *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | { |
| 398 | struct rb_node *n; |
| 399 | |
| 400 | n = root->rb_node; |
| 401 | if (!n) |
| 402 | return NULL; |
| 403 | while (n->rb_right) |
| 404 | n = n->rb_right; |
| 405 | return n; |
| 406 | } |
| 407 | EXPORT_SYMBOL(rb_last); |
| 408 | |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 409 | struct rb_node *rb_next(const struct rb_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 411 | struct rb_node *parent; |
| 412 | |
Michel Lespinasse | 4c199a9 | 2012-10-08 16:30:32 -0700 | [diff] [blame] | 413 | if (RB_EMPTY_NODE(node)) |
Jens Axboe | 10fd48f | 2006-07-11 21:15:52 +0200 | [diff] [blame] | 414 | return NULL; |
| 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | /* If we have a right-hand child, go down and then left as far |
| 417 | as we can. */ |
| 418 | if (node->rb_right) { |
| 419 | node = node->rb_right; |
| 420 | while (node->rb_left) |
| 421 | node=node->rb_left; |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 422 | return (struct rb_node *)node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /* No right-hand children. Everything down and left is |
| 426 | smaller than us, so any 'next' node must be in the general |
| 427 | direction of our parent. Go up the tree; any time the |
| 428 | ancestor is a right-hand child of its parent, keep going |
| 429 | up. First time it's a left-hand child of its parent, said |
| 430 | parent is our 'next' node. */ |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 431 | while ((parent = rb_parent(node)) && node == parent->rb_right) |
| 432 | node = parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 434 | return parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | EXPORT_SYMBOL(rb_next); |
| 437 | |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 438 | struct rb_node *rb_prev(const struct rb_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 440 | struct rb_node *parent; |
| 441 | |
Michel Lespinasse | 4c199a9 | 2012-10-08 16:30:32 -0700 | [diff] [blame] | 442 | if (RB_EMPTY_NODE(node)) |
Jens Axboe | 10fd48f | 2006-07-11 21:15:52 +0200 | [diff] [blame] | 443 | return NULL; |
| 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | /* If we have a left-hand child, go down and then right as far |
| 446 | as we can. */ |
| 447 | if (node->rb_left) { |
| 448 | node = node->rb_left; |
| 449 | while (node->rb_right) |
| 450 | node=node->rb_right; |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 451 | return (struct rb_node *)node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* No left-hand children. Go up till we find an ancestor which |
| 455 | is a right-hand child of its parent */ |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 456 | while ((parent = rb_parent(node)) && node == parent->rb_left) |
| 457 | node = parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 459 | return parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |
| 461 | EXPORT_SYMBOL(rb_prev); |
| 462 | |
| 463 | void rb_replace_node(struct rb_node *victim, struct rb_node *new, |
| 464 | struct rb_root *root) |
| 465 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 466 | struct rb_node *parent = rb_parent(victim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
| 468 | /* Set the surrounding nodes to point to the replacement */ |
| 469 | if (parent) { |
| 470 | if (victim == parent->rb_left) |
| 471 | parent->rb_left = new; |
| 472 | else |
| 473 | parent->rb_right = new; |
| 474 | } else { |
| 475 | root->rb_node = new; |
| 476 | } |
| 477 | if (victim->rb_left) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 478 | rb_set_parent(victim->rb_left, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | if (victim->rb_right) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 480 | rb_set_parent(victim->rb_right, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
| 482 | /* Copy the pointers/colour from the victim to the replacement */ |
| 483 | *new = *victim; |
| 484 | } |
| 485 | EXPORT_SYMBOL(rb_replace_node); |