heaputil.h 1.2 KB

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