Commit 003c68a3 authored by Tom Lane's avatar Tom Lane

Rename rbtree.c functions to use "rbt" prefix not "rb" prefix.

The "rb" prefix is used by Ruby, so that our existing code results
in name collisions that break plruby.  We discussed ways to prevent
that by adjusting dynamic linker options, but it seems that at best
we'd move the pain to other cases.  Renaming to avoid the collision
is the only portable fix anyway.  Fortunately, our rbtree code is
not (yet?) widely used --- in core, there's only a single usage
in GIN --- so it seems likely that we can get away with a rename.

I chose to do this basically as s/rb/rbt/g, except for places where
there already was a "t" after "rb".  The patch could have been made
smaller by only touching linker-visible symbols, but it would have
resulted in oddly inconsistent-looking code.  Better to make it look
like "rbt" was the plan all along.

Back-patch to v10.  The rbtree.c code exists back to 9.5, but
rb_iterate() which is the actual immediate source of pain was added
in v10, so it seems like changing the names before that would have
more risk than benefit.

Per report from Pavel Raiskup.

Discussion: https://postgr.es/m/4738198.8KVIIDhgEB@nb.usersys.redhat.com
parent 8f623bed
......@@ -27,7 +27,7 @@
/* Combiner function for rbtree.c */
static void
ginCombineData(RBNode *existing, const RBNode *newdata, void *arg)
ginCombineData(RBTNode *existing, const RBTNode *newdata, void *arg)
{
GinEntryAccumulator *eo = (GinEntryAccumulator *) existing;
const GinEntryAccumulator *en = (const GinEntryAccumulator *) newdata;
......@@ -69,7 +69,7 @@ ginCombineData(RBNode *existing, const RBNode *newdata, void *arg)
/* Comparator function for rbtree.c */
static int
cmpEntryAccumulator(const RBNode *a, const RBNode *b, void *arg)
cmpEntryAccumulator(const RBTNode *a, const RBTNode *b, void *arg)
{
const GinEntryAccumulator *ea = (const GinEntryAccumulator *) a;
const GinEntryAccumulator *eb = (const GinEntryAccumulator *) b;
......@@ -81,7 +81,7 @@ cmpEntryAccumulator(const RBNode *a, const RBNode *b, void *arg)
}
/* Allocator function for rbtree.c */
static RBNode *
static RBTNode *
ginAllocEntryAccumulator(void *arg)
{
BuildAccumulator *accum = (BuildAccumulator *) arg;
......@@ -89,7 +89,7 @@ ginAllocEntryAccumulator(void *arg)
/*
* Allocate memory by rather big chunks to decrease overhead. We have no
* need to reclaim RBNodes individually, so this costs nothing.
* need to reclaim RBTNodes individually, so this costs nothing.
*/
if (accum->entryallocator == NULL || accum->eas_used >= DEF_NENTRY)
{
......@@ -98,11 +98,11 @@ ginAllocEntryAccumulator(void *arg)
accum->eas_used = 0;
}
/* Allocate new RBNode from current chunk */
/* Allocate new RBTNode from current chunk */
ea = accum->entryallocator + accum->eas_used;
accum->eas_used++;
return (RBNode *) ea;
return (RBTNode *) ea;
}
void
......@@ -112,12 +112,12 @@ ginInitBA(BuildAccumulator *accum)
accum->allocatedMemory = 0;
accum->entryallocator = NULL;
accum->eas_used = 0;
accum->tree = rb_create(sizeof(GinEntryAccumulator),
cmpEntryAccumulator,
ginCombineData,
ginAllocEntryAccumulator,
NULL, /* no freefunc needed */
(void *) accum);
accum->tree = rbt_create(sizeof(GinEntryAccumulator),
cmpEntryAccumulator,
ginCombineData,
ginAllocEntryAccumulator,
NULL, /* no freefunc needed */
(void *) accum);
}
/*
......@@ -163,8 +163,8 @@ ginInsertBAEntry(BuildAccumulator *accum,
/* temporarily set up single-entry itempointer list */
eatmp.list = heapptr;
ea = (GinEntryAccumulator *) rb_insert(accum->tree, (RBNode *) &eatmp,
&isNew);
ea = (GinEntryAccumulator *) rbt_insert(accum->tree, (RBTNode *) &eatmp,
&isNew);
if (isNew)
{
......@@ -256,7 +256,7 @@ qsortCompareItemPointers(const void *a, const void *b)
void
ginBeginBAScan(BuildAccumulator *accum)
{
rb_begin_iterate(accum->tree, LeftRightWalk, &accum->tree_walk);
rbt_begin_iterate(accum->tree, LeftRightWalk, &accum->tree_walk);
}
/*
......@@ -272,7 +272,7 @@ ginGetBAEntry(BuildAccumulator *accum,
GinEntryAccumulator *entry;
ItemPointerData *list;
entry = (GinEntryAccumulator *) rb_iterate(&accum->tree_walk);
entry = (GinEntryAccumulator *) rbt_iterate(&accum->tree_walk);
if (entry == NULL)
return NULL; /* no more entries */
......
This diff is collapsed.
......@@ -391,7 +391,7 @@ extern bool ginvalidate(Oid opclassoid);
/* ginbulk.c */
typedef struct GinEntryAccumulator
{
RBNode rbnode;
RBTNode rbtnode;
Datum key;
GinNullCategory category;
OffsetNumber attnum;
......
......@@ -14,29 +14,29 @@
#define RBTREE_H
/*
* RBNode is intended to be used as the first field of a larger struct,
* RBTNode 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
* rb_create.) RBNode is declared here to support this usage, but
* rbt_create.) RBTNode is declared here to support this usage, but
* callers must treat it as an opaque struct.
*/
typedef struct RBNode
typedef struct RBTNode
{
char color; /* node's current color, red or black */
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;
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;
/* Opaque struct representing a whole tree */
typedef struct RBTree RBTree;
/* Available tree iteration orderings */
typedef enum RBOrderControl
typedef enum RBTOrderControl
{
LeftRightWalk, /* inorder: left child, node, right child */
RightLeftWalk /* reverse inorder: right, node, left */
} RBOrderControl;
} RBTOrderControl;
/*
* RBTreeIterator holds state while traversing a tree. This is declared
......@@ -47,33 +47,33 @@ typedef struct RBTreeIterator RBTreeIterator;
struct RBTreeIterator
{
RBTree *rb;
RBNode *(*iterate) (RBTreeIterator *iter);
RBNode *last_visited;
RBTree *rbt;
RBTNode *(*iterate) (RBTreeIterator *iter);
RBTNode *last_visited;
bool is_over;
};
/* 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);
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);
extern RBTree *rb_create(Size node_size,
rb_comparator comparator,
rb_combiner combiner,
rb_allocfunc allocfunc,
rb_freefunc freefunc,
void *arg);
extern RBTree *rbt_create(Size node_size,
rbt_comparator comparator,
rbt_combiner combiner,
rbt_allocfunc allocfunc,
rbt_freefunc freefunc,
void *arg);
extern RBNode *rb_find(RBTree *rb, const RBNode *data);
extern RBNode *rb_leftmost(RBTree *rb);
extern RBTNode *rbt_find(RBTree *rbt, const RBTNode *data);
extern RBTNode *rbt_leftmost(RBTree *rbt);
extern RBNode *rb_insert(RBTree *rb, const RBNode *data, bool *isNew);
extern void rb_delete(RBTree *rb, RBNode *node);
extern RBTNode *rbt_insert(RBTree *rbt, const RBTNode *data, bool *isNew);
extern void rbt_delete(RBTree *rbt, RBTNode *node);
extern void rb_begin_iterate(RBTree *rb, RBOrderControl ctrl,
RBTreeIterator *iter);
extern RBNode *rb_iterate(RBTreeIterator *iter);
extern void rbt_begin_iterate(RBTree *rbt, RBTOrderControl ctrl,
RBTreeIterator *iter);
extern RBTNode *rbt_iterate(RBTreeIterator *iter);
#endif /* RBTREE_H */
......@@ -25,7 +25,7 @@ PG_MODULE_MAGIC;
*/
typedef struct IntRBTreeNode
{
RBNode rbnode;
RBTNode rbtnode;
int key;
} IntRBTreeNode;
......@@ -35,7 +35,7 @@ typedef struct IntRBTreeNode
* since none of our test keys are negative.
*/
static int
irb_cmp(const RBNode *a, const RBNode *b, void *arg)
irbt_cmp(const RBTNode *a, const RBTNode *b, void *arg)
{
const IntRBTreeNode *ea = (const IntRBTreeNode *) a;
const IntRBTreeNode *eb = (const IntRBTreeNode *) b;
......@@ -48,7 +48,7 @@ irb_cmp(const RBNode *a, const RBNode *b, void *arg)
* try to combine unequal keys.
*/
static void
irb_combine(RBNode *existing, const RBNode *newdata, void *arg)
irbt_combine(RBTNode *existing, const RBTNode *newdata, void *arg)
{
const IntRBTreeNode *eexist = (const IntRBTreeNode *) existing;
const IntRBTreeNode *enew = (const IntRBTreeNode *) newdata;
......@@ -59,15 +59,15 @@ irb_combine(RBNode *existing, const RBNode *newdata, void *arg)
}
/* Node allocator */
static RBNode *
irb_alloc(void *arg)
static RBTNode *
irbt_alloc(void *arg)
{
return (RBNode *) palloc(sizeof(IntRBTreeNode));
return (RBTNode *) palloc(sizeof(IntRBTreeNode));
}
/* Node freer */
static void
irb_free(RBNode *node, void *arg)
irbt_free(RBTNode *node, void *arg)
{
pfree(node);
}
......@@ -78,12 +78,12 @@ irb_free(RBNode *node, void *arg)
static RBTree *
create_int_rbtree(void)
{
return rb_create(sizeof(IntRBTreeNode),
irb_cmp,
irb_combine,
irb_alloc,
irb_free,
NULL);
return rbt_create(sizeof(IntRBTreeNode),
irbt_cmp,
irbt_combine,
irbt_alloc,
irbt_free,
NULL);
}
/*
......@@ -123,7 +123,7 @@ GetPermutation(int size)
* 0, step, 2*step, 3*step, ..., inserting them in random order
*/
static void
rb_populate(RBTree *tree, int size, int step)
rbt_populate(RBTree *tree, int size, int step)
{
int *permutation = GetPermutation(size);
IntRBTreeNode node;
......@@ -134,9 +134,9 @@ rb_populate(RBTree *tree, int size, int step)
for (i = 0; i < size; i++)
{
node.key = step * permutation[i];
rb_insert(tree, (RBNode *) &node, &isNew);
rbt_insert(tree, (RBTNode *) &node, &isNew);
if (!isNew)
elog(ERROR, "unexpected !isNew result from rb_insert");
elog(ERROR, "unexpected !isNew result from rbt_insert");
}
/*
......@@ -146,9 +146,9 @@ rb_populate(RBTree *tree, int size, int step)
if (size > 0)
{
node.key = step * permutation[0];
rb_insert(tree, (RBNode *) &node, &isNew);
rbt_insert(tree, (RBTNode *) &node, &isNew);
if (isNew)
elog(ERROR, "unexpected isNew result from rb_insert");
elog(ERROR, "unexpected isNew result from rbt_insert");
}
pfree(permutation);
......@@ -169,17 +169,17 @@ testleftright(int size)
int count = 0;
/* check iteration over empty tree */
rb_begin_iterate(tree, LeftRightWalk, &iter);
if (rb_iterate(&iter) != NULL)
rbt_begin_iterate(tree, LeftRightWalk, &iter);
if (rbt_iterate(&iter) != NULL)
elog(ERROR, "left-right walk over empty tree produced an element");
/* fill tree with consecutive natural numbers */
rb_populate(tree, size, 1);
rbt_populate(tree, size, 1);
/* iterate over the tree */
rb_begin_iterate(tree, LeftRightWalk, &iter);
rbt_begin_iterate(tree, LeftRightWalk, &iter);
while ((node = (IntRBTreeNode *) rb_iterate(&iter)) != NULL)
while ((node = (IntRBTreeNode *) rbt_iterate(&iter)) != NULL)
{
/* check that order is increasing */
if (node->key <= lastKey)
......@@ -209,17 +209,17 @@ testrightleft(int size)
int count = 0;
/* check iteration over empty tree */
rb_begin_iterate(tree, RightLeftWalk, &iter);
if (rb_iterate(&iter) != NULL)
rbt_begin_iterate(tree, RightLeftWalk, &iter);
if (rbt_iterate(&iter) != NULL)
elog(ERROR, "right-left walk over empty tree produced an element");
/* fill tree with consecutive natural numbers */
rb_populate(tree, size, 1);
rbt_populate(tree, size, 1);
/* iterate over the tree */
rb_begin_iterate(tree, RightLeftWalk, &iter);
rbt_begin_iterate(tree, RightLeftWalk, &iter);
while ((node = (IntRBTreeNode *) rb_iterate(&iter)) != NULL)
while ((node = (IntRBTreeNode *) rbt_iterate(&iter)) != NULL)
{
/* check that order is decreasing */
if (node->key >= lastKey)
......@@ -235,7 +235,7 @@ testrightleft(int size)
}
/*
* Check the correctness of the rb_find operation by searching for
* Check the correctness of the rbt_find operation by searching for
* both elements we inserted and elements we didn't.
*/
static void
......@@ -245,7 +245,7 @@ testfind(int size)
int i;
/* Insert even integers from 0 to 2 * (size-1) */
rb_populate(tree, size, 2);
rbt_populate(tree, size, 2);
/* Check that all inserted elements can be found */
for (i = 0; i < size; i++)
......@@ -254,7 +254,7 @@ testfind(int size)
IntRBTreeNode *resultNode;
node.key = 2 * i;
resultNode = (IntRBTreeNode *) rb_find(tree, (RBNode *) &node);
resultNode = (IntRBTreeNode *) rbt_find(tree, (RBTNode *) &node);
if (resultNode == NULL)
elog(ERROR, "inserted element was not found");
if (node.key != resultNode->key)
......@@ -271,14 +271,14 @@ testfind(int size)
IntRBTreeNode *resultNode;
node.key = i;
resultNode = (IntRBTreeNode *) rb_find(tree, (RBNode *) &node);
resultNode = (IntRBTreeNode *) rbt_find(tree, (RBTNode *) &node);
if (resultNode != NULL)
elog(ERROR, "not-inserted element was found");
}
}
/*
* Check the correctness of the rb_leftmost operation.
* Check the correctness of the rbt_leftmost operation.
* This operation should always return the smallest element of the tree.
*/
static void
......@@ -288,20 +288,20 @@ testleftmost(int size)
IntRBTreeNode *result;
/* Check that empty tree has no leftmost element */
if (rb_leftmost(tree) != NULL)
if (rbt_leftmost(tree) != NULL)
elog(ERROR, "leftmost node of empty tree is not NULL");
/* fill tree with consecutive natural numbers */
rb_populate(tree, size, 1);
rbt_populate(tree, size, 1);
/* Check that leftmost element is the smallest one */
result = (IntRBTreeNode *) rb_leftmost(tree);
result = (IntRBTreeNode *) rbt_leftmost(tree);
if (result == NULL || result->key != 0)
elog(ERROR, "rb_leftmost gave wrong result");
elog(ERROR, "rbt_leftmost gave wrong result");
}
/*
* Check the correctness of the rb_delete operation.
* Check the correctness of the rbt_delete operation.
*/
static void
testdelete(int size, int delsize)
......@@ -312,7 +312,7 @@ testdelete(int size, int delsize)
int i;
/* fill tree with consecutive natural numbers */
rb_populate(tree, size, 1);
rbt_populate(tree, size, 1);
/* Choose unique ids to delete */
deleteIds = (int *) palloc(delsize * sizeof(int));
......@@ -336,11 +336,11 @@ testdelete(int size, int delsize)
find.key = deleteIds[i];
/* Locate the node to be deleted */
node = (IntRBTreeNode *) rb_find(tree, (RBNode *) &find);
node = (IntRBTreeNode *) rbt_find(tree, (RBTNode *) &find);
if (node == NULL || node->key != deleteIds[i])
elog(ERROR, "expected element was not found during deleting");
/* Delete it */
rb_delete(tree, (RBNode *) node);
rbt_delete(tree, (RBTNode *) node);
}
/* Check that deleted elements are deleted */
......@@ -350,7 +350,7 @@ testdelete(int size, int delsize)
IntRBTreeNode *result;
node.key = i;
result = (IntRBTreeNode *) rb_find(tree, (RBNode *) &node);
result = (IntRBTreeNode *) rbt_find(tree, (RBTNode *) &node);
if (chosen[i])
{
/* Deleted element should be absent */
......@@ -375,15 +375,15 @@ testdelete(int size, int delsize)
continue;
find.key = i;
/* Locate the node to be deleted */
node = (IntRBTreeNode *) rb_find(tree, (RBNode *) &find);
node = (IntRBTreeNode *) rbt_find(tree, (RBTNode *) &find);
if (node == NULL || node->key != i)
elog(ERROR, "expected element was not found during deleting");
/* Delete it */
rb_delete(tree, (RBNode *) node);
rbt_delete(tree, (RBTNode *) node);
}
/* Tree should now be empty */
if (rb_leftmost(tree) != NULL)
if (rbt_leftmost(tree) != NULL)
elog(ERROR, "deleting all elements failed");
pfree(deleteIds);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment