-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.h
219 lines (217 loc) · 21.6 KB
/
heap.h
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* Based on "Type-safe generic data structures in C" by Ian Fisher (https://iafisher.com/blog/2020/06/type-safe-generics-in-c) */
#include <stdlib.h>
#include <stdio.h>
/*
* Declare a min heap with elements of type "type".
* All declared functions will have the signature type_name_heap_FUNCTION.
* The heap struct will have the signature type_name_Heap.
* type_compare should return 0 if elements are equal, <0 if first element is bigger, >0 otherwise.
*
* WARNING: DECL_HEAP with a specific type_name may only be used once in a program.
* Recommended usage:
* #ifndef TYPE_NAME_HEAP
* #define TYPE_NAME_HEAP
* DECL_HEAP(type, type_name, type_compare)
* #endif
*
* @param type
* @param type_name
* @param type_compare
*/
#define DECL_HEAP(type, type_name, type_compare) \
typedef struct type_name##_Heap \
{ \
size_t capacity; \
size_t size; \
type *elements; \
} type_name##_Heap; \
\
/* \
* Instanciates a dynamically allocated heap struct and returns a pointer to it. \
* \
* @param size_t capacity \
* @return Heap *heap \
*/ \
type_name##_Heap *type_name##_heap_create(size_t capacity) \
{ \
type_name##_Heap *heap = malloc(sizeof(type_name##_Heap)); \
\
if (heap == NULL) \
{ \
return NULL; \
} \
\
/* Note: To easily compute parent and child indecies of elements, \
heap->elements[0] is not used, the top element is at index 1. */ \
heap->elements = malloc(sizeof(type) * (capacity + 1)); \
\
if (heap->elements == NULL) \
{ \
free(heap); \
\
return NULL; \
} \
\
heap->size = 0; \
heap->capacity = capacity; \
\
return heap; \
} \
\
/* \
* Deallocates the heap struct and its elements. \
* \
* @param Heap *heap \
*/ \
void type_name##_heap_destroy(type_name##_Heap *heap) \
{ \
free(heap->elements); \
free(heap); \
\
heap = NULL; \
\
return; \
} \
\
/* \
* Sorting from the last heap element upwards. \
* \
* @param Heap *heap \
*/ \
void type_name##_heap_sort_up(type_name##_Heap *heap) \
{ \
/* sorting is only necessary if there is more than one element */ \
if (heap->size > 1) \
{ \
/* start at the bottom */ \
size_t currentIndex = heap->size; \
size_t parentIndex; \
/* temporarily save last element as the one to be sorted up */ \
type sortElement = heap->elements[currentIndex]; \
\
while (currentIndex > 1) \
{ \
parentIndex = currentIndex / 2; \
\
if (type_compare(heap->elements[parentIndex], sortElement) < 0) \
{ \
/* move parent element down if it's greater than the current one */ \
heap->elements[currentIndex] = heap->elements[parentIndex]; \
currentIndex = parentIndex; \
} \
else \
{ \
/* currentIndex is correct position for sortElement */ \
break; \
} \
} \
\
/* insert sortElement at determined position */ \
heap->elements[currentIndex] = sortElement; \
} \
\
return; \
} \
\
/* \
* Sorting from the top heap element downwards. \
* \
* @param Heap *heap \
*/ \
void type_name##_heap_sort_down(type_name##_Heap *heap) \
{ \
/* sorting is only necessary if there is more than one element */ \
if (heap->size > 1) \
{ \
/* start at the top */ \
size_t currentIndex = 1; \
size_t leftChildIndex, rightChildIndex; \
/* temporarily save top element as the one to be sorted down */ \
type sortElement = heap->elements[currentIndex]; \
\
while (1) \
{ \
/* for element n: left child at 2n, right child at 2n+1 */ \
leftChildIndex = 2 * currentIndex; \
rightChildIndex = leftChildIndex + 1; \
\
if (leftChildIndex <= heap->size) \
{ \
/* left child exists */ \
if (rightChildIndex <= heap->size && \
type_compare(sortElement, heap->elements[rightChildIndex]) < 0 && \
type_compare(heap->elements[rightChildIndex], heap->elements[leftChildIndex]) > 0) \
{ \
/* right child exists, is smaller than sortElement and left child \
=> move right child up, move on from right child index */ \
heap->elements[currentIndex] = heap->elements[rightChildIndex]; \
currentIndex = rightChildIndex; \
} \
else if (type_compare(sortElement, heap->elements[leftChildIndex]) < 0) \
{ \
/* left child is smaller than sortElement \
=> move left child up, move on from left child index */ \
heap->elements[currentIndex] = heap->elements[leftChildIndex]; \
currentIndex = leftChildIndex; \
} \
else \
{ \
/* currentIndex is correct position for sortElement */ \
break; \
} \
} \
else \
{ \
/* reached the top of the heap */ \
break; \
} \
} \
\
/* insert sortElement at determined position */ \
heap->elements[currentIndex] = sortElement; \
} \
\
return; \
} \
\
/* \
* Inserts a new element into the heap. \
* \
* @param Heap *heap \
* @param type element \
*/ \
void type_name##_heap_push(type_name##_Heap *heap, type element) \
{ \
/*increment heap size, insert element at the bottom of the heap and sort up */ \
heap->elements[++heap->size] = element; \
type_name##_heap_sort_up(heap); \
\
return; \
} \
\
/* \
* Removes and returns the top element from the heap. \
* \
* @param Heap *heap \
* @return type element \
*/ \
type type_name##_heap_pop(type_name##_Heap *heap) \
{ \
type element; \
\
if (heap->size > 0) \
{ \
element = heap->elements[1]; \
\
/* move last element of the heap to the top, decrement heap size and sort down */ \
heap->elements[1] = heap->elements[heap->size--]; \
type_name##_heap_sort_down(heap); \
} \
else \
{ \
printf("[Error]: attempting to get element from empty heap\nTerminating...\n"); \
exit(1); \
} \
\
return element; \
}