rbtree.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*-------------------------------------------------------------------------
  2. *
  3. * rbtree.h
  4. * interface for PostgreSQL generic Red-Black binary tree package
  5. *
  6. * Copyright (c) 2009-2022, 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. * RBTNode is intended to be used as the first field of a larger struct,
  17. * whose additional fields carry whatever payload data the caller needs
  18. * for a tree entry. (The total size of that larger struct is passed to
  19. * rbt_create.) RBTNode is declared here to support this usage, but
  20. * callers must treat it as an opaque struct.
  21. */
  22. typedef struct RBTNode
  23. {
  24. char color; /* node's current color, red or black */
  25. struct RBTNode *left; /* left child, or RBTNIL if none */
  26. struct RBTNode *right; /* right child, or RBTNIL if none */
  27. struct RBTNode *parent; /* parent, or NULL (not RBTNIL!) if none */
  28. } RBTNode;
  29. /* Opaque struct representing a whole tree */
  30. typedef struct RBTree RBTree;
  31. /* Available tree iteration orderings */
  32. typedef enum RBTOrderControl
  33. {
  34. LeftRightWalk, /* inorder: left child, node, right child */
  35. RightLeftWalk /* reverse inorder: right, node, left */
  36. } RBTOrderControl;
  37. /*
  38. * RBTreeIterator holds state while traversing a tree. This is declared
  39. * here so that callers can stack-allocate this, but must otherwise be
  40. * treated as an opaque struct.
  41. */
  42. typedef struct RBTreeIterator RBTreeIterator;
  43. struct RBTreeIterator
  44. {
  45. RBTree *rbt;
  46. RBTNode *(*iterate) (RBTreeIterator *iter);
  47. RBTNode *last_visited;
  48. bool is_over;
  49. };
  50. /* Support functions to be provided by caller */
  51. typedef int (*rbt_comparator) (const RBTNode *a, const RBTNode *b, void *arg);
  52. typedef void (*rbt_combiner) (RBTNode *existing, const RBTNode *newdata, void *arg);
  53. typedef RBTNode *(*rbt_allocfunc) (void *arg);
  54. typedef void (*rbt_freefunc) (RBTNode *x, void *arg);
  55. extern RBTree *rbt_create(Size node_size,
  56. rbt_comparator comparator,
  57. rbt_combiner combiner,
  58. rbt_allocfunc allocfunc,
  59. rbt_freefunc freefunc,
  60. void *arg);
  61. extern RBTNode *rbt_find(RBTree *rbt, const RBTNode *data);
  62. extern RBTNode *rbt_leftmost(RBTree *rbt);
  63. extern RBTNode *rbt_insert(RBTree *rbt, const RBTNode *data, bool *isNew);
  64. extern void rbt_delete(RBTree *rbt, RBTNode *node);
  65. extern void rbt_begin_iterate(RBTree *rbt, RBTOrderControl ctrl,
  66. RBTreeIterator *iter);
  67. extern RBTNode *rbt_iterate(RBTreeIterator *iter);
  68. #endif /* RBTREE_H */