rbtree.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*-------------------------------------------------------------------------
  2. *
  3. * rbtree.h
  4. * interface for PostgreSQL generic Red-Black binary tree package
  5. *
  6. * Copyright (c) 2009-2016, 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. * RBNode 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. * rb_create.) RBNode is declared here to support this usage, but
  20. * callers must treat it as an opaque struct.
  21. */
  22. typedef struct RBNode
  23. {
  24. char iteratorState; /* workspace for iterating through tree */
  25. char color; /* node's current color, red or black */
  26. struct RBNode *left; /* left child, or RBNIL if none */
  27. struct RBNode *right; /* right child, or RBNIL if none */
  28. struct RBNode *parent; /* parent, or NULL (not RBNIL!) if none */
  29. } RBNode;
  30. /* Opaque struct representing a whole tree */
  31. typedef struct RBTree RBTree;
  32. /* Available tree iteration orderings */
  33. typedef enum RBOrderControl
  34. {
  35. LeftRightWalk, /* inorder: left child, node, right child */
  36. RightLeftWalk, /* reverse inorder: right, node, left */
  37. DirectWalk, /* preorder: node, left child, right child */
  38. InvertedWalk /* postorder: left child, right child, node */
  39. } RBOrderControl;
  40. /* Support functions to be provided by caller */
  41. typedef int (*rb_comparator) (const RBNode *a, const RBNode *b, void *arg);
  42. typedef void (*rb_combiner) (RBNode *existing, const RBNode *newdata, void *arg);
  43. typedef RBNode *(*rb_allocfunc) (void *arg);
  44. typedef void (*rb_freefunc) (RBNode *x, void *arg);
  45. extern RBTree *rb_create(Size node_size,
  46. rb_comparator comparator,
  47. rb_combiner combiner,
  48. rb_allocfunc allocfunc,
  49. rb_freefunc freefunc,
  50. void *arg);
  51. extern RBNode *rb_find(RBTree *rb, const RBNode *data);
  52. extern RBNode *rb_leftmost(RBTree *rb);
  53. extern RBNode *rb_insert(RBTree *rb, const RBNode *data, bool *isNew);
  54. extern void rb_delete(RBTree *rb, RBNode *node);
  55. extern void rb_begin_iterate(RBTree *rb, RBOrderControl ctrl);
  56. extern RBNode *rb_iterate(RBTree *rb);
  57. #endif /* RBTREE_H */