| 1 |
/*! |
| 2 |
\file gk_mkpqueue.h |
| 3 |
\brief Templates for priority queues |
| 4 |
|
| 5 |
\date Started 4/09/07 |
| 6 |
\author George |
| 7 |
\version\verbatim $Id: gk_mkpqueue.h 21742 2018-01-26 16:59:15Z karypis $ \endverbatim |
| 8 |
*/ |
| 9 |
|
| 10 |
|
| 11 |
#ifndef _GK_MKPQUEUE_H |
| 12 |
#define _GK_MKPQUEUE_H |
| 13 |
|
| 14 |
|
| 15 |
#define GK_MKPQUEUE(FPRFX, PQT, KVT, KT, VT, KVMALLOC, KMAX, KEY_LT)\ |
| 16 |
/*************************************************************************/\ |
| 17 |
/*! This function creates and initializes a priority queue */\ |
| 18 |
/**************************************************************************/\ |
| 19 |
PQT *FPRFX ## Create(size_t maxnodes)\ |
| 20 |
{\ |
| 21 |
PQT *queue; \ |
| 22 |
\ |
| 23 |
queue = (PQT *)malloc(sizeof(PQT));\ |
| 24 |
FPRFX ## Init(queue, maxnodes);\ |
| 25 |
\ |
| 26 |
return queue;\ |
| 27 |
}\ |
| 28 |
\ |
| 29 |
\ |
| 30 |
/*************************************************************************/\ |
| 31 |
/*! This function initializes the data structures of the priority queue */\ |
| 32 |
/**************************************************************************/\ |
| 33 |
void FPRFX ## Init(PQT *queue, size_t maxnodes)\ |
| 34 |
{\ |
| 35 |
queue->nnodes = 0;\ |
| 36 |
queue->maxnodes = maxnodes;\ |
| 37 |
\ |
| 38 |
queue->heap = KVMALLOC(maxnodes);\ |
| 39 |
queue->locator = (gk_idx_t*)malloc(maxnodes*sizeof(gk_idx_t));\ |
| 40 |
for (size_t i = 0; i < maxnodes; ++i)\ |
| 41 |
queue->locator[i] = -1;\ |
| 42 |
}\ |
| 43 |
\ |
| 44 |
\ |
| 45 |
/*************************************************************************/\ |
| 46 |
/*! This function resets the priority queue */\ |
| 47 |
/**************************************************************************/\ |
| 48 |
void FPRFX ## Reset(PQT *queue)\ |
| 49 |
{\ |
| 50 |
ssize_t i;\ |
| 51 |
ssize_t *locator=queue->locator;\ |
| 52 |
KVT *heap=queue->heap;\ |
| 53 |
\ |
| 54 |
for (i=queue->nnodes-1; i>=0; i--)\ |
| 55 |
locator[heap[i].val] = -1;\ |
| 56 |
queue->nnodes = 0;\ |
| 57 |
}\ |
| 58 |
\ |
| 59 |
\ |
| 60 |
/*************************************************************************/\ |
| 61 |
/*! This function frees the internal datastructures of the priority queue */\ |
| 62 |
/**************************************************************************/\ |
| 63 |
void FPRFX ## Free(PQT *queue)\ |
| 64 |
{\ |
| 65 |
if (queue == NULL) return;\ |
| 66 |
gk_free((void **)&queue->heap);\ |
| 67 |
gk_free((void **)&queue->locator);\ |
| 68 |
queue->maxnodes = 0;\ |
| 69 |
}\ |
| 70 |
\ |
| 71 |
\ |
| 72 |
/*************************************************************************/\ |
| 73 |
/*! This function frees the internal datastructures of the priority queue \ |
| 74 |
and the queue itself */\ |
| 75 |
/**************************************************************************/\ |
| 76 |
void FPRFX ## Destroy(PQT *queue)\ |
| 77 |
{\ |
| 78 |
if (queue == NULL) return;\ |
| 79 |
FPRFX ## Free(queue);\ |
| 80 |
gk_free((void **)&queue);\ |
| 81 |
}\ |
| 82 |
\ |
| 83 |
\ |
| 84 |
/*************************************************************************/\ |
| 85 |
/*! This function returns the length of the queue */\ |
| 86 |
/**************************************************************************/\ |
| 87 |
size_t FPRFX ## Length(PQT *queue)\ |
| 88 |
{\ |
| 89 |
return queue->nnodes;\ |
| 90 |
}\ |
| 91 |
\ |
| 92 |
\ |
| 93 |
/*************************************************************************/\ |
| 94 |
/*! This function adds an item in the priority queue */\ |
| 95 |
/**************************************************************************/\ |
| 96 |
int FPRFX ## Insert(PQT *queue, VT node, KT key)\ |
| 97 |
{\ |
| 98 |
ssize_t i, j;\ |
| 99 |
ssize_t *locator=queue->locator;\ |
| 100 |
KVT *heap=queue->heap;\ |
| 101 |
\ |
| 102 |
i = queue->nnodes++;\ |
| 103 |
while (i > 0) {\ |
| 104 |
j = (i-1)>>1;\ |
| 105 |
if (KEY_LT(key, heap[j].key)) {\ |
| 106 |
heap[i] = heap[j];\ |
| 107 |
locator[heap[i].val] = i;\ |
| 108 |
i = j;\ |
| 109 |
}\ |
| 110 |
else\ |
| 111 |
break;\ |
| 112 |
}\ |
| 113 |
heap[i].key = key;\ |
| 114 |
heap[i].val = node;\ |
| 115 |
locator[node] = i;\ |
| 116 |
\ |
| 117 |
return 0;\ |
| 118 |
}\ |
| 119 |
\ |
| 120 |
\ |
| 121 |
/*************************************************************************/\ |
| 122 |
/*! This function deletes an item from the priority queue */\ |
| 123 |
/**************************************************************************/\ |
| 124 |
int FPRFX ## Delete(PQT *queue, VT node)\ |
| 125 |
{\ |
| 126 |
ssize_t i, j;\ |
| 127 |
size_t nnodes;\ |
| 128 |
KT newkey, oldkey;\ |
| 129 |
ssize_t *locator=queue->locator;\ |
| 130 |
KVT *heap=queue->heap;\ |
| 131 |
\ |
| 132 |
i = locator[node];\ |
| 133 |
locator[node] = -1;\ |
| 134 |
\ |
| 135 |
if (--queue->nnodes > 0 && heap[queue->nnodes].val != node) {\ |
| 136 |
node = heap[queue->nnodes].val;\ |
| 137 |
newkey = heap[queue->nnodes].key;\ |
| 138 |
oldkey = heap[i].key;\ |
| 139 |
\ |
| 140 |
if (KEY_LT(newkey, oldkey)) { /* Filter-up */\ |
| 141 |
while (i > 0) {\ |
| 142 |
j = (i-1)>>1;\ |
| 143 |
if (KEY_LT(newkey, heap[j].key)) {\ |
| 144 |
heap[i] = heap[j];\ |
| 145 |
locator[heap[i].val] = i;\ |
| 146 |
i = j;\ |
| 147 |
}\ |
| 148 |
else\ |
| 149 |
break;\ |
| 150 |
}\ |
| 151 |
}\ |
| 152 |
else { /* Filter down */\ |
| 153 |
nnodes = queue->nnodes;\ |
| 154 |
while ((j=(i<<1)+1) < nnodes) {\ |
| 155 |
if (KEY_LT(heap[j].key, newkey)) {\ |
| 156 |
if (j+1 < nnodes && KEY_LT(heap[j+1].key, heap[j].key))\ |
| 157 |
j++;\ |
| 158 |
heap[i] = heap[j];\ |
| 159 |
locator[heap[i].val] = i;\ |
| 160 |
i = j;\ |
| 161 |
}\ |
| 162 |
else if (j+1 < nnodes && KEY_LT(heap[j+1].key, newkey)) {\ |
| 163 |
j++;\ |
| 164 |
heap[i] = heap[j];\ |
| 165 |
locator[heap[i].val] = i;\ |
| 166 |
i = j;\ |
| 167 |
}\ |
| 168 |
else\ |
| 169 |
break;\ |
| 170 |
}\ |
| 171 |
}\ |
| 172 |
\ |
| 173 |
heap[i].key = newkey;\ |
| 174 |
heap[i].val = node;\ |
| 175 |
locator[node] = i;\ |
| 176 |
}\ |
| 177 |
\ |
| 178 |
return 0;\ |
| 179 |
}\ |
| 180 |
\ |
| 181 |
\ |
| 182 |
/*************************************************************************/\ |
| 183 |
/*! This function updates the key values associated for a particular item */ \ |
| 184 |
/**************************************************************************/\ |
| 185 |
void FPRFX ## Update(PQT *queue, VT node, KT newkey)\ |
| 186 |
{\ |
| 187 |
ssize_t i, j;\ |
| 188 |
size_t nnodes;\ |
| 189 |
KT oldkey;\ |
| 190 |
ssize_t *locator=queue->locator;\ |
| 191 |
KVT *heap=queue->heap;\ |
| 192 |
\ |
| 193 |
oldkey = heap[locator[node]].key;\ |
| 194 |
if (!KEY_LT(newkey, oldkey) && !KEY_LT(oldkey, newkey)) return;\ |
| 195 |
\ |
| 196 |
i = locator[node];\ |
| 197 |
\ |
| 198 |
if (KEY_LT(newkey, oldkey)) { /* Filter-up */\ |
| 199 |
while (i > 0) {\ |
| 200 |
j = (i-1)>>1;\ |
| 201 |
if (KEY_LT(newkey, heap[j].key)) {\ |
| 202 |
heap[i] = heap[j];\ |
| 203 |
locator[heap[i].val] = i;\ |
| 204 |
i = j;\ |
| 205 |
}\ |
| 206 |
else\ |
| 207 |
break;\ |
| 208 |
}\ |
| 209 |
}\ |
| 210 |
else { /* Filter down */\ |
| 211 |
nnodes = queue->nnodes;\ |
| 212 |
while ((j=(i<<1)+1) < nnodes) {\ |
| 213 |
if (KEY_LT(heap[j].key, newkey)) {\ |
| 214 |
if (j+1 < nnodes && KEY_LT(heap[j+1].key, heap[j].key))\ |
| 215 |
j++;\ |
| 216 |
heap[i] = heap[j];\ |
| 217 |
locator[heap[i].val] = i;\ |
| 218 |
i = j;\ |
| 219 |
}\ |
| 220 |
else if (j+1 < nnodes && KEY_LT(heap[j+1].key, newkey)) {\ |
| 221 |
j++;\ |
| 222 |
heap[i] = heap[j];\ |
| 223 |
locator[heap[i].val] = i;\ |
| 224 |
i = j;\ |
| 225 |
}\ |
| 226 |
else\ |
| 227 |
break;\ |
| 228 |
}\ |
| 229 |
}\ |
| 230 |
\ |
| 231 |
heap[i].key = newkey;\ |
| 232 |
heap[i].val = node;\ |
| 233 |
locator[node] = i;\ |
| 234 |
\ |
| 235 |
return;\ |
| 236 |
}\ |
| 237 |
\ |
| 238 |
\ |
| 239 |
/*************************************************************************/\ |
| 240 |
/*! This function returns the item at the top of the queue and removes\ |
| 241 |
it from the priority queue */\ |
| 242 |
/**************************************************************************/\ |
| 243 |
VT FPRFX ## GetTop(PQT *queue)\ |
| 244 |
{\ |
| 245 |
ssize_t i, j;\ |
| 246 |
ssize_t *locator;\ |
| 247 |
KVT *heap;\ |
| 248 |
VT vtx, node;\ |
| 249 |
KT key;\ |
| 250 |
\ |
| 251 |
if (queue->nnodes == 0)\ |
| 252 |
return -1;\ |
| 253 |
\ |
| 254 |
queue->nnodes--;\ |
| 255 |
\ |
| 256 |
heap = queue->heap;\ |
| 257 |
locator = queue->locator;\ |
| 258 |
\ |
| 259 |
vtx = heap[0].val;\ |
| 260 |
locator[vtx] = -1;\ |
| 261 |
\ |
| 262 |
if ((i = queue->nnodes) > 0) {\ |
| 263 |
key = heap[i].key;\ |
| 264 |
node = heap[i].val;\ |
| 265 |
i = 0;\ |
| 266 |
while ((j=2*i+1) < queue->nnodes) {\ |
| 267 |
if (KEY_LT(heap[j].key, key)) {\ |
| 268 |
if (j+1 < queue->nnodes && KEY_LT(heap[j+1].key, heap[j].key))\ |
| 269 |
j = j+1;\ |
| 270 |
heap[i] = heap[j];\ |
| 271 |
locator[heap[i].val] = i;\ |
| 272 |
i = j;\ |
| 273 |
}\ |
| 274 |
else if (j+1 < queue->nnodes && KEY_LT(heap[j+1].key, key)) {\ |
| 275 |
j = j+1;\ |
| 276 |
heap[i] = heap[j];\ |
| 277 |
locator[heap[i].val] = i;\ |
| 278 |
i = j;\ |
| 279 |
}\ |
| 280 |
else\ |
| 281 |
break;\ |
| 282 |
}\ |
| 283 |
\ |
| 284 |
heap[i].key = key;\ |
| 285 |
heap[i].val = node;\ |
| 286 |
locator[node] = i;\ |
| 287 |
}\ |
| 288 |
\ |
| 289 |
return vtx;\ |
| 290 |
}\ |
| 291 |
\ |
| 292 |
\ |
| 293 |
/*************************************************************************/\ |
| 294 |
/*! This function returns the item at the top of the queue. The item is not\ |
| 295 |
deleted from the queue. */\ |
| 296 |
/**************************************************************************/\ |
| 297 |
VT FPRFX ## SeeTopVal(PQT *queue)\ |
| 298 |
{\ |
| 299 |
return (queue->nnodes == 0 ? -1 : queue->heap[0].val);\ |
| 300 |
}\ |
| 301 |
\ |
| 302 |
\ |
| 303 |
/*************************************************************************/\ |
| 304 |
/*! This function returns the key of the top item. The item is not\ |
| 305 |
deleted from the queue. */\ |
| 306 |
/**************************************************************************/\ |
| 307 |
KT FPRFX ## SeeTopKey(PQT *queue)\ |
| 308 |
{\ |
| 309 |
return (queue->nnodes == 0 ? KMAX : queue->heap[0].key);\ |
| 310 |
}\ |
| 311 |
|
| 312 |
|
| 313 |
#define GK_MKPQUEUE_PROTO(FPRFX, PQT, KT, VT)\ |
| 314 |
PQT * FPRFX ## Create(size_t maxnodes);\ |
| 315 |
void FPRFX ## Init(PQT *queue, size_t maxnodes);\ |
| 316 |
void FPRFX ## Reset(PQT *queue);\ |
| 317 |
void FPRFX ## Free(PQT *queue);\ |
| 318 |
void FPRFX ## Destroy(PQT *queue);\ |
| 319 |
size_t FPRFX ## Length(PQT *queue);\ |
| 320 |
int FPRFX ## Insert(PQT *queue, VT node, KT key);\ |
| 321 |
int FPRFX ## Delete(PQT *queue, VT node);\ |
| 322 |
void FPRFX ## Update(PQT *queue, VT node, KT newkey);\ |
| 323 |
VT FPRFX ## GetTop(PQT *queue);\ |
| 324 |
VT FPRFX ## SeeTopVal(PQT *queue);\ |
| 325 |
KT FPRFX ## SeeTopKey(PQT *queue);\ |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
#endif |