1
0

rbtree.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * =============================================================================
  3. *
  4. * Filename: rbtree.h
  5. *
  6. * Description: rbtree(Red-Black tree) implementation adapted from linux
  7. * kernel thus can be used in userspace c program.
  8. *
  9. * Created: 09/02/2012 11:36:11 PM
  10. *
  11. * Author: Fu Haiping (forhappy), haipingf@gmail.com
  12. * Company: ICT ( Institute Of Computing Technology, CAS )
  13. *
  14. * =============================================================================
  15. */
  16. /*
  17. Red Black Trees
  18. (C) 1999 Andrea Arcangeli <andrea@suse.de>
  19. This program is free software; you can redistribute it and/or modify
  20. it under the terms of the GNU General Public License as published by
  21. the Free Software Foundation; either version 2 of the License, or
  22. (at your option) any later version.
  23. This program is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. GNU General Public License for more details.
  27. You should have received a copy of the GNU General Public License
  28. along with this program; if not, write to the Free Software
  29. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. linux/include/linux/rbtree.h
  31. To use rbtrees you'll have to implement your own insert and search cores.
  32. This will avoid us to use callbacks and to drop drammatically performances.
  33. I know it's not the cleaner way, but in C (not in C++) to get
  34. performances and genericity...
  35. Some example of insert and search follows here. The search is a plain
  36. normal search over an ordered tree. The insert instead must be implemented
  37. in two steps: First, the code must insert the element in order as a red leaf
  38. in the tree, and then the support library function rb_insert_color() must
  39. be called. Such function will do the not trivial work to rebalance the
  40. rbtree, if necessary.
  41. -----------------------------------------------------------------------
  42. static inline struct page * rb_search_page_cache(struct inode * inode,
  43. unsigned long offset)
  44. {
  45. struct rb_node * n = inode->i_rb_page_cache.rb_node;
  46. struct page * page;
  47. while (n)
  48. {
  49. page = rb_entry(n, struct page, rb_page_cache);
  50. if (offset < page->offset)
  51. n = n->rb_left;
  52. else if (offset > page->offset)
  53. n = n->rb_right;
  54. else
  55. return page;
  56. }
  57. return NULL;
  58. }
  59. static inline struct page * __rb_insert_page_cache(struct inode * inode,
  60. unsigned long offset,
  61. struct rb_node * node)
  62. {
  63. struct rb_node ** p = &inode->i_rb_page_cache.rb_node;
  64. struct rb_node * parent = NULL;
  65. struct page * page;
  66. while (*p)
  67. {
  68. parent = *p;
  69. page = rb_entry(parent, struct page, rb_page_cache);
  70. if (offset < page->offset)
  71. p = &(*p)->rb_left;
  72. else if (offset > page->offset)
  73. p = &(*p)->rb_right;
  74. else
  75. return page;
  76. }
  77. rb_link_node(node, parent, p);
  78. return NULL;
  79. }
  80. static inline struct page * rb_insert_page_cache(struct inode * inode,
  81. unsigned long offset,
  82. struct rb_node * node)
  83. {
  84. struct page * ret;
  85. if ((ret = __rb_insert_page_cache(inode, offset, node)))
  86. goto out;
  87. rb_insert_color(node, &inode->i_rb_page_cache);
  88. out:
  89. return ret;
  90. }
  91. -----------------------------------------------------------------------
  92. */
  93. #ifndef _LINUX_RBTREE_H
  94. #define _LINUX_RBTREE_H
  95. #ifndef NULL
  96. #ifdef __cplusplus
  97. #define NULL 0
  98. #else
  99. #define NULL ((void*)0)
  100. #endif
  101. #endif
  102. #ifndef offsetof
  103. #define offsetof(type, member) \
  104. ((size_t)(&((type*)0)->member))
  105. #endif
  106. #ifndef container_of
  107. #define container_of(ptr, type, member) \
  108. ((type*)((char*)(ptr) - offsetof(type, member)))
  109. #endif
  110. struct rb_node
  111. {
  112. unsigned long rb_parent_color;
  113. #define RB_RED 0
  114. #define RB_BLACK 1
  115. struct rb_node *rb_right;
  116. struct rb_node *rb_left;
  117. };
  118. struct rb_root
  119. {
  120. struct rb_node *rb_node;
  121. };
  122. #define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
  123. #define rb_color(r) ((r)->rb_parent_color & 1)
  124. #define rb_is_red(r) (!rb_color(r))
  125. #define rb_is_black(r) rb_color(r)
  126. #define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
  127. #define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
  128. static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
  129. {
  130. rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
  131. }
  132. static inline void rb_set_color(struct rb_node *rb, int color)
  133. {
  134. rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
  135. }
  136. #define RB_ROOT (struct rb_root) { NULL, }
  137. #define rb_entry(ptr, type, member) container_of(ptr, type, member)
  138. #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
  139. #define RB_EMPTY_NODE(node) (rb_parent(node) == node)
  140. #define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
  141. static inline void rb_init_node(struct rb_node *rb)
  142. {
  143. rb->rb_parent_color = 0;
  144. rb->rb_right = NULL;
  145. rb->rb_left = NULL;
  146. RB_CLEAR_NODE(rb);
  147. }
  148. void rb_insert_color(struct rb_node *, struct rb_root *);
  149. void rb_erase(struct rb_node *, struct rb_root *);
  150. typedef void (*rb_augment_f)(struct rb_node *node, void *data);
  151. void rb_augment_insert(struct rb_node *node,
  152. rb_augment_f func, void *data);
  153. struct rb_node *rb_augment_erase_begin(struct rb_node *node);
  154. void rb_augment_erase_end(struct rb_node *node,
  155. rb_augment_f func, void *data);
  156. /* Find logical next and previous nodes in a tree */
  157. struct rb_node *rb_next(const struct rb_node *);
  158. struct rb_node *rb_prev(const struct rb_node *);
  159. struct rb_node *rb_first(const struct rb_root *);
  160. struct rb_node *rb_last(const struct rb_root *);
  161. /* Fast replacement of a single node without remove/rebalance/add/rebalance */
  162. void rb_replace_node(struct rb_node *victim, struct rb_node *newer,
  163. struct rb_root *root);
  164. static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
  165. struct rb_node ** rb_link)
  166. {
  167. node->rb_parent_color = (unsigned long )parent;
  168. node->rb_left = node->rb_right = NULL;
  169. *rb_link = node;
  170. }
  171. #endif /* _LINUX_RBTREE_H */