Skip to content

Commit 964a832

Browse files
sayoojkkarunnashif
authored andcommitted
lib: min_heap: Refactor heapify_up/down functions
Remove using `swap()` which was using VLA and replace it with `byteswp()`. Reduce the scope of local variables in `heapify_up/down` to the smallest necessary block. Replace `while(true)` loop in `heapify_down()` with a bounded `for` loop. Signed-off-by: Sayooj K Karun <[email protected]>
1 parent cabf2c4 commit 964a832

File tree

1 file changed

+22
-41
lines changed

1 file changed

+22
-41
lines changed

lib/min_heap/min_heap.c

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,6 @@
1010

1111
LOG_MODULE_REGISTER(min_heap);
1212

13-
/**
14-
* @brief Swap two elements in the heap.
15-
*
16-
* This function swaps the contents of two elements at the specified indices
17-
* within the heap.
18-
*
19-
* @param heap Pointer to the min-heap.
20-
* @param a Index of the first element.
21-
* @param b Index of the second element.
22-
*/
23-
static void swap(struct min_heap *heap, size_t a, size_t b)
24-
{
25-
uint8_t tmp[heap->elem_size];
26-
void *elem_a = min_heap_get_element(heap, a);
27-
void *elem_b = min_heap_get_element(heap, b);
28-
29-
memcpy(tmp, elem_a, heap->elem_size);
30-
memcpy(elem_a, elem_b, heap->elem_size);
31-
memcpy(elem_b, tmp, heap->elem_size);
32-
}
33-
3413
/**
3514
* @brief Restore heap order by moving a node up the tree.
3615
*
@@ -42,17 +21,15 @@ static void swap(struct min_heap *heap, size_t a, size_t b)
4221
*/
4322
static void heapify_up(struct min_heap *heap, size_t index)
4423
{
45-
size_t parent;
46-
void *curr, *par;
47-
4824
while (index > 0) {
49-
parent = (index - 1) / 2;
50-
curr = min_heap_get_element(heap, index);
51-
par = min_heap_get_element(heap, parent);
25+
size_t parent = (index - 1) / 2;
26+
void *curr = min_heap_get_element(heap, index);
27+
void *par = min_heap_get_element(heap, parent);
28+
5229
if (heap->cmp(curr, par) >= 0) {
5330
break;
5431
}
55-
swap(heap, index, parent);
32+
byteswp(curr, par, heap->elem_size);
5633
index = parent;
5734
}
5835
}
@@ -69,30 +46,34 @@ static void heapify_up(struct min_heap *heap, size_t index)
6946

7047
static void heapify_down(struct min_heap *heap, size_t index)
7148
{
72-
size_t left, right, smallest;
49+
/* Terminate the loop naturally when the left child is out of bounds */
50+
for (size_t left = 2 * index + 1; left < heap->size; left = 2 * index + 1) {
7351

74-
while (true) {
75-
left = 2 * index + 1;
76-
right = 2 * index + 2;
77-
smallest = index;
52+
size_t right = left + 1;
53+
size_t smallest = index;
54+
void *elem_index = min_heap_get_element(heap, index);
55+
void *elem_left = min_heap_get_element(heap, left);
56+
void *elem_smallest = elem_index;
7857

79-
if (left < heap->size &&
80-
heap->cmp(min_heap_get_element(heap, left),
81-
min_heap_get_element(heap, smallest)) < 0) {
58+
if (heap->cmp(elem_left, elem_index) < 0) {
8259
smallest = left;
60+
elem_smallest = elem_left;
8361
}
8462

85-
if (right < heap->size &&
86-
heap->cmp(min_heap_get_element(heap, right),
87-
min_heap_get_element(heap, smallest)) < 0) {
88-
smallest = right;
63+
if (right < heap->size) {
64+
void *elem_right = min_heap_get_element(heap, right);
65+
66+
if (heap->cmp(elem_right, elem_smallest) < 0) {
67+
smallest = right;
68+
elem_smallest = elem_right;
69+
}
8970
}
9071

9172
if (smallest == index) {
9273
break;
9374
}
9475

95-
swap(heap, index, smallest);
76+
byteswp(elem_index, elem_smallest, heap->elem_size);
9677
index = smallest;
9778
}
9879
}

0 commit comments

Comments
 (0)