nginx/src/core/ngx_rbtree.c

350 lines
8.5 KiB
C
Raw Normal View History

2003-12-04 22:53:00 +08:00
/*
* Copyright (C) Igor Sysoev
*/
2003-12-04 22:53:00 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
/*
* The code is based on the algorithm described in the "Introduction
* to Algorithms" by Cormen, Leiserson and Rivest.
*/
2003-12-05 15:11:46 +08:00
#define ngx_rbt_red(node) ((node)->color = 1)
#define ngx_rbt_black(node) ((node)->color = 0)
#define ngx_rbt_is_red(node) ((node)->color)
2003-12-04 22:53:00 +08:00
#define ngx_rbt_is_black(node) (!ngx_rbt_is_red(node))
2003-12-05 15:11:46 +08:00
#define ngx_rbt_copy_color(n1, n2) (n1->color = n2->color)
2003-12-04 22:53:00 +08:00
static ngx_inline void ngx_rbtree_left_rotate(ngx_rbtree_t **root,
ngx_rbtree_t *sentinel,
ngx_rbtree_t *node);
static ngx_inline void ngx_rbtree_right_rotate(ngx_rbtree_t **root,
ngx_rbtree_t *sentinel,
ngx_rbtree_t *node);
2003-12-04 22:53:00 +08:00
2003-12-06 01:07:27 +08:00
void ngx_rbtree_insert(ngx_rbtree_t **root, ngx_rbtree_t *sentinel,
ngx_rbtree_t *node)
2003-12-04 22:53:00 +08:00
{
ngx_rbtree_t *temp;
/* a binary tree insert */
2003-12-06 01:07:27 +08:00
if (*root == sentinel) {
2003-12-19 16:15:11 +08:00
node->parent = NULL;
2003-12-06 01:07:27 +08:00
node->left = sentinel;
node->right = sentinel;
2003-12-04 22:53:00 +08:00
ngx_rbt_black(node);
*root = node;
return;
}
temp = *root;
for ( ;; ) {
if (node->key < temp->key) {
2003-12-06 01:07:27 +08:00
if (temp->left == sentinel) {
2003-12-04 22:53:00 +08:00
temp->left = node;
break;
}
temp = temp->left;
continue;
}
2003-12-06 01:07:27 +08:00
if (temp->right == sentinel) {
2003-12-04 22:53:00 +08:00
temp->right = node;
break;
}
temp = temp->right;
continue;
}
node->parent = temp;
2003-12-06 01:07:27 +08:00
node->left = sentinel;
node->right = sentinel;
2003-12-04 22:53:00 +08:00
/* re-balance tree */
ngx_rbt_red(node);
2003-12-19 16:15:11 +08:00
while (node != *root && ngx_rbt_is_red(node->parent)) {
2003-12-04 22:53:00 +08:00
if (node->parent == node->parent->parent->left) {
temp = node->parent->parent->right;
if (ngx_rbt_is_red(temp)) {
ngx_rbt_black(node->parent);
ngx_rbt_black(temp);
ngx_rbt_red(node->parent->parent);
node = node->parent->parent;
} else {
if (node == node->parent->right) {
node = node->parent;
2003-12-06 01:07:27 +08:00
ngx_rbtree_left_rotate(root, sentinel, node);
2003-12-04 22:53:00 +08:00
}
ngx_rbt_black(node->parent);
ngx_rbt_red(node->parent->parent);
2003-12-06 01:07:27 +08:00
ngx_rbtree_right_rotate(root, sentinel, node->parent->parent);
2003-12-04 22:53:00 +08:00
}
} else {
temp = node->parent->parent->left;
if (ngx_rbt_is_red(temp)) {
ngx_rbt_black(node->parent);
ngx_rbt_black(temp);
ngx_rbt_red(node->parent->parent);
node = node->parent->parent;
} else {
if (node == node->parent->left) {
node = node->parent;
2003-12-06 01:07:27 +08:00
ngx_rbtree_right_rotate(root, sentinel, node);
2003-12-04 22:53:00 +08:00
}
ngx_rbt_black(node->parent);
ngx_rbt_red(node->parent->parent);
2003-12-06 01:07:27 +08:00
ngx_rbtree_left_rotate(root, sentinel, node->parent->parent);
2003-12-04 22:53:00 +08:00
}
}
}
ngx_rbt_black(*root);
}
2003-12-06 01:07:27 +08:00
void ngx_rbtree_delete(ngx_rbtree_t **root, ngx_rbtree_t *sentinel,
ngx_rbtree_t *node)
2003-12-04 22:53:00 +08:00
{
2003-12-19 20:45:27 +08:00
ngx_int_t is_red;
2003-12-04 22:53:00 +08:00
ngx_rbtree_t *subst, *temp, *w;
/* a binary tree delete */
2003-12-19 16:15:11 +08:00
if (node->left == sentinel) {
temp = node->right;
2003-12-04 22:53:00 +08:00
subst = node;
2003-12-19 16:15:11 +08:00
} else if (node->right == sentinel) {
temp = node->left;
subst = node;
2003-12-04 22:53:00 +08:00
2003-12-19 16:15:11 +08:00
} else {
subst = ngx_rbtree_min(node->right, sentinel);
2003-12-04 22:53:00 +08:00
2003-12-19 16:15:11 +08:00
if (subst->left != sentinel) {
temp = subst->left;
2003-12-04 22:53:00 +08:00
} else {
2003-12-19 16:15:11 +08:00
temp = subst->right;
2003-12-04 22:53:00 +08:00
}
}
2003-12-19 16:15:11 +08:00
if (subst == *root) {
2004-01-16 01:51:49 +08:00
*root = temp;
ngx_rbt_black(temp);
/* DEBUG stuff */
node->left = NULL;
node->right = NULL;
node->parent = NULL;
node->key = 0;
2003-12-19 16:15:11 +08:00
return;
2003-12-04 22:53:00 +08:00
}
2003-12-19 20:45:27 +08:00
is_red = ngx_rbt_is_red(subst);
2003-12-04 22:53:00 +08:00
2003-12-19 16:15:11 +08:00
if (subst == subst->parent->left) {
2003-12-04 22:53:00 +08:00
subst->parent->left = temp;
} else {
subst->parent->right = temp;
}
2003-12-19 16:15:11 +08:00
if (subst == node) {
temp->parent = subst->parent;
} else {
if (subst->parent == node) {
temp->parent = subst;
} else {
temp->parent = subst->parent;
}
subst->left = node->left;
subst->right = node->right;
subst->parent = node->parent;
ngx_rbt_copy_color(subst, node);
if (node == *root) {
*root = subst;
} else {
if (node == node->parent->left) {
node->parent->left = subst;
} else {
node->parent->right = subst;
}
}
if (subst->left != sentinel) {
subst->left->parent = subst;
}
if (subst->right != sentinel) {
subst->right->parent = subst;
}
2004-01-16 01:51:49 +08:00
/* DEBUG stuff */
node->left = NULL;
node->right = NULL;
node->parent = NULL;
node->key = 0;
2003-12-04 22:53:00 +08:00
}
2003-12-19 20:45:27 +08:00
if (is_red) {
2003-12-04 22:53:00 +08:00
return;
}
/* a delete fixup */
2003-12-19 16:15:11 +08:00
while (temp != *root && ngx_rbt_is_black(temp)) {
2003-12-04 22:53:00 +08:00
if (temp == temp->parent->left) {
w = temp->parent->right;
if (ngx_rbt_is_red(w)) {
ngx_rbt_black(w);
ngx_rbt_red(temp->parent);
2003-12-06 01:07:27 +08:00
ngx_rbtree_left_rotate(root, sentinel, temp->parent);
2003-12-04 22:53:00 +08:00
w = temp->parent->right;
}
if (ngx_rbt_is_black(w->left) && ngx_rbt_is_black(w->right)) {
ngx_rbt_red(w);
temp = temp->parent;
} else {
if (ngx_rbt_is_black(w->right)) {
ngx_rbt_black(w->left);
ngx_rbt_red(w);
2003-12-06 01:07:27 +08:00
ngx_rbtree_right_rotate(root, sentinel, w);
2003-12-04 22:53:00 +08:00
w = temp->parent->right;
}
ngx_rbt_copy_color(w, temp->parent);
ngx_rbt_black(temp->parent);
ngx_rbt_black(w->right);
2003-12-06 01:07:27 +08:00
ngx_rbtree_left_rotate(root, sentinel, temp->parent);
2003-12-04 22:53:00 +08:00
temp = *root;
}
} else {
w = temp->parent->left;
if (ngx_rbt_is_red(w)) {
ngx_rbt_black(w);
ngx_rbt_red(temp->parent);
2003-12-06 01:07:27 +08:00
ngx_rbtree_right_rotate(root, sentinel, temp->parent);
2003-12-04 22:53:00 +08:00
w = temp->parent->left;
}
2003-12-19 20:45:27 +08:00
if (ngx_rbt_is_black(w->left) && ngx_rbt_is_black(w->right)) {
2003-12-04 22:53:00 +08:00
ngx_rbt_red(w);
temp = temp->parent;
} else {
if (ngx_rbt_is_black(w->left)) {
ngx_rbt_black(w->right);
ngx_rbt_red(w);
2003-12-06 01:07:27 +08:00
ngx_rbtree_left_rotate(root, sentinel, w);
2003-12-04 22:53:00 +08:00
w = temp->parent->left;
}
ngx_rbt_copy_color(w, temp->parent);
ngx_rbt_black(temp->parent);
ngx_rbt_black(w->left);
2003-12-06 01:07:27 +08:00
ngx_rbtree_right_rotate(root, sentinel, temp->parent);
2003-12-04 22:53:00 +08:00
temp = *root;
}
}
}
ngx_rbt_black(temp);
}
static ngx_inline void ngx_rbtree_left_rotate(ngx_rbtree_t **root,
ngx_rbtree_t *sentinel,
ngx_rbtree_t *node)
2003-12-04 22:53:00 +08:00
{
ngx_rbtree_t *temp;
temp = node->right;
node->right = temp->left;
2003-12-06 01:07:27 +08:00
if (temp->left != sentinel) {
2003-12-04 22:53:00 +08:00
temp->left->parent = node;
}
temp->parent = node->parent;
2003-12-19 16:15:11 +08:00
if (node == *root) {
2003-12-04 22:53:00 +08:00
*root = temp;
} else if (node == node->parent->left) {
node->parent->left = temp;
} else {
node->parent->right = temp;
}
temp->left = node;
node->parent = temp;
}
static ngx_inline void ngx_rbtree_right_rotate(ngx_rbtree_t **root,
ngx_rbtree_t *sentinel,
ngx_rbtree_t *node)
2003-12-04 22:53:00 +08:00
{
ngx_rbtree_t *temp;
temp = node->left;
node->left = temp->right;
2003-12-06 01:07:27 +08:00
if (temp->right != sentinel) {
2003-12-04 22:53:00 +08:00
temp->right->parent = node;
}
temp->parent = node->parent;
2003-12-19 16:15:11 +08:00
if (node == *root) {
2003-12-04 22:53:00 +08:00
*root = temp;
} else if (node == node->parent->right) {
node->parent->right = temp;
} else {
node->parent->left = temp;
}
temp->right = node;
node->parent = temp;
}