-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.h
247 lines (229 loc) · 7.4 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/// @file
#pragma once
#include <stddef.h>
#include "arena.h"
typedef struct Heap Heap;
typedef struct HeapItem HeapItem;
typedef int HeapDataCompare(const void *, const void *, void *); ///< Data comparison function
typedef void *HeapDataCopy(Arena *, void *, const void *, long); ///< Data copy function
/**
* @brief Represents a binary heap (min-heap by default)
*/
struct Heap {
Arena *arena; ///< Pointer to an arena allocator
struct {
long size; ///< Size of the item data in bytes
HeapDataCompare *compare; ///< Pointer to a data comparison function
HeapDataCopy *copy; ///< Pointer to a data copy function
} data; ///< Data properties
long length; ///< Number of items in the heap
HeapItem *begin; ///< Pointer to the root item of the heap
HeapItem *end; ///< Pointer to the last item in the heap
};
/**
* @brief Represents a single item in the heap
*/
struct HeapItem {
void *data; ///< Pointer to the item data
HeapItem *parent; ///< Pointer to the parent item
HeapItem *left; ///< Pointer to the left child item
HeapItem *right; ///< Pointer to the right child item
HeapItem *next; ///< Pointer to the next item in insertion order
};
/**
* @brief Iterate over all items of a heap
* @param item Current heap item
* @param self Pointer to a heap
*/
#define heap_for_each(item, self) for (auto(item) = (self)->begin; item; (item) = (item)->next)
/**
* @brief Create a new heap
* @param arena Pointer to an arena allocator
* @param size Size of item data in bytes (optional)
* @param compare Pointer to a data comparison function
* @return New heap instance
* @note If `size == 0`, the data pointers will be directly assigned rather than copied
*/
static Heap heap_create(Arena *arena, long size, HeapDataCompare *compare) {
Heap heap = {};
heap.arena = arena;
heap.data.size = size;
heap.data.compare = compare;
heap.data.copy = arena_memcpy;
return heap;
}
/// @private
static void x__heap_item_init(const Heap *self, HeapItem *item, void *data) {
if (data && self->data.size) {
item->data = arena_malloc(self->arena, 1, self->data.size, alignof(max_align_t));
self->data.copy(self->arena, item->data, data, self->data.size);
}
else {
item->data = data;
}
}
/// @private
static HeapItem *x__heap_find_parent(HeapItem *item) {
while (item->parent && item == item->parent->right) {
item = item->parent;
}
if (item->parent) {
if (item->parent->right) {
item = item->parent->right;
}
else {
return item->parent;
}
}
while (item->left) {
item = item->left;
}
return item;
}
/// @private
static void x__heap_sift_up(const Heap *self, HeapItem *item, void *context) {
while (item->parent && self->data.compare(item->parent->data, item->data, context) > 0) {
auto swap = item->data;
item->data = item->parent->data;
item->parent->data = swap;
item = item->parent;
}
}
/**
* @brief Insert a new item into a heap
* @param self Pointer to a heap
* @param data Pointer to the item data
* @param context Pointer to a user-provided context for the comparison function (optional)
*/
static void heap_push(Heap *self, void *data, void *context) {
HeapItem *item = arena_calloc(self->arena, 1, sizeof(HeapItem), alignof(HeapItem));
x__heap_item_init(self, item, data);
if (self->length == 0) {
self->begin = item;
self->end = item;
}
else {
auto parent = x__heap_find_parent(self->end);
assert(!parent->right);
item->parent = parent;
if (!parent->left) {
parent->left = item;
}
else {
parent->right = item;
}
self->end->next = item;
self->end = item;
x__heap_sift_up(self, item, context);
}
self->length += 1;
}
/// @private
static HeapItem *x__heap_find_end(HeapItem *item) {
while (item->parent && item == item->parent->left) {
item = item->parent;
}
if (item->parent) {
assert(item->parent->left);
item = item->parent->left;
}
while (item->right) {
item = item->right;
}
return item;
}
/// @private
static void x__heap_sift_down(const Heap *self, HeapItem *item, void *context) {
while (item) {
auto smallest = item;
if (item->left && self->data.compare(item->left->data, smallest->data, context) < 0) {
smallest = item->left;
}
if (item->right && self->data.compare(item->right->data, smallest->data, context) < 0) {
smallest = item->right;
}
if (smallest == item) {
break;
}
auto swap = item->data;
item->data = smallest->data; // cppcheck-suppress nullPointerRedundantCheck
smallest->data = swap; // cppcheck-suppress nullPointerRedundantCheck
item = smallest;
}
}
/**
* @brief Remove the root item from the heap
* @param self Pointer to a heap
* @param context Pointer to a user-provided context for the comparison function (optional)
* @return Pointer to the item data, or `nullptr` if the heap is empty
*/
static void *heap_pop(Heap *self, void *context) {
if (self->length == 0) {
return nullptr;
}
void *data = self->begin->data;
if (self->length == 1) {
self->begin = nullptr;
self->end = nullptr;
}
else {
assert(self->end->parent);
auto end = x__heap_find_end(self->end);
if (self->end == self->end->parent->right) {
self->end->parent->right = nullptr;
}
else {
self->end->parent->left = nullptr;
}
self->begin->data = self->end->data;
self->end = end;
self->end->next = nullptr;
x__heap_sift_down(self, self->begin, context);
}
self->length -= 1;
return data;
}
/**
* @brief Retrieve the root item of the heap
* @param self Pointer to a heap
* @return Pointer to the item data, or `nullptr` if the heap is empty
*/
static void *heap_peek(const Heap *self) {
if (self->length == 0) {
return nullptr;
}
return self->begin->data;
}
/**
* @brief Create a clone of a heap
* @param self Pointer to a heap
* @param context Pointer to a user-provided context for the comparison function (optional)
* @param arena Pointer to an arena allocator (optional)
* @return Cloned heap instance
* @note If no arena allocator is passed, the arena allocator of the heap is used
*/
static Heap heap_clone(const Heap *self, void *context, Arena *arena) {
Heap heap = {};
heap.arena = arena ? arena : self->arena;
heap.data = self->data;
heap_for_each(item, self) {
heap_push(&heap, item->data, context);
}
return heap;
}
/**
* @brief Retrieve an array of heap items
* @param self Pointer to a heap
* @param arena Pointer to an arena allocator (optional)
* @return Pointer to an array of items
* @note If no arena allocator is passed, the arena allocator of the heap is used
*/
static HeapItem *heap_items(const Heap *self, Arena *arena) {
arena = arena ? arena : self->arena;
HeapItem *items = arena_malloc(arena, self->length, sizeof(HeapItem), alignof(HeapItem));
long index = 0;
heap_for_each(item, self) {
items[index++] = *item;
}
return items;
}