samples: lib: min_heap: Add min-heap usage example
Introduce a simple sample application demonstrating the usage of the min-heap API. The sample performs basic insert and remove operations and logs the results to the console. Signed-off-by: Sayooj K Karun <sayooj@aerlync.com>
This commit is contained in:
parent
ada616b83f
commit
fc6f9e23ce
6 changed files with 178 additions and 0 deletions
6
samples/data_structures/data_structures.rst
Normal file
6
samples/data_structures/data_structures.rst
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
.. zephyr:code-sample-category:: data_structures
|
||||||
|
:name: Data Structures
|
||||||
|
:show-listing:
|
||||||
|
|
||||||
|
These samples demonstrate how to use various Data Structures supported by
|
||||||
|
Zephyr RTOS.
|
8
samples/data_structures/min-heap/CMakeLists.txt
Normal file
8
samples/data_structures/min-heap/CMakeLists.txt
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.20.0)
|
||||||
|
|
||||||
|
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||||
|
project(min_heap)
|
||||||
|
|
||||||
|
target_sources(app PRIVATE src/main.c)
|
55
samples/data_structures/min-heap/README.rst
Normal file
55
samples/data_structures/min-heap/README.rst
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
.. zephyr:code-sample:: min-heap
|
||||||
|
:name: Min-Heap Data Structure
|
||||||
|
|
||||||
|
Demonstrate usage of a min-heap implementation in a Zephyr application.
|
||||||
|
|
||||||
|
Overview
|
||||||
|
********
|
||||||
|
|
||||||
|
This sample demonstrates Min-Heap Data Structure implementation used as a
|
||||||
|
priority queue in a Zephyr application.
|
||||||
|
The example shows basic heap operations such as insert, remove, pop and
|
||||||
|
empty check.
|
||||||
|
|
||||||
|
Building and Running
|
||||||
|
********************
|
||||||
|
|
||||||
|
To build and run this sample on a supported board:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
west build -b <your_board> samples/lib/min_heap
|
||||||
|
west flash
|
||||||
|
|
||||||
|
Replace ``<your_board>`` with your actual board name (e.g., ``native_sim``).
|
||||||
|
|
||||||
|
Sample Output
|
||||||
|
*************
|
||||||
|
|
||||||
|
On startup, the sample application will perform a sequence of heap operations
|
||||||
|
and print the results. Expected output resembles:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
*** Booting Zephyr OS build 9c0c063db09d ***
|
||||||
|
Min-heap sample using static storage
|
||||||
|
Heap elements by order of priority:
|
||||||
|
key=2 value=400
|
||||||
|
key=5 value=200
|
||||||
|
key=30 value=300
|
||||||
|
key=10 value=100
|
||||||
|
Top of heap: key=2 value=400
|
||||||
|
Found element with key 5 at index 1,removing it...
|
||||||
|
Heap after removal:
|
||||||
|
key=2 value=400
|
||||||
|
key=10 value=100
|
||||||
|
key=30 value=300
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Requirements
|
||||||
|
************
|
||||||
|
|
||||||
|
No external hardware is required to run this sample.
|
||||||
|
It runs on any Zephyr-supported board with standard console output or
|
||||||
|
native_sim.
|
2
samples/data_structures/min-heap/prj.conf
Normal file
2
samples/data_structures/min-heap/prj.conf
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
CONFIG_LOG=y
|
||||||
|
CONFIG_MIN_HEAP=y
|
20
samples/data_structures/min-heap/sample.yaml
Normal file
20
samples/data_structures/min-heap/sample.yaml
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
# Copyright (c) 2025 Aerlync Labs Inc.
|
||||||
|
#
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
sample:
|
||||||
|
description: zephyr min-heap library application
|
||||||
|
name: min-heap
|
||||||
|
tests:
|
||||||
|
sample.data_structures.min-heap:
|
||||||
|
tags:
|
||||||
|
- data_structures
|
||||||
|
harness: console
|
||||||
|
integration_platforms:
|
||||||
|
- native_sim
|
||||||
|
harness_config:
|
||||||
|
type: multi_line
|
||||||
|
ordered: false
|
||||||
|
regex:
|
||||||
|
- "Top of heap: key=2 value=400"
|
||||||
|
- "Found element with key 5 at index 1,removing it..."
|
87
samples/data_structures/min-heap/src/main.c
Normal file
87
samples/data_structures/min-heap/src/main.c
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2025 Aerlync Labs Inc.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
#include <zephyr/sys/printk.h>
|
||||||
|
#include <zephyr/sys/min_heap.h>
|
||||||
|
|
||||||
|
struct data {
|
||||||
|
int key;
|
||||||
|
int value;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int compare(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const struct data *da = a;
|
||||||
|
const struct data *db = b;
|
||||||
|
|
||||||
|
return da->key - db->key;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool match_key(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const struct data *da = a;
|
||||||
|
const int *key = b;
|
||||||
|
|
||||||
|
return da->key == *key;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define HEAP_CAPACITY 8
|
||||||
|
|
||||||
|
MIN_HEAP_DEFINE_STATIC(my_heap, HEAP_CAPACITY, sizeof(struct data),
|
||||||
|
__alignof__(struct data), compare);
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
void *elem;
|
||||||
|
int target_key = 5;
|
||||||
|
size_t index;
|
||||||
|
struct data *top, *found, removed;
|
||||||
|
|
||||||
|
printk("Min-heap sample using static storage\n");
|
||||||
|
|
||||||
|
struct data elements[] = {
|
||||||
|
{ .key = 10, .value = 100 },
|
||||||
|
{ .key = 5, .value = 200 },
|
||||||
|
{ .key = 30, .value = 300 },
|
||||||
|
{ .key = 2, .value = 400 },
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int i = 0; i < ARRAY_SIZE(elements); i++) {
|
||||||
|
if (min_heap_push(&my_heap, &elements[i]) != 0) {
|
||||||
|
printk("Insert failed at index %d\n", i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printk("Heap elements by order of priority:\n");
|
||||||
|
MIN_HEAP_FOREACH(&my_heap, elem, {
|
||||||
|
struct data *d = elem;
|
||||||
|
|
||||||
|
printk("key=%d value=%d\n", d->key, d->value);
|
||||||
|
});
|
||||||
|
|
||||||
|
printk("Top of heap: ");
|
||||||
|
top = min_heap_peek(&my_heap);
|
||||||
|
if (top) {
|
||||||
|
printk("key=%d value=%d\n", top->key, top->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
found = min_heap_find(&my_heap, match_key, &target_key, &index);
|
||||||
|
if (found) {
|
||||||
|
printk("Found element with key %d at index %zu,"
|
||||||
|
"removing it...\n", target_key, index);
|
||||||
|
min_heap_remove(&my_heap, index, &removed);
|
||||||
|
}
|
||||||
|
|
||||||
|
printk("Heap after removal:\n");
|
||||||
|
MIN_HEAP_FOREACH(&my_heap, elem, {
|
||||||
|
struct data *d = elem;
|
||||||
|
|
||||||
|
printk("key=%d value=%d\n", d->key, d->value);
|
||||||
|
});
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue