Graphviz 14.1.3~dev.20260204.1019
Loading...
Searching...
No Matches
fPQ.c
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/* Priority Queue Code for shortest path in graph */
12
13#include "config.h"
14#include <assert.h>
15#include <util/alloc.h>
16
17#include <ortho/fPQ.h>
18
19struct pq {
21 int cnt;
23 int size;
24};
25
26pq_t *PQgen(int sz) {
27 pq_t *const pq = gv_alloc(sizeof(*pq));
28 pq->pq = gv_calloc(sz + 1, sizeof(snode *));
29 pq->pq[0] = &pq->guard;
30 pq->size = sz;
31 pq->cnt = 0;
32 return pq;
33}
34
35void PQfree(pq_t *pq) {
36 if (pq != NULL) {
37 free(pq->pq);
38 }
39 free(pq);
40}
41
42void PQinit(pq_t *pq) { pq->cnt = 0; }
43
44static void PQcheck(const pq_t *pq) {
45 for (int i = 1; i <= pq->cnt; i++) {
46 assert(N_IDX(pq->pq[i]) == i);
47 }
48}
49
50static void PQupheap(pq_t *pq, int k) {
51 snode *x = pq->pq[k];
52 int v = x->n_val;
53 int next = k / 2;
54 snode *n;
55
56 while (N_VAL(n = pq->pq[next]) < v) {
57 pq->pq[k] = n;
58 N_IDX(n) = k;
59 k = next;
60 next /= 2;
61 }
62 pq->pq[k] = x;
63 N_IDX(x) = k;
64}
65
66int PQ_insert(pq_t *pq, snode *np) {
67 if (pq->cnt == pq->size) {
68 agerrorf("Heap overflow\n");
69 return 1;
70 }
71 pq->cnt++;
72 pq->pq[pq->cnt] = np;
73 PQupheap(pq, pq->cnt);
74 PQcheck(pq);
75 return 0;
76}
77
78static void PQdownheap(pq_t *pq, int k) {
79 snode *x = pq->pq[k];
80 int v = N_VAL(x);
81 int lim = pq->cnt / 2;
82
83 while (k <= lim) {
84 int j = k + k;
85 snode *n = pq->pq[j];
86 if (j < pq->cnt) {
87 if (N_VAL(n) < N_VAL(pq->pq[j + 1])) {
88 j++;
89 n = pq->pq[j];
90 }
91 }
92 if (v >= N_VAL(n))
93 break;
94 pq->pq[k] = n;
95 N_IDX(n) = k;
96 k = j;
97 }
98 pq->pq[k] = x;
99 N_IDX(x) = k;
100}
101
103 if (pq->cnt) {
104 snode *const n = pq->pq[1];
105 pq->pq[1] = pq->pq[pq->cnt];
106 pq->cnt--;
107 if (pq->cnt)
108 PQdownheap(pq, 1);
109 PQcheck(pq);
110 return n;
111 }
112 return 0;
113}
114
115void PQupdate(pq_t *pq, snode *n, int d) {
116 N_VAL(n) = d;
117 PQupheap(pq, n->n_idx);
118 PQcheck(pq);
119}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
static void * gv_alloc(size_t size)
Definition alloc.h:47
static void PQupheap(pq_t *pq, int k)
Definition fPQ.c:50
void PQinit(pq_t *pq)
Definition fPQ.c:42
static void PQcheck(const pq_t *pq)
Definition fPQ.c:44
snode * PQremove(pq_t *pq)
Definition fPQ.c:102
int PQ_insert(pq_t *pq, snode *np)
Definition fPQ.c:66
pq_t * PQgen(int sz)
Definition fPQ.c:26
void PQupdate(pq_t *pq, snode *n, int d)
Definition fPQ.c:115
void PQfree(pq_t *pq)
Definition fPQ.c:35
static void PQdownheap(pq_t *pq, int k)
Definition fPQ.c:78
void free(void *)
node NULL
Definition grammar.y:181
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:198
void agerrorf(const char *fmt,...)
Definition agerror.c:167
#define N_IDX(pq, n)
#define N_VAL(pq, n)
snode priority queue for shortPath in sgraph
Definition heap.c:20
int cnt
Definition fPQ.c:21
snode guard
Definition fPQ.c:22
snode ** pq
Definition fPQ.c:20
int size
Definition fPQ.c:23
a node of search graph sgraph, is created as a border segment between two adjusted cells of type cell...
Definition sgraph.h:26
int n_idx
Definition sgraph.h:27
int n_val
Definition sgraph.h:27