forked from vieter-v/libvieter
refactor(heap): properly organised code
parent
050e99b413
commit
6823050c2f
|
@ -1,8 +1,8 @@
|
||||||
#ifndef VIETER_HEAP
|
#ifndef VIETER_HEAP
|
||||||
#define VIETER_HEAP
|
#define VIETER_HEAP
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
typedef struct vieter_heap vieter_heap;
|
typedef struct vieter_heap vieter_heap;
|
||||||
|
|
||||||
|
@ -17,7 +17,8 @@ void vieter_heap_free(vieter_heap *heap);
|
||||||
|
|
||||||
uint64_t vieter_heap_size(vieter_heap *heap);
|
uint64_t vieter_heap_size(vieter_heap *heap);
|
||||||
|
|
||||||
vieter_heap_error vieter_heap_insert(vieter_heap *heap, uint64_t key, void *data);
|
vieter_heap_error vieter_heap_insert(vieter_heap *heap, uint64_t key,
|
||||||
|
void *data);
|
||||||
|
|
||||||
vieter_heap_error vieter_heap_pop(void **out, vieter_heap *heap);
|
vieter_heap_error vieter_heap_pop(void **out, vieter_heap *heap);
|
||||||
|
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
#include "vieter_heap.h"
|
|
||||||
|
|
||||||
struct vieter_heap {
|
|
||||||
uint64_t size;
|
|
||||||
};
|
|
||||||
|
|
||||||
vieter_heap *vieter_heap_init() {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t vieter_heap_size(vieter_heap *heap) {
|
|
||||||
return heap->size;
|
|
||||||
}
|
|
||||||
|
|
||||||
void vieter_heap_free(vieter_heap *heap) {}
|
|
||||||
|
|
||||||
|
|
||||||
vieter_heap_error vieter_heap_insert(vieter_heap *heap, uint64_t key, void *data) {
|
|
||||||
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) {
|
|
||||||
return vieter_heap_ok;
|
|
||||||
}
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
#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)); }
|
||||||
|
|
||||||
|
uint64_t vieter_heap_size(vieter_heap *heap) { return 0; }
|
||||||
|
|
||||||
|
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) {
|
||||||
|
return vieter_heap_ok;
|
||||||
|
}
|
|
@ -0,0 +1,38 @@
|
||||||
|
#include "vieter_heap_tree.h"
|
||||||
|
|
||||||
|
vieter_heap_node *vieter_heap_node_init() {
|
||||||
|
return calloc(1, sizeof(vieter_heap_node));
|
||||||
|
}
|
||||||
|
|
||||||
|
vieter_heap_node *vieter_heap_node_merge_same_order(vieter_heap_node *root_a,
|
||||||
|
vieter_heap_node *root_b) {
|
||||||
|
vieter_heap_node *new_root, *new_child;
|
||||||
|
|
||||||
|
if (root_a->key <= root_b->key) {
|
||||||
|
new_root = root_a;
|
||||||
|
new_child = root_b;
|
||||||
|
} else {
|
||||||
|
new_root = root_b;
|
||||||
|
new_child = root_a;
|
||||||
|
}
|
||||||
|
|
||||||
|
new_root->next_largest_order = new_root->largest_order;
|
||||||
|
new_root->largest_order = new_child;
|
||||||
|
|
||||||
|
return new_root;
|
||||||
|
}
|
||||||
|
|
||||||
|
vieter_heap_tree *vieter_heap_tree_init(vieter_heap_node *root,
|
||||||
|
vieter_heap_tree *next,
|
||||||
|
uint64_t order) {
|
||||||
|
vieter_heap_tree *tree = malloc(sizeof(vieter_heap_tree));
|
||||||
|
|
||||||
|
tree->root = root;
|
||||||
|
tree->next = next;
|
||||||
|
tree->order = order;
|
||||||
|
|
||||||
|
return tree;
|
||||||
|
}
|
||||||
|
|
||||||
|
vieter_heap_tree *vieter_heap_tree_merge(vieter_heap_tree *tree_a,
|
||||||
|
vieter_heap_tree *tree_b) {}
|
|
@ -0,0 +1,30 @@
|
||||||
|
#ifndef VIETER_HEAP_TREE
|
||||||
|
#define VIETER_HEAP_TREE
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct vieter_heap_node {
|
||||||
|
uint64_t key;
|
||||||
|
void *data;
|
||||||
|
struct vieter_heap_node *largest_order;
|
||||||
|
struct vieter_heap_node *next_largest_order;
|
||||||
|
} vieter_heap_node;
|
||||||
|
|
||||||
|
vieter_heap_node *vieter_heap_node_init();
|
||||||
|
|
||||||
|
void vieter_heap_node_free(vieter_heap_node *node);
|
||||||
|
|
||||||
|
vieter_heap_node *vieter_heap_node_merge_same_order(vieter_heap_node *root_a, vieter_heap_node *root_b);
|
||||||
|
|
||||||
|
typedef struct vieter_heap_tree {
|
||||||
|
uint64_t order;
|
||||||
|
vieter_heap_node *root;
|
||||||
|
struct vieter_heap_tree *next;
|
||||||
|
} vieter_heap_tree;
|
||||||
|
|
||||||
|
vieter_heap_tree *vieter_heap_tree_init(vieter_heap_node *root, vieter_heap_tree *next, uint64_t order);
|
||||||
|
|
||||||
|
vieter_heap_tree *vieter_heap_tree_merge(vieter_heap_tree *tree_a, vieter_heap_tree *tree_b);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue