swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "kint.h" |
| 4 | |
| 5 | #define HEAP_SIZE 4096 |
| 6 | #define HEAP_MAGIC 0xCAFEBABE |
| 7 | |
| 8 | struct heap_entry |
| 9 | { |
| 10 | uint key; |
| 11 | size_t address; |
| 12 | }; |
| 13 | |
| 14 | struct min_heap |
| 15 | { |
| 16 | struct heap_entry elements[HEAP_SIZE]; |
| 17 | uint size; |
| 18 | }; |
| 19 | |
| 20 | struct heap_alloc_header |
| 21 | { |
| 22 | uint magic; |
| 23 | bool allocated; |
| 24 | // size = size from beginning of header to end of footer |
| 25 | size_t size; |
| 26 | }; |
| 27 | |
| 28 | struct heap_alloc_footer |
| 29 | { |
| 30 | // size = size from beginning of header to end of footer |
| 31 | size_t size; |
| 32 | }; |
| 33 | |
| 34 | int heap_parent(int i); |
| 35 | int heap_left(int i); |
| 36 | int heap_right(int i); |
swissChili | 6575dd7 | 2021-02-28 11:31:28 -0800 | [diff] [blame] | 37 | void heap_delete(struct min_heap *heap, int i); |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 38 | void heap_insert(struct min_heap *heap, struct heap_entry e); |
| 39 | void heap_decrease(struct min_heap *heap, int k, struct heap_entry to); |
swissChili | 6575dd7 | 2021-02-28 11:31:28 -0800 | [diff] [blame] | 40 | void heap_decrease_entry(struct min_heap *heap, struct heap_entry from, |
| 41 | struct heap_entry to); |
| 42 | void heap_delete_entry(struct min_heap *heap, struct heap_entry e); |
swissChili | e4f0199 | 2021-02-25 15:38:12 -0800 | [diff] [blame] | 43 | |
| 44 | struct heap_entry heap_lookup_min(struct min_heap *heap, int min, bool *ok, |
| 45 | bool delete, int *i); |