2004-09-28 16:34:51 +08:00
|
|
|
|
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2012-01-18 23:07:43 +08:00
|
|
|
* Copyright (C) Nginx, Inc.
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-12-04 22:53:00 +08:00
|
|
|
#ifndef _NGX_RBTREE_H_INCLUDED_
|
|
|
|
#define _NGX_RBTREE_H_INCLUDED_
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
|
|
|
|
|
2005-10-07 21:30:52 +08:00
|
|
|
typedef ngx_uint_t ngx_rbtree_key_t;
|
|
|
|
typedef ngx_int_t ngx_rbtree_key_int_t;
|
|
|
|
|
|
|
|
|
2005-10-10 20:59:41 +08:00
|
|
|
typedef struct ngx_rbtree_node_s ngx_rbtree_node_t;
|
|
|
|
|
|
|
|
struct ngx_rbtree_node_s {
|
|
|
|
ngx_rbtree_key_t key;
|
|
|
|
ngx_rbtree_node_t *left;
|
|
|
|
ngx_rbtree_node_t *right;
|
|
|
|
ngx_rbtree_node_t *parent;
|
2006-11-16 23:34:52 +08:00
|
|
|
u_char color;
|
|
|
|
u_char data;
|
2005-10-10 20:59:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-12-04 22:53:00 +08:00
|
|
|
typedef struct ngx_rbtree_s ngx_rbtree_t;
|
|
|
|
|
2006-11-16 23:34:52 +08:00
|
|
|
typedef void (*ngx_rbtree_insert_pt) (ngx_rbtree_node_t *root,
|
2005-10-10 20:59:41 +08:00
|
|
|
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
|
|
|
|
|
2003-12-04 22:53:00 +08:00
|
|
|
struct ngx_rbtree_s {
|
2005-10-10 20:59:41 +08:00
|
|
|
ngx_rbtree_node_t *root;
|
|
|
|
ngx_rbtree_node_t *sentinel;
|
2006-11-16 23:34:52 +08:00
|
|
|
ngx_rbtree_insert_pt insert;
|
2003-12-04 22:53:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-12-03 20:17:15 +08:00
|
|
|
#define ngx_rbtree_init(tree, s, i) \
|
|
|
|
ngx_rbtree_sentinel_init(s); \
|
|
|
|
(tree)->root = s; \
|
|
|
|
(tree)->sentinel = s; \
|
|
|
|
(tree)->insert = i
|
|
|
|
|
|
|
|
|
2005-10-10 20:59:41 +08:00
|
|
|
void ngx_rbtree_insert(ngx_thread_volatile ngx_rbtree_t *tree,
|
|
|
|
ngx_rbtree_node_t *node);
|
|
|
|
void ngx_rbtree_delete(ngx_thread_volatile ngx_rbtree_t *tree,
|
|
|
|
ngx_rbtree_node_t *node);
|
2006-11-21 01:13:21 +08:00
|
|
|
void ngx_rbtree_insert_value(ngx_rbtree_node_t *root, ngx_rbtree_node_t *node,
|
|
|
|
ngx_rbtree_node_t *sentinel);
|
2006-11-16 23:34:52 +08:00
|
|
|
void ngx_rbtree_insert_timer_value(ngx_rbtree_node_t *root,
|
|
|
|
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
|
2003-12-04 22:53:00 +08:00
|
|
|
|
|
|
|
|
2007-01-13 03:48:30 +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)
|
|
|
|
#define ngx_rbt_is_black(node) (!ngx_rbt_is_red(node))
|
|
|
|
#define ngx_rbt_copy_color(n1, n2) (n1->color = n2->color)
|
|
|
|
|
|
|
|
|
2007-01-03 07:04:54 +08:00
|
|
|
/* a sentinel must be black */
|
|
|
|
|
2007-01-13 03:48:30 +08:00
|
|
|
#define ngx_rbtree_sentinel_init(node) ngx_rbt_black(node)
|
2007-01-03 07:04:54 +08:00
|
|
|
|
|
|
|
|
2005-10-10 20:59:41 +08:00
|
|
|
static ngx_inline ngx_rbtree_node_t *
|
|
|
|
ngx_rbtree_min(ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
2003-12-04 22:53:00 +08:00
|
|
|
{
|
2005-10-07 21:30:52 +08:00
|
|
|
while (node->left != sentinel) {
|
|
|
|
node = node->left;
|
|
|
|
}
|
2003-12-04 22:53:00 +08:00
|
|
|
|
2005-10-07 21:30:52 +08:00
|
|
|
return node;
|
2003-12-04 22:53:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* _NGX_RBTREE_H_INCLUDED_ */
|