Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
min_heap.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2025 Aerlync Labs Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7/*
8 * @file
9 * @brief Header-file for min-heap data structure
10 * @ingroup min_heap_apis
11 */
12
13#ifndef ZEPHYR_INCLUDE_SYS_MIN_HEAP_H_
14#define ZEPHYR_INCLUDE_SYS_MIN_HEAP_H_
15
16#include <zephyr/sys/util.h>
17#include <zephyr/kernel.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
33
48typedef int (*min_heap_cmp_t)(const void *a,
49 const void *b);
50
59typedef bool (*min_heap_eq_t)(const void *node,
60 const void *other);
61
65struct min_heap {
67 void *storage;
69 size_t capacity;
71 size_t elem_size;
73 size_t size;
76};
77
87#define MIN_HEAP_DEFINE(name, cap, elem_sz, align, cmp_func) \
88 static uint8_t name##_storage[(cap) * (elem_sz)] __aligned(align); \
89 struct min_heap name = {.storage = name##_storage, \
90 .capacity = (cap), \
91 .elem_size = (elem_sz), \
92 .size = 0, \
93 .cmp = (cmp_func)}
94
104#define MIN_HEAP_DEFINE_STATIC(name, cap, elem_sz, align, cmp_func) \
105 static uint8_t name##_storage[(cap) * (elem_sz)] __aligned(align); \
106 static struct min_heap name = { \
107 .storage = name##_storage, \
108 .capacity = (cap), \
109 .elem_size = (elem_sz), \
110 .size = 0, \
111 .cmp = (cmp_func) \
112 }
113
130void min_heap_init(struct min_heap *heap, void *storage, size_t cap,
131 size_t elem_size, min_heap_cmp_t cmp);
132
145int min_heap_push(struct min_heap *heap, const void *item);
146
156void *min_heap_peek(const struct min_heap *heap);
157
173bool min_heap_remove(struct min_heap *heap, size_t id, void *out_buf);
174
184static inline bool min_heap_is_empty(struct min_heap *heap)
185{
186 __ASSERT_NO_MSG(heap != NULL);
187
188 return (heap->size == 0);
189}
190
203bool min_heap_pop(struct min_heap *heap, void *out_buf);
204
215void *min_heap_find(struct min_heap *heap, min_heap_eq_t eq,
216 const void *other, size_t *out_id);
217
226static inline void *min_heap_get_element(const struct min_heap *heap,
227 size_t index)
228{
229 __ASSERT_NO_MSG(heap != NULL);
230
231 return (void *)((uintptr_t)heap->storage + index * heap->elem_size);
232}
233
248#define MIN_HEAP_FOREACH(heap, node_var) \
249 for (size_t _i = 0; \
250 _i < (heap)->size && (((node_var) = min_heap_get_element((heap), _i)) || true); ++_i)
251
255
256#ifdef __cplusplus
257}
258#endif
259
260#endif /* ZEPHYR_INCLUDE_SYS_MIN_HEAP_H_ */
void * min_heap_peek(const struct min_heap *heap)
Peek at the top element of the min-heap.
void * min_heap_find(struct min_heap *heap, min_heap_eq_t eq, const void *other, size_t *out_id)
Search for a node in the heap matching a condition.
int min_heap_push(struct min_heap *heap, const void *item)
Push an element into the min-heap.
int(* min_heap_cmp_t)(const void *a, const void *b)
Comparator function type for min-heap ordering.
Definition min_heap.h:48
void min_heap_init(struct min_heap *heap, void *storage, size_t cap, size_t elem_size, min_heap_cmp_t cmp)
Initialize a min-heap instance at runtime.
static bool min_heap_is_empty(struct min_heap *heap)
Check if the min heap is empty.
Definition min_heap.h:184
bool(* min_heap_eq_t)(const void *node, const void *other)
Equality function for finding a node in the heap.
Definition min_heap.h:59
bool min_heap_remove(struct min_heap *heap, size_t id, void *out_buf)
Remove a specific element from the min-heap.
static void * min_heap_get_element(const struct min_heap *heap, size_t index)
Get a pointer to the element at the specified index.
Definition min_heap.h:226
bool min_heap_pop(struct min_heap *heap, void *out_buf)
Remove and return the highest priority element in the heap.
#define NULL
Definition iar_missing_defs.h:20
Public kernel APIs.
#define bool
Definition stdbool.h:13
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:105
min-heap data structure with user-provided comparator.
Definition min_heap.h:65
min_heap_cmp_t cmp
Comparator function.
Definition min_heap.h:75
void * storage
Raw pointer to contiguous memory for elements.
Definition min_heap.h:67
size_t size
Current elements count.
Definition min_heap.h:73
size_t elem_size
Size of each element.
Definition min_heap.h:71
size_t capacity
Maximum number of elements.
Definition min_heap.h:69
Misc utilities.