23
23
I know it's not the cleaner way, but in C (not in C++) to get
24
24
performances and genericity...
26
Some example of insert and search follows here. The search is a plain
27
normal search over an ordered tree. The insert instead must be implemented
28
int two steps: as first thing the code must insert the element in
29
order as a red leaf in the tree, then the support library function
30
rb_insert_color() must be called. Such function will do the
31
not trivial work to rebalance the rbtree if necessary.
33
-----------------------------------------------------------------------
34
static inline struct page * rb_search_page_cache(struct inode * inode,
37
struct rb_node * n = inode->i_rb_page_cache.rb_node;
42
page = rb_entry(n, struct page, rb_page_cache);
44
if (offset < page->offset)
46
else if (offset > page->offset)
54
static inline struct page * __rb_insert_page_cache(struct inode * inode,
56
struct rb_node * node)
58
struct rb_node ** p = &inode->i_rb_page_cache.rb_node;
59
struct rb_node * parent = NULL;
65
page = rb_entry(parent, struct page, rb_page_cache);
67
if (offset < page->offset)
69
else if (offset > page->offset)
75
rb_link_node(node, parent, p);
80
static inline struct page * rb_insert_page_cache(struct inode * inode,
82
struct rb_node * node)
85
if ((ret = __rb_insert_page_cache(inode, offset, node)))
87
rb_insert_color(node, &inode->i_rb_page_cache);
91
-----------------------------------------------------------------------
26
See Documentation/rbtree.txt for documentation and samples.
94
29
#ifndef _LINUX_RBTREE_H
99
34
#include <btrfs/kerncompat.h>
100
35
#endif /* BTRFS_FLAT_INCLUDES */
103
unsigned long rb_parent_color;
38
unsigned long __rb_parent_color;
106
39
struct rb_node *rb_right;
107
40
struct rb_node *rb_left;
108
41
} __attribute__((aligned(sizeof(long))));
109
42
/* The alignment might seem pointless, but allegedly CRIS needs it */
113
45
struct rb_node *rb_node;
116
#define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
117
#define rb_color(r) ((r)->rb_parent_color & 1)
118
#define rb_is_red(r) (!rb_color(r))
119
#define rb_is_black(r) rb_color(r)
120
#define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
121
#define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
123
static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
125
rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
127
static inline void rb_set_color(struct rb_node *rb, int color)
129
rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
49
#define rb_parent(r) ((struct rb_node *)((r)->__rb_parent_color & ~3))
132
51
#define RB_ROOT (struct rb_root) { NULL, }
133
52
#define rb_entry(ptr, type, member) container_of(ptr, type, member)
135
#define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
136
#define RB_EMPTY_NODE(node) (rb_parent(node) == node)
137
#define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
54
#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) \
58
((node)->__rb_parent_color == (unsigned long)(node))
59
#define RB_CLEAR_NODE(node) \
60
((node)->__rb_parent_color = (unsigned long)(node))
139
63
extern void rb_insert_color(struct rb_node *, struct rb_root *);
140
64
extern void rb_erase(struct rb_node *, struct rb_root *);
142
67
/* Find logical next and previous nodes in a tree */
143
extern struct rb_node *rb_next(struct rb_node *);
144
extern struct rb_node *rb_prev(struct rb_node *);
145
extern struct rb_node *rb_first(struct rb_root *);
146
extern struct rb_node *rb_last(struct rb_root *);
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 *);
73
/* Postorder iteration - always visit the parent after its children */
74
extern struct rb_node *rb_first_postorder(const struct rb_root *);
75
extern struct rb_node *rb_next_postorder(const struct rb_node *);
148
77
/* Fast replacement of a single node without remove/rebalance/add/rebalance */
149
extern void rb_replace_node(struct rb_node *victim, struct rb_node *xnew,
78
extern void rb_replace_node(struct rb_node *victim, struct rb_node *new,
150
79
struct rb_root *root);
152
81
static inline void rb_link_node(struct rb_node * node, struct rb_node * parent,
153
82
struct rb_node ** rb_link)
155
node->rb_parent_color = (unsigned long )parent;
84
node->__rb_parent_color = (unsigned long)parent;
156
85
node->rb_left = node->rb_right = NULL;
161
/* The common insert/search/free functions */
162
typedef int (*rb_compare_nodes)(struct rb_node *node1, struct rb_node *node2);
163
typedef int (*rb_compare_keys)(struct rb_node *node, void *key);
164
typedef void (*rb_free_node)(struct rb_node *node);
90
#define rb_entry_safe(ptr, type, member) \
91
({ typeof(ptr) ____ptr = (ptr); \
92
____ptr ? rb_entry(____ptr, type, member) : NULL; \
166
int rb_insert(struct rb_root *root, struct rb_node *node,
167
rb_compare_nodes comp);
169
* In some cases, we need return the next node if we don't find the node we
170
* specify. At this time, we can use next_ret.
96
* rbtree_postorder_for_each_entry_safe - iterate over rb_root in post order of
97
* given type safe against removal of rb_node entry
99
* @pos: the 'type *' to use as a loop cursor.
100
* @n: another 'type *' to use as temporary storage
101
* @root: 'rb_root *' of the rbtree.
102
* @field: the name of the rb_node field within 'type'.
172
struct rb_node *rb_search(struct rb_root *root, void *key, rb_compare_keys comp,
173
struct rb_node **next_ret);
174
void rb_free_nodes(struct rb_root *root, rb_free_node free_node);
176
#define FREE_RB_BASED_TREE(name, free_func) \
177
static void free_##name##_tree(struct rb_root *root) \
179
rb_free_nodes(root, free_func); \
104
#define rbtree_postorder_for_each_entry_safe(pos, n, root, field) \
105
for (pos = rb_entry_safe(rb_first_postorder(root), typeof(*pos), field); \
106
pos && ({ n = rb_entry_safe(rb_next_postorder(&pos->field), \
107
typeof(*pos), field); 1; }); \
182
110
#endif /* _LINUX_RBTREE_H */