Header And Logo

PostgreSQL
| The world's most advanced open source database.

rbtree.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * rbtree.h
00004  *    interface for PostgreSQL generic Red-Black binary tree package
00005  *
00006  * Copyright (c) 2009-2013, PostgreSQL Global Development Group
00007  *
00008  * IDENTIFICATION
00009  *      src/include/utils/rbtree.h
00010  *
00011  *-------------------------------------------------------------------------
00012  */
00013 #ifndef RBTREE_H
00014 #define RBTREE_H
00015 
00016 /*
00017  * RBNode is intended to be used as the first field of a larger struct,
00018  * whose additional fields carry whatever payload data the caller needs
00019  * for a tree entry.  (The total size of that larger struct is passed to
00020  * rb_create.)  RBNode is declared here to support this usage, but
00021  * callers must treat it as an opaque struct.
00022  */
00023 typedef struct RBNode
00024 {
00025     char        iteratorState;  /* workspace for iterating through tree */
00026     char color;                 /* node's current color, red or black */
00027     struct RBNode *left;        /* left child, or RBNIL if none */
00028     struct RBNode *right;       /* right child, or RBNIL if none */
00029     struct RBNode *parent;      /* parent, or NULL (not RBNIL!) if none */
00030 } RBNode;
00031 
00032 /* Opaque struct representing a whole tree */
00033 typedef struct RBTree RBTree;
00034 
00035 /* Available tree iteration orderings */
00036 typedef enum RBOrderControl
00037 {
00038     LeftRightWalk,              /* inorder: left child, node, right child */
00039     RightLeftWalk,              /* reverse inorder: right, node, left */
00040     DirectWalk,                 /* preorder: node, left child, right child */
00041     InvertedWalk                /* postorder: left child, right child, node */
00042 } RBOrderControl;
00043 
00044 /* Support functions to be provided by caller */
00045 typedef int (*rb_comparator) (const RBNode *a, const RBNode *b, void *arg);
00046 typedef void (*rb_combiner) (RBNode *existing, const RBNode *newdata, void *arg);
00047 typedef RBNode *(*rb_allocfunc) (void *arg);
00048 typedef void (*rb_freefunc) (RBNode *x, void *arg);
00049 
00050 extern RBTree *rb_create(Size node_size,
00051           rb_comparator comparator,
00052           rb_combiner combiner,
00053           rb_allocfunc allocfunc,
00054           rb_freefunc freefunc,
00055           void *arg);
00056 
00057 extern RBNode *rb_find(RBTree *rb, const RBNode *data);
00058 extern RBNode *rb_leftmost(RBTree *rb);
00059 
00060 extern RBNode *rb_insert(RBTree *rb, const RBNode *data, bool *isNew);
00061 extern void rb_delete(RBTree *rb, RBNode *node);
00062 
00063 extern void rb_begin_iterate(RBTree *rb, RBOrderControl ctrl);
00064 extern RBNode *rb_iterate(RBTree *rb);
00065 
00066 #endif   /* RBTREE_H */