LibJuno 1.0.1
LibJuno is a lightweight C11 library designed specifically for embedded systems.
Loading...
Searching...
No Matches
heap_api.h
Go to the documentation of this file.
1/*
2 MIT License
3
4 Copyright (c) 2025 Robin A. Onsay
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files
8 (the "Software"), to deal in the Software without restriction,
9 including without limitation the rights to use, copy, modify, merge,
10 publish, distribute, sublicense, and/or sell copies of the Software,
11 and to permit persons to whom the Software is furnished to do so,
12 subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16*/
17
55#ifndef JUNO_DS_HEAP_API_H
56#define JUNO_DS_HEAP_API_H
57#include "juno/ds/array_api.h"
58#include "juno/macros.h"
60#include "juno/status.h"
61#include "juno/module.h"
62#include <stdbool.h>
63#include <stddef.h>
64#ifdef __cplusplus
65extern "C"
66{
67#endif
68
70typedef struct JUNO_DS_HEAP_ROOT_TAG JUNO_DS_HEAP_ROOT_T;
75
80JUNO_MODULE_RESULT(JUNO_DS_HEAP_INDEX_RESULT_T, size_t);
85JUNO_MODULE_OPTION(JUNO_DS_HEAP_INDEX_OPTION_T, size_t);
90JUNO_MODULE_RESULT(JUNO_DS_HEAP_INDEX_OPTION_RESULT_T, JUNO_DS_HEAP_INDEX_OPTION_T);
93JUNO_MODULE_RESULT(JUNO_DS_HEAP_COMPARE_RESULT_T, bool);
94
95
100struct JUNO_DS_HEAP_ROOT_TAG JUNO_MODULE_ROOT(JUNO_DS_HEAP_API_T,
101 const JUNO_DS_HEAP_POINTER_API_T *ptHeapPointerApi;
102 JUNO_DS_ARRAY_ROOT_T *ptHeapArray;
103 size_t zLength;
104);
105
111{
116 JUNO_DS_HEAP_COMPARE_RESULT_T (*Compare)(JUNO_DS_HEAP_ROOT_T *ptHeap, JUNO_POINTER_T tParent, JUNO_POINTER_T tChild);
121};
122
138
193JUNO_STATUS_T JunoDs_Heap_Init(JUNO_DS_HEAP_ROOT_T *ptHeap, const JUNO_DS_HEAP_POINTER_API_T *ptHeapPointerApi, JUNO_DS_ARRAY_ROOT_T *ptHeapArray, JUNO_FAILURE_HANDLER_T pfcnFailureHdlr, JUNO_USER_DATA_T *pvUserData);
194
217
228{
230 ptHeap &&
231 ptHeap->ptHeapArray &&
232 ptHeap->ptHeapPointerApi &&
233 ptHeap->ptHeapPointerApi->Compare &&
234 ptHeap->ptHeapPointerApi->Swap &&
235 ptHeap->ptApi &&
236 ptHeap->ptApi->Insert &&
237 ptHeap->ptApi->Heapify &&
238 ptHeap->ptApi->Pop
239 );
240 return JunoDs_ArrayVerify(ptHeap->ptHeapArray);
241}
242#ifdef __cplusplus
243}
244#endif
245#endif // JUNO_DS_HEAP_API_H
Abstract fixed-capacity array interface for DS modules.
struct JUNO_DS_ARRAY_ROOT_TAG JUNO_DS_ARRAY_ROOT_T
Opaque array root carrying capacity and API pointer.
Definition array_api.h:58
static JUNO_STATUS_T JunoDs_ArrayVerify(const JUNO_DS_ARRAY_ROOT_T *ptArray)
Verify an array instance's capacity and API.
Definition array_api.h:117
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:166
JUNO_STATUS_T JunoDs_Heap_Init(JUNO_DS_HEAP_ROOT_T *ptHeap, const JUNO_DS_HEAP_POINTER_API_T *ptHeapPointerApi, JUNO_DS_ARRAY_ROOT_T *ptHeapArray, JUNO_FAILURE_HANDLER_T pfcnFailureHdlr, JUNO_USER_DATA_T *pvUserData)
Initialize a heap over a backing array and element pointer API.
Definition juno_heap.c:32
static JUNO_STATUS_T JunoDs_Heap_Verify(JUNO_DS_HEAP_ROOT_T *ptHeap)
Verify heap configuration and dependent APIs.
Definition heap_api.h:227
JUNO_STATUS_T JunoDs_Heap_Insert(JUNO_DS_HEAP_ROOT_T *ptHeap, JUNO_POINTER_T tValue)
Insert a new element into the heap.
Definition juno_heap.c:257
JUNO_STATUS_T JunoDs_Heap_Heapify(JUNO_DS_HEAP_ROOT_T *ptHeap)
Transform the underlying array into a heap.
Definition juno_heap.c:282
JUNO_STATUS_T JunoDs_Heap_Pop(JUNO_DS_HEAP_ROOT_T *ptHeap, JUNO_POINTER_T tReturn)
Pop the root element into tReturn.
Definition juno_heap.c:322
#define JUNO_ASSERT_EXISTS(ptr)
Returns JUNO_STATUS_NULLPTR_ERROR if the expression is falsy.
Definition macros.h:50
#define JUNO_MODULE_RESULT(NAME_T, OK_T)
Define a result type combining a status and a success payload.
Definition module.h:193
#define JUNO_MODULE_ROOT(API_T,...)
Implement a module root struct containing ptApi and failure fields.
Definition module.h:126
#define JUNO_MODULE_OPTION(NAME_T, SOME_T)
Define an option type combining a presence flag and a payload.
Definition module.h:236
void(* JUNO_FAILURE_HANDLER_T)(JUNO_STATUS_T tStatus, const char *pcCustomMessage, JUNO_USER_DATA_T *pvUserData)
Failure handler callback signature.
Definition status.h:104
int32_t JUNO_STATUS_T
Canonical status type for LibJuno functions.
Definition status.h:49
void JUNO_USER_DATA_T
Opaque user data type for failure callbacks.
Definition status.h:96
struct JUNO_DS_HEAP_ROOT_TAG JUNO_DS_HEAP_ROOT_T
Heap root type holding state and API pointer.
Definition heap_api.h:70
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:114
Common assertion and helper macros for LibJuno modules.
Module system and dependency injection primitives for LibJuno.
Pointer trait and helpers for memory operations.
struct JUNO_POINTER_TAG JUNO_POINTER_T
Definition pointer_api.h:49
Status codes and failure-handling helpers for LibJuno.
Heap API vtable.
Definition heap_api.h:128
JUNO_STATUS_T(* Heapify)(JUNO_DS_HEAP_ROOT_T *ptHeap)
Transform the underlying array into a heap in-place.
Definition heap_api.h:134
JUNO_STATUS_T(* Insert)(JUNO_DS_HEAP_ROOT_T *ptHeap, JUNO_POINTER_T tValue)
Insert a new element into the heap.
Definition heap_api.h:132
JUNO_STATUS_T(* Pop)(JUNO_DS_HEAP_ROOT_T *ptHeap, JUNO_POINTER_T tReturn)
Pop the root element (extreme) into tReturn.
Definition heap_api.h:136
Element-level operations required by the heap.
Definition heap_api.h:111
JUNO_STATUS_T(* Swap)(JUNO_DS_HEAP_ROOT_T *ptHeap, JUNO_POINTER_T tLeft, JUNO_POINTER_T tRight)
Swap two values in the underlying storage.
Definition heap_api.h:120
JUNO_DS_HEAP_COMPARE_RESULT_T(* Compare)(JUNO_DS_HEAP_ROOT_T *ptHeap, JUNO_POINTER_T tParent, JUNO_POINTER_T tChild)
Compare two values to determine ordering (heap property).
Definition heap_api.h:116