2018-06-12 01:56:21 +00:00
|
|
|
/* gtkrbtree.c
|
|
|
|
* Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
#include "gtkrbtreeprivate.h"
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
#include "gtkdebug.h"
|
|
|
|
|
2019-01-20 05:06:18 +00:00
|
|
|
/* Define the following to print adds and removals to stdout.
|
|
|
|
* The format of the printout will be suitable for addition as a new test to
|
|
|
|
* testsuite/gtk/rbtree-crash.c
|
|
|
|
* by just grepping the printouts from the relevant rbtree.
|
|
|
|
*
|
|
|
|
* This is meant to be a trivial way to add rbtree tests to the testsuite.
|
|
|
|
*/
|
|
|
|
#undef DUMP_MODIFICATION
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
typedef struct _GtkRbNode GtkRbNode;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
struct _GtkRbTree
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
guint ref_count;
|
|
|
|
|
|
|
|
gsize element_size;
|
|
|
|
gsize augment_size;
|
2019-01-07 04:37:32 +00:00
|
|
|
GtkRbTreeAugmentFunc augment_func;
|
2018-06-12 01:56:21 +00:00
|
|
|
GDestroyNotify clear_func;
|
|
|
|
GDestroyNotify clear_augment_func;
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
GtkRbNode *root;
|
2018-06-12 01:56:21 +00:00
|
|
|
};
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
struct _GtkRbNode
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
guint red :1;
|
|
|
|
guint dirty :1;
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
GtkRbNode *left;
|
|
|
|
GtkRbNode *right;
|
2019-01-14 00:55:23 +00:00
|
|
|
/* The difference between tree and parent here is that we OR the tree with 1 and because
|
|
|
|
* pointers are always multiples of 4, we can know if we've stored a parent or the tree here */
|
|
|
|
union {
|
|
|
|
gpointer parent_or_tree;
|
|
|
|
GtkRbNode *parent;
|
|
|
|
GtkRbTree *tree;
|
|
|
|
};
|
2018-06-12 01:56:21 +00:00
|
|
|
};
|
|
|
|
|
2020-03-05 05:53:26 +00:00
|
|
|
#define NODE_FROM_POINTER(ptr) ((GtkRbNode *) (((guchar *) (ptr)) - sizeof (GtkRbNode)))
|
|
|
|
#define NODE_TO_POINTER(node) ((node) ? ((gpointer) (((guchar *) (node)) + sizeof (GtkRbNode))) : NULL)
|
2019-01-07 04:37:32 +00:00
|
|
|
#define NODE_TO_AUG_POINTER(tree, node) ((gpointer) ((node) ? (((guchar *) (node)) + sizeof (GtkRbNode) + (tree)->element_size) : NULL))
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-14 00:55:23 +00:00
|
|
|
static inline gboolean
|
|
|
|
is_root (GtkRbNode *node)
|
|
|
|
{
|
|
|
|
return GPOINTER_TO_SIZE (node->parent_or_tree) & 1 ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
2019-01-14 00:44:07 +00:00
|
|
|
static inline GtkRbNode *
|
|
|
|
parent (GtkRbNode *node)
|
|
|
|
{
|
2019-01-14 00:55:23 +00:00
|
|
|
if (is_root (node))
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return node->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkRbTree *
|
|
|
|
tree (GtkRbNode *node)
|
|
|
|
{
|
|
|
|
while (!is_root (node))
|
|
|
|
node = parent (node);
|
|
|
|
|
|
|
|
return GSIZE_TO_POINTER (GPOINTER_TO_SIZE (node->tree) & ~1);
|
2019-01-14 00:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_parent (GtkRbTree *tree,
|
|
|
|
GtkRbNode *node,
|
|
|
|
GtkRbNode *new_parent)
|
|
|
|
{
|
|
|
|
|
2019-01-14 00:55:23 +00:00
|
|
|
if (new_parent != NULL)
|
|
|
|
{
|
|
|
|
node->parent = new_parent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->tree = GSIZE_TO_POINTER (GPOINTER_TO_SIZE (tree) | 1);
|
|
|
|
tree->root = node;
|
|
|
|
}
|
2019-01-14 00:44:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 01:56:21 +00:00
|
|
|
static inline gsize
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_get_size (GtkRbTree *tree)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
return sizeof (GtkRbNode) + tree->element_size + tree->augment_size;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
static GtkRbNode *
|
|
|
|
gtk_rb_node_new (GtkRbTree *tree)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
GtkRbNode *result;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
result = g_slice_alloc0 (gtk_rb_node_get_size (tree));
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
result->red = TRUE;
|
|
|
|
result->dirty = TRUE;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_free (GtkRbTree *tree,
|
|
|
|
GtkRbNode *node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
if (tree->clear_func)
|
|
|
|
tree->clear_func (NODE_TO_POINTER (node));
|
|
|
|
if (tree->clear_augment_func)
|
|
|
|
tree->clear_augment_func (NODE_TO_AUG_POINTER (tree, node));
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
g_slice_free1 (gtk_rb_node_get_size (tree), node);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_free_deep (GtkRbTree *tree,
|
|
|
|
GtkRbNode *node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
GtkRbNode *right = node->right;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
if (node->left)
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_free_deep (tree, node->left);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_free (tree, node);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
if (right)
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_free_deep (tree, right);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_mark_dirty (GtkRbNode *node,
|
|
|
|
gboolean mark_parent)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
if (node->dirty)
|
|
|
|
return;
|
|
|
|
|
|
|
|
node->dirty = TRUE;
|
|
|
|
|
2019-01-14 00:44:07 +00:00
|
|
|
if (mark_parent && parent (node))
|
|
|
|
gtk_rb_node_mark_dirty (parent (node), TRUE);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_clean (GtkRbTree *tree,
|
|
|
|
GtkRbNode *node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
if (!node->dirty)
|
|
|
|
return;
|
|
|
|
|
|
|
|
node->dirty = FALSE;
|
|
|
|
if (tree->augment_func)
|
|
|
|
tree->augment_func (tree,
|
|
|
|
NODE_TO_AUG_POINTER (tree, node),
|
|
|
|
NODE_TO_POINTER (node),
|
|
|
|
NODE_TO_POINTER (node->left),
|
|
|
|
NODE_TO_POINTER (node->right));
|
|
|
|
}
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
static GtkRbNode *
|
|
|
|
gtk_rb_node_get_first (GtkRbNode *node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
while (node->left)
|
|
|
|
node = node->left;
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
static GtkRbNode *
|
|
|
|
gtk_rb_node_get_last (GtkRbNode *node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
while (node->right)
|
|
|
|
node = node->right;
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
static GtkRbNode *
|
|
|
|
gtk_rb_node_get_previous (GtkRbNode *node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
GtkRbNode *p;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
if (node->left)
|
2019-01-07 04:37:32 +00:00
|
|
|
return gtk_rb_node_get_last (node->left);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-14 00:44:07 +00:00
|
|
|
for (p = parent (node); p != NULL; p = parent (node))
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
if (p->right == node)
|
|
|
|
return p;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-14 00:44:07 +00:00
|
|
|
node = p;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
static GtkRbNode *
|
|
|
|
gtk_rb_node_get_next (GtkRbNode *node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
GtkRbNode *p;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
if (node->right)
|
2019-01-07 04:37:32 +00:00
|
|
|
return gtk_rb_node_get_first (node->right);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-14 00:44:07 +00:00
|
|
|
for (p = parent (node); p != NULL; p = parent (node))
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
if (p->left == node)
|
|
|
|
return p;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-14 00:44:07 +00:00
|
|
|
node = p;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-01-20 05:06:18 +00:00
|
|
|
#ifdef DUMP_MODIFICATION
|
|
|
|
static guint
|
|
|
|
position (GtkRbTree *tree,
|
|
|
|
GtkRbNode *node)
|
|
|
|
{
|
|
|
|
GtkRbNode *n;
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
for (n = gtk_rb_node_get_first (tree->root);
|
|
|
|
n != node;
|
|
|
|
n = gtk_rb_node_get_next (n))
|
|
|
|
i++;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-06-12 01:56:21 +00:00
|
|
|
static void
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_rotate_left (GtkRbTree *tree,
|
|
|
|
GtkRbNode *node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
GtkRbNode *right, *p;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
right = node->right;
|
2019-01-14 00:44:07 +00:00
|
|
|
p = parent (node);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
node->right = right->left;
|
|
|
|
if (right->left)
|
2019-01-14 00:44:07 +00:00
|
|
|
set_parent (tree, right->left, node);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-14 00:44:07 +00:00
|
|
|
set_parent (tree, right, p);
|
|
|
|
if (p)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
if (node == p->left)
|
|
|
|
p->left = right;
|
2018-06-12 01:56:21 +00:00
|
|
|
else
|
2019-01-14 00:44:07 +00:00
|
|
|
p->right = right;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
right->left = node;
|
2019-01-14 00:44:07 +00:00
|
|
|
set_parent (tree, node, right);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_mark_dirty (node, FALSE);
|
|
|
|
gtk_rb_node_mark_dirty (right, FALSE);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_rotate_right (GtkRbTree *tree,
|
|
|
|
GtkRbNode *node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
GtkRbNode *left, *p;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
left = node->left;
|
2019-01-14 00:44:07 +00:00
|
|
|
p = parent (node);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
node->left = left->right;
|
|
|
|
if (left->right)
|
2019-01-14 00:44:07 +00:00
|
|
|
set_parent (tree, left->right, node);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-14 00:44:07 +00:00
|
|
|
set_parent (tree, left, p);
|
|
|
|
if (p)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
if (node == p->right)
|
|
|
|
p->right = left;
|
2018-06-12 01:56:21 +00:00
|
|
|
else
|
2019-01-14 00:44:07 +00:00
|
|
|
p->left = left;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* link node and left */
|
|
|
|
left->right = node;
|
2019-01-14 00:44:07 +00:00
|
|
|
set_parent (tree, node, left);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_mark_dirty (node, FALSE);
|
|
|
|
gtk_rb_node_mark_dirty (left, FALSE);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2019-01-07 04:37:32 +00:00
|
|
|
is_red (GtkRbNode *node_or_null)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
if (node_or_null == NULL)
|
|
|
|
return FALSE;
|
|
|
|
else
|
|
|
|
return node_or_null->red;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline gboolean
|
2019-01-07 04:37:32 +00:00
|
|
|
is_black (GtkRbNode *node_or_null)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
return !is_red (node_or_null);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-07 04:37:32 +00:00
|
|
|
set_black (GtkRbNode *node_or_null)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
if (node_or_null == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
node_or_null->red = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-07 04:37:32 +00:00
|
|
|
set_red (GtkRbNode *node_or_null)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
if (node_or_null == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
node_or_null->red = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_insert_fixup (GtkRbTree *tree,
|
|
|
|
GtkRbNode *node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
GtkRbNode *p;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
/* check Red-Black properties */
|
2019-01-14 00:44:07 +00:00
|
|
|
for (p = parent (node);
|
|
|
|
p && is_red (p);
|
|
|
|
p = parent (node))
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
GtkRbNode *pp = parent (p);
|
|
|
|
|
2018-06-12 01:56:21 +00:00
|
|
|
/* we have a violation */
|
2019-01-14 00:44:07 +00:00
|
|
|
g_assert (pp);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-14 00:44:07 +00:00
|
|
|
if (p == pp->left)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
GtkRbNode *uncle = pp->right;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
if (is_red (uncle))
|
|
|
|
{
|
|
|
|
/* uncle is red */
|
2019-01-14 00:44:07 +00:00
|
|
|
set_black (p);
|
2018-06-12 01:56:21 +00:00
|
|
|
set_black (uncle);
|
2019-01-14 00:44:07 +00:00
|
|
|
set_red (pp);
|
|
|
|
node = pp;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* uncle is black */
|
2019-01-14 00:44:07 +00:00
|
|
|
if (node == p->right)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
/* make node a left child */
|
2020-03-05 05:51:32 +00:00
|
|
|
gtk_rb_node_rotate_left (tree, p);
|
|
|
|
p = node;
|
|
|
|
node = p->left;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
/* recolor and rotate */
|
2019-01-14 00:44:07 +00:00
|
|
|
set_black (p);
|
|
|
|
set_red (pp);
|
|
|
|
gtk_rb_node_rotate_right (tree, pp);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* mirror image of above code */
|
2019-01-14 00:44:07 +00:00
|
|
|
GtkRbNode *uncle = pp->left;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
if (is_red (uncle))
|
|
|
|
{
|
|
|
|
/* uncle is red */
|
2019-01-14 00:44:07 +00:00
|
|
|
set_black (p);
|
2018-06-12 01:56:21 +00:00
|
|
|
set_black (uncle);
|
2019-01-14 00:44:07 +00:00
|
|
|
set_red (pp);
|
|
|
|
node = pp;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* uncle is black */
|
2019-01-14 00:44:07 +00:00
|
|
|
if (node == p->left)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2020-03-05 05:51:32 +00:00
|
|
|
gtk_rb_node_rotate_right (tree, p);
|
|
|
|
p = node;
|
|
|
|
node = p->right;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
2019-01-14 00:44:07 +00:00
|
|
|
set_black (p);
|
|
|
|
set_red (pp);
|
|
|
|
gtk_rb_node_rotate_left (tree, pp);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_black (tree->root);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_remove_node_fixup (GtkRbTree *tree,
|
|
|
|
GtkRbNode *node,
|
2019-01-14 00:44:07 +00:00
|
|
|
GtkRbNode *p)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2018-09-19 02:26:37 +00:00
|
|
|
while (node != tree->root && is_black (node))
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
if (node == p->left)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
GtkRbNode *w = p->right;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
if (is_red (w))
|
|
|
|
{
|
|
|
|
set_black (w);
|
2019-01-14 00:44:07 +00:00
|
|
|
set_red (p);
|
|
|
|
gtk_rb_node_rotate_left (tree, p);
|
|
|
|
w = p->right;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
2020-03-05 06:02:24 +00:00
|
|
|
g_assert (w);
|
2018-06-12 01:56:21 +00:00
|
|
|
if (is_black (w->left) && is_black (w->right))
|
|
|
|
{
|
|
|
|
set_red (w);
|
2019-01-14 00:44:07 +00:00
|
|
|
node = p;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (is_black (w->right))
|
|
|
|
{
|
|
|
|
set_black (w->left);
|
|
|
|
set_red (w);
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_rotate_right (tree, w);
|
2019-01-14 00:44:07 +00:00
|
|
|
w = p->right;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
2019-01-14 00:44:07 +00:00
|
|
|
w->red = p->red;
|
|
|
|
set_black (p);
|
2018-06-12 01:56:21 +00:00
|
|
|
set_black (w->right);
|
2019-01-14 00:44:07 +00:00
|
|
|
gtk_rb_node_rotate_left (tree, p);
|
2018-06-12 01:56:21 +00:00
|
|
|
node = tree->root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
GtkRbNode *w = p->left;
|
2018-06-12 01:56:21 +00:00
|
|
|
if (is_red (w))
|
|
|
|
{
|
|
|
|
set_black (w);
|
2019-01-14 00:44:07 +00:00
|
|
|
set_red (p);
|
|
|
|
gtk_rb_node_rotate_right (tree, p);
|
|
|
|
w = p->left;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
2020-03-05 06:02:24 +00:00
|
|
|
g_assert (w);
|
2018-06-12 01:56:21 +00:00
|
|
|
if (is_black (w->right) && is_black (w->left))
|
|
|
|
{
|
|
|
|
set_red (w);
|
2019-01-14 00:44:07 +00:00
|
|
|
node = p;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (is_black (w->left))
|
|
|
|
{
|
|
|
|
set_black (w->right);
|
|
|
|
set_red (w);
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_rotate_left (tree, w);
|
2019-01-14 00:44:07 +00:00
|
|
|
w = p->left;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
2019-01-14 00:44:07 +00:00
|
|
|
w->red = p->red;
|
|
|
|
set_black (p);
|
2018-06-12 01:56:21 +00:00
|
|
|
set_black (w->left);
|
2019-01-14 00:44:07 +00:00
|
|
|
gtk_rb_node_rotate_right (tree, p);
|
2018-06-12 01:56:21 +00:00
|
|
|
node = tree->root;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 00:44:07 +00:00
|
|
|
p = parent (node);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set_black (node);
|
|
|
|
}
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
GtkRbTree *
|
|
|
|
gtk_rb_tree_new_for_size (gsize element_size,
|
|
|
|
gsize augment_size,
|
|
|
|
GtkRbTreeAugmentFunc augment_func,
|
|
|
|
GDestroyNotify clear_func,
|
|
|
|
GDestroyNotify clear_augment_func)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
GtkRbTree *tree;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
tree = g_slice_new0 (GtkRbTree);
|
2018-06-12 01:56:21 +00:00
|
|
|
tree->ref_count = 1;
|
|
|
|
|
|
|
|
tree->element_size = element_size;
|
|
|
|
tree->augment_size = augment_size;
|
|
|
|
tree->augment_func = augment_func;
|
|
|
|
tree->clear_func = clear_func;
|
|
|
|
tree->clear_augment_func = clear_augment_func;
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
GtkRbTree *
|
|
|
|
gtk_rb_tree_ref (GtkRbTree *tree)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
tree->ref_count++;
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_unref (GtkRbTree *tree)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
tree->ref_count--;
|
|
|
|
if (tree->ref_count > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (tree->root)
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_free_deep (tree, tree->root);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
g_slice_free (GtkRbTree, tree);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_get_first (GtkRbTree *tree)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
if (tree->root == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
return NODE_TO_POINTER (gtk_rb_node_get_first (tree->root));
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_get_last (GtkRbTree *tree)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
if (tree->root == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
return NODE_TO_POINTER (gtk_rb_node_get_last (tree->root));
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2019-01-14 01:12:02 +00:00
|
|
|
gtk_rb_tree_node_get_previous (gpointer node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
return NODE_TO_POINTER (gtk_rb_node_get_previous (NODE_FROM_POINTER (node)));
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2019-01-14 01:12:02 +00:00
|
|
|
gtk_rb_tree_node_get_next (gpointer node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
return NODE_TO_POINTER (gtk_rb_node_get_next (NODE_FROM_POINTER (node)));
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_get_root (GtkRbTree *tree)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
return NODE_TO_POINTER (tree->root);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2019-01-14 01:12:02 +00:00
|
|
|
gtk_rb_tree_node_get_parent (gpointer node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
return NODE_TO_POINTER (parent (NODE_FROM_POINTER (node)));
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2019-01-14 01:12:02 +00:00
|
|
|
gtk_rb_tree_node_get_left (gpointer node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
return NODE_TO_POINTER (NODE_FROM_POINTER (node)->left);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2019-01-14 01:12:02 +00:00
|
|
|
gtk_rb_tree_node_get_right (gpointer node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
|
|
|
return NODE_TO_POINTER (NODE_FROM_POINTER (node)->right);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_get_augment (GtkRbTree *tree,
|
|
|
|
gpointer node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
GtkRbNode *rbnode = NODE_FROM_POINTER (node);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_clean (tree, rbnode);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
return NODE_TO_AUG_POINTER (tree, rbnode);
|
|
|
|
}
|
|
|
|
|
2019-01-14 00:55:23 +00:00
|
|
|
GtkRbTree *
|
|
|
|
gtk_rb_tree_node_get_tree (gpointer node)
|
|
|
|
{
|
|
|
|
return tree (NODE_FROM_POINTER (node));
|
|
|
|
}
|
|
|
|
|
2018-06-12 01:56:21 +00:00
|
|
|
void
|
2019-01-14 01:12:02 +00:00
|
|
|
gtk_rb_tree_node_mark_dirty (gpointer node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_mark_dirty (NODE_FROM_POINTER (node), TRUE);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_insert_before (GtkRbTree *tree,
|
|
|
|
gpointer node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
GtkRbNode *result;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (tree->root == NULL)
|
|
|
|
{
|
2019-01-20 05:06:18 +00:00
|
|
|
#ifdef DUMP_MODIFICATION
|
|
|
|
g_print ("add (tree, 0); /* 0x%p */\n", tree);
|
|
|
|
#endif /* DUMP_MODIFICATION */
|
|
|
|
|
2018-06-12 01:56:21 +00:00
|
|
|
g_assert (node == NULL);
|
2019-01-20 05:06:18 +00:00
|
|
|
|
|
|
|
result = gtk_rb_node_new (tree);
|
2018-06-12 01:56:21 +00:00
|
|
|
tree->root = result;
|
|
|
|
}
|
|
|
|
else if (node == NULL)
|
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
return gtk_rb_tree_insert_after (tree, gtk_rb_tree_get_last (tree));
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
GtkRbNode *current = NODE_FROM_POINTER (node);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-20 05:06:18 +00:00
|
|
|
#ifdef DUMP_MODIFICATION
|
|
|
|
g_print ("add (tree, %u); /* 0x%p */\n", position (tree, current), tree);
|
|
|
|
#endif /* DUMP_MODIFICATION */
|
|
|
|
|
|
|
|
/* setup new node */
|
|
|
|
result = gtk_rb_node_new (tree);
|
|
|
|
|
2018-06-12 01:56:21 +00:00
|
|
|
if (current->left)
|
|
|
|
{
|
2019-01-07 04:37:32 +00:00
|
|
|
current = gtk_rb_node_get_last (current->left);
|
2018-06-12 01:56:21 +00:00
|
|
|
current->right = result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
current->left = result;
|
|
|
|
}
|
2019-01-14 00:44:07 +00:00
|
|
|
set_parent (tree, result, current);
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_mark_dirty (current, TRUE);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_insert_fixup (tree, result);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
return NODE_TO_POINTER (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpointer
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_insert_after (GtkRbTree *tree,
|
|
|
|
gpointer node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-20 05:06:18 +00:00
|
|
|
GtkRbNode *current, *result;
|
|
|
|
|
|
|
|
if (node == NULL)
|
|
|
|
return gtk_rb_tree_insert_before (tree, gtk_rb_tree_get_first (tree));
|
|
|
|
|
|
|
|
current = NODE_FROM_POINTER (node);
|
|
|
|
|
|
|
|
#ifdef DUMP_MODIFICATION
|
|
|
|
g_print ("add (tree, %u); /* 0x%p */\n", position (tree, current) + 1, tree);
|
|
|
|
#endif /* DUMP_MODIFICATION */
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
/* setup new node */
|
2019-01-07 04:37:32 +00:00
|
|
|
result = gtk_rb_node_new (tree);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-20 05:06:18 +00:00
|
|
|
if (current->right)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-20 05:06:18 +00:00
|
|
|
current = gtk_rb_node_get_first (current->right);
|
|
|
|
current->left = result;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-20 05:06:18 +00:00
|
|
|
current->right = result;
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
2019-01-20 05:06:18 +00:00
|
|
|
set_parent (tree, result, current);
|
|
|
|
gtk_rb_node_mark_dirty (current, TRUE);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_insert_fixup (tree, result);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
return NODE_TO_POINTER (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_remove (GtkRbTree *tree,
|
|
|
|
gpointer node)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
GtkRbNode *x, *y, *p, *real_node;
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
real_node = NODE_FROM_POINTER (node);
|
2019-01-20 05:06:18 +00:00
|
|
|
|
|
|
|
#ifdef DUMP_MODIFICATION
|
|
|
|
g_print ("delete (tree, %u); /* 0x%p */\n", position (tree, real_node), tree);
|
|
|
|
#endif /* DUMP_MODIFICATION */
|
|
|
|
|
2018-06-12 01:56:21 +00:00
|
|
|
y = real_node;
|
|
|
|
if (y->left && y->right)
|
|
|
|
{
|
|
|
|
y = y->right;
|
|
|
|
|
|
|
|
while (y->left)
|
|
|
|
y = y->left;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* x is y's only child, or nil */
|
|
|
|
if (y->left)
|
|
|
|
x = y->left;
|
|
|
|
else
|
|
|
|
x = y->right;
|
|
|
|
|
|
|
|
/* remove y from the parent chain */
|
2019-01-14 00:44:07 +00:00
|
|
|
p = parent (y);
|
2018-06-12 01:56:21 +00:00
|
|
|
if (x != NULL)
|
2019-01-14 00:44:07 +00:00
|
|
|
set_parent (tree, x, p);
|
|
|
|
if (p)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
if (y == p->left)
|
|
|
|
p->left = x;
|
2018-06-12 01:56:21 +00:00
|
|
|
else
|
2019-01-14 00:44:07 +00:00
|
|
|
p->right = x;
|
|
|
|
gtk_rb_node_mark_dirty (p, TRUE);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
2019-01-14 17:42:18 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (x == NULL)
|
|
|
|
tree->root = NULL;
|
|
|
|
}
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
/* We need to clean up the validity of the tree.
|
|
|
|
*/
|
2018-09-19 02:26:37 +00:00
|
|
|
if (is_black (y))
|
2019-01-14 00:44:07 +00:00
|
|
|
gtk_rb_tree_remove_node_fixup (tree, x, p);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
if (y != real_node)
|
|
|
|
{
|
|
|
|
/* Move the node over */
|
|
|
|
if (is_red (real_node) != is_red (y))
|
|
|
|
y->red = !y->red;
|
|
|
|
|
|
|
|
y->left = real_node->left;
|
|
|
|
if (y->left)
|
2019-01-14 00:44:07 +00:00
|
|
|
set_parent (tree, y->left, y);
|
2018-06-12 01:56:21 +00:00
|
|
|
y->right = real_node->right;
|
|
|
|
if (y->right)
|
2019-01-14 00:44:07 +00:00
|
|
|
set_parent (tree, y->right, y);
|
|
|
|
p = parent (real_node);
|
|
|
|
set_parent (tree, y, p);
|
|
|
|
if (p)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-14 00:44:07 +00:00
|
|
|
if (p->left == real_node)
|
|
|
|
p->left = y;
|
2018-06-12 01:56:21 +00:00
|
|
|
else
|
2019-01-14 00:44:07 +00:00
|
|
|
p->right = y;
|
|
|
|
gtk_rb_node_mark_dirty (p, TRUE);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_mark_dirty (y, TRUE);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_free (tree, real_node);
|
2018-06-12 01:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_tree_remove_all (GtkRbTree *tree)
|
2018-06-12 01:56:21 +00:00
|
|
|
{
|
2019-01-20 05:06:18 +00:00
|
|
|
#ifdef DUMP_MODIFICATION
|
|
|
|
g_print ("delete_all (tree); /* 0x%p */\n", tree);
|
|
|
|
#endif /* DUMP_MODIFICATION */
|
|
|
|
|
2018-06-12 01:56:21 +00:00
|
|
|
if (tree->root)
|
2019-01-07 04:37:32 +00:00
|
|
|
gtk_rb_node_free_deep (tree, tree->root);
|
2018-06-12 01:56:21 +00:00
|
|
|
|
|
|
|
tree->root = NULL;
|
|
|
|
}
|
|
|
|
|