PostgreSQL Source Code  git master
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 */
40 
41 /*
42  * RBTreeIterator holds state while traversing a tree. This is declared
43  * here so that callers can stack-allocate this, but must otherwise be
44  * treated as an opaque struct.
45  */
47 
49 {
51  RBNode *(*iterate) (RBTreeIterator *iter);
53  bool is_over;
54 };
55 
56 /* Support functions to be provided by caller */
57 typedef int (*rb_comparator) (const RBNode *a, const RBNode *b, void *arg);
58 typedef void (*rb_combiner) (RBNode *existing, const RBNode *newdata, void *arg);
59 typedef RBNode *(*rb_allocfunc) (void *arg);
60 typedef void (*rb_freefunc) (RBNode *x, void *arg);
61 
62 extern RBTree *rb_create(Size node_size,
63  rb_comparator comparator,
64  rb_combiner combiner,
65  rb_allocfunc allocfunc,
66  rb_freefunc freefunc,
67  void *arg);
68 
69 extern RBNode *rb_find(RBTree *rb, const RBNode *data);
70 extern RBNode *rb_leftmost(RBTree *rb);
71 
72 extern RBNode *rb_insert(RBTree *rb, const RBNode *data, bool *isNew);
73 extern void rb_delete(RBTree *rb, RBNode *node);
74 
75 extern void rb_begin_iterate(RBTree *rb, RBOrderControl ctrl,
76  RBTreeIterator *iter);
77 extern RBNode *rb_iterate(RBTreeIterator *iter);
78 
79 #endif /* RBTREE_H */
struct RBNode * left
Definition: rbtree.h:26
struct RBNode RBNode
RBNode * rb_iterate(RBTreeIterator *iter)
Definition: rbtree.c:761
RBNode * last_visited
Definition: rbtree.h:52
struct RBNode * right
Definition: rbtree.h:27
RBNode * rb_find(RBTree *rb, const RBNode *data)
Definition: rbtree.c:142
void rb_delete(RBTree *rb, RBNode *node)
Definition: rbtree.c:630
struct RBNode * parent
Definition: rbtree.h:28
Definition: rbtree.h:23
void rb_begin_iterate(RBTree *rb, RBOrderControl ctrl, RBTreeIterator *iter)
Definition: rbtree.c:737
bool is_over
Definition: rbtree.h:53
char color
Definition: rbtree.h:25
RBNode * rb_insert(RBTree *rb, const RBNode *data, bool *isNew)
Definition: rbtree.c:388
RBTree * rb
Definition: rbtree.h:50
RBOrderControl
Definition: rbtree.h:35
size_t Size
Definition: c.h:404
RBTree * rb_create(Size node_size, rb_comparator comparator, rb_combiner combiner, rb_allocfunc allocfunc, rb_freefunc freefunc, void *arg)
Definition: rbtree.c:99
int(* rb_comparator)(const RBNode *a, const RBNode *b, void *arg)
Definition: rbtree.h:57
void(* rb_freefunc)(RBNode *x, void *arg)
Definition: rbtree.h:60
void(* rb_combiner)(RBNode *existing, const RBNode *newdata, void *arg)
Definition: rbtree.h:58
Definition: rbtree.c:41
RBNode * rb_leftmost(RBTree *rb)
Definition: rbtree.c:170
void * arg
RBNode *(* rb_allocfunc)(void *arg)
Definition: rbtree.h:59