PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
rbtree.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * rbtree.h
4  * interface for PostgreSQL generic Red-Black binary tree package
5  *
6  * Copyright (c) 2009-2017, PostgreSQL Global Development Group
7  *
8  * IDENTIFICATION
9  * src/include/lib/rbtree.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef RBTREE_H
14 #define RBTREE_H
15 
16 /*
17  * RBNode is intended to be used as the first field of a larger struct,
18  * whose additional fields carry whatever payload data the caller needs
19  * for a tree entry. (The total size of that larger struct is passed to
20  * rb_create.) RBNode is declared here to support this usage, but
21  * callers must treat it as an opaque struct.
22  */
23 typedef struct RBNode
24 {
25  char color; /* node's current color, red or black */
26  struct RBNode *left; /* left child, or RBNIL if none */
27  struct RBNode *right; /* right child, or RBNIL if none */
28  struct RBNode *parent; /* parent, or NULL (not RBNIL!) if none */
29 } RBNode;
30 
31 /* Opaque struct representing a whole tree */
32 typedef struct RBTree RBTree;
33 
34 /* Available tree iteration orderings */
35 typedef enum RBOrderControl
36 {
37  LeftRightWalk, /* inorder: left child, node, right child */
38  RightLeftWalk, /* reverse inorder: right, node, left */
39  DirectWalk, /* preorder: node, left child, right child */
40  InvertedWalk /* postorder: left child, right child, node */
42 
43 /*
44  * RBTreeIterator holds state while traversing a tree. This is declared
45  * here so that callers can stack-allocate this, but must otherwise be
46  * treated as an opaque struct.
47  */
49 
51 {
53  RBNode *(*iterate) (RBTreeIterator *iter);
55  char next_step;
56  bool is_over;
57 };
58 
59 /* Support functions to be provided by caller */
60 typedef int (*rb_comparator) (const RBNode *a, const RBNode *b, void *arg);
61 typedef void (*rb_combiner) (RBNode *existing, const RBNode *newdata, void *arg);
62 typedef RBNode *(*rb_allocfunc) (void *arg);
63 typedef void (*rb_freefunc) (RBNode *x, void *arg);
64 
65 extern RBTree *rb_create(Size node_size,
66  rb_comparator comparator,
67  rb_combiner combiner,
68  rb_allocfunc allocfunc,
69  rb_freefunc freefunc,
70  void *arg);
71 
72 extern RBNode *rb_find(RBTree *rb, const RBNode *data);
73 extern RBNode *rb_leftmost(RBTree *rb);
74 
75 extern RBNode *rb_insert(RBTree *rb, const RBNode *data, bool *isNew);
76 extern void rb_delete(RBTree *rb, RBNode *node);
77 
78 extern void rb_begin_iterate(RBTree *rb, RBOrderControl ctrl,
79  RBTreeIterator *iter);
80 extern RBNode *rb_iterate(RBTreeIterator *iter);
81 
82 #endif /* RBTREE_H */
struct RBNode * left
Definition: rbtree.h:26
struct RBNode RBNode
RBNode * rb_iterate(RBTreeIterator *iter)
Definition: rbtree.c:886
void(* rb_freefunc)(RBNode *x, void *arg)
Definition: rbtree.h:63
RBNode * last_visited
Definition: rbtree.h:54
int(* rb_comparator)(const RBNode *a, const RBNode *b, void *arg)
Definition: rbtree.h:60
struct RBNode * right
Definition: rbtree.h:27
RBNode * rb_find(RBTree *rb, const RBNode *data)
Definition: rbtree.c:153
void rb_delete(RBTree *rb, RBNode *node)
Definition: rbtree.c:640
char next_step
Definition: rbtree.h:55
struct RBNode * parent
Definition: rbtree.h:28
Definition: rbtree.h:23
void rb_begin_iterate(RBTree *rb, RBOrderControl ctrl, RBTreeIterator *iter)
Definition: rbtree.c:855
bool is_over
Definition: rbtree.h:56
char color
Definition: rbtree.h:25
RBNode * rb_insert(RBTree *rb, const RBNode *data, bool *isNew)
Definition: rbtree.c:399
void(* rb_combiner)(RBNode *existing, const RBNode *newdata, void *arg)
Definition: rbtree.h:61
RBTree * rb
Definition: rbtree.h:52
RBOrderControl
Definition: rbtree.h:35
size_t Size
Definition: c.h:356
RBTree * rb_create(Size node_size, rb_comparator comparator, rb_combiner combiner, rb_allocfunc allocfunc, rb_freefunc freefunc, void *arg)
Definition: rbtree.c:110
RBNode *(* rb_allocfunc)(void *arg)
Definition: rbtree.h:62
Definition: rbtree.c:41
RBNode * rb_leftmost(RBTree *rb)
Definition: rbtree.c:181
void * arg