-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority-queue.c
222 lines (180 loc) · 3.62 KB
/
priority-queue.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
#include "priority-queue.h"
#include <math.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
struct item {
double priority;
uint16_t obj;
};
struct pqueue_ {
struct item *data;
size_t sz;
size_t cap;
enum priority_type type;
};
// Seems reasonable to start with?
enum { DEFAULT_HEAP_CAPACITY = 8 };
static size_t parent(size_t idx)
{
return (idx - 1) / 2;
}
static size_t left(size_t idx)
{
return 2 * idx + 1;
}
static size_t right(size_t idx)
{
return 2 * idx + 2;
}
static void swap(struct item *arr, size_t a, size_t b)
{
uint16_t tmp = arr[a].obj;
arr[a].obj = arr[b].obj;
arr[b].obj = tmp;
double tmp_priority = arr[a].priority;
arr[a].priority = arr[b].priority;
arr[b].priority = tmp_priority;
}
pqueue *pqueue_create(enum priority_type type)
{
pqueue *pq = malloc(sizeof(*pq));
if (!pq) {
return NULL;
}
pq->sz = 0;
pq->cap = DEFAULT_HEAP_CAPACITY;
pq->type = type;
pq->data = malloc(pq->cap * sizeof(*pq->data));
if (!pq->data) {
free(pq);
return NULL;
}
return pq;
}
double pqueue_get_next_priority(const pqueue * pq)
{
if (!pq || pq->sz == 0) {
return NAN;
}
return pq->data[0].priority;
}
uint16_t pqueue_get_next(pqueue * pq)
{
if (!pq || pq->sz == 0) {
return 0xffff;
}
return pq->data[0].obj;
}
bool pqueue_is_empty(const pqueue * pq)
{
if (!pq) {
return true;
}
return pq->sz == 0;
}
bool pqueue_enqueue(pqueue * pq, double priority, uint16_t obj)
{
if (!pq || isnan(priority)) {
return false;
}
if (pq->sz + 1 >= pq->cap) {
struct item *tmp = realloc(pq->data, 2 * pq->cap
* sizeof(*pq->data));
if (!tmp) {
return false;
}
pq->cap *= 2;
pq->data = tmp;
}
pq->data[pq->sz].priority = priority;
pq->data[pq->sz++].obj = obj;
size_t idx = pq->sz - 1;
// Bubble up!
while (idx > 0 &&
(pq->type == MIN ? (pq->data[parent(idx)].priority > priority)
: (pq->data[parent(idx)].priority < priority))) {
swap(pq->data, idx, parent(idx));
idx = parent(idx);
}
return true;
}
double pqueue_get_priority(const pqueue * pq, uint16_t obj)
{
if (!pq || !obj) {
return NAN;
}
for (size_t n = 0; n < pq->sz; ++n) {
if (pq->data[n].obj == obj) {
return pq->data[n].priority;
}
}
return NAN;
}
uint16_t pqueue_dequeue(pqueue * pq)
{
if (!pq || pq->sz == 0) {
return 0xffff;
}
uint16_t result = pq->data[0].obj;
pq->sz--;
swap(pq->data, 0, pq->sz);
size_t idx = 0;
// Bubble down!
while (idx < pq->sz) {
// Zero children case
if (pq->sz <= left(idx)) {
return result;
}
// One-child case
if (pq->sz <= right(idx)) {
if (pq->type == MIN
? pq->data[idx].priority >
pq->data[left(idx)].priority
: pq->data[idx].priority <
pq->data[left(idx)].priority) {
swap(pq->data, idx, left(idx));
}
return result;
}
double our_priority = pq->data[idx].priority;
double left_priority = pq->data[left(idx)].priority;
double right_priority = pq->data[right(idx)].priority;
if (pq->type == MIN
? (our_priority < left_priority &&
our_priority < right_priority)
: (our_priority > left_priority &&
our_priority > right_priority)) {
return result;
}
if (pq->type == MIN
? left_priority < right_priority
: left_priority > right_priority) {
swap(pq->data, idx, left(idx));
idx = left(idx);
} else {
swap(pq->data, idx, right(idx));
idx = right(idx);
}
}
return result;
}
// used for debugging purposes
void pqueue_print(pqueue * pq)
{
if (!pq) {
return;
}
for (size_t n = 0; n < pq->cap; n++) {
printf("id: %u connections: %lf\n", pq->data[n].obj,
pq->data[n].priority);
}
}
void pqueue_destroy(pqueue * pq)
{
if (!pq) {
return;
}
free(pq->data);
free(pq);
}