ptrheap.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef PTRHEAP_H_
  2. #define PTRHEAP_H_
  3. #include <stddef.h>
  4. /**
  5. * Pointer-heap data structure. Arbitrary pointers can be inserted and are
  6. * compared using a provided callback; the usual heapy getmin / increasemin /
  7. * deletemin algorithms are supported. To use three additional functions,
  8. * ptrheap_{delete, increase, decrease}, a setreccookie callback needs to be
  9. * provided. These functions require a record cookie to identify the element
  10. * to operate upon; each time a record's record cookie changes, the
  11. * setreccookie callback will be called. Functions return NULL or (int)(-1)
  12. * on error and set errno; other return types indicate that failure is not
  13. * possible. On error, the heap will be unmodified.
  14. */
  15. /* Opaque pointer-heap type. */
  16. struct ptrheap;
  17. /**
  18. * ptrheap_init(compar, setreccookie, cookie):
  19. * Create and return an empty heap. The function ${compar}(${cookie}, x, y)
  20. * should return less than, equal to, or greater than 0 depending on whether
  21. * x is less than, equal to, or greater than y; and if ${setreccookie} is
  22. * non-zero it will be called as ${setreccookie}(${cookie}, ${ptr}, ${rc}) to
  23. * indicate that the value ${rc} is the current record cookie for the pointer
  24. * ${ptr}. The function ${setreccookie} may not make any ptrheap_* calls.
  25. */
  26. struct ptrheap * ptrheap_init(int (*)(void *, const void *, const void *),
  27. void (*)(void *, void *, size_t), void *);
  28. /**
  29. * ptrheap_create(compar, setreccookie, cookie, N, ptrs):
  30. * Create and return a heap, as in ptrheap_init(), but with the ${N} pointers
  31. * in ${ptrs} as heap elements. This is faster than creating an empty heap
  32. * and adding the elements individually.
  33. */
  34. struct ptrheap * ptrheap_create(int (*)(void *, const void *, const void *),
  35. void (*)(void *, void *, size_t), void *, size_t, void **);
  36. /**
  37. * ptrheap_add(H, ptr):
  38. * Add the pointer ${ptr} to the heap ${H}.
  39. */
  40. int ptrheap_add(struct ptrheap *, void *);
  41. /**
  42. * ptrheap_getmin(H):
  43. * Return the minimum pointer in the heap ${H}. If the heap is empty, NULL
  44. * is returned.
  45. */
  46. void * ptrheap_getmin(struct ptrheap *);
  47. /**
  48. * ptrheap_delete(H, rc):
  49. * Delete from the heap ${H} the element ptr for which the function call
  50. * setreccookie(cookie, ptr, ${rc}) was most recently made.
  51. */
  52. void ptrheap_delete(struct ptrheap *, size_t);
  53. /**
  54. * ptrheap_deletemin(H):
  55. * Delete the minimum element in the heap ${H}. The heap must not be empty.
  56. */
  57. void ptrheap_deletemin(struct ptrheap *);
  58. /**
  59. * ptrheap_decrease(H, rc):
  60. * Adjust the heap ${H} to account for the fact that the element ptr for
  61. * which the function call setreccookie(cookie, ptr, ${rc}) was most recently
  62. * made has decreased.
  63. */
  64. void ptrheap_decrease(struct ptrheap *, size_t);
  65. /**
  66. * ptrheap_increase(H, rc):
  67. * Adjust the heap ${H} to account for the fact that the element ptr for
  68. * which the function call setreccookie(cookie, ptr, ${rc}) was most recently
  69. * made has increased.
  70. */
  71. void ptrheap_increase(struct ptrheap *, size_t);
  72. /**
  73. * ptrheap_increasemin(H):
  74. * Adjust the heap ${H} to account for the fact that the (formerly) minimum
  75. * element has increased.
  76. */
  77. void ptrheap_increasemin(struct ptrheap *);
  78. /**
  79. * ptrheap_free(H):
  80. * Free the pointer heap ${H}.
  81. */
  82. void ptrheap_free(struct ptrheap *);
  83. #endif /* !PTRHEAP_H_ */