| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <util/alloc.h> |
| 13 |
|
| 14 |
/* Priority queue: |
| 15 |
* To work, the following have to be defined before this file is |
| 16 |
* included: |
| 17 |
* - PQTYPE : type of object stored in the queue |
| 18 |
* - PQVTYPE : type of priority value |
| 19 |
* - N_VAL(pq,n) : macro for (negative) priority value of object n in pq |
| 20 |
* - N_IDX(pq,n) : macro for integer index > 0 of n in pq |
| 21 |
* - guard, type PQTYPE, with N_VAL(guard) == 0 |
| 22 |
* |
| 23 |
* Priorities are stored as negative numbers, with the item with the least |
| 24 |
* negative priority at the head (just after the guard). |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifdef PQ_TYPES |
| 28 |
typedef struct { |
| 29 |
PQTYPE* pq; |
| 30 |
int PQcnt; |
| 31 |
int PQsize; |
| 32 |
} PQ; |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifdef PQ_CODE |
| 36 |
static void |
| 37 |
PQgen(PQ* pq, int sz, PQTYPE guard) |
| 38 |
{ |
| 39 |
pq->pq = gv_calloc(sz + 1, sizeof(PQTYPE)); |
| 40 |
pq->pq[0] = guard; |
| 41 |
pq->PQsize = sz; |
| 42 |
pq->PQcnt = 0; |
| 43 |
} |
| 44 |
|
| 45 |
static void |
| 46 |
PQfree(PQ* pq, int freeAll) |
| 47 |
{ |
| 48 |
free (pq->pq); |
| 49 |
if (freeAll) free (pq); |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
PQinit(PQ* pq) |
| 54 |
{ |
| 55 |
pq->PQcnt = 0; |
| 56 |
} |
| 57 |
|
| 58 |
#ifdef PQCHECK |
| 59 |
static int |
| 60 |
PQcheck (PQ* pq) |
| 61 |
{ |
| 62 |
int i; |
| 63 |
|
| 64 |
for (i = 1; i <= pq->PQcnt; i++) { |
| 65 |
if (N_IDX(pq,pq->pq[i]) != i) { |
| 66 |
return 1; |
| 67 |
} |
| 68 |
} |
| 69 |
return 0; |
| 70 |
} |
| 71 |
#endif |
| 72 |
|
| 73 |
static void |
| 74 |
PQupheap(PQ* ppq, int k) |
| 75 |
{ |
| 76 |
PQTYPE* pq = ppq->pq; |
| 77 |
PQTYPE x = pq[k]; |
| 78 |
PQVTYPE v = N_VAL(ppq,x); |
| 79 |
int next = k/2; |
| 80 |
PQTYPE n; |
| 81 |
|
| 82 |
while (N_VAL(ppq,n = pq[next]) < v) { |
| 83 |
pq[k] = n; |
| 84 |
N_IDX(ppq,n) = k; |
| 85 |
k = next; |
| 86 |
next /= 2; |
| 87 |
} |
| 88 |
pq[k] = x; |
| 89 |
N_IDX(ppq,x) = k; |
| 90 |
} |
| 91 |
|
| 92 |
static int |
| 93 |
PQinsert(PQ* pq, PQTYPE np) |
| 94 |
{ |
| 95 |
if (pq->PQcnt == pq->PQsize) { |
| 96 |
agerrorf("Heap overflow\n"); |
| 97 |
return (1); |
| 98 |
} |
| 99 |
pq->PQcnt++; |
| 100 |
pq->pq[pq->PQcnt] = np; |
| 101 |
PQupheap (pq, pq->PQcnt); |
| 102 |
#ifdef PQCHECK |
| 103 |
PQcheck(pq); |
| 104 |
#endif |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
PQdownheap (PQ* ppq, int k) |
| 110 |
{ |
| 111 |
PQTYPE* pq = ppq->pq; |
| 112 |
PQTYPE x = pq[k]; |
| 113 |
PQVTYPE v = N_VAL(ppq,x); |
| 114 |
int lim = ppq->PQcnt/2; |
| 115 |
PQTYPE n; |
| 116 |
int j; |
| 117 |
|
| 118 |
while (k <= lim) { |
| 119 |
j = k+k; |
| 120 |
n = pq[j]; |
| 121 |
if (j < ppq->PQcnt) { |
| 122 |
if (N_VAL(ppq,n) < N_VAL(ppq,pq[j+1])) { |
| 123 |
j++; |
| 124 |
n = pq[j]; |
| 125 |
} |
| 126 |
} |
| 127 |
if (v >= N_VAL(ppq,n)) break; |
| 128 |
pq[k] = n; |
| 129 |
N_IDX(ppq,n) = k; |
| 130 |
k = j; |
| 131 |
} |
| 132 |
pq[k] = x; |
| 133 |
N_IDX(ppq,x) = k; |
| 134 |
} |
| 135 |
|
| 136 |
static PQTYPE |
| 137 |
PQremove (PQ* pq) |
| 138 |
{ |
| 139 |
PQTYPE n; |
| 140 |
|
| 141 |
if (pq->PQcnt) { |
| 142 |
n = pq->pq[1]; |
| 143 |
pq->pq[1] = pq->pq[pq->PQcnt]; |
| 144 |
pq->PQcnt--; |
| 145 |
if (pq->PQcnt) PQdownheap (pq, 1); |
| 146 |
#ifdef PQCHECK |
| 147 |
PQcheck(pq); |
| 148 |
#endif |
| 149 |
return n; |
| 150 |
} |
| 151 |
else return pq->pq[0]; |
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
PQupdate (PQ* pq, PQTYPE n, PQVTYPE d) |
| 156 |
{ |
| 157 |
N_VAL(pq,n) = d; |
| 158 |
PQupheap (pq, N_IDX(pq,n)); |
| 159 |
#ifdef PQCHECK |
| 160 |
PQcheck(pq); |
| 161 |
#endif |
| 162 |
} |
| 163 |
|
| 164 |
#if defined(DEBUG) && DEBUG > 1 |
| 165 |
|
| 166 |
static void |
| 167 |
PQprint (PQ* pq) |
| 168 |
{ |
| 169 |
int i; |
| 170 |
PQTYPE n; |
| 171 |
|
| 172 |
fprintf (stderr, "Q: "); |
| 173 |
for (i = 1; i <= pq->PQcnt; i++) { |
| 174 |
n = pq->pq[i]; |
| 175 |
fprintf (stderr, "(%d:%f) ", N_IDX(pq,n), N_VAL(pq,n)); |
| 176 |
} |
| 177 |
fprintf (stderr, "\n"); |
| 178 |
} |
| 179 |
#endif |
| 180 |
#endif |
| 181 |
|