-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.c
68 lines (56 loc) · 1.56 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
#include <stdio.h>
#include <stdlib.h>
#include "heap.h"
#define HEAP_MIN_SIZE 256
//
// Private functions
//
void* addressDataFunc(LinkedListNode* node) {
HeapEntry* entry = node->data;
return (void*) entry->address;
}
//
// Public functions
//
Heap* createEmptyHeap(void) {
Heap* result = malloc(sizeof(Heap));
result->pList = newList();
result->pList->dataCanBeFreed = 1;
return result;
}
HeapVal read(Heap* heap, int address) {
HeapEntry* entry;
LinkedListFindResult findResult = find(heap->pList, (void*) address, &addressDataFunc);
if (findResult.pNode == NULL) {
fprintf(stderr, "No entry found for address %d in heap! Exiting...\n", address);
exit(-1);
return 0;
}
entry = findResult.pNode->data;
return entry->val;
}
void store(Heap* heap, int address, HeapVal val) {
HeapEntry* foundEntry;
LinkedListFindResult findResult = find(heap->pList, (void*) address, &addressDataFunc);
if (findResult.pNode == NULL) {
HeapEntry* newEntry = malloc(sizeof(HeapEntry));
newEntry->address = address;
newEntry->val = val;
addAtHead(heap->pList, newEntry);
return;
}
foundEntry = findResult.pNode->data;
foundEntry->val = val;
}
void printHeap(Heap* heap) {
int i;
for (i = 0; i < heap->pList->length; i++) {
LinkedListNode* node = get(heap->pList, i);
HeapEntry* entry = node->data;
printf("%d: %d\n", entry->address, entry->val);
}
}
void freeHeap(Heap* heap) {
freeList(heap->pList);
free(heap);
}