|
1 |
| -#include "HPriorityQueue.h" |
2 |
| -#include "../../System/Utils.h" |
3 |
| -#include "../../Unit Test/CuTest/CuTest.h" |
4 |
| -#include "../Trees/Headers/BinaryHeap.h" |
5 |
| -#include "../Trees/Sources/BinaryHeap.c" |
| 1 | +#include "../Headers/HPriorityQueue.h" |
| 2 | +#include "../../../System/Utils.h" |
| 3 | +#include "../../../Unit Test/CuTest/CuTest.h" |
| 4 | +#include "../../Trees/Headers/BinaryHeap.h" |
6 | 5 |
|
7 | 6 |
|
8 | 7 |
|
9 | 8 |
|
| 9 | +/** This function will return the first child index, |
| 10 | + * of the passed parent index. |
| 11 | + * |
| 12 | + * Note: this function should only be called from the inside. |
| 13 | + * |
| 14 | + * @param parentIndex the parent index |
| 15 | + * @return it will return the first child index of the passed parent index |
| 16 | + */ |
| 17 | + |
| 18 | +int hpQueueGetFChildIndex(int parentIndex) { |
| 19 | + return parentIndex * 2 + 1; |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +/** This function will return the second child index, |
| 24 | + * of the passed parent index. |
| 25 | + * |
| 26 | + * Note: this function should only be called from the inside. |
| 27 | + * |
| 28 | + * @param parentIndex the parent index |
| 29 | + * @return it will return the second child index of the passed parent index |
| 30 | + */ |
| 31 | + |
| 32 | +int hpQueueGetSChildIndex(int parentIndex) { |
| 33 | + return parentIndex * 2 + 2; |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +/** This function will swap the passed two indices. |
| 38 | + * |
| 39 | + * @param arr the array pointer |
| 40 | + * @param fIndex the first index |
| 41 | + * @param sIndex the second index |
| 42 | + */ |
| 43 | + |
| 44 | +void hpQueueSwap(void **arr, int fIndex, int sIndex) { |
| 45 | + |
| 46 | + void *tempItem = arr[sIndex]; |
| 47 | + arr[sIndex] = arr[fIndex]; |
| 48 | + arr[fIndex] = tempItem; |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +/** This function will heap down the value in the passed index until it be in the right place. |
| 55 | + * |
| 56 | + * @param arr the array pointer |
| 57 | + * @param currentIndex the current index |
| 58 | + * @param length the length of the array |
| 59 | + * @param cmp the comparator function pointer |
| 60 | + */ |
| 61 | + |
| 62 | +void hpQueueHeapDown(void **arr, int currentIndex, int length, int (*cmp)(const void *, const void *)) { |
| 63 | + |
| 64 | + if (currentIndex >= length) |
| 65 | + return; |
| 66 | + |
| 67 | + int fChildIndex = hpQueueGetFChildIndex(currentIndex); |
| 68 | + int sChildIndex = hpQueueGetSChildIndex(currentIndex); |
| 69 | + |
| 70 | + fChildIndex = fChildIndex >= length ? currentIndex : fChildIndex; |
| 71 | + sChildIndex = sChildIndex >= length ? currentIndex : sChildIndex; |
| 72 | + |
| 73 | + if (cmp(arr[currentIndex], arr[fChildIndex]) < 0 && cmp(arr[currentIndex], arr[sChildIndex]) < 0) { |
| 74 | + |
| 75 | + int biggestChildIndex = cmp(arr[fChildIndex], arr[sChildIndex]) < 0 ? sChildIndex : fChildIndex; |
| 76 | + hpQueueSwap(arr, currentIndex, biggestChildIndex); |
| 77 | + hpQueueHeapDown(arr, biggestChildIndex, length, cmp); |
| 78 | + |
| 79 | + } else if (cmp(arr[currentIndex], arr[fChildIndex]) < 0) { |
| 80 | + |
| 81 | + hpQueueSwap(arr, currentIndex, fChildIndex); |
| 82 | + hpQueueHeapDown(arr, fChildIndex, length, cmp); |
| 83 | + |
| 84 | + } else if (cmp(arr[currentIndex], arr[sChildIndex]) < 0) { |
| 85 | + |
| 86 | + hpQueueSwap(arr, currentIndex, sChildIndex); |
| 87 | + hpQueueHeapDown(arr, sChildIndex, length, cmp); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | + |
10 | 95 | /** This function will allocate a new heap priority queue then it will return its pointer.
|
11 | 96 | *
|
12 | 97 | * @param freeFn the free function pointer, that will be called to free the queue items
|
@@ -269,10 +354,13 @@ void **hpQueueToArray(HPriorityQueue *queue) {
|
269 | 354 | int length = hpQueueGetLength(queue);
|
270 | 355 |
|
271 | 356 | while (length-- > 0) {
|
272 |
| - binaryHeapSwap(arr, 0, length); |
273 |
| - binaryHeapDown(arr, 0, length, queue->heap->cmp); |
| 357 | + hpQueueSwap(arr, 0, length); |
| 358 | + hpQueueHeapDown(arr, 0, length, queue->heap->cmp); |
274 | 359 | }
|
275 | 360 |
|
| 361 | + for (int i = 0; i < hpQueueGetLength(queue) / 2; i++) |
| 362 | + hpQueueSwap(arr, i, hpQueueGetLength(queue) - 1 - i); |
| 363 | + |
276 | 364 | return arr;
|
277 | 365 |
|
278 | 366 | }
|
|
0 commit comments