Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[firefly-linux-kernel-4.4.55.git] / lib / rbtree.c
index 80b092538fa94bc1e3d93a803e05497c6f299ebf..4f56a11d67fa9da105b34337280277d6fe437b2e 100644 (file)
@@ -21,7 +21,7 @@
   linux/lib/rbtree.c
 */
 
-#include <linux/rbtree.h>
+#include <linux/rbtree_augmented.h>
 #include <linux/export.h>
 
 /*
  *  parentheses and have some accompanying text comment.
  */
 
-#define        RB_RED          0
-#define        RB_BLACK        1
-
-#define rb_color(r)   ((r)->__rb_parent_color & 1)
-#define rb_is_red(r)   (!rb_color(r))
-#define rb_is_black(r) rb_color(r)
-
 static inline void rb_set_black(struct rb_node *rb)
 {
        rb->__rb_parent_color |= RB_BLACK;
 }
 
-static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
-{
-       rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
-}
-
-static inline void rb_set_parent_color(struct rb_node *rb,
-                                      struct rb_node *p, int color)
-{
-       rb->__rb_parent_color = (unsigned long)p | color;
-}
-
 static inline struct rb_node *rb_red_parent(struct rb_node *red)
 {
        return (struct rb_node *)red->__rb_parent_color;
 }
 
-static inline void
-__rb_change_child(struct rb_node *old, struct rb_node *new,
-                 struct rb_node *parent, struct rb_root *root)
-{
-       if (parent) {
-               if (parent->rb_left == old)
-                       parent->rb_left = new;
-               else
-                       parent->rb_right = new;
-       } else
-               root->rb_node = new;
-}
-
 /*
  * Helper function for rotations:
  * - old's parent and color get assigned to new
@@ -100,7 +69,9 @@ __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
        __rb_change_child(old, new, parent, root);
 }
 
-void rb_insert_color(struct rb_node *node, struct rb_root *root)
+static __always_inline void
+__rb_insert(struct rb_node *node, struct rb_root *root,
+           void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
 {
        struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
 
@@ -164,6 +135,7 @@ void rb_insert_color(struct rb_node *node, struct rb_root *root)
                                        rb_set_parent_color(tmp, parent,
                                                            RB_BLACK);
                                rb_set_parent_color(parent, node, RB_RED);
+                               augment_rotate(parent, node);
                                parent = node;
                                tmp = node->rb_right;
                        }
@@ -182,6 +154,7 @@ void rb_insert_color(struct rb_node *node, struct rb_root *root)
                        if (tmp)
                                rb_set_parent_color(tmp, gparent, RB_BLACK);
                        __rb_rotate_set_parents(gparent, parent, root, RB_RED);
+                       augment_rotate(gparent, parent);
                        break;
                } else {
                        tmp = gparent->rb_left;
@@ -204,6 +177,7 @@ void rb_insert_color(struct rb_node *node, struct rb_root *root)
                                        rb_set_parent_color(tmp, parent,
                                                            RB_BLACK);
                                rb_set_parent_color(parent, node, RB_RED);
+                               augment_rotate(parent, node);
                                parent = node;
                                tmp = node->rb_left;
                        }
@@ -214,13 +188,15 @@ void rb_insert_color(struct rb_node *node, struct rb_root *root)
                        if (tmp)
                                rb_set_parent_color(tmp, gparent, RB_BLACK);
                        __rb_rotate_set_parents(gparent, parent, root, RB_RED);
+                       augment_rotate(gparent, parent);
                        break;
                }
        }
 }
-EXPORT_SYMBOL(rb_insert_color);
 
-static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
+__always_inline void
+__rb_erase_color(struct rb_node *parent, struct rb_root *root,
+       void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
 {
        struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
 
@@ -249,6 +225,7 @@ static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
                                rb_set_parent_color(tmp1, parent, RB_BLACK);
                                __rb_rotate_set_parents(parent, sibling, root,
                                                        RB_RED);
+                               augment_rotate(parent, sibling);
                                sibling = tmp1;
                        }
                        tmp1 = sibling->rb_right;
@@ -300,6 +277,7 @@ static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
                                if (tmp1)
                                        rb_set_parent_color(tmp1, sibling,
                                                            RB_BLACK);
+                               augment_rotate(sibling, tmp2);
                                tmp1 = sibling;
                                sibling = tmp2;
                        }
@@ -322,6 +300,7 @@ static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
                                rb_set_parent(tmp2, parent);
                        __rb_rotate_set_parents(parent, sibling, root,
                                                RB_BLACK);
+                       augment_rotate(parent, sibling);
                        break;
                } else {
                        sibling = parent->rb_left;
@@ -332,6 +311,7 @@ static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
                                rb_set_parent_color(tmp1, parent, RB_BLACK);
                                __rb_rotate_set_parents(parent, sibling, root,
                                                        RB_RED);
+                               augment_rotate(parent, sibling);
                                sibling = tmp1;
                        }
                        tmp1 = sibling->rb_left;
@@ -358,6 +338,7 @@ static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
                                if (tmp1)
                                        rb_set_parent_color(tmp1, sibling,
                                                            RB_BLACK);
+                               augment_rotate(sibling, tmp2);
                                tmp1 = sibling;
                                sibling = tmp2;
                        }
@@ -369,146 +350,53 @@ static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
                                rb_set_parent(tmp2, parent);
                        __rb_rotate_set_parents(parent, sibling, root,
                                                RB_BLACK);
+                       augment_rotate(parent, sibling);
                        break;
                }
        }
 }
+EXPORT_SYMBOL(__rb_erase_color);
 
-void rb_erase(struct rb_node *node, struct rb_root *root)
-{
-       struct rb_node *child = node->rb_right, *tmp = node->rb_left;
-       struct rb_node *parent, *rebalance;
-
-       if (!tmp) {
-               /*
-                * Case 1: node to erase has no more than 1 child (easy!)
-                *
-                * Note that if there is one child it must be red due to 5)
-                * and node must be black due to 4). We adjust colors locally
-                * so as to bypass __rb_erase_color() later on.
-                */
-
-               parent = rb_parent(node);
-               __rb_change_child(node, child, parent, root);
-               if (child) {
-                       rb_set_parent_color(child, parent, RB_BLACK);
-                       rebalance = NULL;
-               } else {
-                       rebalance = rb_is_black(node) ? parent : NULL;
-               }
-       } else if (!child) {
-               /* Still case 1, but this time the child is node->rb_left */
-               parent = rb_parent(node);
-               __rb_change_child(node, tmp, parent, root);
-               rb_set_parent_color(tmp, parent, RB_BLACK);
-               rebalance = NULL;
-       } else {
-               struct rb_node *old = node, *left;
-
-               node = child;
-               while ((left = node->rb_left) != NULL)
-                       node = left;
-
-               __rb_change_child(old, node, rb_parent(old), root);
-
-               child = node->rb_right;
-               parent = rb_parent(node);
-
-               if (parent == old) {
-                       parent = node;
-               } else {
-                       parent->rb_left = child;
-
-                       node->rb_right = old->rb_right;
-                       rb_set_parent(old->rb_right, node);
-               }
+/*
+ * Non-augmented rbtree manipulation functions.
+ *
+ * We use dummy augmented callbacks here, and have the compiler optimize them
+ * out of the rb_insert_color() and rb_erase() function definitions.
+ */
 
-               if (child) {
-                       rb_set_parent_color(child, parent, RB_BLACK);
-                       rebalance = NULL;
-               } else {
-                       rebalance = rb_is_black(node) ? parent : NULL;
-               }
-               node->__rb_parent_color = old->__rb_parent_color;
-               node->rb_left = old->rb_left;
-               rb_set_parent(old->rb_left, node);
-       }
+static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
+static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
+static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
 
-       if (rebalance)
-               __rb_erase_color(rebalance, root);
-}
-EXPORT_SYMBOL(rb_erase);
+static const struct rb_augment_callbacks dummy_callbacks = {
+       dummy_propagate, dummy_copy, dummy_rotate
+};
 
-static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
+void rb_insert_color(struct rb_node *node, struct rb_root *root)
 {
-       struct rb_node *parent;
-
-up:
-       func(node, data);
-       parent = rb_parent(node);
-       if (!parent)
-               return;
-
-       if (node == parent->rb_left && parent->rb_right)
-               func(parent->rb_right, data);
-       else if (parent->rb_left)
-               func(parent->rb_left, data);
-
-       node = parent;
-       goto up;
+       __rb_insert(node, root, dummy_rotate);
 }
+EXPORT_SYMBOL(rb_insert_color);
 
-/*
- * after inserting @node into the tree, update the tree to account for
- * both the new entry and any damage done by rebalance
- */
-void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
+void rb_erase(struct rb_node *node, struct rb_root *root)
 {
-       if (node->rb_left)
-               node = node->rb_left;
-       else if (node->rb_right)
-               node = node->rb_right;
-
-       rb_augment_path(node, func, data);
+       rb_erase_augmented(node, root, &dummy_callbacks);
 }
-EXPORT_SYMBOL(rb_augment_insert);
+EXPORT_SYMBOL(rb_erase);
 
 /*
- * before removing the node, find the deepest node on the rebalance path
- * that will still be there after @node gets removed
+ * Augmented rbtree manipulation functions.
+ *
+ * This instantiates the same __always_inline functions as in the non-augmented
+ * case, but this time with user-defined callbacks.
  */
-struct rb_node *rb_augment_erase_begin(struct rb_node *node)
-{
-       struct rb_node *deepest;
-
-       if (!node->rb_right && !node->rb_left)
-               deepest = rb_parent(node);
-       else if (!node->rb_right)
-               deepest = node->rb_left;
-       else if (!node->rb_left)
-               deepest = node->rb_right;
-       else {
-               deepest = rb_next(node);
-               if (deepest->rb_right)
-                       deepest = deepest->rb_right;
-               else if (rb_parent(deepest) != node)
-                       deepest = rb_parent(deepest);
-       }
-
-       return deepest;
-}
-EXPORT_SYMBOL(rb_augment_erase_begin);
 
-/*
- * after removal, update the tree to account for the removed entry
- * and any rebalance damage.
- */
-void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
+void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
+       void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
 {
-       if (node)
-               rb_augment_path(node, func, data);
+       __rb_insert(node, root, augment_rotate);
 }
-EXPORT_SYMBOL(rb_augment_erase_end);
+EXPORT_SYMBOL(__rb_insert_augmented);
 
 /*
  * This function returns the first node (in sort order) of the tree.