libite 2.6.1
tree.h File Reference

Go to the source code of this file.

Macros

#define _SYS_TREE_H_
 
#define SPLAY_HEAD(name, type)
 
#define SPLAY_INITIALIZER(root)
 
#define SPLAY_INIT(root)
 
#define SPLAY_ENTRY(type)
 
#define SPLAY_LEFT(elm, field)
 
#define SPLAY_RIGHT(elm, field)
 
#define SPLAY_ROOT(head)
 
#define SPLAY_EMPTY(head)
 
#define SPLAY_ROTATE_RIGHT(head, tmp, field)
 
#define SPLAY_ROTATE_LEFT(head, tmp, field)
 
#define SPLAY_LINKLEFT(head, tmp, field)
 
#define SPLAY_LINKRIGHT(head, tmp, field)
 
#define SPLAY_ASSEMBLE(head, node, left, right, field)
 
#define SPLAY_PROTOTYPE(name, type, field, cmp)
 
#define SPLAY_GENERATE(name, type, field, cmp)
 
#define SPLAY_NEGINF   -1
 
#define SPLAY_INF   1
 
#define SPLAY_INSERT(name, x, y)
 
#define SPLAY_REMOVE(name, x, y)
 
#define SPLAY_FIND(name, x, y)
 
#define SPLAY_NEXT(name, x, y)
 
#define SPLAY_MIN(name, x)
 
#define SPLAY_MAX(name, x)
 
#define SPLAY_FOREACH(x, name, head)
 
#define RB_HEAD(name, type)
 
#define RB_INITIALIZER(root)
 
#define RB_INIT(root)
 
#define RB_BLACK   0
 
#define RB_RED   1
 
#define RB_ENTRY(type)
 
#define RB_LEFT(elm, field)
 
#define RB_RIGHT(elm, field)
 
#define RB_PARENT(elm, field)
 
#define RB_COLOR(elm, field)
 
#define RB_ROOT(head)
 
#define RB_EMPTY(head)
 
#define RB_SET(elm, parent, field)
 
#define RB_SET_BLACKRED(black, red, field)
 
#define RB_AUGMENT(x)
 
#define RB_ROTATE_LEFT(head, elm, tmp, field)
 
#define RB_ROTATE_RIGHT(head, elm, tmp, field)
 
#define RB_PROTOTYPE(name, type, field, cmp)
 
#define RB_PROTOTYPE_STATIC(name, type, field, cmp)
 
#define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr)
 
#define RB_GENERATE(name, type, field, cmp)
 
#define RB_GENERATE_STATIC(name, type, field, cmp)
 
#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr)
 
#define RB_NEGINF   -1
 
#define RB_INF   1
 
#define RB_INSERT(name, x, y)
 
#define RB_REMOVE(name, x, y)
 
#define RB_FIND(name, x, y)
 
#define RB_NFIND(name, x, y)
 
#define RB_NEXT(name, x, y)
 
#define RB_PREV(name, x, y)
 
#define RB_MIN(name, x)
 
#define RB_MAX(name, x)
 
#define RB_FOREACH(x, name, head)
 
#define RB_FOREACH_SAFE(x, name, head, y)
 
#define RB_FOREACH_REVERSE(x, name, head)
 
#define RB_FOREACH_REVERSE_SAFE(x, name, head, y)
 

Detailed Description

Author
Niels Provos
Date
2002

This file defines data structures for different types of trees: splay trees and red-black trees.

A splay tree is a self-organizing data structure. Every operation on the tree causes a splay to happen. The splay moves the requested node to the root of the tree and partly rebalances it.

This has the benefit that request locality causes faster lookups as the requested nodes move to the top of the tree. On the other hand, every lookup causes memory writes.

The Balance Theorem bounds the total access time for m operations and n inserts on an initially empty tree as O((m + n)lg n). The amortized cost for a sequence of m accesses to a splay tree is O(lg n);

A red-black tree is a binary search tree with the node color as an extra attribute. It fulfills a set of conditions:

  • every search path from the root to a leaf consists of the same number of black nodes,
  • each red node (except for the root) has a black parent,
  • each leaf node is black.

Every operation on a red-black tree is bounded as O(lg n). The maximum height of a red-black tree is 2lg (n+1).

Definition in file tree.h.

Macro Definition Documentation

◆ _SYS_TREE_H_

#define _SYS_TREE_H_

Definition at line 33 of file tree.h.

◆ RB_AUGMENT

#define RB_AUGMENT ( x)
Value:
do {} while (0)

Definition at line 340 of file tree.h.

◆ RB_BLACK

#define RB_BLACK   0

Definition at line 311 of file tree.h.

◆ RB_COLOR

#define RB_COLOR ( elm,
field )
Value:
(elm)->field.rbe_color

Definition at line 324 of file tree.h.

◆ RB_EMPTY

#define RB_EMPTY ( head)
Value:
(RB_ROOT(head) == NULL)

Definition at line 326 of file tree.h.

◆ RB_ENTRY

#define RB_ENTRY ( type)
Value:
struct { \
struct type *rbe_left; /* left element */ \
struct type *rbe_right; /* right element */ \
struct type *rbe_parent; /* parent element */ \
int rbe_color; /* node color */ \
}

Definition at line 313 of file tree.h.

◆ RB_FIND

#define RB_FIND ( name,
x,
y )
Value:
name##_RB_FIND(x, y)

Definition at line 729 of file tree.h.

◆ RB_FOREACH

#define RB_FOREACH ( x,
name,
head )
Value:
for ((x) = RB_MIN(name, head); \
(x) != NULL; \
(x) = name##_RB_NEXT(x))

Definition at line 736 of file tree.h.

◆ RB_FOREACH_REVERSE

#define RB_FOREACH_REVERSE ( x,
name,
head )
Value:
for ((x) = RB_MAX(name, head); \
(x) != NULL; \
(x) = name##_RB_PREV(x))

Definition at line 746 of file tree.h.

◆ RB_FOREACH_REVERSE_SAFE

#define RB_FOREACH_REVERSE_SAFE ( x,
name,
head,
y )
Value:
for ((x) = RB_MAX(name, head); \
((x) != NULL) && ((y) = name##_RB_PREV(x), 1); \
(x) = (y))

Definition at line 751 of file tree.h.

◆ RB_FOREACH_SAFE

#define RB_FOREACH_SAFE ( x,
name,
head,
y )
Value:
for ((x) = RB_MIN(name, head); \
((x) != NULL) && ((y) = name##_RB_NEXT(x), 1); \
(x) = (y))

Definition at line 741 of file tree.h.

◆ RB_GENERATE

#define RB_GENERATE ( name,
type,
field,
cmp )
Value:
RB_GENERATE_INTERNAL(name, type, field, cmp,)

Definition at line 403 of file tree.h.

◆ RB_GENERATE_INTERNAL

#define RB_GENERATE_INTERNAL ( name,
type,
field,
cmp,
attr )

Definition at line 407 of file tree.h.

◆ RB_GENERATE_STATIC

#define RB_GENERATE_STATIC ( name,
type,
field,
cmp )
Value:
RB_GENERATE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)

Definition at line 405 of file tree.h.

◆ RB_HEAD

#define RB_HEAD ( name,
type )
Value:
struct name { \
struct type *rbh_root; /* root of the tree */ \
}

Definition at line 299 of file tree.h.

◆ RB_INF

#define RB_INF   1

Definition at line 725 of file tree.h.

◆ RB_INIT

#define RB_INIT ( root)
Value:
do { \
(root)->rbh_root = NULL; \
} while (0)

Definition at line 307 of file tree.h.

◆ RB_INITIALIZER

#define RB_INITIALIZER ( root)
Value:
{ NULL }

Definition at line 304 of file tree.h.

◆ RB_INSERT

#define RB_INSERT ( name,
x,
y )
Value:
name##_RB_INSERT(x, y)

Definition at line 727 of file tree.h.

◆ RB_LEFT

#define RB_LEFT ( elm,
field )
Value:
(elm)->field.rbe_left

Definition at line 321 of file tree.h.

◆ RB_MAX

#define RB_MAX ( name,
x )
Value:
name##_RB_MINMAX(x, RB_INF)

Definition at line 734 of file tree.h.

◆ RB_MIN

#define RB_MIN ( name,
x )
Value:
name##_RB_MINMAX(x, RB_NEGINF)

Definition at line 733 of file tree.h.

◆ RB_NEGINF

#define RB_NEGINF   -1

Definition at line 724 of file tree.h.

◆ RB_NEXT

#define RB_NEXT ( name,
x,
y )
Value:
name##_RB_NEXT(y)

Definition at line 731 of file tree.h.

◆ RB_NFIND

#define RB_NFIND ( name,
x,
y )
Value:
name##_RB_NFIND(x, y)

Definition at line 730 of file tree.h.

◆ RB_PARENT

#define RB_PARENT ( elm,
field )
Value:
(elm)->field.rbe_parent

Definition at line 323 of file tree.h.

◆ RB_PREV

#define RB_PREV ( name,
x,
y )
Value:
name##_RB_PREV(y)

Definition at line 732 of file tree.h.

◆ RB_PROTOTYPE

#define RB_PROTOTYPE ( name,
type,
field,
cmp )
Value:
RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)

Definition at line 384 of file tree.h.

◆ RB_PROTOTYPE_INTERNAL

#define RB_PROTOTYPE_INTERNAL ( name,
type,
field,
cmp,
attr )
Value:
attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
attr struct type *name##_RB_INSERT(struct name *, struct type *); \
attr struct type *name##_RB_FIND(struct name *, struct type *); \
attr struct type *name##_RB_NFIND(struct name *, struct type *); \
attr struct type *name##_RB_NEXT(struct type *); \
attr struct type *name##_RB_PREV(struct type *); \
attr struct type *name##_RB_MINMAX(struct name *, int); \
\

Definition at line 388 of file tree.h.

◆ RB_PROTOTYPE_STATIC

#define RB_PROTOTYPE_STATIC ( name,
type,
field,
cmp )
Value:
RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)

Definition at line 386 of file tree.h.

◆ RB_RED

#define RB_RED   1

Definition at line 312 of file tree.h.

◆ RB_REMOVE

#define RB_REMOVE ( name,
x,
y )
Value:
name##_RB_REMOVE(x, y)

Definition at line 728 of file tree.h.

◆ RB_RIGHT

#define RB_RIGHT ( elm,
field )
Value:
(elm)->field.rbe_right

Definition at line 322 of file tree.h.

◆ RB_ROOT

#define RB_ROOT ( head)
Value:
(head)->rbh_root

Definition at line 325 of file tree.h.

◆ RB_ROTATE_LEFT

#define RB_ROTATE_LEFT ( head,
elm,
tmp,
field )
Value:
do { \
(tmp) = RB_RIGHT(elm, field); \
if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \
RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
} \
RB_AUGMENT(elm); \
if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
else \
RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
} else \
(head)->rbh_root = (tmp); \
RB_LEFT(tmp, field) = (elm); \
RB_PARENT(elm, field) = (tmp); \
RB_AUGMENT(tmp); \
if ((RB_PARENT(tmp, field))) \
RB_AUGMENT(RB_PARENT(tmp, field)); \
} while (0)

Definition at line 343 of file tree.h.

◆ RB_ROTATE_RIGHT

#define RB_ROTATE_RIGHT ( head,
elm,
tmp,
field )
Value:
do { \
(tmp) = RB_LEFT(elm, field); \
if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \
RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
} \
RB_AUGMENT(elm); \
if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
else \
RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
} else \
(head)->rbh_root = (tmp); \
RB_RIGHT(tmp, field) = (elm); \
RB_PARENT(elm, field) = (tmp); \
RB_AUGMENT(tmp); \
if ((RB_PARENT(tmp, field))) \
RB_AUGMENT(RB_PARENT(tmp, field)); \
} while (0)

Definition at line 363 of file tree.h.

◆ RB_SET

#define RB_SET ( elm,
parent,
field )
Value:
do { \
RB_PARENT(elm, field) = parent; \
RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
RB_COLOR(elm, field) = RB_RED; \
} while (0)

Definition at line 328 of file tree.h.

◆ RB_SET_BLACKRED

#define RB_SET_BLACKRED ( black,
red,
field )
Value:
do { \
RB_COLOR(black, field) = RB_BLACK; \
RB_COLOR(red, field) = RB_RED; \
} while (0)

Definition at line 334 of file tree.h.

◆ SPLAY_ASSEMBLE

#define SPLAY_ASSEMBLE ( head,
node,
left,
right,
field )
Value:
do { \
SPLAY_RIGHT(left, field) = SPLAY_LEFT((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_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
} while (0)

Definition at line 115 of file tree.h.

◆ SPLAY_EMPTY

#define SPLAY_EMPTY ( head)
Value:
(SPLAY_ROOT(head) == NULL)

Definition at line 88 of file tree.h.

◆ SPLAY_ENTRY

#define SPLAY_ENTRY ( type)
Value:
struct { \
struct type *spe_left; /* left element */ \
struct type *spe_right; /* right element */ \
}

Definition at line 79 of file tree.h.

◆ SPLAY_FIND

#define SPLAY_FIND ( name,
x,
y )
Value:
name##_SPLAY_FIND(x, y)

Definition at line 286 of file tree.h.

◆ SPLAY_FOREACH

#define SPLAY_FOREACH ( x,
name,
head )
Value:
for ((x) = SPLAY_MIN(name, head); \
(x) != NULL; \
(x) = SPLAY_NEXT(name, head, x))

Definition at line 293 of file tree.h.

◆ SPLAY_GENERATE

#define SPLAY_GENERATE ( name,
type,
field,
cmp )

Definition at line 166 of file tree.h.

◆ SPLAY_HEAD

#define SPLAY_HEAD ( name,
type )
Value:
struct name { \
struct type *sph_root; /* root of the tree */ \
}

Definition at line 67 of file tree.h.

◆ SPLAY_INF

#define SPLAY_INF   1

Definition at line 282 of file tree.h.

◆ SPLAY_INIT

#define SPLAY_INIT ( root)
Value:
do { \
(root)->sph_root = NULL; \
} while (0)

Definition at line 75 of file tree.h.

◆ SPLAY_INITIALIZER

#define SPLAY_INITIALIZER ( root)
Value:
{ NULL }

Definition at line 72 of file tree.h.

◆ SPLAY_INSERT

#define SPLAY_INSERT ( name,
x,
y )
Value:
name##_SPLAY_INSERT(x, y)

Definition at line 284 of file tree.h.

◆ SPLAY_LEFT

#define SPLAY_LEFT ( elm,
field )
Value:
(elm)->field.spe_left

Definition at line 85 of file tree.h.

◆ SPLAY_LINKLEFT

#define SPLAY_LINKLEFT ( head,
tmp,
field )
Value:
do { \
SPLAY_LEFT(tmp, field) = (head)->sph_root; \
tmp = (head)->sph_root; \
(head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
} while (0)

Definition at line 103 of file tree.h.

◆ SPLAY_LINKRIGHT

#define SPLAY_LINKRIGHT ( head,
tmp,
field )
Value:
do { \
SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
tmp = (head)->sph_root; \
(head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
} while (0)

Definition at line 109 of file tree.h.

◆ SPLAY_MAX

#define SPLAY_MAX ( name,
x )
Value:
(SPLAY_EMPTY(x) ? NULL \
: name##_SPLAY_MIN_MAX(x, SPLAY_INF))

Definition at line 290 of file tree.h.

◆ SPLAY_MIN

#define SPLAY_MIN ( name,
x )
Value:
(SPLAY_EMPTY(x) ? NULL \
: name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))

Definition at line 288 of file tree.h.

◆ SPLAY_NEGINF

#define SPLAY_NEGINF   -1

Definition at line 281 of file tree.h.

◆ SPLAY_NEXT

#define SPLAY_NEXT ( name,
x,
y )
Value:
name##_SPLAY_NEXT(x, y)

Definition at line 287 of file tree.h.

◆ SPLAY_PROTOTYPE

#define SPLAY_PROTOTYPE ( name,
type,
field,
cmp )

Definition at line 124 of file tree.h.

◆ SPLAY_REMOVE

#define SPLAY_REMOVE ( name,
x,
y )
Value:
name##_SPLAY_REMOVE(x, y)

Definition at line 285 of file tree.h.

◆ SPLAY_RIGHT

#define SPLAY_RIGHT ( elm,
field )
Value:
(elm)->field.spe_right

Definition at line 86 of file tree.h.

◆ SPLAY_ROOT

#define SPLAY_ROOT ( head)
Value:
(head)->sph_root

Definition at line 87 of file tree.h.

◆ SPLAY_ROTATE_LEFT

#define SPLAY_ROTATE_LEFT ( head,
tmp,
field )
Value:
do { \
SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
SPLAY_LEFT(tmp, field) = (head)->sph_root; \
(head)->sph_root = tmp; \
} while (0)

Definition at line 97 of file tree.h.

◆ SPLAY_ROTATE_RIGHT

#define SPLAY_ROTATE_RIGHT ( head,
tmp,
field )
Value:
do { \
SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
(head)->sph_root = tmp; \
} while (0)

Definition at line 91 of file tree.h.