2017-06-16 13:38:21 -07:00
|
|
|
/*
|
|
|
|
* Red-black search tree support
|
|
|
|
*
|
|
|
|
* Copyright 2009 Henri Verbeet
|
|
|
|
* Copyright 2009 Andrew Riedi
|
|
|
|
* Copyright 2016 Jacek Caban for CodeWeavers
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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 GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __WINE_WINE_RBTREE_H
|
|
|
|
#define __WINE_WINE_RBTREE_H
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
#define RB_ENTRY_VALUE(element, type, field) \
|
2017-06-16 13:38:21 -07:00
|
|
|
((type *)((char *)(element) - offsetof(type, field)))
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
struct rb_entry
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
struct rb_entry *parent;
|
|
|
|
struct rb_entry *left;
|
|
|
|
struct rb_entry *right;
|
2017-06-16 13:38:21 -07:00
|
|
|
unsigned int flags;
|
|
|
|
};
|
|
|
|
|
2018-10-18 07:14:31 -07:00
|
|
|
typedef int (*rb_compare_func)(const void *key, const struct rb_entry *entry);
|
2017-06-16 13:38:21 -07:00
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
struct rb_tree
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2018-10-18 07:14:31 -07:00
|
|
|
rb_compare_func compare;
|
2017-06-16 13:38:21 -07:00
|
|
|
struct rb_entry *root;
|
2017-06-16 13:38:21 -07:00
|
|
|
};
|
|
|
|
|
2018-10-18 07:14:31 -07:00
|
|
|
typedef void (rb_traverse_func)(struct rb_entry *entry, void *context);
|
2017-06-16 13:38:21 -07:00
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
#define RB_FLAG_RED 0x1
|
2017-06-16 13:38:21 -07:00
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static inline int rb_is_red(struct rb_entry *entry)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
return entry && (entry->flags & RB_FLAG_RED);
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static inline void rb_rotate_left(struct rb_tree *tree, struct rb_entry *e)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
struct rb_entry *right = e->right;
|
2017-06-16 13:38:21 -07:00
|
|
|
|
|
|
|
if (!e->parent)
|
|
|
|
tree->root = right;
|
|
|
|
else if (e->parent->left == e)
|
|
|
|
e->parent->left = right;
|
|
|
|
else
|
|
|
|
e->parent->right = right;
|
|
|
|
|
|
|
|
e->right = right->left;
|
|
|
|
if (e->right) e->right->parent = e;
|
|
|
|
right->left = e;
|
|
|
|
right->parent = e->parent;
|
|
|
|
e->parent = right;
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static inline void rb_rotate_right(struct rb_tree *tree, struct rb_entry *e)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
struct rb_entry *left = e->left;
|
2017-06-16 13:38:21 -07:00
|
|
|
|
|
|
|
if (!e->parent)
|
|
|
|
tree->root = left;
|
|
|
|
else if (e->parent->left == e)
|
|
|
|
e->parent->left = left;
|
|
|
|
else
|
|
|
|
e->parent->right = left;
|
|
|
|
|
|
|
|
e->left = left->right;
|
|
|
|
if (e->left) e->left->parent = e;
|
|
|
|
left->right = e;
|
|
|
|
left->parent = e->parent;
|
|
|
|
e->parent = left;
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static inline void rb_flip_color(struct rb_entry *entry)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
entry->flags ^= RB_FLAG_RED;
|
|
|
|
entry->left->flags ^= RB_FLAG_RED;
|
|
|
|
entry->right->flags ^= RB_FLAG_RED;
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static inline struct rb_entry *rb_head(struct rb_entry *iter)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
|
|
|
if (!iter) return NULL;
|
|
|
|
while (iter->left) iter = iter->left;
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static inline struct rb_entry *rb_next(struct rb_entry *iter)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
if (iter->right) return rb_head(iter->right);
|
2017-06-16 13:38:21 -07:00
|
|
|
while (iter->parent && iter->parent->right == iter) iter = iter->parent;
|
|
|
|
return iter->parent;
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static inline struct rb_entry *rb_postorder_head(struct rb_entry *iter)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
|
|
|
if (!iter) return NULL;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
while (iter->left) iter = iter->left;
|
|
|
|
if (!iter->right) return iter;
|
|
|
|
iter = iter->right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static inline struct rb_entry *rb_postorder_next(struct rb_entry *iter)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
|
|
|
if (!iter->parent) return NULL;
|
|
|
|
if (iter == iter->parent->right || !iter->parent->right) return iter->parent;
|
2017-06-16 13:38:21 -07:00
|
|
|
return rb_postorder_head(iter->parent->right);
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* iterate through the tree */
|
2020-07-31 08:59:54 -07:00
|
|
|
#define RB_FOR_EACH(cursor, tree) \
|
2017-06-16 13:38:21 -07:00
|
|
|
for ((cursor) = rb_head((tree)->root); (cursor); (cursor) = rb_next(cursor))
|
2017-06-16 13:38:21 -07:00
|
|
|
|
|
|
|
/* iterate through the tree using a tree entry */
|
2020-07-31 08:59:54 -07:00
|
|
|
#define RB_FOR_EACH_ENTRY(elem, tree, type, field) \
|
|
|
|
for ((elem) = RB_ENTRY_VALUE(rb_head((tree)->root), type, field); \
|
2021-12-17 09:48:17 -08:00
|
|
|
(elem) != RB_ENTRY_VALUE(0, type, field); \
|
2020-07-31 08:59:54 -07:00
|
|
|
(elem) = RB_ENTRY_VALUE(rb_next(&elem->field), type, field))
|
2017-06-16 13:38:21 -07:00
|
|
|
|
|
|
|
/* iterate through the tree using using postorder, making it safe to free the entry */
|
2020-07-31 08:59:54 -07:00
|
|
|
#define RB_FOR_EACH_DESTRUCTOR(cursor, cursor2, tree) \
|
2017-06-16 13:38:21 -07:00
|
|
|
for ((cursor) = rb_postorder_head((tree)->root); \
|
|
|
|
(cursor) && (((cursor2) = rb_postorder_next(cursor)) || 1); \
|
2017-06-16 13:38:21 -07:00
|
|
|
(cursor) = (cursor2))
|
|
|
|
|
|
|
|
/* iterate through the tree using a tree entry and postorder, making it safe to free the entry */
|
2020-07-31 08:59:54 -07:00
|
|
|
#define RB_FOR_EACH_ENTRY_DESTRUCTOR(elem, elem2, tree, type, field) \
|
|
|
|
for ((elem) = RB_ENTRY_VALUE(rb_postorder_head((tree)->root), type, field); \
|
2021-12-17 09:48:17 -08:00
|
|
|
(elem) != WINE_RB_ENTRY_VALUE(0, type, field) \
|
2020-07-31 08:59:54 -07:00
|
|
|
&& (((elem2) = RB_ENTRY_VALUE(rb_postorder_next(&(elem)->field), type, field)) || 1); \
|
2017-06-16 13:38:21 -07:00
|
|
|
(elem) = (elem2))
|
|
|
|
|
|
|
|
|
2018-10-18 07:14:31 -07:00
|
|
|
static inline void rb_postorder(struct rb_tree *tree, rb_traverse_func *callback, void *context)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
struct rb_entry *iter, *next;
|
2020-07-31 08:59:54 -07:00
|
|
|
RB_FOR_EACH_DESTRUCTOR(iter, next, tree) callback(iter, context);
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
2018-10-18 07:14:31 -07:00
|
|
|
static inline void rb_init(struct rb_tree *tree, rb_compare_func compare)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
|
|
|
tree->compare = compare;
|
|
|
|
tree->root = NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:14:31 -07:00
|
|
|
static inline void rb_for_each_entry(struct rb_tree *tree, rb_traverse_func *callback, void *context)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
struct rb_entry *iter;
|
2020-07-31 08:59:54 -07:00
|
|
|
RB_FOR_EACH(iter, tree) callback(iter, context);
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
2018-10-18 07:14:31 -07:00
|
|
|
static inline void rb_clear(struct rb_tree *tree, rb_traverse_func *callback, void *context)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
|
|
|
/* Note that we use postorder here because the callback will likely free the entry. */
|
2017-06-16 13:38:21 -07:00
|
|
|
if (callback) rb_postorder(tree, callback, context);
|
2017-06-16 13:38:21 -07:00
|
|
|
tree->root = NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-18 07:14:31 -07:00
|
|
|
static inline void rb_destroy(struct rb_tree *tree, rb_traverse_func *callback, void *context)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
rb_clear(tree, callback, context);
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static inline struct rb_entry *rb_get(const struct rb_tree *tree, const void *key)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
struct rb_entry *entry = tree->root;
|
2017-06-16 13:38:21 -07:00
|
|
|
while (entry)
|
|
|
|
{
|
|
|
|
int c = tree->compare(key, entry);
|
|
|
|
if (!c) return entry;
|
|
|
|
entry = c < 0 ? entry->left : entry->right;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static inline int rb_put(struct rb_tree *tree, const void *key, struct rb_entry *entry)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
struct rb_entry **iter = &tree->root, *parent = tree->root;
|
2017-06-16 13:38:21 -07:00
|
|
|
|
|
|
|
while (*iter)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
parent = *iter;
|
|
|
|
c = tree->compare(key, parent);
|
|
|
|
if (!c) return -1;
|
|
|
|
else if (c < 0) iter = &parent->left;
|
|
|
|
else iter = &parent->right;
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
entry->flags = RB_FLAG_RED;
|
2017-06-16 13:38:21 -07:00
|
|
|
entry->parent = parent;
|
|
|
|
entry->left = NULL;
|
|
|
|
entry->right = NULL;
|
|
|
|
*iter = entry;
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
while (rb_is_red(entry->parent))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
|
|
|
if (entry->parent == entry->parent->parent->left)
|
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
if (rb_is_red(entry->parent->parent->right))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
rb_flip_color(entry->parent->parent);
|
2017-06-16 13:38:21 -07:00
|
|
|
entry = entry->parent->parent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (entry == entry->parent->right)
|
|
|
|
{
|
|
|
|
entry = entry->parent;
|
2017-06-16 13:38:21 -07:00
|
|
|
rb_rotate_left(tree, entry);
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
2017-06-16 13:38:21 -07:00
|
|
|
entry->parent->flags &= ~RB_FLAG_RED;
|
|
|
|
entry->parent->parent->flags |= RB_FLAG_RED;
|
|
|
|
rb_rotate_right(tree, entry->parent->parent);
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
if (rb_is_red(entry->parent->parent->left))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
rb_flip_color(entry->parent->parent);
|
2017-06-16 13:38:21 -07:00
|
|
|
entry = entry->parent->parent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (entry == entry->parent->left)
|
|
|
|
{
|
|
|
|
entry = entry->parent;
|
2017-06-16 13:38:21 -07:00
|
|
|
rb_rotate_right(tree, entry);
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
2017-06-16 13:38:21 -07:00
|
|
|
entry->parent->flags &= ~RB_FLAG_RED;
|
|
|
|
entry->parent->parent->flags |= RB_FLAG_RED;
|
|
|
|
rb_rotate_left(tree, entry->parent->parent);
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
tree->root->flags &= ~RB_FLAG_RED;
|
2017-06-16 13:38:21 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static inline void rb_remove(struct rb_tree *tree, struct rb_entry *entry)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
struct rb_entry *iter, *child, *parent, *w;
|
2017-06-16 13:38:21 -07:00
|
|
|
int need_fixup;
|
|
|
|
|
|
|
|
if (entry->right && entry->left)
|
|
|
|
for(iter = entry->right; iter->left; iter = iter->left);
|
|
|
|
else
|
|
|
|
iter = entry;
|
|
|
|
|
|
|
|
child = iter->left ? iter->left : iter->right;
|
|
|
|
|
|
|
|
if (!iter->parent)
|
|
|
|
tree->root = child;
|
|
|
|
else if (iter == iter->parent->left)
|
|
|
|
iter->parent->left = child;
|
|
|
|
else
|
|
|
|
iter->parent->right = child;
|
|
|
|
|
|
|
|
if (child) child->parent = iter->parent;
|
|
|
|
parent = iter->parent;
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
need_fixup = !rb_is_red(iter);
|
2017-06-16 13:38:21 -07:00
|
|
|
|
|
|
|
if (entry != iter)
|
|
|
|
{
|
|
|
|
*iter = *entry;
|
|
|
|
if (!iter->parent)
|
|
|
|
tree->root = iter;
|
|
|
|
else if (entry == iter->parent->left)
|
|
|
|
iter->parent->left = iter;
|
|
|
|
else
|
|
|
|
iter->parent->right = iter;
|
|
|
|
|
|
|
|
if (iter->right) iter->right->parent = iter;
|
|
|
|
if (iter->left) iter->left->parent = iter;
|
|
|
|
if (parent == entry) parent = iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_fixup)
|
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
while (parent && !rb_is_red(child))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
|
|
|
if (child == parent->left)
|
|
|
|
{
|
|
|
|
w = parent->right;
|
2017-06-16 13:38:21 -07:00
|
|
|
if (rb_is_red(w))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
w->flags &= ~RB_FLAG_RED;
|
|
|
|
parent->flags |= RB_FLAG_RED;
|
|
|
|
rb_rotate_left(tree, parent);
|
2017-06-16 13:38:21 -07:00
|
|
|
w = parent->right;
|
|
|
|
}
|
2017-06-16 13:38:21 -07:00
|
|
|
if (rb_is_red(w->left) || rb_is_red(w->right))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
if (!rb_is_red(w->right))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
w->left->flags &= ~RB_FLAG_RED;
|
|
|
|
w->flags |= RB_FLAG_RED;
|
|
|
|
rb_rotate_right(tree, w);
|
2017-06-16 13:38:21 -07:00
|
|
|
w = parent->right;
|
|
|
|
}
|
2017-06-16 13:38:21 -07:00
|
|
|
w->flags = (w->flags & ~RB_FLAG_RED) | (parent->flags & RB_FLAG_RED);
|
|
|
|
parent->flags &= ~RB_FLAG_RED;
|
2017-06-16 13:38:21 -07:00
|
|
|
if (w->right)
|
2017-06-16 13:38:21 -07:00
|
|
|
w->right->flags &= ~RB_FLAG_RED;
|
|
|
|
rb_rotate_left(tree, parent);
|
2017-06-16 13:38:21 -07:00
|
|
|
child = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
w = parent->left;
|
2017-06-16 13:38:21 -07:00
|
|
|
if (rb_is_red(w))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
w->flags &= ~RB_FLAG_RED;
|
|
|
|
parent->flags |= RB_FLAG_RED;
|
|
|
|
rb_rotate_right(tree, parent);
|
2017-06-16 13:38:21 -07:00
|
|
|
w = parent->left;
|
|
|
|
}
|
2017-06-16 13:38:21 -07:00
|
|
|
if (rb_is_red(w->left) || rb_is_red(w->right))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
if (!rb_is_red(w->left))
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
w->right->flags &= ~RB_FLAG_RED;
|
|
|
|
w->flags |= RB_FLAG_RED;
|
|
|
|
rb_rotate_left(tree, w);
|
2017-06-16 13:38:21 -07:00
|
|
|
w = parent->left;
|
|
|
|
}
|
2017-06-16 13:38:21 -07:00
|
|
|
w->flags = (w->flags & ~RB_FLAG_RED) | (parent->flags & RB_FLAG_RED);
|
|
|
|
parent->flags &= ~RB_FLAG_RED;
|
2017-06-16 13:38:21 -07:00
|
|
|
if (w->left)
|
2017-06-16 13:38:21 -07:00
|
|
|
w->left->flags &= ~RB_FLAG_RED;
|
|
|
|
rb_rotate_right(tree, parent);
|
2017-06-16 13:38:21 -07:00
|
|
|
child = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 13:38:21 -07:00
|
|
|
w->flags |= RB_FLAG_RED;
|
2017-06-16 13:38:21 -07:00
|
|
|
child = parent;
|
|
|
|
parent = child->parent;
|
|
|
|
}
|
2017-06-16 13:38:21 -07:00
|
|
|
if (child) child->flags &= ~RB_FLAG_RED;
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
if (tree->root) tree->root->flags &= ~RB_FLAG_RED;
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
2017-06-16 13:38:21 -07:00
|
|
|
static inline void rb_remove_key(struct rb_tree *tree, const void *key)
|
2017-06-16 13:38:21 -07:00
|
|
|
{
|
2017-06-16 13:38:21 -07:00
|
|
|
struct rb_entry *entry = rb_get(tree, key);
|
|
|
|
if (entry) rb_remove(tree, entry);
|
2017-06-16 13:38:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __WINE_WINE_RBTREE_H */
|