2010-02-11 15:29:50 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* rbtree.h
|
|
|
|
* interface for PostgreSQL generic Red-Black binary tree package
|
|
|
|
*
|
2022-01-08 01:04:57 +01:00
|
|
|
* Copyright (c) 2009-2022, 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
|
|
|
/*
|
2018-11-06 19:25:24 +01:00
|
|
|
* RBTNode is intended to be used as the first field of a larger struct,
|
2010-08-01 04:12:42 +02:00
|
|
|
* whose additional fields carry whatever payload data the caller needs
|
|
|
|
* for a tree entry. (The total size of that larger struct is passed to
|
2018-11-06 19:25:24 +01:00
|
|
|
* rbt_create.) RBTNode is declared here to support this usage, but
|
2010-08-01 04:12:42 +02:00
|
|
|
* callers must treat it as an opaque struct.
|
|
|
|
*/
|
2018-11-06 19:25:24 +01:00
|
|
|
typedef struct RBTNode
|
2010-08-01 04:12:42 +02:00
|
|
|
{
|
|
|
|
char color; /* node's current color, red or black */
|
2018-11-06 19:25:24 +01:00
|
|
|
struct RBTNode *left; /* left child, or RBTNIL if none */
|
|
|
|
struct RBTNode *right; /* right child, or RBTNIL if none */
|
|
|
|
struct RBTNode *parent; /* parent, or NULL (not RBTNIL!) if none */
|
|
|
|
} RBTNode;
|
2010-08-01 04:12:42 +02:00
|
|
|
|
|
|
|
/* 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 */
|
2018-11-06 19:25:24 +01:00
|
|
|
typedef enum RBTOrderControl
|
2010-08-01 04:12:42 +02:00
|
|
|
{
|
|
|
|
LeftRightWalk, /* inorder: left child, node, right child */
|
2017-09-10 19:19:11 +02:00
|
|
|
RightLeftWalk /* reverse inorder: right, node, left */
|
2018-11-06 19:25:24 +01:00
|
|
|
} RBTOrderControl;
|
2010-08-01 04:12:42 +02:00
|
|
|
|
2016-09-02 07:39:39 +02:00
|
|
|
/*
|
|
|
|
* RBTreeIterator holds state while traversing a tree. This is declared
|
|
|
|
* here so that callers can stack-allocate this, but must otherwise be
|
|
|
|
* treated as an opaque struct.
|
|
|
|
*/
|
|
|
|
typedef struct RBTreeIterator RBTreeIterator;
|
|
|
|
|
|
|
|
struct RBTreeIterator
|
|
|
|
{
|
2018-11-06 19:25:24 +01:00
|
|
|
RBTree *rbt;
|
|
|
|
RBTNode *(*iterate) (RBTreeIterator *iter);
|
|
|
|
RBTNode *last_visited;
|
2016-09-02 07:39:39 +02:00
|
|
|
bool is_over;
|
|
|
|
};
|
|
|
|
|
2010-08-01 04:12:42 +02:00
|
|
|
/* Support functions to be provided by caller */
|
2018-11-06 19:25:24 +01:00
|
|
|
typedef int (*rbt_comparator) (const RBTNode *a, const RBTNode *b, void *arg);
|
|
|
|
typedef void (*rbt_combiner) (RBTNode *existing, const RBTNode *newdata, void *arg);
|
|
|
|
typedef RBTNode *(*rbt_allocfunc) (void *arg);
|
|
|
|
typedef void (*rbt_freefunc) (RBTNode *x, void *arg);
|
2010-02-11 15:29:50 +01:00
|
|
|
|
2018-11-06 19:25:24 +01:00
|
|
|
extern RBTree *rbt_create(Size node_size,
|
|
|
|
rbt_comparator comparator,
|
|
|
|
rbt_combiner combiner,
|
|
|
|
rbt_allocfunc allocfunc,
|
|
|
|
rbt_freefunc freefunc,
|
|
|
|
void *arg);
|
2010-02-11 15:29:50 +01:00
|
|
|
|
2018-11-06 19:25:24 +01:00
|
|
|
extern RBTNode *rbt_find(RBTree *rbt, const RBTNode *data);
|
|
|
|
extern RBTNode *rbt_leftmost(RBTree *rbt);
|
2010-02-11 15:29:50 +01:00
|
|
|
|
2018-11-06 19:25:24 +01:00
|
|
|
extern RBTNode *rbt_insert(RBTree *rbt, const RBTNode *data, bool *isNew);
|
|
|
|
extern void rbt_delete(RBTree *rbt, RBTNode *node);
|
2010-02-11 15:29:50 +01:00
|
|
|
|
2018-11-06 19:25:24 +01:00
|
|
|
extern void rbt_begin_iterate(RBTree *rbt, RBTOrderControl ctrl,
|
|
|
|
RBTreeIterator *iter);
|
|
|
|
extern RBTNode *rbt_iterate(RBTreeIterator *iter);
|
2010-02-11 15:29:50 +01:00
|
|
|
|
2010-08-01 04:12:42 +02:00
|
|
|
#endif /* RBTREE_H */
|