当前位置:   article > 正文

Linux内核的红黑树源码实现以及调用_linux内核红黑树源码

linux内核红黑树源码

红黑树可以说是程序员经常遇到的一种数据结构,不管是工作还是面试都会涉及,有时候还会让你写一段红黑树代码。

本文主要是讲Linux中的红黑树,关于红黑树定义参考wiki:https://en.wikipedia.org/wiki/Red%E2%80%93black_tree

其中《算法导论》中的定义最为清晰:

红黑树图示:

 

Linux中的红黑树(rbtree)

 

下面是我翻译的参考Linux内核文档,https://www.kernel.org/doc/Documentation/rbtree.txt 

英文作者:Rob Landley <rob@landley.net>


什么是红黑树,它适用于什么?
------------------------------------------------

红黑树是一种自平衡二叉搜索树,用于存储可排序的键/值数据对。

它不同于基数树(用来有效地存储稀疏数组,因此使用长整数索来插入/存取/删除节点)
和哈希表(它不用进行排序就可以很容易地按序遍历,但必须设定具体大小和散列函数,而红黑树优雅的扩展存储任意键)

红黑树类似于AVL树,但提供更快的实时有界插入和删除,最坏的情况表现是三次旋转(插入仅需2次),虽然查找略慢略慢但是时间仍然在O(log n)以内。

引用Linux Weekly News:

    内核中很多地方使用红黑树:
    deadline和CFQ I/O调度程序使用rbtree跟踪请求; CD/DVD块数据驱动器也是如此。
    高分辨率计时器代码使用rbtree来组织计时器请求。
    Ext3文件系统使用rbtree跟踪目录项。
    虚拟存储区(VMA)的跟踪,以及epoll的文件描述符,加密密钥,“分层令牌桶”调度网络数据包也有rbtree的使用。

本文档介绍了Linux rbtree实现的使用。
更多有关红黑树性质和使用的信息,
请参阅:关于红黑树的Linux Weekly News文章
http://lwn.net/Articles/184495/

维基百科在红黑树上的条目
http://en.wikipedia.org/wiki/Red-black_tree

Linux实现红黑树
---------------------------------------

Linux的rbtree实现

存在于文件“lib/rbtree.c”中,要使用头文件“#include <linux/rbtree.h>”。
linux\lib\rbtree.c
linux\include\linux\rbtree.h

文末有rbtree.c和rbtree.h的源码

Linux rbtree在实现上针对速度进行了优化,用户需要编写自己的树搜索和插入函数调用内核提供的rbtree。

 

创建一个新的rbtree
---------------------

rbtree树中的数据节点是包含struct rb_node成员的结构:

  1. struct mytype {
  2.       struct rb_node node;
  3.       char * keystring;
  4.   };

当处理嵌入式struct rb_node的指针时,包含数据可以使用标准的container_of()宏访问结构。
此外,可以通过rb_entry(节点,类型,成员)直接访问各个成员。

每个rbtree的根是一个rb_root结构,它被初始化为:struct rb_root mytree = RB_ROOT;

 

在rbtree中搜索值
----------------------------------

为您的树编写搜索功能非常简单:从root开始比较每个值,并根据需要进入左或右分支。

例如:

  1. struct mytype *my_search(struct rb_root *root, char *string)
  2. {
  3. struct rb_node *node = root->rb_node;
  4. while (node) {
  5. struct mytype *data = container_of(node, struct mytype, node);
  6. int result;
  7. result = strcmp(string, data->keystring);
  8. if (result < 0)
  9. node = node->rb_left;
  10. else if (result > 0)
  11. node = node->rb_right;
  12. else
  13. return data;
  14. }
  15. return NULL;
  16. }

将数据插入rbtree
-----------------------------

在树中插入数据涉及首先搜索要插入的位置新节点,然后插入节点并重新平衡(“重新着色”)树。

插入的搜索与先前的搜索不同,找到了指针的位置,用于移植新节点。
新节点也需要指向其父节点的链接以进行重新平衡。

例如:

  1. int my_insert(struct rb_root *root, struct mytype *data)
  2. {
  3. struct rb_node **new = &(root->rb_node), *parent = NULL;
  4. /* Figure out where to put new node */
  5. while (*new) {
  6. struct mytype *this = container_of(*new, struct mytype, node);
  7. int result = strcmp(data->keystring, this->keystring);
  8. parent = *new;
  9. if (result < 0)
  10. new = &((*new)->rb_left);
  11. else if (result > 0)
  12. new = &((*new)->rb_right);
  13. else
  14. return FALSE;
  15. }
  16. /* Add new node and rebalance tree. */
  17. rb_link_node(&data->node, parent, new);
  18. rb_insert_color(&data->node, root);
  19. return TRUE;
  20. }

 

删除或替换rbtree中的现有数据
------------------------------------------------

要从树中删除现有节点,请调用:

void rb_erase(struct rb_node * victim,struct rb_root * tree);

例如:

  1. struct mytype *data = mysearch(&mytree, "walrus");
  2. if (data) {
  3. rb_erase(&data->node, &mytree);
  4. myfree(data);
  5. }

要使用具有相同键的新节点替换树中的现有节点,请调用:

void rb_replace_node(struct rb_node * old,struct rb_node * new,struct rb_root * tree);

以这种方式替换节点不会对树重新排序:如果新节点不重新排序与旧节点具有相同的key,rbtree可能会损坏。

 

迭代存储在rbtree中的元素(按排序顺序)
-------------------------------------------------- ----------------

提供了四个函数用来迭代rbtree的内容排序顺序。

这些工作在任意树上,不应该是修改或包装(锁用途除外):

  1. struct rb_node *rb_first(struct rb_root *tree);
  2. struct rb_node *rb_last(struct rb_root *tree);
  3. struct rb_node *rb_next(struct rb_node *node);
  4. struct rb_node *rb_prev(struct rb_node *node);

要开始迭代,请使用指向根的指针调用树的rb_first()或rb_last(),将返回树中的指向包含在其中的节点结构的第一个或最后一个元素指针。
要继续,通过在当前节点上调用rb_next()或rb_prev()来获取下一个或上一个节点。
如果没有剩余节点时返回为NULL。

迭代器函数返回一个指向嵌入式struct rb_node的指针。
可以使用container_of()访问包含数据结构宏,可以通过直接访问个人成员rb_entry(节点,类型,成员)。

例如:

  1. struct rb_node *node;
  2. for (node = rb_first(&mytree);node;node = rb_next(node))
  3. printk("key=%s\n",rb_entry(node,struct mytype,node)->keystring);

缓存的rbtrees
--------------

计算最左边(最小)的节点是二叉搜索树的一个常见任务。

为此,用户可以使用'struct rb_root_cached'优化O(logN) rb_first()调用避免可能很昂贵的树迭代。
虽然内存占用更大,在可忽略的运行时完成维护开销。

与rb_root结构类似,缓存的rbtree被初始化为:
 

struct rb_root_cached mytree = RB_ROOT_CACHED;

缓存的rbtree只是一个常规的rb_root,带有一个额外的指针来缓存它最左边的节点。
这允许rb_root_cached存在于rb_root所在的任何地方,
它允许支持增强树以及仅额外的一些接口:

  1. struct rb_node * rb_first_cached(struct rb_root_cached * tree);
  2. void rb_insert_color_cached(struct rb_node *,struct rb_root_cached *,bool);
  3. void rb_erase_cached(struct rb_node * node,struct rb_root_cached *);

插入和删除调用都有各自的增强对应树:

  1. void rb_insert_augmented_cached(struct rb_node * node,struct rb_root_cached *,bool,struct rb_augment_callbacks *);
  2. void rb_erase_augmented_cached(struct rb_node *,struct rb_root_cached *,struct rb_augment_callbacks *);

支持增强rbtrees
-----------------------------

增强的rbtree是一个rbtree,其中存储了每个节点的“一些”附加数据,其中节点N的附加数据必须是函数子树中所有节点的内容都以N为根。
这个数据可以用于增加rbtree的一些新功能。
增强的rbtree是基于基本rbtree基础结构构建的可选功能。
想要此功能的rbtree用户必须调用增强功能函数与用户在插入时提供了扩充回调和删除节点。

实现增强型rbtree操作的C文件必须包含<linux/rbtree_augmented.h>而不是<linux/rbtree.h>。
注意linux/rbtree_augmented.h公开了一些你不应该依赖的rbtree实现细节;
请坚持使用已记录的API,那里并没有包含头文件中的<linux/rbtree_augmented.h>。
这样的实施细节要么是为了尽量减少用户不小心依赖的机会。

在插入时,用户必须更新路径上的增强信息到插入的节点,然后像往常一样调用rb_link_node()和rb_augment_inserted()代替rb_insert_color()调用。
如果rb_augment_inserted()重新平衡rbtree,它将回调到用户提供的功能来在受影响的子树上更新增强信息。

删除节点时,用户必须调用rb_erase_augmented()而不是rb_erase()。
rb_erase_augmented()回调用户提供的函数更新受影响子树的增强信息。

在这两种情况下,回调都是通过struct rb_augment_callbacks提供的。
必须定义3个回调:

- 传播回调,它更新给定的增强值节点及其祖先,直到给定的停止点(或一直到根更新为NULL)。

- 复制回调,复制给定子树的增强值到新分配的子树根。

- 树旋转回调,复制给定的增强值子树到新分配的子树根并重新计算扩充前子树根的信息。

rb_erase_augmented()的编译代码可以内联传播和复制回调,这导致一个大的函数,所以每个增强rbtree用户应该有一个rb_erase_augmented()调用节点以限制编译代码大小。

 


样例用法
^^^^^^^^^^^^

区间树是增强的rb树的示例,参考《算法导论》。


经典rbtree只有一个key,不能直接用于存储区间范围如[lo:hi],并快速查找任何重叠新的lo:hi:或者找到与一个新的lo:hi是否完全匹配。

但是,可以扩展rbtree以在结构化中存储这样的间隔范围,这样就可以进行有效的查找和完全匹配。

存储在每个节点中的这个“额外信息”是最大的hi(max_hi)作为其后代的所有节点中的值。
这个只需要查看节点,就可以在每个节点上维护信息及其直系子节点。
这将用于O(log n)查找最低匹配(所有可能匹配中的最低起始地址)。

  1. struct interval_tree_node *
  2. interval_tree_first_match(struct rb_root *root,
  3. unsigned long start, unsigned long last)
  4. {
  5. struct interval_tree_node *node;
  6. if (!root->rb_node)
  7. return NULL;
  8. node = rb_entry(root->rb_node, struct interval_tree_node, rb);
  9. while (true) {
  10. if (node->rb.rb_left) {
  11. struct interval_tree_node *left =
  12. rb_entry(node->rb.rb_left,
  13. struct interval_tree_node, rb);
  14. if (left->__subtree_last >= start) {
  15. /*
  16. * Some nodes in left subtree satisfy Cond2.
  17. * Iterate to find the leftmost such node N.
  18. * If it also satisfies Cond1, that's the match
  19. * we are looking for. Otherwise, there is no
  20. * matching interval as nodes to the right of N
  21. * can't satisfy Cond1 either.
  22. */
  23. node = left;
  24. continue;
  25. }
  26. }
  27. if (node->start <= last) { /* Cond1 */
  28. if (node->last >= start) /* Cond2 */
  29. return node; /* node is leftmost match */
  30. if (node->rb.rb_right) {
  31. node = rb_entry(node->rb.rb_right,
  32. struct interval_tree_node, rb);
  33. if (node->__subtree_last >= start)
  34. continue;
  35. }
  36. }
  37. return NULL; /* No match */
  38. }
  39. }

使用以下扩充回调定义插入/删除:

  1. static inline unsigned long
  2. compute_subtree_last(struct interval_tree_node *node)
  3. {
  4. unsigned long max = node->last, subtree_last;
  5. if (node->rb.rb_left) {
  6. subtree_last = rb_entry(node->rb.rb_left,
  7. struct interval_tree_node, rb)->__subtree_last;
  8. if (max < subtree_last)
  9. max = subtree_last;
  10. }
  11. if (node->rb.rb_right) {
  12. subtree_last = rb_entry(node->rb.rb_right,
  13. struct interval_tree_node, rb)->__subtree_last;
  14. if (max < subtree_last)
  15. max = subtree_last;
  16. }
  17. return max;
  18. }
  19. static void augment_propagate(struct rb_node *rb, struct rb_node *stop)
  20. {
  21. while (rb != stop) {
  22. struct interval_tree_node *node =
  23. rb_entry(rb, struct interval_tree_node, rb);
  24. unsigned long subtree_last = compute_subtree_last(node);
  25. if (node->__subtree_last == subtree_last)
  26. break;
  27. node->__subtree_last = subtree_last;
  28. rb = rb_parent(&node->rb);
  29. }
  30. }
  31. static void augment_copy(struct rb_node *rb_old, struct rb_node *rb_new)
  32. {
  33. struct interval_tree_node *old =
  34. rb_entry(rb_old, struct interval_tree_node, rb);
  35. struct interval_tree_node *new =
  36. rb_entry(rb_new, struct interval_tree_node, rb);
  37. new->__subtree_last = old->__subtree_last;
  38. }
  39. static void augment_rotate(struct rb_node *rb_old, struct rb_node *rb_new)
  40. {
  41. struct interval_tree_node *old =
  42. rb_entry(rb_old, struct interval_tree_node, rb);
  43. struct interval_tree_node *new =
  44. rb_entry(rb_new, struct interval_tree_node, rb);
  45. new->__subtree_last = old->__subtree_last;
  46. old->__subtree_last = compute_subtree_last(old);
  47. }
  48. static const struct rb_augment_callbacks augment_callbacks = {
  49. augment_propagate, augment_copy, augment_rotate
  50. };
  51. void interval_tree_insert(struct interval_tree_node *node,
  52. struct rb_root *root)
  53. {
  54. struct rb_node **link = &root->rb_node, *rb_parent = NULL;
  55. unsigned long start = node->start, last = node->last;
  56. struct interval_tree_node *parent;
  57. while (*link) {
  58. rb_parent = *link;
  59. parent = rb_entry(rb_parent, struct interval_tree_node, rb);
  60. if (parent->__subtree_last < last)
  61. parent->__subtree_last = last;
  62. if (start < parent->start)
  63. link = &parent->rb.rb_left;
  64. else
  65. link = &parent->rb.rb_right;
  66. }
  67. node->__subtree_last = last;
  68. rb_link_node(&node->rb, rb_parent, link);
  69. rb_insert_augmented(&node->rb, root, &augment_callbacks);
  70. }
  71. void interval_tree_remove(struct interval_tree_node *node,
  72. struct rb_root *root)
  73. {
  74. rb_erase_augmented(&node->rb, root, &augment_callbacks);
  75. }

 

下面是内核的rbtree源码:

  1. /*
  2. Red Black Trees
  3. (C) 1999 Andrea Arcangeli <andrea@suse.de>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. linux/include/linux/rbtree.h
  16. To use rbtrees you'll have to implement your own insert and search cores.
  17. This will avoid us to use callbacks and to drop drammatically performances.
  18. I know it's not the cleaner way, but in C (not in C++) to get
  19. performances and genericity...
  20. Some example of insert and search follows here. The search is a plain
  21. normal search over an ordered tree. The insert instead must be implemented
  22. int two steps: as first thing the code must insert the element in
  23. order as a red leaf in the tree, then the support library function
  24. rb_insert_color() must be called. Such function will do the
  25. not trivial work to rebalance the rbtree if necessary.
  26. -----------------------------------------------------------------------
  27. static inline struct page * rb_search_page_cache(struct inode * inode,
  28. unsigned long offset)
  29. {
  30. struct rb_node * n = inode->i_rb_page_cache.rb_node;
  31. struct page * page;
  32. while (n)
  33. {
  34. page = rb_entry(n, struct page, rb_page_cache);
  35. if (offset < page->offset)
  36. n = n->rb_left;
  37. else if (offset > page->offset)
  38. n = n->rb_right;
  39. else
  40. return page;
  41. }
  42. return NULL;
  43. }
  44. static inline struct page * __rb_insert_page_cache(struct inode * inode,
  45. unsigned long offset,
  46. struct rb_node * node)
  47. {
  48. struct rb_node ** p = &inode->i_rb_page_cache.rb_node;
  49. struct rb_node * parent = NULL;
  50. struct page * page;
  51. while (*p)
  52. {
  53. parent = *p;
  54. page = rb_entry(parent, struct page, rb_page_cache);
  55. if (offset < page->offset)
  56. p = &(*p)->rb_left;
  57. else if (offset > page->offset)
  58. p = &(*p)->rb_right;
  59. else
  60. return page;
  61. }
  62. rb_link_node(node, parent, p);
  63. return NULL;
  64. }
  65. static inline struct page * rb_insert_page_cache(struct inode * inode,
  66. unsigned long offset,
  67. struct rb_node * node)
  68. {
  69. struct page * ret;
  70. if ((ret = __rb_insert_page_cache(inode, offset, node)))
  71. goto out;
  72. rb_insert_color(node, &inode->i_rb_page_cache);
  73. out:
  74. return ret;
  75. }
  76. -----------------------------------------------------------------------
  77. */
  78. #ifndef _LINUX_RBTREE_H
  79. #define _LINUX_RBTREE_H
  80. #include <linux/kernel.h>
  81. #include <linux/stddef.h>
  82. struct rb_node
  83. {
  84. unsigned long rb_parent_color;
  85. #define RB_RED 0
  86. #define RB_BLACK 1
  87. struct rb_node *rb_right;
  88. struct rb_node *rb_left;
  89. } __attribute__((aligned(sizeof(long))));
  90. /* The alignment might seem pointless, but allegedly CRIS needs it */
  91. struct rb_root
  92. {
  93. struct rb_node *rb_node;
  94. };
  95. #define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
  96. #define rb_color(r) ((r)->rb_parent_color & 1)
  97. #define rb_is_red(r) (!rb_color(r))
  98. #define rb_is_black(r) rb_color(r)
  99. #define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
  100. #define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
  101. static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
  102. {
  103. rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
  104. }
  105. static inline void rb_set_color(struct rb_node *rb, int color)
  106. {
  107. rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
  108. }
  109. #define RB_ROOT (struct rb_root) { NULL, }
  110. #define rb_entry(ptr, type, member) container_of(ptr, type, member)
  111. #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
  112. #define RB_EMPTY_NODE(node) (rb_parent(node) == node)
  113. #define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
  114. extern void rb_insert_color(struct rb_node *, struct rb_root *);
  115. extern void rb_erase(struct rb_node *, struct rb_root *);
  116. /* Find logical next and previous nodes in a tree */
  117. extern struct rb_node *rb_next(const struct rb_node *);
  118. extern struct rb_node *rb_prev(const struct rb_node *);
  119. extern struct rb_node *rb_first(const struct rb_root *);
  120. extern struct rb_node *rb_last(const struct rb_root *);
  121. /* Fast replacement of a single node without remove/rebalance/add/rebalance */
  122. extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  123. struct rb_root *root);
  124. static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
  125. struct rb_node ** rb_link)
  126. {
  127. node->rb_parent_color = (unsigned long )parent;
  128. node->rb_left = node->rb_right = NULL;
  129. *rb_link = node;
  130. }
  131. #endif /* _LINUX_RBTREE_H */
  1. /*
  2. Red Black Trees
  3. (C) 1999 Andrea Arcangeli <andrea@suse.de>
  4. (C) 2002 David Woodhouse <dwmw2@infradead.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. linux/lib/rbtree.c
  17. */
  18. #include <linux/rbtree.h>
  19. #include <linux/module.h>
  20. static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
  21. {
  22. struct rb_node *right = node->rb_right;
  23. struct rb_node *parent = rb_parent(node);
  24. if ((node->rb_right = right->rb_left))
  25. rb_set_parent(right->rb_left, node);
  26. right->rb_left = node;
  27. rb_set_parent(right, parent);
  28. if (parent)
  29. {
  30. if (node == parent->rb_left)
  31. parent->rb_left = right;
  32. else
  33. parent->rb_right = right;
  34. }
  35. else
  36. root->rb_node = right;
  37. rb_set_parent(node, right);
  38. }
  39. static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
  40. {
  41. struct rb_node *left = node->rb_left;
  42. struct rb_node *parent = rb_parent(node);
  43. if ((node->rb_left = left->rb_right))
  44. rb_set_parent(left->rb_right, node);
  45. left->rb_right = node;
  46. rb_set_parent(left, parent);
  47. if (parent)
  48. {
  49. if (node == parent->rb_right)
  50. parent->rb_right = left;
  51. else
  52. parent->rb_left = left;
  53. }
  54. else
  55. root->rb_node = left;
  56. rb_set_parent(node, left);
  57. }
  58. void rb_insert_color(struct rb_node *node, struct rb_root *root)
  59. {
  60. struct rb_node *parent, *gparent;
  61. while ((parent = rb_parent(node)) && rb_is_red(parent))
  62. {
  63. gparent = rb_parent(parent);
  64. if (parent == gparent->rb_left)
  65. {
  66. {
  67. register struct rb_node *uncle = gparent->rb_right;
  68. if (uncle && rb_is_red(uncle))
  69. {
  70. rb_set_black(uncle);
  71. rb_set_black(parent);
  72. rb_set_red(gparent);
  73. node = gparent;
  74. continue;
  75. }
  76. }
  77. if (parent->rb_right == node)
  78. {
  79. register struct rb_node *tmp;
  80. __rb_rotate_left(parent, root);
  81. tmp = parent;
  82. parent = node;
  83. node = tmp;
  84. }
  85. rb_set_black(parent);
  86. rb_set_red(gparent);
  87. __rb_rotate_right(gparent, root);
  88. } else {
  89. {
  90. register struct rb_node *uncle = gparent->rb_left;
  91. if (uncle && rb_is_red(uncle))
  92. {
  93. rb_set_black(uncle);
  94. rb_set_black(parent);
  95. rb_set_red(gparent);
  96. node = gparent;
  97. continue;
  98. }
  99. }
  100. if (parent->rb_left == node)
  101. {
  102. register struct rb_node *tmp;
  103. __rb_rotate_right(parent, root);
  104. tmp = parent;
  105. parent = node;
  106. node = tmp;
  107. }
  108. rb_set_black(parent);
  109. rb_set_red(gparent);
  110. __rb_rotate_left(gparent, root);
  111. }
  112. }
  113. rb_set_black(root->rb_node);
  114. }
  115. EXPORT_SYMBOL(rb_insert_color);
  116. static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
  117. struct rb_root *root)
  118. {
  119. struct rb_node *other;
  120. while ((!node || rb_is_black(node)) && node != root->rb_node)
  121. {
  122. if (parent->rb_left == node)
  123. {
  124. other = parent->rb_right;
  125. if (rb_is_red(other))
  126. {
  127. rb_set_black(other);
  128. rb_set_red(parent);
  129. __rb_rotate_left(parent, root);
  130. other = parent->rb_right;
  131. }
  132. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  133. (!other->rb_right || rb_is_black(other->rb_right)))
  134. {
  135. rb_set_red(other);
  136. node = parent;
  137. parent = rb_parent(node);
  138. }
  139. else
  140. {
  141. if (!other->rb_right || rb_is_black(other->rb_right))
  142. {
  143. rb_set_black(other->rb_left);
  144. rb_set_red(other);
  145. __rb_rotate_right(other, root);
  146. other = parent->rb_right;
  147. }
  148. rb_set_color(other, rb_color(parent));
  149. rb_set_black(parent);
  150. rb_set_black(other->rb_right);
  151. __rb_rotate_left(parent, root);
  152. node = root->rb_node;
  153. break;
  154. }
  155. }
  156. else
  157. {
  158. other = parent->rb_left;
  159. if (rb_is_red(other))
  160. {
  161. rb_set_black(other);
  162. rb_set_red(parent);
  163. __rb_rotate_right(parent, root);
  164. other = parent->rb_left;
  165. }
  166. if ((!other->rb_left || rb_is_black(other->rb_left)) &&
  167. (!other->rb_right || rb_is_black(other->rb_right)))
  168. {
  169. rb_set_red(other);
  170. node = parent;
  171. parent = rb_parent(node);
  172. }
  173. else
  174. {
  175. if (!other->rb_left || rb_is_black(other->rb_left))
  176. {
  177. rb_set_black(other->rb_right);
  178. rb_set_red(other);
  179. __rb_rotate_left(other, root);
  180. other = parent->rb_left;
  181. }
  182. rb_set_color(other, rb_color(parent));
  183. rb_set_black(parent);
  184. rb_set_black(other->rb_left);
  185. __rb_rotate_right(parent, root);
  186. node = root->rb_node;
  187. break;
  188. }
  189. }
  190. }
  191. if (node)
  192. rb_set_black(node);
  193. }
  194. void rb_erase(struct rb_node *node, struct rb_root *root)
  195. {
  196. struct rb_node *child, *parent;
  197. int color;
  198. if (!node->rb_left)
  199. child = node->rb_right;
  200. else if (!node->rb_right)
  201. child = node->rb_left;
  202. else
  203. {
  204. struct rb_node *old = node, *left;
  205. node = node->rb_right;
  206. while ((left = node->rb_left) != NULL)
  207. node = left;
  208. if (rb_parent(old)) {
  209. if (rb_parent(old)->rb_left == old)
  210. rb_parent(old)->rb_left = node;
  211. else
  212. rb_parent(old)->rb_right = node;
  213. } else
  214. root->rb_node = node;
  215. child = node->rb_right;
  216. parent = rb_parent(node);
  217. color = rb_color(node);
  218. if (parent == old) {
  219. parent = node;
  220. } else {
  221. if (child)
  222. rb_set_parent(child, parent);
  223. parent->rb_left = child;
  224. node->rb_right = old->rb_right;
  225. rb_set_parent(old->rb_right, node);
  226. }
  227. node->rb_parent_color = old->rb_parent_color;
  228. node->rb_left = old->rb_left;
  229. rb_set_parent(old->rb_left, node);
  230. goto color;
  231. }
  232. parent = rb_parent(node);
  233. color = rb_color(node);
  234. if (child)
  235. rb_set_parent(child, parent);
  236. if (parent)
  237. {
  238. if (parent->rb_left == node)
  239. parent->rb_left = child;
  240. else
  241. parent->rb_right = child;
  242. }
  243. else
  244. root->rb_node = child;
  245. color:
  246. if (color == RB_BLACK)
  247. __rb_erase_color(child, parent, root);
  248. }
  249. EXPORT_SYMBOL(rb_erase);
  250. /*
  251. * This function returns the first node (in sort order) of the tree.
  252. */
  253. struct rb_node *rb_first(const struct rb_root *root)
  254. {
  255. struct rb_node *n;
  256. n = root->rb_node;
  257. if (!n)
  258. return NULL;
  259. while (n->rb_left)
  260. n = n->rb_left;
  261. return n;
  262. }
  263. EXPORT_SYMBOL(rb_first);
  264. struct rb_node *rb_last(const struct rb_root *root)
  265. {
  266. struct rb_node *n;
  267. n = root->rb_node;
  268. if (!n)
  269. return NULL;
  270. while (n->rb_right)
  271. n = n->rb_right;
  272. return n;
  273. }
  274. EXPORT_SYMBOL(rb_last);
  275. struct rb_node *rb_next(const struct rb_node *node)
  276. {
  277. struct rb_node *parent;
  278. if (rb_parent(node) == node)
  279. return NULL;
  280. /* If we have a right-hand child, go down and then left as far
  281. as we can. */
  282. if (node->rb_right) {
  283. node = node->rb_right;
  284. while (node->rb_left)
  285. node=node->rb_left;
  286. return (struct rb_node *)node;
  287. }
  288. /* No right-hand children. Everything down and left is
  289. smaller than us, so any 'next' node must be in the general
  290. direction of our parent. Go up the tree; any time the
  291. ancestor is a right-hand child of its parent, keep going
  292. up. First time it's a left-hand child of its parent, said
  293. parent is our 'next' node. */
  294. while ((parent = rb_parent(node)) && node == parent->rb_right)
  295. node = parent;
  296. return parent;
  297. }
  298. EXPORT_SYMBOL(rb_next);
  299. struct rb_node *rb_prev(const struct rb_node *node)
  300. {
  301. struct rb_node *parent;
  302. if (rb_parent(node) == node)
  303. return NULL;
  304. /* If we have a left-hand child, go down and then right as far
  305. as we can. */
  306. if (node->rb_left) {
  307. node = node->rb_left;
  308. while (node->rb_right)
  309. node=node->rb_right;
  310. return (struct rb_node *)node;
  311. }
  312. /* No left-hand children. Go up till we find an ancestor which
  313. is a right-hand child of its parent */
  314. while ((parent = rb_parent(node)) && node == parent->rb_left)
  315. node = parent;
  316. return parent;
  317. }
  318. EXPORT_SYMBOL(rb_prev);
  319. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  320. struct rb_root *root)
  321. {
  322. struct rb_node *parent = rb_parent(victim);
  323. /* Set the surrounding nodes to point to the replacement */
  324. if (parent) {
  325. if (victim == parent->rb_left)
  326. parent->rb_left = new;
  327. else
  328. parent->rb_right = new;
  329. } else {
  330. root->rb_node = new;
  331. }
  332. if (victim->rb_left)
  333. rb_set_parent(victim->rb_left, new);
  334. if (victim->rb_right)
  335. rb_set_parent(victim->rb_right, new);
  336. /* Copy the pointers/colour from the victim to the replacement */
  337. *new = *victim;
  338. }
  339. EXPORT_SYMBOL(rb_replace_node);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/246020
推荐阅读
相关标签
  

闽ICP备14008679号