Import the original directly from NetBSD instead of via OpenBSD.
This commit is contained in:
parent
5e5fc567c3
commit
395c16bd72
@ -1,3 +1,4 @@
|
|||||||
|
/* $NetBSD: tree.h,v 1.7 2004/01/24 21:59:47 dbj Exp $ */
|
||||||
/* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */
|
/* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */
|
||||||
/*
|
/*
|
||||||
* Copyright 2002 Niels Provos <provos@citi.umich.edu>
|
* Copyright 2002 Niels Provos <provos@citi.umich.edu>
|
||||||
@ -64,7 +65,7 @@ struct name { \
|
|||||||
|
|
||||||
#define SPLAY_INIT(root) do { \
|
#define SPLAY_INIT(root) do { \
|
||||||
(root)->sph_root = NULL; \
|
(root)->sph_root = NULL; \
|
||||||
} while (0)
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
#define SPLAY_ENTRY(type) \
|
#define SPLAY_ENTRY(type) \
|
||||||
struct { \
|
struct { \
|
||||||
@ -82,32 +83,32 @@ struct { \
|
|||||||
SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
|
SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
|
||||||
SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
|
SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
|
||||||
(head)->sph_root = tmp; \
|
(head)->sph_root = tmp; \
|
||||||
} while (0)
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
#define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
|
#define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
|
||||||
SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
|
SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
|
||||||
SPLAY_LEFT(tmp, field) = (head)->sph_root; \
|
SPLAY_LEFT(tmp, field) = (head)->sph_root; \
|
||||||
(head)->sph_root = tmp; \
|
(head)->sph_root = tmp; \
|
||||||
} while (0)
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
#define SPLAY_LINKLEFT(head, tmp, field) do { \
|
#define SPLAY_LINKLEFT(head, tmp, field) do { \
|
||||||
SPLAY_LEFT(tmp, field) = (head)->sph_root; \
|
SPLAY_LEFT(tmp, field) = (head)->sph_root; \
|
||||||
tmp = (head)->sph_root; \
|
tmp = (head)->sph_root; \
|
||||||
(head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
|
(head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
|
||||||
} while (0)
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
#define SPLAY_LINKRIGHT(head, tmp, field) do { \
|
#define SPLAY_LINKRIGHT(head, tmp, field) do { \
|
||||||
SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
|
SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
|
||||||
tmp = (head)->sph_root; \
|
tmp = (head)->sph_root; \
|
||||||
(head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
|
(head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
|
||||||
} while (0)
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
#define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
|
#define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
|
||||||
SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
|
SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
|
||||||
SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
|
SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
|
||||||
SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
|
SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
|
||||||
SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
|
SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
|
||||||
} while (0)
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
/* Generates prototypes and inline functions */
|
/* Generates prototypes and inline functions */
|
||||||
|
|
||||||
@ -208,7 +209,7 @@ name##_SPLAY(struct name *head, struct type *elm) \
|
|||||||
SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
|
SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
|
||||||
__left = __right = &__node; \
|
__left = __right = &__node; \
|
||||||
\
|
\
|
||||||
while ((__comp = (cmp)(elm, (head)->sph_root))) { \
|
while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \
|
||||||
if (__comp < 0) { \
|
if (__comp < 0) { \
|
||||||
__tmp = SPLAY_LEFT((head)->sph_root, field); \
|
__tmp = SPLAY_LEFT((head)->sph_root, field); \
|
||||||
if (__tmp == NULL) \
|
if (__tmp == NULL) \
|
||||||
@ -287,7 +288,7 @@ void name##_SPLAY_MINMAX(struct name *head, int __comp) \
|
|||||||
(x) != NULL; \
|
(x) != NULL; \
|
||||||
(x) = SPLAY_NEXT(name, head, x))
|
(x) = SPLAY_NEXT(name, head, x))
|
||||||
|
|
||||||
/* Macros that define a red-back tree */
|
/* Macros that define a red-black tree */
|
||||||
#define RB_HEAD(name, type) \
|
#define RB_HEAD(name, type) \
|
||||||
struct name { \
|
struct name { \
|
||||||
struct type *rbh_root; /* root of the tree */ \
|
struct type *rbh_root; /* root of the tree */ \
|
||||||
@ -298,7 +299,7 @@ struct name { \
|
|||||||
|
|
||||||
#define RB_INIT(root) do { \
|
#define RB_INIT(root) do { \
|
||||||
(root)->rbh_root = NULL; \
|
(root)->rbh_root = NULL; \
|
||||||
} while (0)
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
#define RB_BLACK 0
|
#define RB_BLACK 0
|
||||||
#define RB_RED 1
|
#define RB_RED 1
|
||||||
@ -321,12 +322,12 @@ struct { \
|
|||||||
RB_PARENT(elm, field) = parent; \
|
RB_PARENT(elm, field) = parent; \
|
||||||
RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
|
RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
|
||||||
RB_COLOR(elm, field) = RB_RED; \
|
RB_COLOR(elm, field) = RB_RED; \
|
||||||
} while (0)
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
#define RB_SET_BLACKRED(black, red, field) do { \
|
#define RB_SET_BLACKRED(black, red, field) do { \
|
||||||
RB_COLOR(black, field) = RB_BLACK; \
|
RB_COLOR(black, field) = RB_BLACK; \
|
||||||
RB_COLOR(red, field) = RB_RED; \
|
RB_COLOR(red, field) = RB_RED; \
|
||||||
} while (0)
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
#ifndef RB_AUGMENT
|
#ifndef RB_AUGMENT
|
||||||
#define RB_AUGMENT(x)
|
#define RB_AUGMENT(x)
|
||||||
@ -334,11 +335,11 @@ struct { \
|
|||||||
|
|
||||||
#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
|
#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
|
||||||
(tmp) = RB_RIGHT(elm, field); \
|
(tmp) = RB_RIGHT(elm, field); \
|
||||||
if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \
|
if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \
|
||||||
RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
|
RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
|
||||||
} \
|
} \
|
||||||
RB_AUGMENT(elm); \
|
RB_AUGMENT(elm); \
|
||||||
if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
|
if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
|
||||||
if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
|
if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
|
||||||
RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
|
RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
|
||||||
else \
|
else \
|
||||||
@ -350,15 +351,15 @@ struct { \
|
|||||||
RB_AUGMENT(tmp); \
|
RB_AUGMENT(tmp); \
|
||||||
if ((RB_PARENT(tmp, field))) \
|
if ((RB_PARENT(tmp, field))) \
|
||||||
RB_AUGMENT(RB_PARENT(tmp, field)); \
|
RB_AUGMENT(RB_PARENT(tmp, field)); \
|
||||||
} while (0)
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
|
#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
|
||||||
(tmp) = RB_LEFT(elm, field); \
|
(tmp) = RB_LEFT(elm, field); \
|
||||||
if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \
|
if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \
|
||||||
RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
|
RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
|
||||||
} \
|
} \
|
||||||
RB_AUGMENT(elm); \
|
RB_AUGMENT(elm); \
|
||||||
if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
|
if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
|
||||||
if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
|
if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
|
||||||
RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
|
RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
|
||||||
else \
|
else \
|
||||||
@ -370,7 +371,7 @@ struct { \
|
|||||||
RB_AUGMENT(tmp); \
|
RB_AUGMENT(tmp); \
|
||||||
if ((RB_PARENT(tmp, field))) \
|
if ((RB_PARENT(tmp, field))) \
|
||||||
RB_AUGMENT(RB_PARENT(tmp, field)); \
|
RB_AUGMENT(RB_PARENT(tmp, field)); \
|
||||||
} while (0)
|
} while (/*CONSTCOND*/ 0)
|
||||||
|
|
||||||
/* Generates prototypes and inline functions */
|
/* Generates prototypes and inline functions */
|
||||||
#define RB_PROTOTYPE(name, type, field, cmp) \
|
#define RB_PROTOTYPE(name, type, field, cmp) \
|
||||||
@ -391,7 +392,7 @@ void \
|
|||||||
name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
|
name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
|
||||||
{ \
|
{ \
|
||||||
struct type *parent, *gparent, *tmp; \
|
struct type *parent, *gparent, *tmp; \
|
||||||
while ((parent = RB_PARENT(elm, field)) && \
|
while ((parent = RB_PARENT(elm, field)) != NULL && \
|
||||||
RB_COLOR(parent, field) == RB_RED) { \
|
RB_COLOR(parent, field) == RB_RED) { \
|
||||||
gparent = RB_PARENT(parent, field); \
|
gparent = RB_PARENT(parent, field); \
|
||||||
if (parent == RB_LEFT(gparent, field)) { \
|
if (parent == RB_LEFT(gparent, field)) { \
|
||||||
@ -455,7 +456,8 @@ name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm)
|
|||||||
if (RB_RIGHT(tmp, field) == NULL || \
|
if (RB_RIGHT(tmp, field) == NULL || \
|
||||||
RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
|
RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
|
||||||
struct type *oleft; \
|
struct type *oleft; \
|
||||||
if ((oleft = RB_LEFT(tmp, field)))\
|
if ((oleft = RB_LEFT(tmp, field)) \
|
||||||
|
!= NULL) \
|
||||||
RB_COLOR(oleft, field) = RB_BLACK;\
|
RB_COLOR(oleft, field) = RB_BLACK;\
|
||||||
RB_COLOR(tmp, field) = RB_RED; \
|
RB_COLOR(tmp, field) = RB_RED; \
|
||||||
RB_ROTATE_RIGHT(head, tmp, oleft, field);\
|
RB_ROTATE_RIGHT(head, tmp, oleft, field);\
|
||||||
@ -487,7 +489,8 @@ name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm)
|
|||||||
if (RB_LEFT(tmp, field) == NULL || \
|
if (RB_LEFT(tmp, field) == NULL || \
|
||||||
RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
|
RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
|
||||||
struct type *oright; \
|
struct type *oright; \
|
||||||
if ((oright = RB_RIGHT(tmp, field)))\
|
if ((oright = RB_RIGHT(tmp, field)) \
|
||||||
|
!= NULL) \
|
||||||
RB_COLOR(oright, field) = RB_BLACK;\
|
RB_COLOR(oright, field) = RB_BLACK;\
|
||||||
RB_COLOR(tmp, field) = RB_RED; \
|
RB_COLOR(tmp, field) = RB_RED; \
|
||||||
RB_ROTATE_LEFT(head, tmp, oright, field);\
|
RB_ROTATE_LEFT(head, tmp, oright, field);\
|
||||||
@ -519,7 +522,7 @@ name##_RB_REMOVE(struct name *head, struct type *elm) \
|
|||||||
else { \
|
else { \
|
||||||
struct type *left; \
|
struct type *left; \
|
||||||
elm = RB_RIGHT(elm, field); \
|
elm = RB_RIGHT(elm, field); \
|
||||||
while ((left = RB_LEFT(elm, field))) \
|
while ((left = RB_LEFT(elm, field)) != NULL) \
|
||||||
elm = left; \
|
elm = left; \
|
||||||
child = RB_RIGHT(elm, field); \
|
child = RB_RIGHT(elm, field); \
|
||||||
parent = RB_PARENT(elm, field); \
|
parent = RB_PARENT(elm, field); \
|
||||||
@ -552,7 +555,7 @@ name##_RB_REMOVE(struct name *head, struct type *elm) \
|
|||||||
left = parent; \
|
left = parent; \
|
||||||
do { \
|
do { \
|
||||||
RB_AUGMENT(left); \
|
RB_AUGMENT(left); \
|
||||||
} while ((left = RB_PARENT(left, field))); \
|
} while ((left = RB_PARENT(left, field)) != NULL); \
|
||||||
} \
|
} \
|
||||||
goto color; \
|
goto color; \
|
||||||
} \
|
} \
|
||||||
@ -623,6 +626,7 @@ name##_RB_FIND(struct name *head, struct type *elm) \
|
|||||||
return (NULL); \
|
return (NULL); \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
|
/* ARGSUSED */ \
|
||||||
struct type * \
|
struct type * \
|
||||||
name##_RB_NEXT(struct name *head, struct type *elm) \
|
name##_RB_NEXT(struct name *head, struct type *elm) \
|
||||||
{ \
|
{ \
|
||||||
|
Loading…
Reference in New Issue
Block a user