13#ifndef ZEPHYR_INCLUDE_SYS_MIN_HEAP_REF_H_
14#define ZEPHYR_INCLUDE_SYS_MIN_HEAP_REF_H_
83#define MIN_HEAP_REF_DEFINE(name, cap, cmp_func) \
84 static struct min_heap_handle *(name##_storage)[(cap)]; \
85 struct min_heap_ref name = { \
86 .storage = (name##_storage), \
99#define MIN_HEAP_REF_DEFINE_STATIC(name, cap, cmp_func) \
100 static struct min_heap_handle *(name##_storage)[(cap)]; \
101 static struct min_heap_ref name = { \
102 .storage = (name##_storage), \
126 __ASSERT_NO_MSG(heap != NULL);
127 __ASSERT_NO_MSG(storage != NULL);
128 __ASSERT_NO_MSG(cmp != NULL);
164 size_t parent = (index - 1U) / 2U;
185 for (
size_t left = 2U * index + 1U; left < heap->
size; left = 2U * index + 1U) {
186 size_t right = left + 1U;
187 size_t smallest = index;
192 if (right < heap->size &&
196 if (smallest == index) {
218 __ASSERT_NO_MSG(heap != NULL);
219 __ASSERT_NO_MSG(handle != NULL);
220 __ASSERT_NO_MSG(handle->
idx == 0U);
246 __ASSERT_NO_MSG(heap != NULL);
262 __ASSERT_NO_MSG(heap != NULL);
263 __ASSERT_NO_MSG(handle != NULL);
265 if (handle->
idx == 0U || (
size_t)handle->
idx > heap->
size) {
269 size_t id = (size_t)(handle->
idx - 1U);
274 if (
id != heap->
size) {
300 __ASSERT_NO_MSG(heap != NULL);
301 return (heap->
size == 0);
314 __ASSERT_NO_MSG(heap != NULL);
316 if (heap->
size == 0U) {
345#define MIN_HEAP_REF_FOREACH(__heap, __handle) \
346 for (size_t __mh_i = 0; \
347 (__mh_i < (__heap)->size) && ((__handle) = (__heap)->storage[__mh_i], true); \
364#define MIN_HEAP_REF_FOREACH_CONTAINER(__heap, __cn, __field) \
365 for (size_t __mh_i = 0; \
366 (__mh_i < (__heap)->size) && \
367 ((__cn) = CONTAINER_OF((__heap)->storage[__mh_i], __typeof__(*(__cn)), __field), \
static void min_heap_ref_heapify_up(struct min_heap_ref *heap, size_t index)
Restore heap order by moving a node up the tree.
Definition min_heap_ref.h:161
static void min_heap_ref_swap(struct min_heap_ref *heap, size_t a, size_t b)
Swap two elements in the heap storage, updating their idx fields.
Definition min_heap_ref.h:143
static const struct min_heap_handle * min_heap_ref_peek(const struct min_heap_ref *heap)
Peek at the top element of the min-heap.
Definition min_heap_ref.h:244
static void min_heap_ref_heapify_down(struct min_heap_ref *heap, size_t index)
Restore heap order by moving a node down the tree.
Definition min_heap_ref.h:183
static struct min_heap_handle * min_heap_ref_pop(struct min_heap_ref *heap)
Remove and return the highest priority element's handle.
Definition min_heap_ref.h:312
static bool min_heap_ref_remove(struct min_heap_ref *heap, struct min_heap_handle *handle)
Remove a specific element from the min-heap by handle.
Definition min_heap_ref.h:260
static void min_heap_ref_init(struct min_heap_ref *heap, struct min_heap_handle **storage, size_t capacity, min_heap_ref_cmp_t cmp)
Initialize a min-heap instance at runtime.
Definition min_heap_ref.h:123
static bool min_heap_ref_is_empty(struct min_heap_ref *heap)
Check if the min heap is empty.
Definition min_heap_ref.h:298
static int min_heap_ref_push(struct min_heap_ref *heap, struct min_heap_handle *handle)
Push a handle into the min-heap.
Definition min_heap_ref.h:216
int(* min_heap_ref_cmp_t)(const struct min_heap_handle *a, const struct min_heap_handle *b)
Comparator function type for min-heap ordering.
Definition min_heap_ref.h:60
#define min(a, b)
Return smaller value of two provided expressions.
Definition minmax.h:81
#define UINT16_MAX
Definition stdint.h:28
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Embeddable heap position handle.
Definition min_heap_ref.h:41
uint16_t idx
1-based storage index; 0 means not in heap
Definition min_heap_ref.h:43
min-heap data structure with user-provided comparator.
Definition min_heap_ref.h:65
min_heap_ref_cmp_t cmp
Comparator function: returns <0, 0, >0.
Definition min_heap_ref.h:73
size_t capacity
Maximum number of elements.
Definition min_heap_ref.h:69
size_t size
Current elements count.
Definition min_heap_ref.h:71
struct min_heap_handle ** storage
Array of handle pointers: one slot per element.
Definition min_heap_ref.h:67