-
Notifications
You must be signed in to change notification settings - Fork 64
/
gk_mkpqueue.h
440 lines (429 loc) · 11.5 KB
/
gk_mkpqueue.h
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*!
\file gk_mkpqueue.h
\brief Templates for priority queues
\date Started 4/09/07
\author George
\version\verbatim $Id: gk_mkpqueue.h 21742 2018-01-26 16:59:15Z karypis $ \endverbatim
*/
#ifndef _GK_MKPQUEUE_H
#define _GK_MKPQUEUE_H
#define GK_MKPQUEUE(FPRFX, PQT, KVT, KT, VT, KVMALLOC, KMAX, KEY_LT)\
/*************************************************************************/\
/*! This function creates and initializes a priority queue */\
/**************************************************************************/\
PQT *FPRFX ## Create(size_t maxnodes)\
{\
PQT *queue; \
\
queue = (PQT *)gk_malloc(sizeof(PQT), "gk_pqCreate: queue");\
FPRFX ## Init(queue, maxnodes);\
\
return queue;\
}\
\
\
/*************************************************************************/\
/*! This function initializes the data structures of the priority queue */\
/**************************************************************************/\
void FPRFX ## Init(PQT *queue, size_t maxnodes)\
{\
queue->nnodes = 0;\
queue->maxnodes = maxnodes;\
\
queue->heap = KVMALLOC(maxnodes, "gk_PQInit: heap");\
queue->locator = gk_idxsmalloc(maxnodes, -1, "gk_PQInit: locator");\
}\
\
\
/*************************************************************************/\
/*! This function resets the priority queue */\
/**************************************************************************/\
void FPRFX ## Reset(PQT *queue)\
{\
ssize_t i;\
ssize_t *locator=queue->locator;\
KVT *heap=queue->heap;\
\
for (i=queue->nnodes-1; i>=0; i--)\
locator[heap[i].val] = -1;\
queue->nnodes = 0;\
}\
\
\
/*************************************************************************/\
/*! This function frees the internal datastructures of the priority queue */\
/**************************************************************************/\
void FPRFX ## Free(PQT *queue)\
{\
if (queue == NULL) return;\
gk_free((void **)&queue->heap, &queue->locator, LTERM);\
queue->maxnodes = 0;\
}\
\
\
/*************************************************************************/\
/*! This function frees the internal datastructures of the priority queue \
and the queue itself */\
/**************************************************************************/\
void FPRFX ## Destroy(PQT *queue)\
{\
if (queue == NULL) return;\
FPRFX ## Free(queue);\
gk_free((void **)&queue, LTERM);\
}\
\
\
/*************************************************************************/\
/*! This function returns the length of the queue */\
/**************************************************************************/\
size_t FPRFX ## Length(PQT *queue)\
{\
return queue->nnodes;\
}\
\
\
/*************************************************************************/\
/*! This function adds an item in the priority queue */\
/**************************************************************************/\
int FPRFX ## Insert(PQT *queue, VT node, KT key)\
{\
ssize_t i, j;\
ssize_t *locator=queue->locator;\
KVT *heap=queue->heap;\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
ASSERT(locator[node] == -1);\
\
i = queue->nnodes++;\
while (i > 0) {\
j = (i-1)>>1;\
if (KEY_LT(key, heap[j].key)) {\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else\
break;\
}\
ASSERT(i >= 0);\
heap[i].key = key;\
heap[i].val = node;\
locator[node] = i;\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
return 0;\
}\
\
\
/*************************************************************************/\
/*! This function deletes an item from the priority queue */\
/**************************************************************************/\
int FPRFX ## Delete(PQT *queue, VT node)\
{\
ssize_t i, j;\
size_t nnodes;\
KT newkey, oldkey;\
ssize_t *locator=queue->locator;\
KVT *heap=queue->heap;\
\
ASSERT(locator[node] != -1);\
ASSERT(heap[locator[node]].val == node);\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
i = locator[node];\
locator[node] = -1;\
\
if (--queue->nnodes > 0 && heap[queue->nnodes].val != node) {\
node = heap[queue->nnodes].val;\
newkey = heap[queue->nnodes].key;\
oldkey = heap[i].key;\
\
if (KEY_LT(newkey, oldkey)) { /* Filter-up */\
while (i > 0) {\
j = (i-1)>>1;\
if (KEY_LT(newkey, heap[j].key)) {\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else\
break;\
}\
}\
else { /* Filter down */\
nnodes = queue->nnodes;\
while ((j=(i<<1)+1) < nnodes) {\
if (KEY_LT(heap[j].key, newkey)) {\
if (j+1 < nnodes && KEY_LT(heap[j+1].key, heap[j].key))\
j++;\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else if (j+1 < nnodes && KEY_LT(heap[j+1].key, newkey)) {\
j++;\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else\
break;\
}\
}\
\
heap[i].key = newkey;\
heap[i].val = node;\
locator[node] = i;\
}\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
return 0;\
}\
\
\
/*************************************************************************/\
/*! This function updates the key values associated for a particular item */ \
/**************************************************************************/\
void FPRFX ## Update(PQT *queue, VT node, KT newkey)\
{\
ssize_t i, j;\
size_t nnodes;\
KT oldkey;\
ssize_t *locator=queue->locator;\
KVT *heap=queue->heap;\
\
oldkey = heap[locator[node]].key;\
if (!KEY_LT(newkey, oldkey) && !KEY_LT(oldkey, newkey)) return;\
\
ASSERT(locator[node] != -1);\
ASSERT(heap[locator[node]].val == node);\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
i = locator[node];\
\
if (KEY_LT(newkey, oldkey)) { /* Filter-up */\
while (i > 0) {\
j = (i-1)>>1;\
if (KEY_LT(newkey, heap[j].key)) {\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else\
break;\
}\
}\
else { /* Filter down */\
nnodes = queue->nnodes;\
while ((j=(i<<1)+1) < nnodes) {\
if (KEY_LT(heap[j].key, newkey)) {\
if (j+1 < nnodes && KEY_LT(heap[j+1].key, heap[j].key))\
j++;\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else if (j+1 < nnodes && KEY_LT(heap[j+1].key, newkey)) {\
j++;\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else\
break;\
}\
}\
\
heap[i].key = newkey;\
heap[i].val = node;\
locator[node] = i;\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
return;\
}\
\
\
/*************************************************************************/\
/*! This function returns the item at the top of the queue and removes\
it from the priority queue */\
/**************************************************************************/\
VT FPRFX ## GetTop(PQT *queue)\
{\
ssize_t i, j;\
ssize_t *locator;\
KVT *heap;\
VT vtx, node;\
KT key;\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
\
if (queue->nnodes == 0)\
return -1;\
\
queue->nnodes--;\
\
heap = queue->heap;\
locator = queue->locator;\
\
vtx = heap[0].val;\
locator[vtx] = -1;\
\
if ((i = queue->nnodes) > 0) {\
key = heap[i].key;\
node = heap[i].val;\
i = 0;\
while ((j=2*i+1) < queue->nnodes) {\
if (KEY_LT(heap[j].key, key)) {\
if (j+1 < queue->nnodes && KEY_LT(heap[j+1].key, heap[j].key))\
j = j+1;\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else if (j+1 < queue->nnodes && KEY_LT(heap[j+1].key, key)) {\
j = j+1;\
heap[i] = heap[j];\
locator[heap[i].val] = i;\
i = j;\
}\
else\
break;\
}\
\
heap[i].key = key;\
heap[i].val = node;\
locator[node] = i;\
}\
\
ASSERT2(FPRFX ## CheckHeap(queue));\
return vtx;\
}\
\
\
/*************************************************************************/\
/*! This function returns the item at the top of the queue. The item is not\
deleted from the queue. */\
/**************************************************************************/\
VT FPRFX ## SeeTopVal(PQT *queue)\
{\
return (queue->nnodes == 0 ? -1 : queue->heap[0].val);\
}\
\
\
/*************************************************************************/\
/*! This function returns the key of the top item. The item is not\
deleted from the queue. */\
/**************************************************************************/\
KT FPRFX ## SeeTopKey(PQT *queue)\
{\
return (queue->nnodes == 0 ? KMAX : queue->heap[0].key);\
}\
\
\
/*************************************************************************/\
/*! This function returns the key of a specific item */\
/**************************************************************************/\
KT FPRFX ## SeeKey(PQT *queue, VT node)\
{\
ssize_t *locator;\
KVT *heap;\
\
heap = queue->heap;\
locator = queue->locator;\
\
return heap[locator[node]].key;\
}\
\
\
/*************************************************************************/\
/*! This function returns the first item in a breadth-first traversal of\
the heap whose key is less than maxwgt. This function is here due to\
hMETIS and is not general!*/\
/**************************************************************************/\
/*\
VT FPRFX ## SeeConstraintTop(PQT *queue, KT maxwgt, KT *wgts)\
{\
ssize_t i;\
\
if (queue->nnodes == 0)\
return -1;\
\
if (maxwgt <= 1000)\
return FPRFX ## SeeTopVal(queue);\
\
for (i=0; i<queue->nnodes; i++) {\
if (queue->heap[i].key > 0) {\
if (wgts[queue->heap[i].val] <= maxwgt)\
return queue->heap[i].val;\
}\
else {\
if (queue->heap[i/2].key <= 0)\
break;\
}\
}\
\
return queue->heap[0].val;\
\
}\
*/\
\
\
/*************************************************************************/\
/*! This functions checks the consistency of the heap */\
/**************************************************************************/\
int FPRFX ## CheckHeap(PQT *queue)\
{\
ssize_t i, j;\
size_t nnodes;\
ssize_t *locator;\
KVT *heap;\
\
heap = queue->heap;\
locator = queue->locator;\
nnodes = queue->nnodes;\
\
if (nnodes == 0)\
return 1;\
\
ASSERT(locator[heap[0].val] == 0);\
for (i=1; i<nnodes; i++) {\
ASSERT(locator[heap[i].val] == i);\
ASSERT(!KEY_LT(heap[i].key, heap[(i-1)/2].key));\
}\
for (i=1; i<nnodes; i++)\
ASSERT(!KEY_LT(heap[i].key, heap[0].key));\
\
for (j=i=0; i<queue->maxnodes; i++) {\
if (locator[i] != -1)\
j++;\
}\
ASSERTP(j == nnodes, ("%jd %jd\n", (intmax_t)j, (intmax_t)nnodes));\
\
return 1;\
}\
#define GK_MKPQUEUE_PROTO(FPRFX, PQT, KT, VT)\
PQT * FPRFX ## Create(size_t maxnodes);\
void FPRFX ## Init(PQT *queue, size_t maxnodes);\
void FPRFX ## Reset(PQT *queue);\
void FPRFX ## Free(PQT *queue);\
void FPRFX ## Destroy(PQT *queue);\
size_t FPRFX ## Length(PQT *queue);\
int FPRFX ## Insert(PQT *queue, VT node, KT key);\
int FPRFX ## Delete(PQT *queue, VT node);\
void FPRFX ## Update(PQT *queue, VT node, KT newkey);\
VT FPRFX ## GetTop(PQT *queue);\
VT FPRFX ## SeeTopVal(PQT *queue);\
KT FPRFX ## SeeTopKey(PQT *queue);\
KT FPRFX ## SeeKey(PQT *queue, VT node);\
VT FPRFX ## SeeConstraintTop(PQT *queue, KT maxwgt, KT *wgts);\
int FPRFX ## CheckHeap(PQT *queue);\
/* This is how these macros are used
GK_MKPQUEUE(gk_dkvPQ, gk_dkvPQ_t, double, gk_idx_t, gk_dkvmalloc, DBL_MAX)
GK_MKPQUEUE_PROTO(gk_dkvPQ, gk_dkvPQ_t, double, gk_idx_t)
*/
#endif