2
0

binaryheap.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * binaryheap.h
  3. *
  4. * A simple binary heap implementation
  5. *
  6. * Portions Copyright (c) 2012-2022, PostgreSQL Global Development Group
  7. *
  8. * src/include/lib/binaryheap.h
  9. */
  10. #ifndef BINARYHEAP_H
  11. #define BINARYHEAP_H
  12. /*
  13. * For a max-heap, the comparator must return <0 iff a < b, 0 iff a == b,
  14. * and >0 iff a > b. For a min-heap, the conditions are reversed.
  15. */
  16. typedef int (*binaryheap_comparator) (Datum a, Datum b, void *arg);
  17. /*
  18. * binaryheap
  19. *
  20. * bh_size how many nodes are currently in "nodes"
  21. * bh_space how many nodes can be stored in "nodes"
  22. * bh_has_heap_property no unordered operations since last heap build
  23. * bh_compare comparison function to define the heap property
  24. * bh_arg user data for comparison function
  25. * bh_nodes variable-length array of "space" nodes
  26. */
  27. typedef struct binaryheap
  28. {
  29. int bh_size;
  30. int bh_space;
  31. bool bh_has_heap_property; /* debugging cross-check */
  32. binaryheap_comparator bh_compare;
  33. void *bh_arg;
  34. Datum bh_nodes[FLEXIBLE_ARRAY_MEMBER];
  35. } binaryheap;
  36. extern binaryheap *binaryheap_allocate(int capacity,
  37. binaryheap_comparator compare,
  38. void *arg);
  39. extern void binaryheap_reset(binaryheap *heap);
  40. extern void binaryheap_free(binaryheap *heap);
  41. extern void binaryheap_add_unordered(binaryheap *heap, Datum d);
  42. extern void binaryheap_build(binaryheap *heap);
  43. extern void binaryheap_add(binaryheap *heap, Datum d);
  44. extern Datum binaryheap_first(binaryheap *heap);
  45. extern Datum binaryheap_remove_first(binaryheap *heap);
  46. extern void binaryheap_replace_first(binaryheap *heap, Datum d);
  47. #define binaryheap_empty(h) ((h)->bh_size == 0)
  48. #endif /* BINARYHEAP_H */