rbtree.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. See Documentation/rbtree.txt for documentation and samples.
  21. */
  22. #ifndef _LINUX_RBTREE_H
  23. #define _LINUX_RBTREE_H
  24. #include <linux/kernel.h>
  25. #include <linux/stddef.h>
  26. struct rb_node {
  27. unsigned long __rb_parent_color;
  28. struct rb_node *rb_right;
  29. struct rb_node *rb_left;
  30. } __attribute__((aligned(sizeof(long))));
  31. /* The alignment might seem pointless, but allegedly CRIS needs it */
  32. struct rb_root {
  33. struct rb_node *rb_node;
  34. };
  35. #define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3))
  36. #define RB_ROOT (struct rb_root) { NULL, }
  37. #define rb_entry(ptr, type, member) container_of(ptr, type, member)
  38. #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
  39. /* 'empty' nodes are nodes that are known not to be inserted in an rbree */
  40. #define RB_EMPTY_NODE(node) \
  41. ((node)->__rb_parent_color == (unsigned long)(node))
  42. #define RB_CLEAR_NODE(node) \
  43. ((node)->__rb_parent_color = (unsigned long)(node))
  44. extern void rb_insert_color(struct rb_node *, struct rb_root *);
  45. extern void rb_erase(struct rb_node *, struct rb_root *);
  46. struct rb_augment_callbacks {
  47. void (*propagate)(struct rb_node *node, struct rb_node *stop);
  48. void (*copy)(struct rb_node *old, struct rb_node *new);
  49. void (*rotate)(struct rb_node *old, struct rb_node *new);
  50. };
  51. extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
  52. void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
  53. extern void rb_erase_augmented(struct rb_node *node, struct rb_root *root,
  54. const struct rb_augment_callbacks *augment);
  55. static inline void
  56. rb_insert_augmented(struct rb_node *node, struct rb_root *root,
  57. const struct rb_augment_callbacks *augment)
  58. {
  59. __rb_insert_augmented(node, root, augment->rotate);
  60. }
  61. #define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \
  62. rbtype, rbaugmented, rbcompute) \
  63. static void rbname ## _propagate(struct rb_node *rb, struct rb_node *stop) \
  64. { \
  65. while (rb != stop) { \
  66. rbstruct *node = rb_entry(rb, rbstruct, rbfield); \
  67. rbtype augmented = rbcompute(node); \
  68. if (node->rbaugmented == augmented) \
  69. break; \
  70. node->rbaugmented = augmented; \
  71. rb = rb_parent(&node->rbfield); \
  72. } \
  73. } \
  74. static void rbname ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
  75. { \
  76. rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
  77. rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
  78. new->rbaugmented = old->rbaugmented; \
  79. } \
  80. static void rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
  81. { \
  82. rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
  83. rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
  84. new->rbaugmented = old->rbaugmented; \
  85. old->rbaugmented = rbcompute(old); \
  86. } \
  87. rbstatic const struct rb_augment_callbacks rbname = { \
  88. rbname ## _propagate, rbname ## _copy, rbname ## _rotate \
  89. };
  90. /* Find logical next and previous nodes in a tree */
  91. extern struct rb_node *rb_next(const struct rb_node *);
  92. extern struct rb_node *rb_prev(const struct rb_node *);
  93. extern struct rb_node *rb_first(const struct rb_root *);
  94. extern struct rb_node *rb_last(const struct rb_root *);
  95. /* Fast replacement of a single node without remove/rebalance/add/rebalance */
  96. extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  97. struct rb_root *root);
  98. static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
  99. struct rb_node ** rb_link)
  100. {
  101. node->__rb_parent_color = (unsigned long)parent;
  102. node->rb_left = node->rb_right = NULL;
  103. *rb_link = node;
  104. }
  105. #endif /* _LINUX_RBTREE_H */