-
Notifications
You must be signed in to change notification settings - Fork 0
/
pq.c
231 lines (177 loc) · 3.66 KB
/
pq.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
#include "./pq.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INIT_SIZE 64
#define SIZE_INC_MUL 2
/* Function to return the index of the parent node of a given node */
static int parent(int i);
/* Function to return the index of the left child of the given node */
static int leftChild(int i);
/* Function to return the index of the right child of the given node */
static int rightChild(int i);
/* Function to shift up the node in order to maintain the heap property */
static void shiftUp(struct pq *pq, int i);
/* Function to shift down the node in order to maintain the heap property */
static void shiftDown(struct pq *pq, int i);
int pq_init(struct pq *pq, const size_t sizeof_elem,
void *(*d_copy)(const void *src),
int (*d_compare_smaller)(void *a, void *b),
void (*d_free)(void *a))
{
if (!pq || !sizeof_elem || !d_compare_smaller)
return -1;
pq->allocated = INIT_SIZE;
pq->size = -1;
pq->d_copy = d_copy;
pq->d_compare = d_compare_smaller;
pq->d_free = d_free;
pq->v = calloc(pq->allocated, sizeof(void *));
if (!pq->v) {
return -1;
}
return 0;
}
int pq_insert(struct pq *pq, const void *data)
{
void *np;
if (!pq || !data)
return -1;
np = pq->d_copy(data);
if (!np)
return -1;
return pq_emplace(pq, np);
}
int pq_emplace(struct pq *pq, void *data)
{
void **p;
if (!pq || !data)
return -1;
++pq->size;
if ((size_t)pq->size >= pq->allocated) {
p = (void **)realloc(pq->v,
pq->allocated * SIZE_INC_MUL * sizeof(void *));
if (!p) {
--pq->size;
return -1;
}
pq->allocated *= SIZE_INC_MUL;
pq->v = p;
}
pq->v[pq->size] = data;
/* Shift Up to maintain heap property */
shiftUp(pq, pq->size);
return 0;
}
void *pq_peek(const struct pq *pq)
{
void *p;
if (!pq)
return NULL;
if (pq->size >= 0)
p = pq->d_copy(pq->v[0]);
else
p = NULL;
return p;
}
void *pq_peek_p(const struct pq *pq)
{
void *p;
if (!pq)
return NULL;
if (pq->size >= 0)
p = pq->v[0];
else
p = NULL;
return p;
}
void *pq_pop(struct pq *pq, const int return_data)
{
void *n;
void **p;
if (!pq)
return NULL;
if (pq->size < 0)
return NULL;
n = pq->v[0];
pq->v[0] = pq->v[pq->size];
pq->v[pq->size] = NULL;
--pq->size;
if (pq->size < (int)pq->allocated / (SIZE_INC_MUL * SIZE_INC_MUL)
&& pq->size > INIT_SIZE) {
p = (void **)realloc(
pq->v,
pq->allocated / SIZE_INC_MUL * sizeof(void *));
if (p) {
pq->v = p;
pq->allocated /= SIZE_INC_MUL;
}
}
shiftDown(pq, 0);
if (!return_data) {
if (pq->d_free)
pq->d_free(n);
n = NULL;
}
return n;
}
void pq_free(struct pq *pq)
{
int i;
if (!pq)
return;
if (pq->d_free) {
for (i = 0; i <= pq->size; ++i) {
pq->d_free(pq->v[i]);
}
}
free(pq->v);
}
int parent(int i)
{
return (i - 1) / 2;
}
int leftChild(int i)
{
return ((2 * i) + 1);
}
int rightChild(int i)
{
return ((2 * i) + 2);
}
void shiftUp(struct pq *pq, int i)
{
void *temp;
while (i > 0 && pq->d_compare(pq->v[parent(i)], pq->v[i])) {
/* Swap parent and current node */
temp = pq->v[i];
pq->v[i] = pq->v[parent(i)];
pq->v[parent(i)] = temp;
/* Update i to parent of i */
i = parent(i);
}
}
void shiftDown(struct pq *pq, int i)
{
int l, r, max_index;
void *temp;
max_index = i;
/* Left Child */
l = leftChild(i);
if (l <= pq->size && !pq->d_compare(pq->v[l], pq->v[max_index]))
max_index = l;
/* Right Child */
r = rightChild(i);
if (r <= pq->size && !pq->d_compare(pq->v[r], pq->v[max_index]))
max_index = r;
/* If i not same as max_index */
if (i != max_index) {
temp = pq->v[i];
pq->v[i] = pq->v[max_index];
pq->v[max_index] = temp;
shiftDown(pq, max_index);
}
}
#undef INIT_SIZE
#undef SIZE_INC_MUL