heaputil.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "precompile.h"
  2. #include "heaputil.h"
  3. /* heap */
  4. static void swap_elem(void**arr, int x, int y)
  5. {
  6. void* t = arr[x];
  7. arr[x] = arr[y];
  8. arr[y] = t;
  9. }
  10. TOOLKIT_API void heap_up(void**arr, int arr_n, int slot, heap_priority_cmp prio_cmp)
  11. {
  12. int i;
  13. assert(arr);
  14. assert(prio_cmp);
  15. i = slot;
  16. while (i) {
  17. int p = heap_parent(i);
  18. if (prio_cmp(arr[i], arr[p]) < 0) {
  19. swap_elem(arr, i, p);
  20. i = p;
  21. } else {
  22. break;
  23. }
  24. }
  25. }
  26. TOOLKIT_API void heap_down(void**arr, int arr_n, int slot, heap_priority_cmp prio_cmp)
  27. {
  28. int i = slot;
  29. int n = arr_n;
  30. assert(arr);
  31. assert(prio_cmp);
  32. while (i < n) {
  33. int l = heap_left_child(i);
  34. int r = heap_right_child(i);
  35. if (r < n) {
  36. int min = (prio_cmp(arr[l], arr[r]) < 0) ? l : r;
  37. if (prio_cmp(arr[min], arr[i]) < 0) {
  38. swap_elem(arr, i, min);
  39. i = min;
  40. } else {
  41. break;
  42. }
  43. } else if (l < n) { /* r is out of range */
  44. if (prio_cmp(arr[l], arr[i]) < 0) {
  45. swap_elem(arr, i, l);
  46. i = l;
  47. }
  48. else{
  49. break;
  50. }
  51. } else {
  52. break;
  53. }
  54. }
  55. }
  56. TOOLKIT_API void heap_push(void**arr, int *arr_n, void* obj, heap_priority_cmp prio_cmp)
  57. {
  58. assert(arr);
  59. assert(arr_n);
  60. assert(obj);
  61. assert(prio_cmp);
  62. arr[*arr_n] = obj;
  63. ++*arr_n;
  64. heap_up(arr, *arr_n, *arr_n-1, prio_cmp);
  65. }
  66. TOOLKIT_API void* heap_pop(void** arr, int *arr_n, heap_priority_cmp prio_cmp)
  67. {
  68. void *root = NULL;
  69. assert(arr);
  70. assert(arr_n);
  71. assert(prio_cmp);
  72. if (*arr_n) {
  73. root = arr[0];
  74. --*arr_n;
  75. }
  76. if (*arr_n) {
  77. arr[0] = arr[*arr_n];
  78. heap_down(arr, *arr_n, 0, prio_cmp);
  79. }
  80. return root;
  81. }