[INCLUDE/WINE] Update rbtree.h. CORE-14656
authorAmine Khaldi <amine.khaldi@reactos.org>
Mon, 4 Jun 2018 02:54:55 +0000 (03:54 +0100)
committerAmine Khaldi <amine.khaldi@reactos.org>
Mon, 4 Jun 2018 02:54:55 +0000 (03:54 +0100)
sdk/include/reactos/wine/rbtree.h

index 441e039..8130deb 100644 (file)
@@ -107,6 +107,13 @@ static inline struct wine_rb_entry *wine_rb_head(struct wine_rb_entry *iter)
     return iter;
 }
 
+static inline struct wine_rb_entry *wine_rb_tail(struct wine_rb_entry *iter)
+{
+    if (!iter) return NULL;
+    while (iter->right) iter = iter->right;
+    return iter;
+}
+
 static inline struct wine_rb_entry *wine_rb_next(struct wine_rb_entry *iter)
 {
     if (iter->right) return wine_rb_head(iter->right);
@@ -114,6 +121,13 @@ static inline struct wine_rb_entry *wine_rb_next(struct wine_rb_entry *iter)
     return iter->parent;
 }
 
+static inline struct wine_rb_entry *wine_rb_prev(struct wine_rb_entry *iter)
+{
+    if (iter->left) return wine_rb_tail(iter->left);
+    while (iter->parent && iter->parent->left == iter) iter = iter->parent;
+    return iter->parent;
+}
+
 static inline struct wine_rb_entry *wine_rb_postorder_head(struct wine_rb_entry *iter)
 {
     if (!iter) return NULL;
@@ -139,19 +153,27 @@ static inline struct wine_rb_entry *wine_rb_postorder_next(struct wine_rb_entry
 /* iterate through the tree using a tree entry */
 #define WINE_RB_FOR_EACH_ENTRY(elem, tree, type, field) \
     for ((elem) = WINE_RB_ENTRY_VALUE(wine_rb_head((tree)->root), type, field); \
-         &(elem)->field; \
+         (elem) != WINE_RB_ENTRY_VALUE(0, type, field); \
          (elem) = WINE_RB_ENTRY_VALUE(wine_rb_next(&elem->field), type, field))
 
+/* iterate through the tree using using postorder, making it safe to free the entry */
+#define WINE_RB_FOR_EACH_DESTRUCTOR(cursor, cursor2, tree) \
+    for ((cursor) = wine_rb_postorder_head((tree)->root); \
+         (cursor) && (((cursor2) = wine_rb_postorder_next(cursor)) || 1); \
+         (cursor) = (cursor2))
+
+/* iterate through the tree using a tree entry and postorder, making it safe to free the entry */
+#define WINE_RB_FOR_EACH_ENTRY_DESTRUCTOR(elem, elem2, tree, type, field) \
+    for ((elem) = WINE_RB_ENTRY_VALUE(wine_rb_postorder_head((tree)->root), type, field); \
+         (elem) != WINE_RB_ENTRY_VALUE(0, type, field) \
+             && (((elem2) = WINE_RB_ENTRY_VALUE(wine_rb_postorder_next(&(elem)->field), type, field)) || 1); \
+         (elem) = (elem2))
+
 
 static inline void wine_rb_postorder(struct wine_rb_tree *tree, wine_rb_traverse_func_t *callback, void *context)
 {
     struct wine_rb_entry *iter, *next;
-
-    for (iter = wine_rb_postorder_head(tree->root); iter; iter = next)
-    {
-        next = wine_rb_postorder_next(iter);
-        callback(iter, context);
-    }
+    WINE_RB_FOR_EACH_DESTRUCTOR(iter, next, tree) callback(iter, context);
 }
 
 static inline void wine_rb_init(struct wine_rb_tree *tree, wine_rb_compare_func_t compare)