libvieter/src/heap/vieter_heap.c

67 lines
1.4 KiB
C
Raw Normal View History

#include "vieter_heap.h"
#include "vieter_heap_tree.h"
#include <stdlib.h>
struct vieter_heap {
vieter_heap_tree *tree;
};
vieter_heap *vieter_heap_init() { return calloc(1, sizeof(vieter_heap)); }
2023-01-22 20:34:05 +01:00
uint64_t vieter_heap_size(vieter_heap *heap) {
uint64_t size = 0;
vieter_heap_tree *tree = heap->tree;
while (tree != NULL) {
size |= 1 << tree->order;
tree = tree->next;
}
return size;
}
void vieter_heap_free(vieter_heap *heap) {}
vieter_heap_error vieter_heap_insert(vieter_heap *heap, uint64_t key,
void *data) {
vieter_heap_node *new_node = vieter_heap_node_init();
new_node->key = key;
new_node->data = data;
vieter_heap_tree *new_tree = vieter_heap_tree_init(new_node, NULL, 1);
if (heap->tree == NULL) {
heap->tree = new_tree;
} else {
heap->tree = vieter_heap_tree_merge(heap->tree, new_tree);
}
return vieter_heap_ok;
}
vieter_heap_error vieter_heap_pop(void **out, vieter_heap *heap) {
return vieter_heap_ok;
}
vieter_heap_error vieter_heap_peek(void **out, vieter_heap *heap) {
2023-01-22 20:34:05 +01:00
if (heap->tree == NULL) {
return vieter_heap_empty;
}
uint64_t smallest_key;
vieter_heap_tree *tree = heap->tree;
while (tree != NULL) {
if (tree->root->key < smallest_key) {
smallest_key = tree->root->key;
*out = tree->root->data;
}
tree = tree->next;
}
return vieter_heap_ok;
}