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 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 94 | while ((parent = rb_parent(node)) && rb_is_red(parent)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 96 | gparent = rb_parent(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | if (parent == gparent->rb_left) |
| 99 | { |
| 100 | { |
| 101 | register struct rb_node *uncle = gparent->rb_right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 102 | if (uncle && rb_is_red(uncle)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 104 | rb_set_black(uncle); |
| 105 | rb_set_black(parent); |
| 106 | rb_set_red(gparent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | node = gparent; |
| 108 | continue; |
| 109 | } |
| 110 | } |
| 111 | |
Michel Lespinasse | 1f05286 | 2012-10-08 16:30:42 -0700 | [diff] [blame^] | 112 | if (parent->rb_right == node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | __rb_rotate_left(parent, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | parent = node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 117 | rb_set_black(parent); |
| 118 | rb_set_red(gparent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | __rb_rotate_right(gparent, root); |
Michel Lespinasse | 1f05286 | 2012-10-08 16:30:42 -0700 | [diff] [blame^] | 120 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } else { |
| 122 | { |
| 123 | register struct rb_node *uncle = gparent->rb_left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 124 | if (uncle && rb_is_red(uncle)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 126 | rb_set_black(uncle); |
| 127 | rb_set_black(parent); |
| 128 | rb_set_red(gparent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | node = gparent; |
| 130 | continue; |
| 131 | } |
| 132 | } |
| 133 | |
Michel Lespinasse | 1f05286 | 2012-10-08 16:30:42 -0700 | [diff] [blame^] | 134 | if (parent->rb_left == node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | __rb_rotate_right(parent, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | parent = node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 139 | rb_set_black(parent); |
| 140 | rb_set_red(gparent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | __rb_rotate_left(gparent, root); |
Michel Lespinasse | 1f05286 | 2012-10-08 16:30:42 -0700 | [diff] [blame^] | 142 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 146 | rb_set_black(root->rb_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | EXPORT_SYMBOL(rb_insert_color); |
| 149 | |
| 150 | static void __rb_erase_color(struct rb_node *node, struct rb_node *parent, |
| 151 | struct rb_root *root) |
| 152 | { |
| 153 | struct rb_node *other; |
| 154 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 155 | while ((!node || rb_is_black(node)) && node != root->rb_node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | if (parent->rb_left == node) |
| 158 | { |
| 159 | other = parent->rb_right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 160 | if (rb_is_red(other)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 162 | rb_set_black(other); |
| 163 | rb_set_red(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | __rb_rotate_left(parent, root); |
| 165 | other = parent->rb_right; |
| 166 | } |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 167 | if ((!other->rb_left || rb_is_black(other->rb_left)) && |
| 168 | (!other->rb_right || rb_is_black(other->rb_right))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 170 | rb_set_red(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | node = parent; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 172 | parent = rb_parent(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | else |
| 175 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 176 | if (!other->rb_right || rb_is_black(other->rb_right)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
Wolfram Strepp | 55a6399 | 2009-03-31 15:23:45 -0700 | [diff] [blame] | 178 | rb_set_black(other->rb_left); |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 179 | rb_set_red(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | __rb_rotate_right(other, root); |
| 181 | other = parent->rb_right; |
| 182 | } |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 183 | rb_set_color(other, rb_color(parent)); |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 184 | rb_set_black(parent); |
Wolfram Strepp | 55a6399 | 2009-03-31 15:23:45 -0700 | [diff] [blame] | 185 | rb_set_black(other->rb_right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | __rb_rotate_left(parent, root); |
| 187 | node = root->rb_node; |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | other = parent->rb_left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 194 | if (rb_is_red(other)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 196 | rb_set_black(other); |
| 197 | rb_set_red(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | __rb_rotate_right(parent, root); |
| 199 | other = parent->rb_left; |
| 200 | } |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 201 | if ((!other->rb_left || rb_is_black(other->rb_left)) && |
| 202 | (!other->rb_right || rb_is_black(other->rb_right))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 204 | rb_set_red(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | node = parent; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 206 | parent = rb_parent(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | else |
| 209 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 210 | if (!other->rb_left || rb_is_black(other->rb_left)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
Wolfram Strepp | 55a6399 | 2009-03-31 15:23:45 -0700 | [diff] [blame] | 212 | rb_set_black(other->rb_right); |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 213 | rb_set_red(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | __rb_rotate_left(other, root); |
| 215 | other = parent->rb_left; |
| 216 | } |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 217 | rb_set_color(other, rb_color(parent)); |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 218 | rb_set_black(parent); |
Wolfram Strepp | 55a6399 | 2009-03-31 15:23:45 -0700 | [diff] [blame] | 219 | rb_set_black(other->rb_left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | __rb_rotate_right(parent, root); |
| 221 | node = root->rb_node; |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | if (node) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 227 | rb_set_black(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void rb_erase(struct rb_node *node, struct rb_root *root) |
| 231 | { |
| 232 | struct rb_node *child, *parent; |
| 233 | int color; |
| 234 | |
| 235 | if (!node->rb_left) |
| 236 | child = node->rb_right; |
| 237 | else if (!node->rb_right) |
| 238 | child = node->rb_left; |
| 239 | else |
| 240 | { |
| 241 | struct rb_node *old = node, *left; |
| 242 | |
| 243 | node = node->rb_right; |
| 244 | while ((left = node->rb_left) != NULL) |
| 245 | node = left; |
Wolfram Strepp | 16c047a | 2009-06-16 15:34:11 -0700 | [diff] [blame] | 246 | |
| 247 | if (rb_parent(old)) { |
| 248 | if (rb_parent(old)->rb_left == old) |
| 249 | rb_parent(old)->rb_left = node; |
| 250 | else |
| 251 | rb_parent(old)->rb_right = node; |
| 252 | } else |
| 253 | root->rb_node = node; |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | child = node->rb_right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 256 | parent = rb_parent(node); |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 257 | color = rb_color(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 259 | if (parent == old) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | parent = node; |
Wolfram Strepp | 4c60117 | 2009-06-16 15:34:12 -0700 | [diff] [blame] | 261 | } else { |
| 262 | if (child) |
| 263 | rb_set_parent(child, parent); |
David Woodhouse | 1975e59 | 2006-04-21 13:30:36 +0100 | [diff] [blame] | 264 | parent->rb_left = child; |
Wolfram Strepp | 4b32412 | 2009-06-16 15:34:13 -0700 | [diff] [blame] | 265 | |
| 266 | node->rb_right = old->rb_right; |
| 267 | rb_set_parent(old->rb_right, node); |
Wolfram Strepp | 4c60117 | 2009-06-16 15:34:12 -0700 | [diff] [blame] | 268 | } |
David Woodhouse | 1975e59 | 2006-04-21 13:30:36 +0100 | [diff] [blame] | 269 | |
Michel Lespinasse | bf7ad8e | 2012-10-08 16:30:37 -0700 | [diff] [blame] | 270 | node->__rb_parent_color = old->__rb_parent_color; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | node->rb_left = old->rb_left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 272 | rb_set_parent(old->rb_left, node); |
Wolfram Strepp | 4b32412 | 2009-06-16 15:34:13 -0700 | [diff] [blame] | 273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | goto color; |
| 275 | } |
| 276 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 277 | parent = rb_parent(node); |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 278 | color = rb_color(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | if (child) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 281 | rb_set_parent(child, parent); |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 282 | if (parent) |
| 283 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | if (parent->rb_left == node) |
| 285 | parent->rb_left = child; |
| 286 | else |
| 287 | parent->rb_right = child; |
Pallipadi, Venkatesh | 17d9ddc | 2010-02-10 15:23:44 -0800 | [diff] [blame] | 288 | } |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 289 | else |
| 290 | root->rb_node = child; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | color: |
| 293 | if (color == RB_BLACK) |
| 294 | __rb_erase_color(child, parent, root); |
| 295 | } |
| 296 | EXPORT_SYMBOL(rb_erase); |
| 297 | |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 298 | static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data) |
| 299 | { |
| 300 | struct rb_node *parent; |
| 301 | |
| 302 | up: |
| 303 | func(node, data); |
| 304 | parent = rb_parent(node); |
| 305 | if (!parent) |
| 306 | return; |
| 307 | |
| 308 | if (node == parent->rb_left && parent->rb_right) |
| 309 | func(parent->rb_right, data); |
| 310 | else if (parent->rb_left) |
| 311 | func(parent->rb_left, data); |
| 312 | |
| 313 | node = parent; |
| 314 | goto up; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * after inserting @node into the tree, update the tree to account for |
| 319 | * both the new entry and any damage done by rebalance |
| 320 | */ |
| 321 | void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data) |
| 322 | { |
| 323 | if (node->rb_left) |
| 324 | node = node->rb_left; |
| 325 | else if (node->rb_right) |
| 326 | node = node->rb_right; |
| 327 | |
| 328 | rb_augment_path(node, func, data); |
| 329 | } |
Andreas Gruenbacher | 0b6bb66 | 2011-01-26 15:55:36 +0100 | [diff] [blame] | 330 | EXPORT_SYMBOL(rb_augment_insert); |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 331 | |
| 332 | /* |
| 333 | * before removing the node, find the deepest node on the rebalance path |
| 334 | * that will still be there after @node gets removed |
| 335 | */ |
| 336 | struct rb_node *rb_augment_erase_begin(struct rb_node *node) |
| 337 | { |
| 338 | struct rb_node *deepest; |
| 339 | |
| 340 | if (!node->rb_right && !node->rb_left) |
| 341 | deepest = rb_parent(node); |
| 342 | else if (!node->rb_right) |
| 343 | deepest = node->rb_left; |
| 344 | else if (!node->rb_left) |
| 345 | deepest = node->rb_right; |
| 346 | else { |
| 347 | deepest = rb_next(node); |
| 348 | if (deepest->rb_right) |
| 349 | deepest = deepest->rb_right; |
| 350 | else if (rb_parent(deepest) != node) |
| 351 | deepest = rb_parent(deepest); |
| 352 | } |
| 353 | |
| 354 | return deepest; |
| 355 | } |
Andreas Gruenbacher | 0b6bb66 | 2011-01-26 15:55:36 +0100 | [diff] [blame] | 356 | EXPORT_SYMBOL(rb_augment_erase_begin); |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | * after removal, update the tree to account for the removed entry |
| 360 | * and any rebalance damage. |
| 361 | */ |
| 362 | void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data) |
| 363 | { |
| 364 | if (node) |
| 365 | rb_augment_path(node, func, data); |
| 366 | } |
Andreas Gruenbacher | 0b6bb66 | 2011-01-26 15:55:36 +0100 | [diff] [blame] | 367 | EXPORT_SYMBOL(rb_augment_erase_end); |
Peter Zijlstra | b945d6b | 2010-05-29 15:31:43 +0200 | [diff] [blame] | 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | /* |
| 370 | * This function returns the first node (in sort order) of the tree. |
| 371 | */ |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 372 | struct rb_node *rb_first(const struct rb_root *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
| 374 | struct rb_node *n; |
| 375 | |
| 376 | n = root->rb_node; |
| 377 | if (!n) |
| 378 | return NULL; |
| 379 | while (n->rb_left) |
| 380 | n = n->rb_left; |
| 381 | return n; |
| 382 | } |
| 383 | EXPORT_SYMBOL(rb_first); |
| 384 | |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 385 | struct rb_node *rb_last(const struct rb_root *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
| 387 | struct rb_node *n; |
| 388 | |
| 389 | n = root->rb_node; |
| 390 | if (!n) |
| 391 | return NULL; |
| 392 | while (n->rb_right) |
| 393 | n = n->rb_right; |
| 394 | return n; |
| 395 | } |
| 396 | EXPORT_SYMBOL(rb_last); |
| 397 | |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 398 | struct rb_node *rb_next(const struct rb_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 400 | struct rb_node *parent; |
| 401 | |
Michel Lespinasse | 4c199a9 | 2012-10-08 16:30:32 -0700 | [diff] [blame] | 402 | if (RB_EMPTY_NODE(node)) |
Jens Axboe | 10fd48f | 2006-07-11 21:15:52 +0200 | [diff] [blame] | 403 | return NULL; |
| 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | /* If we have a right-hand child, go down and then left as far |
| 406 | as we can. */ |
| 407 | if (node->rb_right) { |
| 408 | node = node->rb_right; |
| 409 | while (node->rb_left) |
| 410 | node=node->rb_left; |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 411 | return (struct rb_node *)node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* No right-hand children. Everything down and left is |
| 415 | smaller than us, so any 'next' node must be in the general |
| 416 | direction of our parent. Go up the tree; any time the |
| 417 | ancestor is a right-hand child of its parent, keep going |
| 418 | up. First time it's a left-hand child of its parent, said |
| 419 | parent is our 'next' node. */ |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 420 | while ((parent = rb_parent(node)) && node == parent->rb_right) |
| 421 | node = parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 423 | return parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | EXPORT_SYMBOL(rb_next); |
| 426 | |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 427 | struct rb_node *rb_prev(const struct rb_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 429 | struct rb_node *parent; |
| 430 | |
Michel Lespinasse | 4c199a9 | 2012-10-08 16:30:32 -0700 | [diff] [blame] | 431 | if (RB_EMPTY_NODE(node)) |
Jens Axboe | 10fd48f | 2006-07-11 21:15:52 +0200 | [diff] [blame] | 432 | return NULL; |
| 433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | /* If we have a left-hand child, go down and then right as far |
| 435 | as we can. */ |
| 436 | if (node->rb_left) { |
| 437 | node = node->rb_left; |
| 438 | while (node->rb_right) |
| 439 | node=node->rb_right; |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 440 | return (struct rb_node *)node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* No left-hand children. Go up till we find an ancestor which |
| 444 | is a right-hand child of its parent */ |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 445 | while ((parent = rb_parent(node)) && node == parent->rb_left) |
| 446 | node = parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 448 | return parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | } |
| 450 | EXPORT_SYMBOL(rb_prev); |
| 451 | |
| 452 | void rb_replace_node(struct rb_node *victim, struct rb_node *new, |
| 453 | struct rb_root *root) |
| 454 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 455 | struct rb_node *parent = rb_parent(victim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
| 457 | /* Set the surrounding nodes to point to the replacement */ |
| 458 | if (parent) { |
| 459 | if (victim == parent->rb_left) |
| 460 | parent->rb_left = new; |
| 461 | else |
| 462 | parent->rb_right = new; |
| 463 | } else { |
| 464 | root->rb_node = new; |
| 465 | } |
| 466 | if (victim->rb_left) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 467 | rb_set_parent(victim->rb_left, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | if (victim->rb_right) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 469 | rb_set_parent(victim->rb_right, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | /* Copy the pointers/colour from the victim to the replacement */ |
| 472 | *new = *victim; |
| 473 | } |
| 474 | EXPORT_SYMBOL(rb_replace_node); |