-
Notifications
You must be signed in to change notification settings - Fork 15
/
heap.c
456 lines (341 loc) · 12.4 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/**
* This file defines the methods declared in heap.h
* These are used to create and manipulate a heap
* data structure.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <assert.h>
#include "heap.h"
// Helpful Macro's
#define LEFT_CHILD(i) ((i<<1)+1)
#define RIGHT_CHILD(i) ((i<<1)+2)
#define PARENT_ENTRY(i) ((i-1)>>1)
#define SWAP_ENTRIES(parent,child) { \
void* temp = parent->key; \
parent->key = child->key; \
child->key = temp; \
temp = parent->value; \
parent->value = child->value; \
child->value = temp; \
}
#define GET_ENTRY(index,map_table) (((heap_entry*)*(map_table+index/ENTRIES_PER_PAGE))+(index % ENTRIES_PER_PAGE))
/**
* Stores the number of heap_entry structures
* we can fit into a single page of memory.
*
* This is determined by the page size, so we
* need to determine this at run time.
*/
static int ENTRIES_PER_PAGE = 0;
/**
* Stores the number of bytes in a single
* page of memory.
*/
static int PAGE_SIZE = 0;
// Helper function to map a number of pages into memory
// Returns NULL on error, otherwise returns a pointer to the
// first page.
static void* map_in_pages(int page_count) {
// Check everything
assert(page_count > 0);
// Call mmmap to get the pages
void* addr = mmap(NULL, page_count*PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
if (addr == MAP_FAILED)
return NULL;
else {
// Clear the memory
bzero(addr,page_count*PAGE_SIZE);
// Return the address
return addr;
}
}
// Helper function to map a number of pages out of memory
static void map_out_pages(void* addr, int page_count) {
// Check everything
assert(addr != NULL);
assert(page_count > 0);
// Call munmap to get rid of the pages
int result = munmap(addr, page_count*PAGE_SIZE);
// The result should be 0
assert(result == 0);
}
// This is a comparison function that treats keys as signed ints
int compare_int_keys(register void* key1, register void* key2) {
// Cast them as int* and read them in
register int key1_v = *((int*)key1);
register int key2_v = *((int*)key2);
// Perform the comparison
if (key1_v < key2_v)
return -1;
else if (key1_v == key2_v)
return 0;
else
return 1;
}
// Creates a new heap
void heap_create(heap* h, int initial_size, int (*comp_func)(void*,void*)) {
// Check if we need to setup our globals
if (PAGE_SIZE == 0) {
// Get the page size
PAGE_SIZE = getpagesize();
// Calculate the max entries
ENTRIES_PER_PAGE = PAGE_SIZE / sizeof(heap_entry);
}
// Check that initial size is greater than 0, else set it to ENTRIES_PER_PAGE
if (initial_size <= 0)
initial_size = ENTRIES_PER_PAGE;
// If the comp_func is null, treat the keys as signed ints
if (comp_func == NULL)
comp_func = compare_int_keys;
// Store the compare function
h->compare_func = comp_func;
// Set active entries to 0
h->active_entries = 0;
// Determine how many pages of entries we need
h->minimum_pages = initial_size / ENTRIES_PER_PAGE + ((initial_size % ENTRIES_PER_PAGE > 0) ? 1 : 0);
// Determine how big the map table should be
h->map_pages = sizeof(void*) * h->minimum_pages / PAGE_SIZE + 1;
// Allocate the map table
h->mapping_table = (void**)map_in_pages(h->map_pages);
assert(h->mapping_table != NULL);
// Allocate the entry pages
void* addr = map_in_pages(h->minimum_pages);
assert(addr != NULL);
// Add these to the map table
for (int i=0;i<h->minimum_pages;i++) {
*(h->mapping_table+i) = addr+(i*PAGE_SIZE);
}
// Set the allocated pages
h->allocated_pages = h->minimum_pages;
}
// Cleanup a heap
void heap_destroy(heap* h) {
// Check that h is not null
assert(h != NULL);
// Un-map all the entry pages
void** map_table = h->mapping_table;
assert(map_table != NULL);
for (int i=0; i < h->allocated_pages; i++) {
map_out_pages(*(map_table+i),1);
}
// Map out the map table
map_out_pages(map_table, h->map_pages);
// Clear everything
h->active_entries = 0;
h->allocated_pages = 0;
h->map_pages = 0;
h->mapping_table = NULL;
}
// Gets the size of the heap
int heap_size(heap* h) {
// Return the active entries
return h->active_entries;
}
/* In-line version is much faster
// Fetches the address of a heap_entry
static heap_entry* heap_get_entry(int index, void** map_table) {
// Determine which page that index falls in
int entry_page = index / ENTRIES_PER_PAGE;
// Determine the offset into the page
int page_offset = index % ENTRIES_PER_PAGE;
// Get the address of the page
heap_entry* page_address = (heap_entry*)*(map_table+entry_page);
// Get the corrent entry
return page_address+page_offset;
}
*/
// Gets the minimum element
int heap_min(heap* h, void** key, void** value) {
// Check the number of elements, abort if 0
if (h->active_entries == 0)
return 0;
// Get the 0th element
heap_entry* root = GET_ENTRY(0, h->mapping_table);
// Set the key and value
*key = root->key;
*value = root->value;
// Success
return 1;
}
// Insert a new element
void heap_insert(heap *h, void* key, void* value) {
// Check if this heap is not destoyed
assert(h->mapping_table != NULL);
// Check if we have room
int max_entries = h->allocated_pages * ENTRIES_PER_PAGE;
if (h->active_entries + 1 > max_entries) {
// Get the number of map pages
int map_pages = h->map_pages;
// We need a new page, do we have room?
int mapable_pages = map_pages * PAGE_SIZE / sizeof(void*);
// Check if we need to grow the map table
if (h->allocated_pages + 1 > mapable_pages) {
// Allocate a new table, slightly bigger
void *new_table = map_in_pages(map_pages + 1);
// Get the old table
void *old_table = (void*)h->mapping_table;
// Copy the old entries to the new table
memcpy(new_table, old_table, map_pages * PAGE_SIZE);
// Delete the old table
map_out_pages(old_table, map_pages);
// Swap to the new table
h->mapping_table = (void**)new_table;
// Update the number of map pages
h->map_pages = map_pages + 1;
}
// Allocate a new page
void* addr = map_in_pages(1);
// Add this to the map
*(h->mapping_table+h->allocated_pages) = addr;
// Update the number of allocated pages
h->allocated_pages++;
}
// Store the comparison function
int (*cmp_func)(void*,void*) = h->compare_func;
// Store the map table address
void** map_table = h->mapping_table;
// Get the current index
int current_index = h->active_entries;
heap_entry* current = GET_ENTRY(current_index, map_table);
// Loop variables
int parent_index;
heap_entry *parent;
// While we can, keep swapping with our parent
while (current_index > 0) {
// Get the parent index
parent_index = PARENT_ENTRY(current_index);
// Get the parent entry
parent = GET_ENTRY(parent_index, map_table);
// Compare the keys, and swap if we need to
if (cmp_func(key, parent->key) < 0) {
// Move the parent down
current->key = parent->key;
current->value = parent->value;
// Move our reference
current_index = parent_index;
current = parent;
// We are done swapping
} else
break;
}
// Insert at the current index
current->key = key;
current->value = value;
// Increase the number of active entries
h->active_entries++;
}
// Deletes the minimum entry in the heap
int heap_delmin(heap* h, void** key, void** value) {
// Check there is a minimum
if (h->active_entries == 0)
return 0;
// Load in the map table
void** map_table = h->mapping_table;
// Get the root element
int current_index = 0;
heap_entry* current = GET_ENTRY(current_index, map_table);
// Store the outputs
if (key != NULL && value != NULL) {
*key = current->key;
*value = current->value;
}
// Reduce the number of active entries
h->active_entries--;
// Get the active entries
int entries = h->active_entries;
// If there are any other nodes, we may need to move them up
if (h->active_entries > 0) {
// Move the last element to the root
heap_entry* last = GET_ENTRY(entries,map_table);
current->key = last->key;
current->value = last->value;
// Loop variables
heap_entry* left_child;
heap_entry* right_child;
// Load the comparison function
int (*cmp_func)(void*,void*) = h->compare_func;
// Store the left index
int left_child_index;
while (left_child_index = LEFT_CHILD(current_index), left_child_index < entries) {
// Load the left child
left_child = GET_ENTRY(left_child_index, map_table);
// We have a left + right child
if (left_child_index+1 < entries) {
// Load the right child
right_child = GET_ENTRY((left_child_index+1), map_table);
// Find the smaller child
if (cmp_func(left_child->key, right_child->key) <= 0) {
// Swap with the left if it is smaller
if (cmp_func(current->key, left_child->key) == 1) {
SWAP_ENTRIES(current,left_child);
current_index = left_child_index;
current = left_child;
// Otherwise, the current is smaller
} else
break;
// Right child is smaller
} else {
// Swap with the right if it is smaller
if (cmp_func(current->key, right_child->key) == 1) {
SWAP_ENTRIES(current,right_child);
current_index = left_child_index+1;
current = right_child;
// Current is smaller
} else
break;
}
// We only have a left child, only do something if the left is smaller
} else if (cmp_func(current->key, left_child->key) == 1) {
SWAP_ENTRIES(current,left_child);
current_index = left_child_index;
current = left_child;
// Done otherwise
} else
break;
}
}
// Check if we should release a page of memory
int used_pages = entries / ENTRIES_PER_PAGE + ((entries % ENTRIES_PER_PAGE > 0) ? 1 : 0);
// Allow one empty page, but not two
if (h->allocated_pages > used_pages + 1 && h->allocated_pages > h->minimum_pages) {
// Get the address of the page to delete
void* addr = *(map_table+h->allocated_pages-1);
// Map out
map_out_pages(addr, 1);
// Decrement the allocated count
h->allocated_pages--;
}
// Success
return 1;
}
// Allows a user to iterate over all entries, e.g. to free() the memory
void heap_foreach(heap* h, void (*func)(void*,void*)) {
// Store the current index and max index
int index = 0;
int entries = h->active_entries;
heap_entry* entry;
void** map_table = h->mapping_table;
for (;index<entries;index++) {
// Get the entry
entry = GET_ENTRY(index,map_table);
// Call the user function
func(entry->key, entry->value);
}
}
void heap_foreach_entry(heap* h, void (*func)(heap_entry*, void*), void* arg) {
// Store the current index and max index
int index = 0;
int entries = h->active_entries;
heap_entry* entry;
void** map_table = h->mapping_table;
for (;index<entries;index++) {
// Get the entry
entry = GET_ENTRY(index,map_table);
// Call the user function
func(entry, arg);
}
}