include: zephyr: sys: simplify MIN_HEAP_FOREACH macro
Refactor the `MIN_HEAP_FOREACH` macro to use a cleaner for-loop style removing the need for a third `body` argument. Update the sample application with the new macro changes. Signed-off-by: Sayooj K Karun <sayooj@aerlync.com>
This commit is contained in:
committed by
Anas Nashif
parent
964a832702
commit
e719ba239f
@@ -82,6 +82,7 @@ ForEachMacros:
|
|||||||
- 'HTTP_SERVICE_FOREACH_RESOURCE'
|
- 'HTTP_SERVICE_FOREACH_RESOURCE'
|
||||||
- 'I3C_BUS_FOR_EACH_I3CDEV'
|
- 'I3C_BUS_FOR_EACH_I3CDEV'
|
||||||
- 'I3C_BUS_FOR_EACH_I2CDEV'
|
- 'I3C_BUS_FOR_EACH_I2CDEV'
|
||||||
|
- 'MIN_HEAP_FOREACH'
|
||||||
IfMacros:
|
IfMacros:
|
||||||
- 'CHECKIF'
|
- 'CHECKIF'
|
||||||
# Disabled for now, see bug https://github.com/zephyrproject-rtos/zephyr/issues/48520
|
# Disabled for now, see bug https://github.com/zephyrproject-rtos/zephyr/issues/48520
|
||||||
|
|||||||
@@ -220,23 +220,18 @@ static inline void *min_heap_get_element(const struct min_heap *heap,
|
|||||||
*
|
*
|
||||||
* @param heap Pointer to the heap.
|
* @param heap Pointer to the heap.
|
||||||
* @param node_var The loop variable used to reference each node.
|
* @param node_var The loop variable used to reference each node.
|
||||||
* @param body Code block to execute for each node.
|
|
||||||
*
|
*
|
||||||
* Example:
|
* Example:
|
||||||
* ```
|
* ```
|
||||||
* void *node;
|
* void *node;
|
||||||
* MIN_HEAP_FOREACH(&heap, node, {
|
* MIN_HEAP_FOREACH(&heap, node) {
|
||||||
* printk("Value: %d\n", node->value);
|
* printk("Value: %d\n", node->value);
|
||||||
* });
|
* }
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
#define MIN_HEAP_FOREACH(heap, node_var, body) \
|
#define MIN_HEAP_FOREACH(heap, node_var) \
|
||||||
do { for (size_t _i = 0; _i < (heap)->size && \
|
for (size_t _i = 0; \
|
||||||
(((node_var) = min_heap_get_element((heap), _i)) || true); \
|
_i < (heap)->size && (((node_var) = min_heap_get_element((heap), _i)) || true); ++_i)
|
||||||
++_i) { \
|
|
||||||
body; \
|
|
||||||
} \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
|
|||||||
@@ -57,11 +57,11 @@ int main(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
printk("Heap elements by order of priority:\n");
|
printk("Heap elements by order of priority:\n");
|
||||||
MIN_HEAP_FOREACH(&my_heap, elem, {
|
MIN_HEAP_FOREACH(&my_heap, elem) {
|
||||||
struct data *d = elem;
|
struct data *d = elem;
|
||||||
|
|
||||||
printk("key=%d value=%d\n", d->key, d->value);
|
printk("key=%d value=%d\n", d->key, d->value);
|
||||||
});
|
}
|
||||||
|
|
||||||
printk("Top of heap: ");
|
printk("Top of heap: ");
|
||||||
top = min_heap_peek(&my_heap);
|
top = min_heap_peek(&my_heap);
|
||||||
@@ -77,11 +77,11 @@ int main(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
printk("Heap after removal:\n");
|
printk("Heap after removal:\n");
|
||||||
MIN_HEAP_FOREACH(&my_heap, elem, {
|
MIN_HEAP_FOREACH(&my_heap, elem) {
|
||||||
struct data *d = elem;
|
struct data *d = elem;
|
||||||
|
|
||||||
printk("key=%d value=%d\n", d->key, d->value);
|
printk("key=%d value=%d\n", d->key, d->value);
|
||||||
});
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user