pairingheap.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * pairingheap.h
  3. *
  4. * A Pairing Heap implementation
  5. *
  6. * Portions Copyright (c) 2012-2016, PostgreSQL Global Development Group
  7. *
  8. * src/include/lib/pairingheap.h
  9. */
  10. #ifndef PAIRINGHEAP_H
  11. #define PAIRINGHEAP_H
  12. #include "lib/stringinfo.h"
  13. /* Enable if you need the pairingheap_dump() debug function */
  14. /* #define PAIRINGHEAP_DEBUG */
  15. /*
  16. * This represents an element stored in the heap. Embed this in a larger
  17. * struct containing the actual data you're storing.
  18. *
  19. * A node can have multiple children, which form a double-linked list.
  20. * first_child points to the node's first child, and the subsequent children
  21. * can be found by following the next_sibling pointers. The last child has
  22. * next_sibling == NULL. The prev_or_parent pointer points to the node's
  23. * previous sibling, or if the node is its parent's first child, to the
  24. * parent.
  25. */
  26. typedef struct pairingheap_node
  27. {
  28. struct pairingheap_node *first_child;
  29. struct pairingheap_node *next_sibling;
  30. struct pairingheap_node *prev_or_parent;
  31. } pairingheap_node;
  32. /*
  33. * Return the containing struct of 'type' where 'membername' is the
  34. * pairingheap_node pointed at by 'ptr'.
  35. *
  36. * This is used to convert a pairingheap_node * back to its containing struct.
  37. */
  38. #define pairingheap_container(type, membername, ptr) \
  39. (AssertVariableIsOfTypeMacro(ptr, pairingheap_node *), \
  40. AssertVariableIsOfTypeMacro(((type *) NULL)->membername, pairingheap_node), \
  41. ((type *) ((char *) (ptr) - offsetof(type, membername))))
  42. /*
  43. * Like pairingheap_container, but used when the pointer is 'const ptr'
  44. */
  45. #define pairingheap_const_container(type, membername, ptr) \
  46. (AssertVariableIsOfTypeMacro(ptr, const pairingheap_node *), \
  47. AssertVariableIsOfTypeMacro(((type *) NULL)->membername, pairingheap_node), \
  48. ((const type *) ((const char *) (ptr) - offsetof(type, membername))))
  49. /*
  50. * For a max-heap, the comparator must return <0 iff a < b, 0 iff a == b,
  51. * and >0 iff a > b. For a min-heap, the conditions are reversed.
  52. */
  53. typedef int (*pairingheap_comparator) (const pairingheap_node *a,
  54. const pairingheap_node *b,
  55. void *arg);
  56. /*
  57. * A pairing heap.
  58. *
  59. * You can use pairingheap_allocate() to create a new palloc'd heap, or embed
  60. * this in a larger struct, set ph_compare and ph_arg directly and initialize
  61. * ph_root to NULL.
  62. */
  63. typedef struct pairingheap
  64. {
  65. pairingheap_comparator ph_compare; /* comparison function */
  66. void *ph_arg; /* opaque argument to ph_compare */
  67. pairingheap_node *ph_root; /* current root of the heap */
  68. } pairingheap;
  69. extern pairingheap *pairingheap_allocate(pairingheap_comparator compare,
  70. void *arg);
  71. extern void pairingheap_free(pairingheap *heap);
  72. extern void pairingheap_add(pairingheap *heap, pairingheap_node *node);
  73. extern pairingheap_node *pairingheap_first(pairingheap *heap);
  74. extern pairingheap_node *pairingheap_remove_first(pairingheap *heap);
  75. extern void pairingheap_remove(pairingheap *heap, pairingheap_node *node);
  76. #ifdef PAIRINGHEAP_DEBUG
  77. extern char *pairingheap_dump(pairingheap *heap,
  78. void (*dumpfunc) (pairingheap_node *node, StringInfo buf, void *opaque),
  79. void *opaque);
  80. #endif
  81. /* Resets the heap to be empty. */
  82. #define pairingheap_reset(h) ((h)->ph_root = NULL)
  83. /* Is the heap empty? */
  84. #define pairingheap_is_empty(h) ((h)->ph_root == NULL)
  85. /* Is there exactly one node in the heap? */
  86. #define pairingheap_is_singular(h) \
  87. ((h)->ph_root && (h)->ph_root->first_child == NULL)
  88. #endif /* PAIRINGHEAP_H */