-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.c
55 lines (46 loc) · 1.45 KB
/
heap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include "heap.h"
#include "slice.h"
#include "stack.h"
void heap_init(struct Heap * const self) {
stack_init(&self->stack);
}
struct Heap heap_create(void) {
struct Heap self;
heap_init(&self);
return self;
}
void heap_init_with_capacity(struct Heap * const self, const size_t cap) {
stack_init_with_capacity(&self->stack, cap);
}
struct Heap heap_create_with_capacity(const size_t cap) {
struct Heap self;
heap_init_with_capacity(&self, cap);
return self;
}
void heap_free(struct Heap * const self) {
stack_free(&self->stack);
}
void heap_push(struct Heap * const self, const uint8_t data) {
// Resize if necessary
stack_ensure_has_capacity(&self->stack);
stack_ensure_has_vacancy(&self->stack);
// Push data to the correct place
size_t child = self->stack.slice.length++;
size_t parent = (child - 1) / 2;
while (child > 0 && data < self->stack.slice.buffer[parent]) {
// Bubble up the child
self->stack.slice.buffer[child] = self->stack.slice.buffer[parent];
child = parent;
parent = (child - 1) / 2;
}
self->stack.slice.buffer[child] = data;
}
uint8_t heap_pop(struct Heap * const self) {
// Swap root with the right-most, bottom-most node (which should yield an almost-heap)
const uint8_t root = slice_swap_remove(&self->stack.slice, 0);
slice_heapify(self->stack.slice);
return root;
}