-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.c
38 lines (32 loc) · 866 Bytes
/
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
#include "heap.h"
#include <stdlib.h>
typedef struct heap {
/** Generic array of pointers. */
int32_t **ptr;
/** How many pointers there are currently in the array. */
int32_t count;
} heap_t;
heap_t *heap_init() {
// The heap array is initially allocated to hold zero elements.
heap_t *heap = malloc(sizeof(heap_t));
heap->ptr = NULL;
heap->count = 0;
return heap;
}
int32_t heap_add(heap_t *heap, int32_t *ptr) {
heap->ptr = realloc(heap->ptr, (heap->count + 1) * sizeof(int32_t *));
heap->ptr[heap->count] = ptr;
int32_t temp = heap->count;
heap->count += 1;
return temp;
}
int32_t *heap_get(heap_t *heap, int32_t ref) {
return heap->ptr[ref];
}
void heap_free(heap_t *heap) {
for (int32_t i = 0; i < heap->count; i++) {
free(heap->ptr[i]);
}
free(heap->ptr);
free(heap);
}