-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.c
117 lines (91 loc) · 2.27 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdlib.h>
#include <stdio.h>
#include "heap.h"
struct heap_t {
compare_func compare;
void** data;
int size;
int capacity;
};
#define INITIAL_SIZE 10
struct heap_t* heap_create(compare_func compare) {
struct heap_t* heap = malloc(sizeof(struct heap_t));
if(!heap)
return NULL;
heap->data = malloc(INITIAL_SIZE * sizeof(void*));
if(!heap->data) {
free(heap);
return NULL;
}
heap->compare = compare;
heap->size = 0;
heap->capacity = INITIAL_SIZE;
return heap;
}
static int resize_heap(struct heap_t* heap) {
unsigned int newCap = heap->capacity * 3 / 2;
printf("Resizing heap: current capacity: %d, new capacity: %d\n", heap->capacity, newCap);
heap->data = realloc(heap->data, newCap * sizeof(void*));
if(!heap->data) {
printf("What in the hell happened... realloc failed?!\n");
return -1;
}
heap->capacity = newCap;
return 0;
}
int heap_insert(struct heap_t* heap, void* elem) {
if(!elem)
return -1;
if(heap->size == heap->capacity)
if(resize_heap(heap) == -1)
return -1;
int pos = heap->size++;
int prevPos = pos;
while(pos && heap->compare(elem, heap->data[(pos / 2) + ((pos & 0x1) - 1)]) < 0) {
pos = (pos / 2) + ((pos & 0x1) - 1);
heap->data[prevPos] = heap->data[pos];
prevPos = pos;
}
heap->data[pos] = elem;
return 0;
}
void* heap_peek(struct heap_t* heap) {
return heap->size ? heap->data[0] : NULL;
}
void* heap_pop(struct heap_t* heap) {
if(!heap->size)
return NULL;
void* elem = heap->data[0];
void* lostElem = heap->data[0] = heap->data[--heap->size];
int pos = 0;
while(1) {
int compLeft = 0;
if(pos * 2 + 1 < heap->size)
compLeft = heap->compare(lostElem, heap->data[pos * 2 + 1]);
int compRight = 0;
if(pos * 2 + 2 < heap->size)
compRight = heap->compare(lostElem, heap->data[pos * 2 + 2]);
if(compLeft <= 0 && compRight <= 0)
break;
if(compRight <= 0 || compLeft > compRight) {
heap->data[pos] = heap->data[pos * 2 + 1];
pos = pos * 2 + 1;
}
else {
heap->data[pos] = heap->data[pos * 2 + 2];
pos = pos * 2 + 2;
}
}
heap->data[pos] = lostElem;
return elem;
}
int heap_size(struct heap_t* heap) {
return heap->size;
}
void heap_delete(struct heap_t* heap, free_func free_f) {
for(int i = 0; i < heap->size; i++) {
free_f(heap->data[i]);
}
free(heap->data);
free(heap);
}