Add priority_heap iter and unit test
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 6s

This commit is contained in:
Stephen Seo 2024-12-04 15:16:12 +09:00
parent 78a36b48db
commit 5a613d1841
3 changed files with 34 additions and 0 deletions

View file

@ -189,3 +189,16 @@ void *simple_archiver_priority_heap_pop(SDArchiverPHeap *priority_heap) {
return data; 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;
}

View file

@ -67,4 +67,9 @@ void *simple_archiver_priority_heap_top(SDArchiverPHeap *priority_heap);
/// ownership of the returned data pointer. /// ownership of the returned data pointer.
void *simple_archiver_priority_heap_pop(SDArchiverPHeap *priority_heap); 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 #endif

View file

@ -89,6 +89,10 @@ int hash_map_iter_check_fn2(__attribute__((unused)) const void *key,
return 2; return 2;
} }
void test_iter_fn_priority_heap(void *data) {
printf("Got data %" PRIu32 "\n", *((uint32_t*)data));
}
int main(void) { int main(void) {
// Test LinkedList. // Test LinkedList.
{ {
@ -438,6 +442,18 @@ int main(void) {
free(array); free(array);
simple_archiver_priority_heap_free(&priority_heap); 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); printf("Checks checked: %" PRId32 "\n", checks_checked);