4 * A Pairing Heap implementation
6 * Portions Copyright (c) 2012-2025, PostgreSQL Global Development Group
8 * src/include/lib/pairingheap.h
16/* Enable if you need the pairingheap_dump() debug function */
17/* #define PAIRINGHEAP_DEBUG */
20 * This represents an element stored in the heap. Embed this in a larger
21 * struct containing the actual data you're storing.
23 * A node can have multiple children, which form a double-linked list.
24 * first_child points to the node's first child, and the subsequent children
25 * can be found by following the next_sibling pointers. The last child has
26 * next_sibling == NULL. The prev_or_parent pointer points to the node's
27 * previous sibling, or if the node is its parent's first child, to the
38 * Return the containing struct of 'type' where 'membername' is the
39 * pairingheap_node pointed at by 'ptr'.
41 * This is used to convert a pairingheap_node * back to its containing struct.
43 #define pairingheap_container(type, membername, ptr) \
44 (AssertVariableIsOfTypeMacro(ptr, pairingheap_node *), \
45 AssertVariableIsOfTypeMacro(((type *) NULL)->membername, pairingheap_node), \
46 ((type *) ((char *) (ptr) - offsetof(type, membername))))
49 * Like pairingheap_container, but used when the pointer is 'const ptr'
51 #define pairingheap_const_container(type, membername, ptr) \
52 (AssertVariableIsOfTypeMacro(ptr, const pairingheap_node *), \
53 AssertVariableIsOfTypeMacro(((type *) NULL)->membername, pairingheap_node), \
54 ((const type *) ((const char *) (ptr) - offsetof(type, membername))))
57 * For a max-heap, the comparator must return <0 iff a < b, 0 iff a == b,
58 * and >0 iff a > b. For a min-heap, the conditions are reversed.
67 * You can use pairingheap_allocate() to create a new palloc'd heap, or embed
68 * this in a larger struct, set ph_compare and ph_arg directly and initialize
74 void *
ph_arg;
/* opaque argument to ph_compare */
86#ifdef PAIRINGHEAP_DEBUG
92/* Resets the heap to be empty. */
93 #define pairingheap_reset(h) ((h)->ph_root = NULL)
95/* Is the heap empty? */
96 #define pairingheap_is_empty(h) ((h)->ph_root == NULL)
98/* Is there exactly one node in the heap? */
99 #define pairingheap_is_singular(h) \
100 ((h)->ph_root && (h)->ph_root->first_child == NULL)
102#endif /* PAIRINGHEAP_H */
static int compare(const void *arg1, const void *arg2)
void pairingheap_remove(pairingheap *heap, pairingheap_node *node)
void pairingheap_add(pairingheap *heap, pairingheap_node *node)
struct pairingheap pairingheap
pairingheap * pairingheap_allocate(pairingheap_comparator compare, void *arg)
int(* pairingheap_comparator)(const pairingheap_node *a, const pairingheap_node *b, void *arg)
pairingheap_node * pairingheap_remove_first(pairingheap *heap)
void pairingheap_free(pairingheap *heap)
struct pairingheap_node pairingheap_node
pairingheap_node * pairingheap_first(pairingheap *heap)
struct pairingheap_node * next_sibling
struct pairingheap_node * prev_or_parent
struct pairingheap_node * first_child
pairingheap_comparator ph_compare
pairingheap_node * ph_root