rbtree.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. in two steps: First, the code must insert the element in order as a red leaf
  23. in the tree, and then the support library function rb_insert_color() must
  24. be called. Such function will do the not trivial work to rebalance the
  25. 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 <stdint.h> // for uintptr_t
  81. struct rb_node
  82. {
  83. struct rb_node *rb_parent;
  84. struct rb_node *rb_right;
  85. struct rb_node *rb_left;
  86. char rb_color;
  87. #define RB_RED 0
  88. #define RB_BLACK 1
  89. };
  90. struct rb_root
  91. {
  92. struct rb_node *rb_node;
  93. };
  94. #define RB_ROOT (struct rb_root){ (struct rb_node *)0, }
  95. #define rb_entry(ptr, type, member) \
  96. ((type *)((char *)(ptr)-(uintptr_t)(&((type *)0)->member)))
  97. #ifdef __cplusplus
  98. extern "C"
  99. {
  100. #endif
  101. void rb_insert_color(struct rb_node *node, struct rb_root *root);
  102. void rb_erase(struct rb_node *node, struct rb_root *root);
  103. /* Find logical next and previous nodes in a tree */
  104. struct rb_node *rb_next(struct rb_node *);
  105. struct rb_node *rb_prev(struct rb_node *);
  106. struct rb_node *rb_first(struct rb_root *);
  107. struct rb_node *rb_last(struct rb_root *);
  108. /* Fast replacement of a single node without remove/rebalance/add/rebalance */
  109. void rb_replace_node(struct rb_node *victim, struct rb_node *newnode,
  110. struct rb_root *root);
  111. #ifdef __cplusplus
  112. }
  113. #endif
  114. static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
  115. struct rb_node **link)
  116. {
  117. node->rb_parent = parent;
  118. node->rb_color = RB_RED;
  119. node->rb_left = node->rb_right = (struct rb_node *)0;
  120. *link = node;
  121. }
  122. #endif /* _LINUX_RBTREE_H */