diff --git a/src/data_structures/priority_heap.c b/src/data_structures/priority_heap.c index ff1a068..593b512 100644 --- a/src/data_structures/priority_heap.c +++ b/src/data_structures/priority_heap.c @@ -189,3 +189,16 @@ void *simple_archiver_priority_heap_pop(SDArchiverPHeap *priority_heap) { return data; } + +void simple_archiver_priority_heap_iter(SDArchiverPHeap *priority_heap, + void(*iter_fn)(void*)) { + if (priority_heap->size == 0) { + return; + } + for (uint64_t idx = 0; idx < priority_heap->size; ++idx) { + if (priority_heap->nodes[idx].is_valid) { + iter_fn(priority_heap->nodes[idx].data); + } + } + return; +} diff --git a/src/data_structures/priority_heap.h b/src/data_structures/priority_heap.h index 3416421..a597f31 100644 --- a/src/data_structures/priority_heap.h +++ b/src/data_structures/priority_heap.h @@ -67,4 +67,9 @@ void *simple_archiver_priority_heap_top(SDArchiverPHeap *priority_heap); /// ownership of the returned data pointer. void *simple_archiver_priority_heap_pop(SDArchiverPHeap *priority_heap); +/// Iterates through all items in the priority heap (breadth-width search not +/// depth-first search). +void simple_archiver_priority_heap_iter(SDArchiverPHeap *priority_heap, + void(*iter_fn)(void*)); + #endif diff --git a/src/data_structures/test.c b/src/data_structures/test.c index 23c928b..044c15b 100644 --- a/src/data_structures/test.c +++ b/src/data_structures/test.c @@ -89,6 +89,10 @@ int hash_map_iter_check_fn2(__attribute__((unused)) const void *key, return 2; } +void test_iter_fn_priority_heap(void *data) { + printf("Got data %" PRIu32 "\n", *((uint32_t*)data)); +} + int main(void) { // Test LinkedList. { @@ -438,6 +442,18 @@ int main(void) { free(array); simple_archiver_priority_heap_free(&priority_heap); + + priority_heap = simple_archiver_priority_heap_init(); + for (uint32_t idx = 0; idx < 50; ++idx) { + uint32_t *data = malloc(sizeof(uint32_t)); + *data = idx; + simple_archiver_priority_heap_insert(priority_heap, idx, data, NULL); + } + printf("Begin iteration of 50 elements:\n"); + + simple_archiver_priority_heap_iter(priority_heap, + test_iter_fn_priority_heap); + simple_archiver_priority_heap_free(&priority_heap); } printf("Checks checked: %" PRId32 "\n", checks_checked);