feat(heap): possibly working pop

pull/4/head
Jef Roosens 2023-01-24 19:45:01 +01:00
parent 95d8c9972b
commit 6845e67cb6
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 78 additions and 81 deletions

View File

@ -51,23 +51,23 @@ vieter_heap_error vieter_heap_insert(vieter_heap *heap, uint64_t key,
return vieter_heap_ok;
}
/* vieter_heap_error vieter_heap_pop(void **out, vieter_heap *heap) { */
/* if (heap->tree == NULL) { */
/* return vieter_heap_empty; */
/* } */
vieter_heap_error vieter_heap_pop(void **out, vieter_heap *heap) {
if (heap->tree == NULL) {
return vieter_heap_empty;
}
/* if (heap->tree->order == 0) { */
/* *out = heap->tree->root->data; */
/* vieter_heap_tree_free(heap->tree); */
/* heap->tree = NULL; */
if (heap->tree->order == 0 && heap->tree->ptr.next_tree == NULL) {
*out = heap->tree->data;
vieter_heap_tree_free(heap->tree);
heap->tree = NULL;
/* return vieter_heap_ok; */
/* } */
return vieter_heap_ok;
}
/* vieter_heap_tree_pop(out, heap->tree); */
heap->tree = vieter_heap_tree_pop(out, heap->tree);
/* return vieter_heap_ok; */
/* } */
return vieter_heap_ok;
}
vieter_heap_error vieter_heap_peek(void **out, vieter_heap *heap) {
if (heap->tree == NULL) {

View File

@ -41,18 +41,6 @@ end:
vieter_heap_node_free(root);
}
/* vieter_heap_tree *vieter_heap_tree_init(vieter_heap_node *root, */
/* vieter_heap_tree *next, uint8_t
* order) { */
/* vieter_heap_tree *tree = malloc(sizeof(vieter_heap_tree)); */
/* tree->root = root; */
/* tree->next = next; */
/* tree->order = order; */
/* return tree; */
/* } */
vieter_heap_node *vieter_heap_tree_merge_same_order(vieter_heap_node *root_a,
vieter_heap_node *root_b) {
vieter_heap_node *new_root;
@ -144,52 +132,61 @@ vieter_heap_node *vieter_heap_tree_merge(vieter_heap_node *root_a,
return out;
}
/* vieter_heap_tree *vieter_heap_tree_pop(void **out, vieter_heap_tree *tree) {
*/
/* vieter_heap_tree *tree_before_smallest = NULL; */
/* vieter_heap_tree *previous_tree = NULL; */
/* vieter_heap_tree *tree_out = tree; */
vieter_heap_node *vieter_heap_tree_pop(void **out, vieter_heap_node *tree) {
vieter_heap_node *tree_before_smallest = NULL;
vieter_heap_node *previous_tree = NULL;
vieter_heap_node *original_root = tree;
/* uint64_t smallest_key = tree->root->key; */
uint64_t smallest_key = tree->key;
/* while (tree->next != NULL) { */
/* previous_tree = tree; */
/* tree = tree->next; */
while (tree->ptr.next_tree != NULL) {
previous_tree = tree;
tree = tree->ptr.next_tree;
/* if (tree->root->key < smallest_key) { */
/* smallest_key = tree->root->key; */
/* *out = tree->root->data; */
/* tree_before_smallest = previous_tree; */
/* } */
/* } */
if (tree->key < smallest_key) {
smallest_key = tree->key;
tree_before_smallest = previous_tree;
}
}
/* if (tree_before_smallest != NULL) { */
/* previous_tree->next = tree->next; */
/* } else { */
/* tree_out = tree_out->next; */
/* } */
vieter_heap_node *tree_to_pop;
/* if (tree->order == 0) { */
/* vieter_heap_tree_free(tree); */
if (tree_before_smallest != NULL) {
tree_to_pop = tree_before_smallest->ptr.next_tree;
tree_before_smallest->ptr.next_tree = tree_to_pop->ptr.next_tree;
} else {
tree_to_pop = original_root;
original_root = original_root->ptr.next_tree;
}
/* return NULL; */
/* } */
*out = tree_to_pop->data;
/* uint8_t old_order = tree->order; */
if (tree_to_pop->order == 0) {
vieter_heap_tree_free(tree_to_pop);
/* vieter_heap_node *node = tree->root->largest_order; */
/* free(tree->root); */
return original_root;
}
/* previous_tree = vieter_heap_tree_init(node, NULL, old_order - 1); */
previous_tree = tree_to_pop->largest_order;
vieter_heap_node_free(tree_to_pop);
/* uint8_t i = 2; */
/* while (node->next_largest_order != NULL) { */
/* node = node->next_largest_order; */
/* previous_tree = vieter_heap_tree_init(node, previous_tree, old_order -
* i); */
tree = previous_tree->ptr.next_largest_order;
previous_tree->ptr.next_tree = NULL;
/* i++; */
/* } */
vieter_heap_node *next_tree;
/* return vieter_heap_tree_merge(tree_out, previous_tree); */
/* } */
while (tree != NULL) {
next_tree = tree->ptr.next_largest_order;
tree->ptr.next_tree = previous_tree;
previous_tree = tree;
tree = next_tree;
}
// original_root is zero if the heap only contained a single tree.
if (original_root != NULL) {
return vieter_heap_tree_merge(original_root, previous_tree);
} else {
return previous_tree;
}
}

View File

@ -51,35 +51,35 @@ void test_insert() {
vieter_heap_free(heap);
}
/* void test_pop() { */
/* vieter_heap *heap = vieter_heap_init(); */
/* TEST_SIZE(heap, 0); */
void test_pop() {
vieter_heap *heap = vieter_heap_init();
TEST_SIZE(heap, 0);
/* void *data; */
void *data;
/* for (uint64_t i = 50; i > 0; i--) { */
/* vieter_heap_insert(heap, i, (void *)i); */
/* TEST_SIZE(heap, (uint64_t)51 - i); */
for (uint64_t i = 50; i > 0; i--) {
vieter_heap_insert(heap, i, (void *)i);
TEST_SIZE(heap, (uint64_t)51 - i);
/* TEST_CHECK(vieter_heap_peek(&data, heap) == vieter_heap_ok); */
/* TEST_CHECK(data == (void*)i); */
/* } */
TEST_CHECK(vieter_heap_peek(&data, heap) == vieter_heap_ok);
TEST_CHECK(data == (void*)i);
}
/* data = NULL; */
data = NULL;
/* for (uint64_t i = 1; i <= 50; i++) { */
/* TEST_CHECK(vieter_heap_pop(&data, heap) == vieter_heap_ok); */
/* TEST_CHECK(data == (void*)i); */
/* TEST_SIZE(heap, (uint64_t)50 - i); */
/* } */
for (uint64_t i = 1; i <= 50; i++) {
TEST_CHECK(vieter_heap_pop(&data, heap) == vieter_heap_ok);
TEST_CHECK(data == (void*)i);
TEST_SIZE(heap, (uint64_t)50 - i);
}
/* vieter_heap_free(heap); */
/* } */
vieter_heap_free(heap);
}
TEST_LIST = {
{"init", test_init},
{"merge same order", test_merge_same_order},
{"insert", test_insert},
/* {"pop", test_pop}, */
{"pop", test_pop},
{NULL, NULL}
};