2023-01-22 09:40:35 +01:00
|
|
|
#include "vieter_heap.h"
|
|
|
|
#include "vieter_heap_tree.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
struct vieter_heap {
|
2023-01-24 17:22:11 +01:00
|
|
|
vieter_heap_node *tree;
|
2023-01-22 09:40:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2023-01-24 17:22:11 +01:00
|
|
|
vieter_heap_node *tree = heap->tree;
|
2023-01-22 20:34:05 +01:00
|
|
|
|
|
|
|
while (tree != NULL) {
|
2023-01-24 12:07:30 +01:00
|
|
|
size |= (uint64_t)1 << tree->order;
|
2023-01-22 20:34:05 +01:00
|
|
|
|
2023-01-24 17:22:11 +01:00
|
|
|
tree = tree->ptr.next_tree;
|
2023-01-22 20:34:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
2023-01-22 09:40:35 +01:00
|
|
|
|
2023-01-24 12:07:30 +01:00
|
|
|
void vieter_heap_free(vieter_heap *heap) {
|
2023-01-24 17:22:11 +01:00
|
|
|
vieter_heap_node *tree = heap->tree;
|
|
|
|
vieter_heap_node *next;
|
2023-01-24 12:07:30 +01:00
|
|
|
|
|
|
|
while (tree != NULL) {
|
2023-01-24 17:22:11 +01:00
|
|
|
next = tree->ptr.next_tree;
|
2023-01-24 12:07:30 +01:00
|
|
|
vieter_heap_tree_free(tree);
|
|
|
|
tree = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(heap);
|
|
|
|
}
|
2023-01-22 09:40:35 +01:00
|
|
|
|
|
|
|
vieter_heap_error vieter_heap_insert(vieter_heap *heap, uint64_t key,
|
|
|
|
void *data) {
|
2023-01-24 17:22:11 +01:00
|
|
|
vieter_heap_node *new_tree = vieter_heap_node_init();
|
|
|
|
new_tree->key = key;
|
|
|
|
new_tree->data = data;
|
|
|
|
new_tree->order = 0;
|
2023-01-22 09:40:35 +01:00
|
|
|
|
|
|
|
if (heap->tree == NULL) {
|
|
|
|
heap->tree = new_tree;
|
|
|
|
} else {
|
|
|
|
heap->tree = vieter_heap_tree_merge(heap->tree, new_tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
return vieter_heap_ok;
|
|
|
|
}
|
|
|
|
|
2023-01-24 17:22:11 +01:00
|
|
|
/* vieter_heap_error vieter_heap_pop(void **out, vieter_heap *heap) { */
|
|
|
|
/* if (heap->tree == NULL) { */
|
|
|
|
/* return vieter_heap_empty; */
|
|
|
|
/* } */
|
2023-01-24 12:07:30 +01:00
|
|
|
|
2023-01-24 17:22:11 +01:00
|
|
|
/* if (heap->tree->order == 0) { */
|
|
|
|
/* *out = heap->tree->root->data; */
|
|
|
|
/* vieter_heap_tree_free(heap->tree); */
|
|
|
|
/* heap->tree = NULL; */
|
2023-01-24 12:07:30 +01:00
|
|
|
|
2023-01-24 17:22:11 +01:00
|
|
|
/* return vieter_heap_ok; */
|
|
|
|
/* } */
|
2023-01-24 12:07:30 +01:00
|
|
|
|
2023-01-24 17:22:11 +01:00
|
|
|
/* vieter_heap_tree_pop(out, heap->tree); */
|
2023-01-24 17:01:37 +01:00
|
|
|
|
2023-01-24 17:22:11 +01:00
|
|
|
/* return vieter_heap_ok; */
|
|
|
|
/* } */
|
2023-01-22 09:40:35 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-01-24 17:22:11 +01:00
|
|
|
vieter_heap_node *tree = heap->tree;
|
|
|
|
uint64_t smallest_key = tree->key;
|
|
|
|
*out = tree->data;
|
2023-01-24 12:07:30 +01:00
|
|
|
|
2023-01-24 17:22:11 +01:00
|
|
|
while (tree->ptr.next_tree != NULL) {
|
|
|
|
tree = tree->ptr.next_tree;
|
2023-01-22 20:34:05 +01:00
|
|
|
|
2023-01-24 17:22:11 +01:00
|
|
|
if (tree->key < smallest_key) {
|
|
|
|
smallest_key = tree->key;
|
|
|
|
*out = tree->data;
|
2023-01-22 20:34:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 09:40:35 +01:00
|
|
|
return vieter_heap_ok;
|
|
|
|
}
|