diff --git a/samples/data_structures/data_structures.rst b/samples/data_structures/data_structures.rst new file mode 100644 index 00000000000..81ecc9c5cef --- /dev/null +++ b/samples/data_structures/data_structures.rst @@ -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. diff --git a/samples/data_structures/min-heap/CMakeLists.txt b/samples/data_structures/min-heap/CMakeLists.txt new file mode 100644 index 00000000000..2d7d4260101 --- /dev/null +++ b/samples/data_structures/min-heap/CMakeLists.txt @@ -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) diff --git a/samples/data_structures/min-heap/README.rst b/samples/data_structures/min-heap/README.rst new file mode 100644 index 00000000000..43a28461457 --- /dev/null +++ b/samples/data_structures/min-heap/README.rst @@ -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 samples/lib/min_heap + west flash + +Replace ```` 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. diff --git a/samples/data_structures/min-heap/prj.conf b/samples/data_structures/min-heap/prj.conf new file mode 100644 index 00000000000..9ffea79b431 --- /dev/null +++ b/samples/data_structures/min-heap/prj.conf @@ -0,0 +1,2 @@ +CONFIG_LOG=y +CONFIG_MIN_HEAP=y diff --git a/samples/data_structures/min-heap/sample.yaml b/samples/data_structures/min-heap/sample.yaml new file mode 100644 index 00000000000..bc66a6eff15 --- /dev/null +++ b/samples/data_structures/min-heap/sample.yaml @@ -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..." diff --git a/samples/data_structures/min-heap/src/main.c b/samples/data_structures/min-heap/src/main.c new file mode 100644 index 00000000000..d81284e929d --- /dev/null +++ b/samples/data_structures/min-heap/src/main.c @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2025 Aerlync Labs Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +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; +}