*
* The following copying information applies to this specific source code file:
*
- * Written in 2014 by Eric Biggers <ebiggers3@gmail.com>
+ * Written in 2014-2016 by Eric Biggers <ebiggers3@gmail.com>
*
* 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
#define _AVL_TREE_H_
#include "wimlib/types.h"
+#define AVL_INLINE forceinline
/* Node in an AVL tree. Embed this in some other data structure. */
struct avl_tree_node {
/* Returns a pointer to the parent of the specified AVL tree node, or NULL if it
* is already the root of the tree. */
-static forceinline struct avl_tree_node *
+static AVL_INLINE struct avl_tree_node *
avl_get_parent(const struct avl_tree_node *node)
{
return (struct avl_tree_node *)(node->parent_balance & ~3);
* return result ? true : false;
* }
*/
-static forceinline struct avl_tree_node *
+static AVL_INLINE struct avl_tree_node *
avl_tree_lookup(const struct avl_tree_node *root,
const void *cmp_ctx,
int (*cmp)(const void *, const struct avl_tree_node *))
* function. Specifically, with this function the item being searched for is
* expected to be in the same format as those already in the tree, with an
* embedded 'struct avl_tree_node'. */
-static forceinline struct avl_tree_node *
+static AVL_INLINE struct avl_tree_node *
avl_tree_lookup_node(const struct avl_tree_node *root,
const struct avl_tree_node *node,
int (*cmp)(const struct avl_tree_node *,
* return true;
* }
*/
-static forceinline struct avl_tree_node *
+static AVL_INLINE struct avl_tree_node *
avl_tree_insert(struct avl_tree_node **root_ptr,
struct avl_tree_node *item,
int (*cmp)(const struct avl_tree_node *,
extern struct avl_tree_node *
avl_tree_first_in_order(const struct avl_tree_node *root);
+extern struct avl_tree_node *
+avl_tree_last_in_order(const struct avl_tree_node *root);
+
extern struct avl_tree_node *
avl_tree_next_in_order(const struct avl_tree_node *node);
struct_member), 1); \
_cur = avl_tree_next_in_order(_cur))
+/*
+ * Like avl_tree_for_each_in_order(), but uses the reverse order.
+ */
+#define avl_tree_for_each_in_reverse_order(child_struct, root, \
+ struct_name, struct_member) \
+ for (struct avl_tree_node *_cur = \
+ avl_tree_last_in_order(root); \
+ _cur && ((child_struct) = \
+ avl_tree_entry(_cur, struct_name, \
+ struct_member), 1); \
+ _cur = avl_tree_prev_in_order(_cur))
+
/*
* Like avl_tree_for_each_in_order(), but iterates through the nodes in
* postorder, so the current node may be deleted or freed.