-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathheap.c
172 lines (150 loc) · 3.43 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
#ifdef DEBUG
#include <stdio.h>
#endif // DEBUG
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include "heap.h"
#define DEFAULT_CAP 128
struct heap_s
{
/* size of array */
unsigned int size;
/* items within heap */
unsigned int count;
/* user data */
void *udata;
int (*cmp)(int, int, void *);
int array[];
};
heap_t *
heap_new() {
heap_t *h = (heap_t *)malloc(sizeof(heap_t) + DEFAULT_CAP * sizeof(int));
if (!h) return NULL;
h->size = DEFAULT_CAP;
h->count = 0;
h->udata = NULL;
h->cmp = NULL;
memset(h->array, 0, h->size * sizeof(h->array[0]));
return h;
}
void
heap_init(heap_t *h, int (*cmp)(int, int, void *udata), void *udata) {
h->cmp = cmp;
h->udata = udata;
}
void
heap_free(heap_t *h) {
free(h);
}
static int __left(const int idx) {
return idx * 2 + 1;
}
static int __right(const int idx) {
return idx * 2 + 2;
}
static int __parent(const int idx) {
return (idx - 1) / 2;
}
static void __swap(heap_t * h, const int i1, const int i2) {
int tmp = h->array[i1];
h->array[i1] = h->array[i2];
h->array[i2] = tmp;
}
static void __pushdown(heap_t *h, unsigned int idx) {
while (1) {
unsigned int lt = __left(idx);
unsigned int rt = __right(idx);
unsigned int c;
if (rt >= h->count) {
if (lt >= h->count) return;
c = lt;
} else if (h->cmp(h->array[lt], h->array[rt], h->udata) < 0) {
c = rt;
} else {
c = lt;
}
if (h->cmp(h->array[idx], h->array[c], h->udata) < 0) {
__swap(h, idx, c);
idx = c;
} else return;
}
}
static void __pushup(heap_t *h, unsigned int idx) {
while (idx > 0) {
int p = __parent(idx);
if (h->cmp(h->array[idx], h->array[p], h->udata) < 0) {
return;
} else {
__swap(h, idx, p);
}
idx = p;
}
}
static heap_t * __ensure_size(heap_t *h) {
if (h->count < h->size) return h;
h->size *= 2;
return realloc(h, sizeof(heap_t) + h->size * sizeof(h->array[0]));
}
static int __item_get_idx(heap_t *h, int item) {
unsigned int idx;
for (idx = 0; idx < h->count; idx++)
if (h->array[idx] == item)
return idx;
return -1;
}
int
heap_insert(heap_t **h, int item) {
*h = __ensure_size(*h);
if (*h == NULL) return -1;
(*h)->array[(*h)->count] = item;
__pushup(*h, (*h)->count++);
return 0;
}
int
heap_pop(heap_t *h) {
if (h->count <= 0) return -1;
int item = h->array[0];
h->array[0] = h->array[h->count - 1];
h->count--;
if (h->count > 1) {
__pushdown(h, 0);
}
return item;
}
int
heap_peek(heap_t *h) {
if (h->count <= 0) return -1;
return h->array[0];
}
void
heap_clear(heap_t *h) {
h->count = 0;
memset(h->array, 0, h->size * sizeof(h->array[0]));
}
int
heap_count(heap_t *h) {
return h->count;
}
void
push_up(heap_t *h, int item) {
int idx = __item_get_idx(h, item);
if (idx == -1) return;
__pushup(h, idx);
}
void
push_down(heap_t *h, int item) {
int idx = __item_get_idx(h, item);
if (idx == -1) return;
__pushdown(h, idx);
}
#ifdef DEBUG
void
heap_dump(heap_t *h) {
unsigned int idx;
printf("===== head %p, count:%d, size:%d\n", h, h->count, h->size);
for (idx = 0; idx < h->count; idx++)
printf("%d ", h->array[idx]);
printf("===== head end\n");
}
#endif // DEBUG