rbtree.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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(struct rb_node *);
  118. extern struct rb_node *rb_prev(struct rb_node *);
  119. extern struct rb_node *rb_first(struct rb_root *);
  120. extern struct rb_node *rb_last(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 */