]> wimlib.net Git - wimlib/blobdiff - src/avl_tree.c
avl_tree: add avl_tree_prev_in_order()
[wimlib] / src / avl_tree.c
index 293db45d408903580fb001747082af28ac6b97bc..de95270ee99c56ff538af526875fbff32f8f168b 100644 (file)
@@ -1,17 +1,29 @@
 /*
- * avl_tree.c
+ * avl_tree.c - intrusive, nonrecursive AVL tree data structure (self-balancing
+ *             binary search tree), implementation file
  *
- * Intrusive, nonrecursive AVL tree data structure (self-balancing binary search
- * tree), implementation file.
+ * The following copying information applies to this specific source code file:
  *
- * Author:  Eric Biggers
- * Year:    2014
+ * Written in 2014 by Eric Biggers <ebiggers3@gmail.com>
  *
- * The author dedicates this file to the public domain.
- * You can do whatever you want with this file.
+ * To the extent possible under law, the author(s) have dedicated all copyright
+ * and related and neighboring rights to this software to the public domain
+ * worldwide via the Creative Commons Zero 1.0 Universal Public Domain
+ * Dedication (the "CC0").
+ *
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the CC0 for more details.
+ *
+ * You should have received a copy of the CC0 along with this software; if not
+ * see <http://creativecommons.org/publicdomain/zero/1.0/>.
  */
 
-#include <wimlib/avl_tree.h>
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include "wimlib/avl_tree.h"
 
 /* Starts an in-order traversal of the tree: returns the least-valued node, or
  * NULL if the tree is empty.  */
@@ -29,23 +41,43 @@ avl_tree_first_in_order(const struct avl_tree_node *root)
 /* Continues an in-order traversal of the tree: returns the next-greatest-valued
  * node, or NULL if there is none.  */
 struct avl_tree_node *
-avl_tree_next_in_order(const struct avl_tree_node *prev)
+avl_tree_next_in_order(const struct avl_tree_node *node)
 {
        const struct avl_tree_node *next;
 
-       if (prev->right)
-               for (next = prev->right;
+       if (node->right)
+               for (next = node->right;
                     next->left;
                     next = next->left)
                        ;
        else
-               for (next = avl_get_parent(prev);
-                    next && prev == next->right;
-                    prev = next, next = avl_get_parent(next))
+               for (next = avl_get_parent(node);
+                    next && node == next->right;
+                    node = next, next = avl_get_parent(next))
                        ;
        return (struct avl_tree_node *)next;
 }
 
+/* Continues a *reverse* in-order traversal of the tree: returns the
+ * previous-greatest-valued node, or NULL if there is none.  */
+struct avl_tree_node *
+avl_tree_prev_in_order(const struct avl_tree_node *node)
+{
+       const struct avl_tree_node *prev;
+
+       if (node->left)
+               for (prev = node->left;
+                    prev->right;
+                    prev = prev->right)
+                       ;
+       else
+               for (prev = avl_get_parent(node);
+                    prev && node == prev->left;
+                    node = prev, prev = avl_get_parent(prev))
+                       ;
+       return (struct avl_tree_node *)prev;
+}
+
 /* Starts a postorder traversal of the tree.  */
 struct avl_tree_node *
 avl_tree_first_in_postorder(const struct avl_tree_node *root)
@@ -81,7 +113,7 @@ avl_tree_next_in_postorder(const struct avl_tree_node *prev,
  * specified AVL tree node.
  * Note: for all calls of this, 'sign' is constant at compilation time,
  * so the compiler can remove the conditional.  */
-static AVL_INLINE struct avl_tree_node *
+static forceinline struct avl_tree_node *
 avl_get_child(const struct avl_tree_node *parent, int sign)
 {
        if (sign < 0)
@@ -94,7 +126,7 @@ avl_get_child(const struct avl_tree_node *parent, int sign)
  * specified AVL tree node.
  * Note: for all calls of this, 'sign' is constant at compilation time,
  * so the compiler can remove the conditional.  */
-static AVL_INLINE void
+static forceinline void
 avl_set_child(struct avl_tree_node *parent, int sign,
              struct avl_tree_node *child)
 {
@@ -105,7 +137,7 @@ avl_set_child(struct avl_tree_node *parent, int sign,
 }
 
 /* Sets the parent and balance factor of the specified AVL tree node.  */
-static AVL_INLINE void
+static forceinline void
 avl_set_parent_balance(struct avl_tree_node *node, struct avl_tree_node *parent,
                       int balance_factor)
 {
@@ -113,7 +145,7 @@ avl_set_parent_balance(struct avl_tree_node *node, struct avl_tree_node *parent,
 }
 
 /* Sets the parent of the specified AVL tree node.  */
-static AVL_INLINE void
+static forceinline void
 avl_set_parent(struct avl_tree_node *node, struct avl_tree_node *parent)
 {
        node->parent_balance = (uintptr_t)parent | (node->parent_balance & 3);
@@ -121,31 +153,22 @@ avl_set_parent(struct avl_tree_node *node, struct avl_tree_node *parent)
 
 /* Returns the balance factor of the specified AVL tree node --- that is, the
  * height of its right subtree minus the height of its left subtree.  */
-static AVL_INLINE int
+static forceinline int
 avl_get_balance_factor(const struct avl_tree_node *node)
 {
        return (int)(node->parent_balance & 3) - 1;
 }
 
-/* Sets the balance factor of the specified AVL tree node.  This must be
- * -1, 0, or 1.  */
-static AVL_INLINE void
-avl_set_balance_factor(struct avl_tree_node *node, int balance_factor)
-{
-       node->parent_balance =
-               (node->parent_balance & ~3) | (balance_factor + 1);
-}
-
 /* Adds @amount to the balance factor of the specified AVL tree node.
  * The caller must ensure this still results in a valid balance factor
  * (-1, 0, or 1).  */
-static AVL_INLINE void
+static forceinline void
 avl_adjust_balance_factor(struct avl_tree_node *node, int amount)
 {
        node->parent_balance += amount;
 }
 
-static AVL_INLINE void
+static forceinline void
 avl_replace_child(struct avl_tree_node **root_ptr,
                  struct avl_tree_node *parent,
                  struct avl_tree_node *old_child,
@@ -188,7 +211,7 @@ avl_replace_child(struct avl_tree_node **root_ptr,
  *
  * This updates pointers but not balance factors!
  */
-static AVL_INLINE void
+static forceinline void
 avl_rotate(struct avl_tree_node ** const root_ptr,
           struct avl_tree_node * const A, const int sign)
 {
@@ -247,7 +270,7 @@ avl_rotate(struct avl_tree_node ** const root_ptr,
  * See comment in avl_handle_subtree_growth() for explanation of balance
  * factor updates.
  */
-static AVL_INLINE struct avl_tree_node *
+static forceinline struct avl_tree_node *
 avl_do_double_rotate(struct avl_tree_node ** const root_ptr,
                     struct avl_tree_node * const B,
                     struct avl_tree_node * const A, const int sign)
@@ -305,7 +328,7 @@ avl_do_double_rotate(struct avl_tree_node ** const root_ptr,
  * Indeed, a single node insertion cannot require that more than one
  * (single or double) rotation be done.
  */
-static AVL_INLINE bool
+static forceinline bool
 avl_handle_subtree_growth(struct avl_tree_node ** const root_ptr,
                          struct avl_tree_node * const node,
                          struct avl_tree_node * const parent,
@@ -380,12 +403,10 @@ avl_handle_subtree_growth(struct avl_tree_node ** const root_ptr,
                 */
                avl_rotate(root_ptr, parent, -sign);
 
-               /* Equivalent to:
-                *    avl_set_balance_factor(parent, 0);  */
+               /* Equivalent to setting @parent's balance factor to 0.  */
                avl_adjust_balance_factor(parent, -sign); /* A */
 
-               /* Equivalent to:
-                *    avl_set_balance_factor(node, 0);  */
+               /* Equivalent to setting @node's balance factor to 0.  */
                avl_adjust_balance_factor(node, -sign);   /* B */
        } else {
                /* @node (B below) is heavy in the direction opposite
@@ -512,7 +533,7 @@ avl_tree_rebalance_after_insert(struct avl_tree_node **root_ptr,
  * decreased in height by 1.  Also in the latter case, *left_deleted_ret
  * will be set.
  */
-static AVL_INLINE struct avl_tree_node *
+static forceinline struct avl_tree_node *
 avl_handle_subtree_shrink(struct avl_tree_node ** const root_ptr,
                          struct avl_tree_node *parent,
                          const int sign,
@@ -626,7 +647,7 @@ avl_handle_subtree_shrink(struct avl_tree_node ** const root_ptr,
 /* Swaps node X, which must have 2 children, with its in-order successor, then
  * unlinks node X.  Returns the parent of X just before unlinking, without its
  * balance factor having been updated to account for the unlink.  */
-static AVL_INLINE struct avl_tree_node *
+static forceinline struct avl_tree_node *
 avl_tree_swap_with_successor(struct avl_tree_node **root_ptr,
                             struct avl_tree_node *X,
                             bool *left_deleted_ret)