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> |
| 24 | #include <linux/module.h> |
| 25 | |
| 26 | static void __rb_rotate_left(struct rb_node *node, struct rb_root *root) |
| 27 | { |
| 28 | struct rb_node *right = node->rb_right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 29 | struct rb_node *parent = rb_parent(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | if ((node->rb_right = right->rb_left)) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 32 | rb_set_parent(right->rb_left, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | right->rb_left = node; |
| 34 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 35 | rb_set_parent(right, parent); |
| 36 | |
| 37 | if (parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 39 | if (node == parent->rb_left) |
| 40 | parent->rb_left = right; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | else |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 42 | parent->rb_right = right; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | } |
| 44 | else |
| 45 | root->rb_node = right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 46 | rb_set_parent(node, right); |
Pallipadi, Venkatesh | 17d9ddc | 2010-02-10 15:23:44 -0800 | [diff] [blame] | 47 | |
| 48 | if (root->augment_cb) { |
| 49 | root->augment_cb(node); |
| 50 | root->augment_cb(right); |
| 51 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static void __rb_rotate_right(struct rb_node *node, struct rb_root *root) |
| 55 | { |
| 56 | struct rb_node *left = node->rb_left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 57 | struct rb_node *parent = rb_parent(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | if ((node->rb_left = left->rb_right)) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 60 | rb_set_parent(left->rb_right, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | left->rb_right = node; |
| 62 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 63 | rb_set_parent(left, parent); |
| 64 | |
| 65 | if (parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 67 | if (node == parent->rb_right) |
| 68 | parent->rb_right = left; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | else |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 70 | parent->rb_left = left; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | else |
| 73 | root->rb_node = left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 74 | rb_set_parent(node, left); |
Pallipadi, Venkatesh | 17d9ddc | 2010-02-10 15:23:44 -0800 | [diff] [blame] | 75 | |
| 76 | if (root->augment_cb) { |
| 77 | root->augment_cb(node); |
| 78 | root->augment_cb(left); |
| 79 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void rb_insert_color(struct rb_node *node, struct rb_root *root) |
| 83 | { |
| 84 | struct rb_node *parent, *gparent; |
| 85 | |
Pallipadi, Venkatesh | 17d9ddc | 2010-02-10 15:23:44 -0800 | [diff] [blame] | 86 | if (root->augment_cb) |
| 87 | root->augment_cb(node); |
| 88 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 89 | while ((parent = rb_parent(node)) && rb_is_red(parent)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 91 | gparent = rb_parent(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | if (parent == gparent->rb_left) |
| 94 | { |
| 95 | { |
| 96 | register struct rb_node *uncle = gparent->rb_right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 97 | if (uncle && rb_is_red(uncle)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 99 | rb_set_black(uncle); |
| 100 | rb_set_black(parent); |
| 101 | rb_set_red(gparent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | node = gparent; |
| 103 | continue; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (parent->rb_right == node) |
| 108 | { |
| 109 | register struct rb_node *tmp; |
| 110 | __rb_rotate_left(parent, root); |
| 111 | tmp = parent; |
| 112 | parent = node; |
| 113 | node = tmp; |
| 114 | } |
| 115 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 116 | rb_set_black(parent); |
| 117 | rb_set_red(gparent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | __rb_rotate_right(gparent, root); |
| 119 | } else { |
| 120 | { |
| 121 | register struct rb_node *uncle = gparent->rb_left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 122 | if (uncle && rb_is_red(uncle)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 124 | rb_set_black(uncle); |
| 125 | rb_set_black(parent); |
| 126 | rb_set_red(gparent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | node = gparent; |
| 128 | continue; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (parent->rb_left == node) |
| 133 | { |
| 134 | register struct rb_node *tmp; |
| 135 | __rb_rotate_right(parent, root); |
| 136 | tmp = parent; |
| 137 | parent = node; |
| 138 | node = tmp; |
| 139 | } |
| 140 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 141 | rb_set_black(parent); |
| 142 | rb_set_red(gparent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | __rb_rotate_left(gparent, root); |
| 144 | } |
| 145 | } |
| 146 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 147 | rb_set_black(root->rb_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | EXPORT_SYMBOL(rb_insert_color); |
| 150 | |
| 151 | static void __rb_erase_color(struct rb_node *node, struct rb_node *parent, |
| 152 | struct rb_root *root) |
| 153 | { |
| 154 | struct rb_node *other; |
| 155 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 156 | while ((!node || rb_is_black(node)) && node != root->rb_node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
| 158 | if (parent->rb_left == node) |
| 159 | { |
| 160 | other = parent->rb_right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 161 | if (rb_is_red(other)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 163 | rb_set_black(other); |
| 164 | rb_set_red(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | __rb_rotate_left(parent, root); |
| 166 | other = parent->rb_right; |
| 167 | } |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 168 | if ((!other->rb_left || rb_is_black(other->rb_left)) && |
| 169 | (!other->rb_right || rb_is_black(other->rb_right))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 171 | rb_set_red(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | node = parent; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 173 | parent = rb_parent(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | else |
| 176 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 177 | if (!other->rb_right || rb_is_black(other->rb_right)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Wolfram Strepp | 55a6399 | 2009-03-31 15:23:45 -0700 | [diff] [blame] | 179 | rb_set_black(other->rb_left); |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 180 | rb_set_red(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | __rb_rotate_right(other, root); |
| 182 | other = parent->rb_right; |
| 183 | } |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 184 | rb_set_color(other, rb_color(parent)); |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 185 | rb_set_black(parent); |
Wolfram Strepp | 55a6399 | 2009-03-31 15:23:45 -0700 | [diff] [blame] | 186 | rb_set_black(other->rb_right); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | __rb_rotate_left(parent, root); |
| 188 | node = root->rb_node; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | other = parent->rb_left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 195 | if (rb_is_red(other)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 197 | rb_set_black(other); |
| 198 | rb_set_red(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | __rb_rotate_right(parent, root); |
| 200 | other = parent->rb_left; |
| 201 | } |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 202 | if ((!other->rb_left || rb_is_black(other->rb_left)) && |
| 203 | (!other->rb_right || rb_is_black(other->rb_right))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 205 | rb_set_red(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | node = parent; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 207 | parent = rb_parent(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | else |
| 210 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 211 | if (!other->rb_left || rb_is_black(other->rb_left)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
Wolfram Strepp | 55a6399 | 2009-03-31 15:23:45 -0700 | [diff] [blame] | 213 | rb_set_black(other->rb_right); |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 214 | rb_set_red(other); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | __rb_rotate_left(other, root); |
| 216 | other = parent->rb_left; |
| 217 | } |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 218 | rb_set_color(other, rb_color(parent)); |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 219 | rb_set_black(parent); |
Wolfram Strepp | 55a6399 | 2009-03-31 15:23:45 -0700 | [diff] [blame] | 220 | rb_set_black(other->rb_left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | __rb_rotate_right(parent, root); |
| 222 | node = root->rb_node; |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | if (node) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 228 | rb_set_black(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void rb_erase(struct rb_node *node, struct rb_root *root) |
| 232 | { |
| 233 | struct rb_node *child, *parent; |
| 234 | int color; |
| 235 | |
| 236 | if (!node->rb_left) |
| 237 | child = node->rb_right; |
| 238 | else if (!node->rb_right) |
| 239 | child = node->rb_left; |
| 240 | else |
| 241 | { |
| 242 | struct rb_node *old = node, *left; |
Pallipadi, Venkatesh | 17d9ddc | 2010-02-10 15:23:44 -0800 | [diff] [blame] | 243 | int old_parent_cb = 0; |
| 244 | int successor_parent_cb = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | node = node->rb_right; |
| 247 | while ((left = node->rb_left) != NULL) |
| 248 | node = left; |
Wolfram Strepp | 16c047a | 2009-06-16 15:34:11 -0700 | [diff] [blame] | 249 | |
| 250 | if (rb_parent(old)) { |
Pallipadi, Venkatesh | 17d9ddc | 2010-02-10 15:23:44 -0800 | [diff] [blame] | 251 | old_parent_cb = 1; |
Wolfram Strepp | 16c047a | 2009-06-16 15:34:11 -0700 | [diff] [blame] | 252 | if (rb_parent(old)->rb_left == old) |
| 253 | rb_parent(old)->rb_left = node; |
| 254 | else |
| 255 | rb_parent(old)->rb_right = node; |
| 256 | } else |
| 257 | root->rb_node = node; |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | child = node->rb_right; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 260 | parent = rb_parent(node); |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 261 | color = rb_color(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 263 | if (parent == old) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | parent = node; |
Wolfram Strepp | 4c60117 | 2009-06-16 15:34:12 -0700 | [diff] [blame] | 265 | } else { |
Pallipadi, Venkatesh | 17d9ddc | 2010-02-10 15:23:44 -0800 | [diff] [blame] | 266 | successor_parent_cb = 1; |
Wolfram Strepp | 4c60117 | 2009-06-16 15:34:12 -0700 | [diff] [blame] | 267 | if (child) |
| 268 | rb_set_parent(child, parent); |
Pallipadi, Venkatesh | 17d9ddc | 2010-02-10 15:23:44 -0800 | [diff] [blame] | 269 | |
David Woodhouse | 1975e59 | 2006-04-21 13:30:36 +0100 | [diff] [blame] | 270 | parent->rb_left = child; |
Wolfram Strepp | 4b32412 | 2009-06-16 15:34:13 -0700 | [diff] [blame] | 271 | |
| 272 | node->rb_right = old->rb_right; |
| 273 | rb_set_parent(old->rb_right, node); |
Wolfram Strepp | 4c60117 | 2009-06-16 15:34:12 -0700 | [diff] [blame] | 274 | } |
David Woodhouse | 1975e59 | 2006-04-21 13:30:36 +0100 | [diff] [blame] | 275 | |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 276 | node->rb_parent_color = old->rb_parent_color; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | node->rb_left = old->rb_left; |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 278 | rb_set_parent(old->rb_left, node); |
Wolfram Strepp | 4b32412 | 2009-06-16 15:34:13 -0700 | [diff] [blame] | 279 | |
Pallipadi, Venkatesh | 17d9ddc | 2010-02-10 15:23:44 -0800 | [diff] [blame] | 280 | if (root->augment_cb) { |
| 281 | /* |
| 282 | * Here, three different nodes can have new children. |
| 283 | * The parent of the successor node that was selected |
| 284 | * to replace the node to be erased. |
| 285 | * The node that is getting erased and is now replaced |
| 286 | * by its successor. |
| 287 | * The parent of the node getting erased-replaced. |
| 288 | */ |
| 289 | if (successor_parent_cb) |
| 290 | root->augment_cb(parent); |
| 291 | |
| 292 | root->augment_cb(node); |
| 293 | |
| 294 | if (old_parent_cb) |
| 295 | root->augment_cb(rb_parent(old)); |
| 296 | } |
| 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | goto color; |
| 299 | } |
| 300 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 301 | parent = rb_parent(node); |
David Woodhouse | 2f3243a | 2006-06-05 20:19:05 +0100 | [diff] [blame] | 302 | color = rb_color(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
| 304 | if (child) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 305 | rb_set_parent(child, parent); |
Pallipadi, Venkatesh | 17d9ddc | 2010-02-10 15:23:44 -0800 | [diff] [blame] | 306 | |
| 307 | if (parent) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | if (parent->rb_left == node) |
| 309 | parent->rb_left = child; |
| 310 | else |
| 311 | parent->rb_right = child; |
Pallipadi, Venkatesh | 17d9ddc | 2010-02-10 15:23:44 -0800 | [diff] [blame] | 312 | |
| 313 | if (root->augment_cb) |
| 314 | root->augment_cb(parent); |
| 315 | |
| 316 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | root->rb_node = child; |
Pallipadi, Venkatesh | 17d9ddc | 2010-02-10 15:23:44 -0800 | [diff] [blame] | 318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | color: |
| 321 | if (color == RB_BLACK) |
| 322 | __rb_erase_color(child, parent, root); |
| 323 | } |
| 324 | EXPORT_SYMBOL(rb_erase); |
| 325 | |
| 326 | /* |
| 327 | * This function returns the first node (in sort order) of the tree. |
| 328 | */ |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 329 | struct rb_node *rb_first(const struct rb_root *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | { |
| 331 | struct rb_node *n; |
| 332 | |
| 333 | n = root->rb_node; |
| 334 | if (!n) |
| 335 | return NULL; |
| 336 | while (n->rb_left) |
| 337 | n = n->rb_left; |
| 338 | return n; |
| 339 | } |
| 340 | EXPORT_SYMBOL(rb_first); |
| 341 | |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 342 | struct rb_node *rb_last(const struct rb_root *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
| 344 | struct rb_node *n; |
| 345 | |
| 346 | n = root->rb_node; |
| 347 | if (!n) |
| 348 | return NULL; |
| 349 | while (n->rb_right) |
| 350 | n = n->rb_right; |
| 351 | return n; |
| 352 | } |
| 353 | EXPORT_SYMBOL(rb_last); |
| 354 | |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 355 | struct rb_node *rb_next(const struct rb_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 357 | struct rb_node *parent; |
| 358 | |
Jens Axboe | 10fd48f | 2006-07-11 21:15:52 +0200 | [diff] [blame] | 359 | if (rb_parent(node) == node) |
| 360 | return NULL; |
| 361 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | /* If we have a right-hand child, go down and then left as far |
| 363 | as we can. */ |
| 364 | if (node->rb_right) { |
| 365 | node = node->rb_right; |
| 366 | while (node->rb_left) |
| 367 | node=node->rb_left; |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 368 | return (struct rb_node *)node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* No right-hand children. Everything down and left is |
| 372 | smaller than us, so any 'next' node must be in the general |
| 373 | direction of our parent. Go up the tree; any time the |
| 374 | ancestor is a right-hand child of its parent, keep going |
| 375 | up. First time it's a left-hand child of its parent, said |
| 376 | parent is our 'next' node. */ |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 377 | while ((parent = rb_parent(node)) && node == parent->rb_right) |
| 378 | node = parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 380 | return parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | EXPORT_SYMBOL(rb_next); |
| 383 | |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 384 | struct rb_node *rb_prev(const struct rb_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 386 | struct rb_node *parent; |
| 387 | |
Jens Axboe | 10fd48f | 2006-07-11 21:15:52 +0200 | [diff] [blame] | 388 | if (rb_parent(node) == node) |
| 389 | return NULL; |
| 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | /* If we have a left-hand child, go down and then right as far |
| 392 | as we can. */ |
| 393 | if (node->rb_left) { |
| 394 | node = node->rb_left; |
| 395 | while (node->rb_right) |
| 396 | node=node->rb_right; |
Artem Bityutskiy | f4b477c | 2009-01-10 11:12:09 +0000 | [diff] [blame] | 397 | return (struct rb_node *)node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* No left-hand children. Go up till we find an ancestor which |
| 401 | is a right-hand child of its parent */ |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 402 | while ((parent = rb_parent(node)) && node == parent->rb_left) |
| 403 | node = parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 405 | return parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | EXPORT_SYMBOL(rb_prev); |
| 408 | |
| 409 | void rb_replace_node(struct rb_node *victim, struct rb_node *new, |
| 410 | struct rb_root *root) |
| 411 | { |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 412 | struct rb_node *parent = rb_parent(victim); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
| 414 | /* Set the surrounding nodes to point to the replacement */ |
| 415 | if (parent) { |
| 416 | if (victim == parent->rb_left) |
| 417 | parent->rb_left = new; |
| 418 | else |
| 419 | parent->rb_right = new; |
| 420 | } else { |
| 421 | root->rb_node = new; |
| 422 | } |
| 423 | if (victim->rb_left) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 424 | rb_set_parent(victim->rb_left, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | if (victim->rb_right) |
David Woodhouse | 55a9810 | 2006-04-21 13:35:51 +0100 | [diff] [blame] | 426 | rb_set_parent(victim->rb_right, new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | /* Copy the pointers/colour from the victim to the replacement */ |
| 429 | *new = *victim; |
| 430 | } |
| 431 | EXPORT_SYMBOL(rb_replace_node); |