2003-12-04 22:53:00 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-12-04 22:53:00 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-10-10 20:59:41 +08:00
|
|
|
* The red-black tree code is based on the algorithm described in
|
|
|
|
* the "Introduction to Algorithms" by Cormen, Leiserson and Rivest.
|
2003-12-04 22:53:00 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2005-10-10 20:59:41 +08:00
|
|
|
static ngx_inline void ngx_rbtree_left_rotate(ngx_rbtree_node_t **root,
|
|
|
|
ngx_rbtree_node_t *sentinel, ngx_rbtree_node_t *node);
|
|
|
|
static ngx_inline void ngx_rbtree_right_rotate(ngx_rbtree_node_t **root,
|
|
|
|
ngx_rbtree_node_t *sentinel, ngx_rbtree_node_t *node);
|
2003-12-04 22:53:00 +08:00
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
void
|
2005-10-10 20:59:41 +08:00
|
|
|
ngx_rbtree_insert(ngx_thread_volatile ngx_rbtree_t *tree,
|
|
|
|
ngx_rbtree_node_t *node)
|
2003-12-04 22:53:00 +08:00
|
|
|
{
|
2005-10-10 20:59:41 +08:00
|
|
|
ngx_rbtree_node_t **root, *temp, *sentinel;
|
2003-12-04 22:53:00 +08:00
|
|
|
|
|
|
|
/* a binary tree insert */
|
|
|
|
|
2005-10-10 20:59:41 +08:00
|
|
|
root = (ngx_rbtree_node_t **) &tree->root;
|
|
|
|
sentinel = tree->sentinel;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-11-16 23:34:52 +08:00
|
|
|
tree->insert(*root, node, sentinel);
|
2003-12-04 22:53:00 +08:00
|
|
|
|
|
|
|
/* re-balance tree */
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2006-10-28 22:20:13 +08:00
|
|
|
}
|
|
|
|
|
2006-10-28 22:32:39 +08:00
|
|
|
ngx_rbt_black(*root);
|
2006-10-28 22:20:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-21 01:13:21 +08:00
|
|
|
void
|
|
|
|
ngx_rbtree_insert_value(ngx_rbtree_node_t *temp, ngx_rbtree_node_t *node,
|
|
|
|
ngx_rbtree_node_t *sentinel)
|
|
|
|
{
|
2007-12-17 16:52:00 +08:00
|
|
|
ngx_rbtree_node_t **p;
|
2006-11-21 01:13:21 +08:00
|
|
|
|
2007-12-17 16:52:00 +08:00
|
|
|
for ( ;; ) {
|
2006-11-21 01:13:21 +08:00
|
|
|
|
2007-12-17 16:52:00 +08:00
|
|
|
p = (node->key < temp->key) ? &temp->left : &temp->right;
|
2006-11-21 01:13:21 +08:00
|
|
|
|
2007-12-17 16:52:00 +08:00
|
|
|
if (*p == sentinel) {
|
|
|
|
break;
|
2006-11-21 01:13:21 +08:00
|
|
|
}
|
2007-12-17 16:52:00 +08:00
|
|
|
|
|
|
|
temp = *p;
|
2006-11-21 01:13:21 +08:00
|
|
|
}
|
|
|
|
|
2007-12-17 16:52:00 +08:00
|
|
|
*p = node;
|
2006-11-21 01:13:21 +08:00
|
|
|
node->parent = temp;
|
|
|
|
node->left = sentinel;
|
|
|
|
node->right = sentinel;
|
|
|
|
ngx_rbt_red(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-16 23:34:52 +08:00
|
|
|
void
|
|
|
|
ngx_rbtree_insert_timer_value(ngx_rbtree_node_t *temp, ngx_rbtree_node_t *node,
|
|
|
|
ngx_rbtree_node_t *sentinel)
|
|
|
|
{
|
2007-12-17 16:52:00 +08:00
|
|
|
ngx_rbtree_node_t **p;
|
|
|
|
|
2006-11-16 23:34:52 +08:00
|
|
|
for ( ;; ) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Timer values
|
|
|
|
* 1) are spread in small range, usually several minutes,
|
|
|
|
* 2) and overflow each 49 days, if milliseconds are stored in 32 bits.
|
|
|
|
* The comparison takes into account that overflow.
|
|
|
|
*/
|
|
|
|
|
2007-12-17 16:52:00 +08:00
|
|
|
/* node->key < temp->key */
|
2006-11-16 23:34:52 +08:00
|
|
|
|
2007-12-17 16:52:00 +08:00
|
|
|
p = ((ngx_rbtree_key_int_t) node->key - (ngx_rbtree_key_int_t) temp->key
|
|
|
|
< 0)
|
|
|
|
? &temp->left : &temp->right;
|
2006-11-16 23:34:52 +08:00
|
|
|
|
2007-12-17 16:52:00 +08:00
|
|
|
if (*p == sentinel) {
|
|
|
|
break;
|
2006-11-16 23:34:52 +08:00
|
|
|
}
|
|
|
|
|
2007-12-17 16:52:00 +08:00
|
|
|
temp = *p;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = node;
|
2006-11-16 23:34:52 +08:00
|
|
|
node->parent = temp;
|
|
|
|
node->left = sentinel;
|
|
|
|
node->right = sentinel;
|
|
|
|
ngx_rbt_red(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
void
|
2005-10-10 20:59:41 +08:00
|
|
|
ngx_rbtree_delete(ngx_thread_volatile ngx_rbtree_t *tree,
|
|
|
|
ngx_rbtree_node_t *node)
|
2003-12-04 22:53:00 +08:00
|
|
|
{
|
2006-11-16 23:34:52 +08:00
|
|
|
ngx_uint_t red;
|
2005-10-10 20:59:41 +08:00
|
|
|
ngx_rbtree_node_t **root, *sentinel, *subst, *temp, *w;
|
2003-12-04 22:53:00 +08:00
|
|
|
|
|
|
|
/* a binary tree delete */
|
|
|
|
|
2005-10-10 20:59:41 +08:00
|
|
|
root = (ngx_rbtree_node_t **) &tree->root;
|
|
|
|
sentinel = tree->sentinel;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-11-16 23:15:16 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-11-16 23:15:16 +08:00
|
|
|
if (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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
static ngx_inline void
|
2005-10-10 20:59:41 +08:00
|
|
|
ngx_rbtree_left_rotate(ngx_rbtree_node_t **root, ngx_rbtree_node_t *sentinel,
|
|
|
|
ngx_rbtree_node_t *node)
|
2003-12-04 22:53:00 +08:00
|
|
|
{
|
2005-10-10 20:59:41 +08:00
|
|
|
ngx_rbtree_node_t *temp;
|
2003-12-04 22:53:00 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-22 22:40:13 +08:00
|
|
|
static ngx_inline void
|
2005-10-10 20:59:41 +08:00
|
|
|
ngx_rbtree_right_rotate(ngx_rbtree_node_t **root, ngx_rbtree_node_t *sentinel,
|
|
|
|
ngx_rbtree_node_t *node)
|
2003-12-04 22:53:00 +08:00
|
|
|
{
|
2005-10-10 20:59:41 +08:00
|
|
|
ngx_rbtree_node_t *temp;
|
2003-12-04 22:53:00 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|