btrfs-progs: alias btrfs device delete to btrfs device remove
[platform/upstream/btrfs-progs.git] / rbtree_augmented.h
1 /*
2   Red Black Trees
3   (C) 1999  Andrea Arcangeli <andrea@suse.de>
4   (C) 2002  David Woodhouse <dwmw2@infradead.org>
5   (C) 2012  Michel Lespinasse <walken@google.com>
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21   linux/include/linux/rbtree_augmented.h
22 */
23
24 #ifndef _LINUX_RBTREE_AUGMENTED_H
25 #define _LINUX_RBTREE_AUGMENTED_H
26
27 #include "rbtree.h"
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /*
34  * Please note - only struct rb_augment_callbacks and the prototypes for
35  * rb_insert_augmented() and rb_erase_augmented() are intended to be public.
36  * The rest are implementation details you are not expected to depend on.
37  *
38  * See Documentation/rbtree.txt for documentation and samples.
39  */
40
41 struct rb_augment_callbacks {
42         void (*propagate)(struct rb_node *node, struct rb_node *stop);
43         void (*copy)(struct rb_node *old, struct rb_node *new);
44         void (*rotate)(struct rb_node *old, struct rb_node *new);
45 };
46
47 extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
48         void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
49 static inline void
50 rb_insert_augmented(struct rb_node *node, struct rb_root *root,
51                     const struct rb_augment_callbacks *augment)
52 {
53         __rb_insert_augmented(node, root, augment->rotate);
54 }
55
56 #define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield,       \
57                              rbtype, rbaugmented, rbcompute)            \
58 static inline void                                                      \
59 rbname ## _propagate(struct rb_node *rb, struct rb_node *stop)          \
60 {                                                                       \
61         while (rb != stop) {                                            \
62                 rbstruct *node = rb_entry(rb, rbstruct, rbfield);       \
63                 rbtype augmented = rbcompute(node);                     \
64                 if (node->rbaugmented == augmented)                     \
65                         break;                                          \
66                 node->rbaugmented = augmented;                          \
67                 rb = rb_parent(&node->rbfield);                         \
68         }                                                               \
69 }                                                                       \
70 static inline void                                                      \
71 rbname ## _copy(struct rb_node *rb_old, struct rb_node *rb_new)         \
72 {                                                                       \
73         rbstruct *old = rb_entry(rb_old, rbstruct, rbfield);            \
74         rbstruct *new = rb_entry(rb_new, rbstruct, rbfield);            \
75         new->rbaugmented = old->rbaugmented;                            \
76 }                                                                       \
77 static void                                                             \
78 rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new)       \
79 {                                                                       \
80         rbstruct *old = rb_entry(rb_old, rbstruct, rbfield);            \
81         rbstruct *new = rb_entry(rb_new, rbstruct, rbfield);            \
82         new->rbaugmented = old->rbaugmented;                            \
83         old->rbaugmented = rbcompute(old);                              \
84 }                                                                       \
85 rbstatic const struct rb_augment_callbacks rbname = {                   \
86         rbname ## _propagate, rbname ## _copy, rbname ## _rotate        \
87 };
88
89
90 #define RB_RED          0
91 #define RB_BLACK        1
92
93 #define __rb_parent(pc)    ((struct rb_node *)(pc & ~3))
94
95 #define __rb_color(pc)     ((pc) & 1)
96 #define __rb_is_black(pc)  __rb_color(pc)
97 #define __rb_is_red(pc)    (!__rb_color(pc))
98 #define rb_color(rb)       __rb_color((rb)->__rb_parent_color)
99 #define rb_is_red(rb)      __rb_is_red((rb)->__rb_parent_color)
100 #define rb_is_black(rb)    __rb_is_black((rb)->__rb_parent_color)
101
102 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
103 {
104         rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
105 }
106
107 static inline void rb_set_parent_color(struct rb_node *rb,
108                                        struct rb_node *p, int color)
109 {
110         rb->__rb_parent_color = (unsigned long)p | color;
111 }
112
113 static inline void
114 __rb_change_child(struct rb_node *old, struct rb_node *new,
115                   struct rb_node *parent, struct rb_root *root)
116 {
117         if (parent) {
118                 if (parent->rb_left == old)
119                         parent->rb_left = new;
120                 else
121                         parent->rb_right = new;
122         } else
123                 root->rb_node = new;
124 }
125
126 extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
127         void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
128
129 static __always_inline struct rb_node *
130 __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
131                      const struct rb_augment_callbacks *augment)
132 {
133         struct rb_node *child = node->rb_right, *tmp = node->rb_left;
134         struct rb_node *parent, *rebalance;
135         unsigned long pc;
136
137         if (!tmp) {
138                 /*
139                  * Case 1: node to erase has no more than 1 child (easy!)
140                  *
141                  * Note that if there is one child it must be red due to 5)
142                  * and node must be black due to 4). We adjust colors locally
143                  * so as to bypass __rb_erase_color() later on.
144                  */
145                 pc = node->__rb_parent_color;
146                 parent = __rb_parent(pc);
147                 __rb_change_child(node, child, parent, root);
148                 if (child) {
149                         child->__rb_parent_color = pc;
150                         rebalance = NULL;
151                 } else
152                         rebalance = __rb_is_black(pc) ? parent : NULL;
153                 tmp = parent;
154         } else if (!child) {
155                 /* Still case 1, but this time the child is node->rb_left */
156                 tmp->__rb_parent_color = pc = node->__rb_parent_color;
157                 parent = __rb_parent(pc);
158                 __rb_change_child(node, tmp, parent, root);
159                 rebalance = NULL;
160                 tmp = parent;
161         } else {
162                 struct rb_node *successor = child, *child2;
163                 tmp = child->rb_left;
164                 if (!tmp) {
165                         /*
166                          * Case 2: node's successor is its right child
167                          *
168                          *    (n)          (s)
169                          *    / \          / \
170                          *  (x) (s)  ->  (x) (c)
171                          *        \
172                          *        (c)
173                          */
174                         parent = successor;
175                         child2 = successor->rb_right;
176                         augment->copy(node, successor);
177                 } else {
178                         /*
179                          * Case 3: node's successor is leftmost under
180                          * node's right child subtree
181                          *
182                          *    (n)          (s)
183                          *    / \          / \
184                          *  (x) (y)  ->  (x) (y)
185                          *      /            /
186                          *    (p)          (p)
187                          *    /            /
188                          *  (s)          (c)
189                          *    \
190                          *    (c)
191                          */
192                         do {
193                                 parent = successor;
194                                 successor = tmp;
195                                 tmp = tmp->rb_left;
196                         } while (tmp);
197                         parent->rb_left = child2 = successor->rb_right;
198                         successor->rb_right = child;
199                         rb_set_parent(child, successor);
200                         augment->copy(node, successor);
201                         augment->propagate(parent, successor);
202                 }
203
204                 successor->rb_left = tmp = node->rb_left;
205                 rb_set_parent(tmp, successor);
206
207                 pc = node->__rb_parent_color;
208                 tmp = __rb_parent(pc);
209                 __rb_change_child(node, successor, tmp, root);
210                 if (child2) {
211                         successor->__rb_parent_color = pc;
212                         rb_set_parent_color(child2, parent, RB_BLACK);
213                         rebalance = NULL;
214                 } else {
215                         unsigned long pc2 = successor->__rb_parent_color;
216                         successor->__rb_parent_color = pc;
217                         rebalance = __rb_is_black(pc2) ? parent : NULL;
218                 }
219                 tmp = successor;
220         }
221
222         augment->propagate(tmp, NULL);
223         return rebalance;
224 }
225
226 static __always_inline void
227 rb_erase_augmented(struct rb_node *node, struct rb_root *root,
228                    const struct rb_augment_callbacks *augment)
229 {
230         struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
231         if (rebalance)
232                 __rb_erase_color(rebalance, root, augment->rotate);
233 }
234
235 #ifdef __cplusplus
236 }
237 #endif
238
239 #endif  /* _LINUX_RBTREE_AUGMENTED_H */