-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpqueue.c
More file actions
202 lines (179 loc) · 5.09 KB
/
pqueue.c
File metadata and controls
202 lines (179 loc) · 5.09 KB
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
/*******************************************************************************
Copyright 2014 Matthew Thiffault
This file is part of HeatheRTOS.
HeatheRTOS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
HeatheRTOS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with HeatheRTOS. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
#include "config.h"
#include "xint.h"
#include "xdef.h"
#include "pqueue.h"
#include "xbool.h"
#include "xassert.h"
#ifndef PQ_RING
/* Only used for heap implementation */
static inline void pqueue_swap(struct pqueue *q, int i, int j)
{
struct pqueue_entry tmp;
tmp = q->nodes[i].entry;
q->nodes[i].entry = q->nodes[j].entry;
q->nodes[j].entry = tmp;
q->nodes[q->nodes[i].entry.val].pos = i;
q->nodes[q->nodes[j].entry.val].pos = j;
}
#endif
/* Initialize a priority queue */
void
pqueue_init(struct pqueue *q, size_t maxsize, struct pqueue_node *mem)
{
size_t i;
assert(maxsize > 0);
q->nodes = mem;
q->maxsize = maxsize;
q->count = 0;
for (i = 0; i < maxsize; i++)
q->nodes[i].pos = maxsize;
#ifdef PQ_RING
q->start = 0;
#else
/* No PQ_HEAP-specific initialization */
#endif
}
/* Add a value to a priority queue */
int
pqueue_add(struct pqueue* q, size_t val, intptr_t key)
{
size_t i;
#ifdef PQ_RING
size_t start;
#endif
if (val >= q->maxsize)
return -1; /* value too large */
if (q->nodes[val].pos < q->maxsize)
return -2; /* already exists */
/* Should be impossible to more than fill the queue given
* constraints on values stored in the queue. */
assert (q->count < q->maxsize);
#ifdef PQ_RING
start = q->start;
i = (start + q->count) % q->maxsize;
while (i != start) {
size_t prev = i == 0 ? q->maxsize - 1 : i - 1;
if (q->nodes[prev].entry.key <= key)
break;
q->nodes[i].entry = q->nodes[prev].entry;
q->nodes[q->nodes[i].entry.val].pos = i;
i = prev;
}
q->nodes[i].entry.key = key;
q->nodes[i].entry.val = val;
q->nodes[val].pos = i;
q->count++;
return 0;
#else
i = q->count++;
q->nodes[i].entry.key = key;
q->nodes[i].entry.val = val;
q->nodes[val].pos = i;
while (i > 0) {
size_t parent = (i - 1) / 2;
if (q->nodes[parent].entry.key <= key)
break;
pqueue_swap(q, i, parent);
i = parent;
}
return 0;
#endif
}
/* Reduce the key of a value in the priority queue */
int
pqueue_decreasekey(struct pqueue *q, size_t val, intptr_t new_key)
{
#ifdef PQ_RING
size_t start;
#endif
size_t i = q->nodes[val].pos;
if (i == q->maxsize)
return -1; /* value not in queue */
assert(q->nodes[i].entry.val == val);
if (new_key >= q->nodes[i].entry.key)
return -2; /* new_key is not a decrease */
#ifdef PQ_RING
start = q->start;
while (i != start) {
size_t prev = i == 0 ? q->maxsize - 1 : i - 1;
if (q->nodes[prev].entry.key <= new_key)
break;
q->nodes[i].entry = q->nodes[prev].entry;
q->nodes[q->nodes[i].entry.val].pos = i;
i = prev;
}
q->nodes[i].entry.key = new_key;
q->nodes[i].entry.val = val;
q->nodes[val].pos = i;
return 0;
#else
q->nodes[i].entry.key = new_key;
while (i > 0) {
size_t parent = (i - 1) / 2;
if (q->nodes[parent].entry.key <= new_key)
break;
pqueue_swap(q, i, parent);
i = parent;
}
return 0;
#endif
}
/* Peek at the highest priority entry */
struct pqueue_entry*
pqueue_peekmin(struct pqueue *q)
{
#ifdef PQ_RING
return q->count == 0 ? NULL : &q->nodes[q->start].entry;
#else
return q->count == 0 ? NULL : &q->nodes[0].entry;
#endif
}
/* Pop the highest priority entry from the queue */
void
pqueue_popmin(struct pqueue *q)
{
#ifdef PQ_RING
assert(q->count > 0);
q->count--;
q->nodes[q->nodes[q->start].entry.val].pos = q->maxsize;
q->start += 1;
q->start %= q->maxsize;
#else
size_t i;
size_t popped_val;
assert(q->count > 0);
popped_val = q->nodes[0].entry.val;
q->count--;
pqueue_swap(q, 0, q->count);
i = 0;
for (;;) {
size_t l = 2 * i + 1;
size_t r = l + 1;
size_t min;
if (l >= q->count)
break;
min = q->nodes[l].entry.key < q->nodes[i].entry.key ? l : i;
if (r < q->count)
min = q->nodes[r].entry.key < q->nodes[min].entry.key ? r : min;
if (min == i)
break;
pqueue_swap(q, i, min);
i = min;
}
q->nodes[popped_val].pos = q->maxsize;
#endif
}