Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
 4.1.99
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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#ifndef ZEPHYR_INCLUDE_SYS_MIN_HEAP_H_
8#define ZEPHYR_INCLUDE_SYS_MIN_HEAP_H_
9
10#include <zephyr/sys/util.h>
11#include <zephyr/kernel.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
38typedef int (*min_heap_cmp_t)(const void *a,
39 const void *b);
40
49typedef bool (*min_heap_eq_t)(const void *node,
50 const void *other);
51
55struct min_heap {
57 void *storage;
59 size_t capacity;
61 size_t elem_size;
63 size_t size;
66};
67
77#define MIN_HEAP_DEFINE(name, storage, cap, size, cmp_func) \
78 struct min_heap name; \
79 min_heap_init(&name, storage, cap, size, cmp_func)
80
90#define MIN_HEAP_DEFINE_STATIC(name, cap, elem_sz, align, cmp_func) \
91 static uint8_t name##_storage[(cap) * (elem_sz)] __aligned(align); \
92 static struct min_heap name = { \
93 .storage = name##_storage, \
94 .capacity = (cap), \
95 .elem_size = (elem_sz), \
96 .size = 0, \
97 .cmp = (cmp_func) \
98 }
99
116void min_heap_init(struct min_heap *heap, void *storage, size_t cap,
117 size_t elem_size, min_heap_cmp_t cmp);
118
131int min_heap_push(struct min_heap *heap, const void *item);
132
142void *min_heap_peek(const struct min_heap *heap);
143
159bool min_heap_remove(struct min_heap *heap, size_t id, void *out_buf);
160
170static inline bool min_heap_is_empty(struct min_heap *heap)
171{
172 __ASSERT_NO_MSG(heap != NULL);
173
174 return (heap->size == 0);
175}
176
189bool min_heap_pop(struct min_heap *heap, void *out_buf);
190
201void *min_heap_find(struct min_heap *heap, min_heap_eq_t eq,
202 const void *other, size_t *out_id);
203
212static inline void *min_heap_get_element(const struct min_heap *heap,
213 size_t index)
214{
215 return (void *)((uintptr_t)heap->storage + index * heap->elem_size);
216}
217
233#define MIN_HEAP_FOREACH(heap, node_var, body) \
234 do { for (size_t _i = 0; _i < (heap)->size && \
235 (((node_var) = min_heap_get_element((heap), _i)) || true); \
236 ++_i) { \
237 body; \
238 } \
239 } while (0)
240
245#ifdef __cplusplus
246}
247#endif
248
249#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:38
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:170
bool(* min_heap_eq_t)(const void *node, const void *other)
Equality function for finding a node in the heap.
Definition min_heap.h:49
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:212
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:55
min_heap_cmp_t cmp
Comparator function.
Definition min_heap.h:65
void * storage
Raw pointer to contiguous memory for elements.
Definition min_heap.h:57
size_t size
Current elements count.
Definition min_heap.h:63
size_t elem_size
Size of each element.
Definition min_heap.h:61
size_t capacity
Maximum number of elements.
Definition min_heap.h:59
Misc utilities.