210 if(!(ptHeap && ptHeap->ptApi && ptHeap->zCapacity))
231 ptHeap->ptApi = ptApi;
232 ptHeap->zCapacity = zCapacity;
250 iIndex = 2 * iIndex + 1;
251 if(iIndex > ptHeap->zCapacity || ptHeap->zLength > ptHeap->zCapacity)
256 if(iIndex >= ptHeap->zLength)
259 tResult.tOk.bIsSome =
false;
263 tResult.tOk.bIsSome =
true;
264 tResult.tOk.tSome = iIndex;
277 iIndex = 2 * iIndex + 2;
278 if(iIndex > ptHeap->zCapacity || ptHeap->zLength > ptHeap->zCapacity)
283 if(iIndex >= ptHeap->zLength)
286 tResult.tOk.bIsSome =
false;
290 tResult.tOk.bIsSome =
true;
291 tResult.tOk.tSome = iIndex;
304 iIndex = (iIndex - 1)/2;
305 if(iIndex > ptHeap->zCapacity || ptHeap->zLength > ptHeap->zCapacity)
310 if(iIndex >= ptHeap->zLength)
313 tResult.tOk.bIsSome =
false;
317 tResult.tOk.bIsSome =
true;
318 tResult.tOk.tSome = iIndex;
337 if(ptHeap->zLength >= ptHeap->zCapacity)
342 tResult.tOk = ptHeap->zLength;
343 ptHeap->zLength += 1;
360 if(ptHeap->zLength <= 0)
367 if(!iIndexResult.tOk.bIsSome)
371 size_t iIndex = iIndexResult.tOk.tSome;
372 for(
size_t i = 0; i <= iIndex; ++i)
374 size_t iCurrentIndex = iIndex - i;
397 if(ptHeap->zLength <= 0)
402 tStatus = ptHeap->ptApi->Swap(ptHeap, ptHeap->zLength-1, 0);
404 ptHeap->ptApi->Reset(ptHeap, ptHeap->zLength-1);
406 ptHeap->zLength -= 1;
JUNO_STATUS_T JunoDs_Heap_SiftDown(JUNO_DS_HEAP_ROOT_T *ptHeap, size_t iStart)
Sift down from a starting index to restore the heap property.
Definition juno_heap.c:51
static JUNO_DS_HEAP_INDEX_RESULT_T JunoDs_Heap_Insert(JUNO_DS_HEAP_ROOT_T *ptHeap)
Reserve a new slot at the end of the heap array and return its index.
Definition heap_api.h:332
static JUNO_STATUS_T JunoDs_Heap_Verify(JUNO_DS_HEAP_ROOT_T *ptHeap)
Verify that the heap root has been initialized properly.
Definition heap_api.h:207
static JUNO_DS_HEAP_INDEX_OPTION_RESULT_T JunoDs_Heap_ChildGetLeft(JUNO_DS_HEAP_ROOT_T *ptHeap, size_t iIndex)
Compute the left child index of iIndex.
Definition heap_api.h:245
static JUNO_STATUS_T JunoDs_Heap_Heapify(JUNO_DS_HEAP_ROOT_T *ptHeap)
Build a heap in-place from the current array contents.
Definition heap_api.h:355
static JUNO_DS_HEAP_INDEX_OPTION_RESULT_T JunoDs_Heap_ChildGetRight(JUNO_DS_HEAP_ROOT_T *ptHeap, size_t iIndex)
Compute the right child index of iIndex.
Definition heap_api.h:272
struct JUNO_DS_HEAP_ROOT_TAG JUNO_DS_HEAP_ROOT_T
Definition heap_api.h:114
JUNO_STATUS_T JunoDs_Heap_Update(JUNO_DS_HEAP_ROOT_T *ptHeap)
Bubble-up the last inserted element to restore the heap property.
Definition juno_heap.c:5
static JUNO_DS_HEAP_INDEX_OPTION_RESULT_T JunoDs_Heap_ChildGetParent(JUNO_DS_HEAP_ROOT_T *ptHeap, size_t iIndex)
Compute the parent index of iIndex.
Definition heap_api.h:299
static JUNO_STATUS_T JunoDs_Heap_Delete(JUNO_DS_HEAP_ROOT_T *ptHeap)
Remove the root element and restore the heap property.
Definition heap_api.h:392
static JUNO_STATUS_T JunoDs_Heap_Init(JUNO_DS_HEAP_ROOT_T *ptHeap, const JUNO_DS_HEAP_API_T *ptApi, size_t zCapacity)
Initialize a heap root with the given API and capacity.
Definition heap_api.h:228
#define JUNO_ASSERT_EXISTS(ptr)
Definition macros.h:28
#define JUNO_ASSERT_SUCCESS(tStatus, failOp)
Definition macros.h:52
#define JUNO_MODULE_RESULT(NAME_T, OK_T)
Defines a result type combining a status and a success payload.
Definition module.h:223
#define JUNO_MODULE_ROOT(API_T,...)
Definition module.h:182
#define JUNO_MODULE_OPTION(NAME_T, SOME_T)
Defines an option type combining a flag to indicate some and a success payload.
Definition module.h:242
#define JUNO_STATUS_ERR
Definition status.h:25
int32_t JUNO_STATUS_T
Definition status.h:23
#define JUNO_STATUS_NULLPTR_ERROR
Definition status.h:26
#define JUNO_STATUS_SUCCESS
Definition status.h:24
API vtable you must implement to adapt the heap to your storage.
Definition heap_api.h:167
JUNO_STATUS_T(* Swap)(JUNO_DS_HEAP_ROOT_T *ptHeap, size_t iFrom, size_t iTo)
Definition heap_api.h:170
JUNO_STATUS_T(* Reset)(JUNO_DS_HEAP_ROOT_T *ptHeap, size_t iIndex)
Definition heap_api.h:171
JUNO_DS_HEAP_COMPARE_RESULT_T(* Compare)(JUNO_DS_HEAP_ROOT_T *ptHeap, size_t parent, size_t child)
Compare two values to perform the heap operation.
Definition heap_api.h:169