heaputil.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * category: [data structure]
  3. * apply status: timerqueue
  4. * edit status: not
  5. * build status:
  6. * description:
  7. */
  8. #ifndef HEAPUTIL_H
  9. #define HEAPUTIL_H
  10. #pragma once
  11. #include "config.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /* array based heap helper */
  16. #define heap_parent(x) (((x)-1)/2)
  17. #define heap_left_child(x) ((x)*2+1)
  18. #define heap_right_child(x) ((x)*2+2)
  19. /* calculate heapelement priority, this is the min heap implementation based on the priority */
  20. typedef int (*heap_priority_cmp)(void*, void*);
  21. /** adjust up */
  22. TOOLKIT_API void heap_up(void**arr, int arr_n, int slot, heap_priority_cmp prio_cmp);
  23. /** adjust down */
  24. TOOLKIT_API void heap_down(void**arr, int arr_n, int slot, heap_priority_cmp prio_cmp);
  25. /**
  26. * arr must have enougth space to place obj
  27. * @param arr heap array
  28. * @param arr_n [in,out] elements in the array
  29. * @param obj object to insert
  30. * @param prio_fn get priority from object
  31. */
  32. TOOLKIT_API void heap_push(void**arr, int *arr_n, void* obj, heap_priority_cmp prio_cmp);
  33. /**
  34. * @param arr heap array
  35. * @param arr_n [in,out] elements in the array
  36. * @param prio_fn get priority from object
  37. * @return root value from heap
  38. */
  39. TOOLKIT_API void* heap_pop(void** arr, int *arr_n, heap_priority_cmp prio_cmp);
  40. #ifdef __cplusplus
  41. } // extern "C" {
  42. #endif
  43. #endif // HEAPUTIL_H