2010-02-11 15:29:50 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* rbtree.h
|
2010-02-26 03:01:40 +01:00
|
|
|
* interface for PostgreSQL generic Red-Black binary tree package
|
2010-02-11 15:29:50 +01:00
|
|
|
*
|
2016-01-02 19:33:40 +01:00
|
|
|
* Copyright (c) 2009-2016, PostgreSQL Global Development Group
|
2010-02-11 15:29:50 +01:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2014-12-22 16:52:08 +01:00
|
|
|
* src/include/lib/rbtree.h
|
2010-02-11 15:29:50 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef RBTREE_H
|
|
|
|
#define RBTREE_H
|
|
|
|
|
2010-08-01 04:12:42 +02:00
|
|
|
/*
|
|
|
|
* RBNode is intended to be used as the first field of a larger struct,
|
|
|
|
* whose additional fields carry whatever payload data the caller needs
|
|
|
|
* for a tree entry. (The total size of that larger struct is passed to
|
2011-04-10 17:42:00 +02:00
|
|
|
* rb_create.) RBNode is declared here to support this usage, but
|
2010-08-01 04:12:42 +02:00
|
|
|
* callers must treat it as an opaque struct.
|
|
|
|
*/
|
|
|
|
typedef struct RBNode
|
|
|
|
{
|
|
|
|
char iteratorState; /* workspace for iterating through tree */
|
2011-04-10 17:42:00 +02:00
|
|
|
char color; /* node's current color, red or black */
|
2010-08-01 04:12:42 +02:00
|
|
|
struct RBNode *left; /* left child, or RBNIL if none */
|
|
|
|
struct RBNode *right; /* right child, or RBNIL if none */
|
|
|
|
struct RBNode *parent; /* parent, or NULL (not RBNIL!) if none */
|
|
|
|
} RBNode;
|
|
|
|
|
|
|
|
/* Opaque struct representing a whole tree */
|
2010-02-11 15:29:50 +01:00
|
|
|
typedef struct RBTree RBTree;
|
|
|
|
|
2010-08-01 04:12:42 +02:00
|
|
|
/* Available tree iteration orderings */
|
|
|
|
typedef enum RBOrderControl
|
|
|
|
{
|
|
|
|
LeftRightWalk, /* inorder: left child, node, right child */
|
|
|
|
RightLeftWalk, /* reverse inorder: right, node, left */
|
|
|
|
DirectWalk, /* preorder: node, left child, right child */
|
|
|
|
InvertedWalk /* postorder: left child, right child, node */
|
|
|
|
} RBOrderControl;
|
|
|
|
|
|
|
|
/* Support functions to be provided by caller */
|
|
|
|
typedef int (*rb_comparator) (const RBNode *a, const RBNode *b, void *arg);
|
|
|
|
typedef void (*rb_combiner) (RBNode *existing, const RBNode *newdata, void *arg);
|
|
|
|
typedef RBNode *(*rb_allocfunc) (void *arg);
|
|
|
|
typedef void (*rb_freefunc) (RBNode *x, void *arg);
|
2010-02-11 15:29:50 +01:00
|
|
|
|
2010-08-01 04:12:42 +02:00
|
|
|
extern RBTree *rb_create(Size node_size,
|
|
|
|
rb_comparator comparator,
|
|
|
|
rb_combiner combiner,
|
|
|
|
rb_allocfunc allocfunc,
|
2010-02-26 03:01:40 +01:00
|
|
|
rb_freefunc freefunc,
|
|
|
|
void *arg);
|
2010-02-11 15:29:50 +01:00
|
|
|
|
2010-08-01 04:12:42 +02:00
|
|
|
extern RBNode *rb_find(RBTree *rb, const RBNode *data);
|
|
|
|
extern RBNode *rb_leftmost(RBTree *rb);
|
2010-02-11 15:29:50 +01:00
|
|
|
|
2010-08-01 04:12:42 +02:00
|
|
|
extern RBNode *rb_insert(RBTree *rb, const RBNode *data, bool *isNew);
|
|
|
|
extern void rb_delete(RBTree *rb, RBNode *node);
|
2010-02-11 15:29:50 +01:00
|
|
|
|
2010-08-01 04:12:42 +02:00
|
|
|
extern void rb_begin_iterate(RBTree *rb, RBOrderControl ctrl);
|
|
|
|
extern RBNode *rb_iterate(RBTree *rb);
|
2010-02-11 15:29:50 +01:00
|
|
|
|
2011-04-10 17:42:00 +02:00
|
|
|
#endif /* RBTREE_H */
|