Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
fPQ.h
Go to the documentation of this file.
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
28typedef struct {
29 PQTYPE* pq;
30 int PQcnt;
31 int PQsize;
32} PQ;
33#endif
34
35#ifdef PQ_CODE
36static void
37PQgen(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
45static void
46PQfree(PQ* pq, int freeAll)
47{
48 free (pq->pq);
49 if (freeAll) free (pq);
50}
51
52static void
53PQinit(PQ* pq)
54{
55 pq->PQcnt = 0;
56}
57
58#ifdef PQCHECK
59static int
60PQcheck (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
73static void
74PQupheap(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
92static int
93PQinsert(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
108static void
109PQdownheap (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
136static PQTYPE
137PQremove (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
154static void
155PQupdate (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
166static void
167PQprint (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
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
static snode guard
Definition fPQ.c:21
void PQinit(void)
Definition fPQ.c:44
void PQdownheap(int k)
Definition fPQ.c:94
void PQcheck(void)
Definition fPQ.c:50
void PQgen(int sz)
Definition fPQ.c:25
void PQfree(void)
Definition fPQ.c:36
snode * PQremove(void)
Definition fPQ.c:121
static int PQsize
Definition fPQ.c:22
void PQupheap(int k)
Definition fPQ.c:62
void PQprint(void)
Definition fPQ.c:145
void PQupdate(snode *n, int d)
Definition fPQ.c:137
static int PQcnt
Definition fPQ.c:20
void free(void *)
void agerrorf(const char *fmt,...)
Definition agerror.c:165
void PQinsert(pq_t *pq, Halfedge *he, Site *v, double offset)
Definition heap.c:54
#define PQVTYPE
#define N_IDX(pq, n)
#define N_VAL(pq, n)
#define PQTYPE
Definition heap.c:19