rbtree.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. {
  28. unsigned long rb_parent_color;
  29. #define RB_RED 0
  30. #define RB_BLACK 1
  31. struct rb_node *rb_right;
  32. struct rb_node *rb_left;
  33. } __attribute__((aligned(sizeof(long))));
  34. /* The alignment might seem pointless, but allegedly CRIS needs it */
  35. struct rb_root
  36. {
  37. struct rb_node *rb_node;
  38. };
  39. #define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
  40. #define rb_color(r) ((r)->rb_parent_color & 1)
  41. #define rb_is_red(r) (!rb_color(r))
  42. #define rb_is_black(r) rb_color(r)
  43. #define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
  44. #define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
  45. static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
  46. {
  47. rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
  48. }
  49. static inline void rb_set_color(struct rb_node *rb, int color)
  50. {
  51. rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
  52. }
  53. #define RB_ROOT (struct rb_root) { NULL, }
  54. #define rb_entry(ptr, type, member) container_of(ptr, type, member)
  55. #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
  56. /* 'empty' nodes are nodes that are known not to be inserted in an rbree */
  57. #define RB_EMPTY_NODE(node) ((node)->rb_parent_color == (unsigned long)(node))
  58. #define RB_CLEAR_NODE(node) ((node)->rb_parent_color = (unsigned long)(node))
  59. extern void rb_insert_color(struct rb_node *, struct rb_root *);
  60. extern void rb_erase(struct rb_node *, struct rb_root *);
  61. typedef void (*rb_augment_f)(struct rb_node *node, void *data);
  62. extern void rb_augment_insert(struct rb_node *node,
  63. rb_augment_f func, void *data);
  64. extern struct rb_node *rb_augment_erase_begin(struct rb_node *node);
  65. extern void rb_augment_erase_end(struct rb_node *node,
  66. rb_augment_f func, void *data);
  67. /* Find logical next and previous nodes in a tree */
  68. extern struct rb_node *rb_next(const struct rb_node *);
  69. extern struct rb_node *rb_prev(const struct rb_node *);
  70. extern struct rb_node *rb_first(const struct rb_root *);
  71. extern struct rb_node *rb_last(const struct rb_root *);
  72. /* Fast replacement of a single node without remove/rebalance/add/rebalance */
  73. extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  74. struct rb_root *root);
  75. static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
  76. struct rb_node ** rb_link)
  77. {
  78. node->rb_parent_color = (unsigned long )parent;
  79. node->rb_left = node->rb_right = NULL;
  80. *rb_link = node;
  81. }
  82. #endif /* _LINUX_RBTREE_H */