rbtree.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. typedef void (*rb_augment_f)(struct rb_node *node, void *data);
  47. extern void rb_augment_insert(struct rb_node *node,
  48. rb_augment_f func, void *data);
  49. extern struct rb_node *rb_augment_erase_begin(struct rb_node *node);
  50. extern void rb_augment_erase_end(struct rb_node *node,
  51. rb_augment_f func, void *data);
  52. /* Find logical next and previous nodes in a tree */
  53. extern struct rb_node *rb_next(const struct rb_node *);
  54. extern struct rb_node *rb_prev(const struct rb_node *);
  55. extern struct rb_node *rb_first(const struct rb_root *);
  56. extern struct rb_node *rb_last(const struct rb_root *);
  57. /* Fast replacement of a single node without remove/rebalance/add/rebalance */
  58. extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  59. struct rb_root *root);
  60. static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
  61. struct rb_node ** rb_link)
  62. {
  63. node->__rb_parent_color = (unsigned long)parent;
  64. node->rb_left = node->rb_right = NULL;
  65. *rb_link = node;
  66. }
  67. #endif /* _LINUX_RBTREE_H */