Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
conc.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
12/*
13 * build edge_t concentrators for parallel edges with a common endpoint
14 */
15
16#include <dotgen/dot.h>
17#include <stdbool.h>
18
19#define UP 0
20#define DOWN 1
21
22static bool samedir(edge_t * e, edge_t * f)
23{
24 edge_t *e0, *f0;
25
26 for (e0 = e; e0 != NULL && ED_edge_type(e0) != NORMAL; e0 = ED_to_orig(e0));
27 if (e0 == NULL)
28 return false;
29 for (f0 = f; f0 != NULL && ED_edge_type(f0) != NORMAL; f0 = ED_to_orig(f0));
30 if (f0 == NULL)
31 return false;
32 if (ED_conc_opp_flag(e0))
33 return false;
34 if (ED_conc_opp_flag(f0))
35 return false;
36 return ((ND_rank(agtail(f0)) - ND_rank(aghead(f0)))
37 * (ND_rank(agtail(e0)) - ND_rank(aghead(e0))) > 0);
38}
39
40static bool downcandidate(node_t * v)
41{
42 return ND_node_type(v) == VIRTUAL && ND_in(v).size == 1
43 && ND_out(v).size == 1 && ND_label(v) == NULL;
44}
45
46static bool bothdowncandidates(node_t * u, node_t * v)
47{
48 edge_t *e, *f;
49 e = ND_in(u).list[0];
50 f = ND_in(v).list[0];
51 if (downcandidate(v) && agtail(e) == agtail(f)) {
52 return samedir(e, f)
53 && portcmp(ED_tail_port(e), ED_tail_port(f)) == 0;
54 }
55 return false;
56}
57
58static bool upcandidate(node_t * v)
59{
60 return ND_node_type(v) == VIRTUAL && ND_out(v).size == 1
61 && ND_in(v).size == 1 && ND_label(v) == NULL;
62}
63
64static bool bothupcandidates(node_t * u, node_t * v)
65{
66 edge_t *e, *f;
67 e = ND_out(u).list[0];
68 f = ND_out(v).list[0];
69 if (upcandidate(v) && aghead(e) == aghead(f)) {
70 return samedir(e, f)
71 && portcmp(ED_head_port(e), ED_head_port(f)) == 0;
72 }
73 return false;
74}
75
76static void mergevirtual(graph_t * g, int r, int lpos, int rpos, int dir)
77{
78 int i, k;
79 node_t *left, *right;
80 edge_t *e, *f, *e0;
81
82 left = GD_rank(g)[r].v[lpos];
83 /* merge all right nodes into the leftmost one */
84 for (i = lpos + 1; i <= rpos; i++) {
85 right = GD_rank(g)[r].v[i];
86 if (dir == DOWN) {
87 while ((e = ND_out(right).list[0])) {
88 for (k = 0; (f = ND_out(left).list[k]); k++)
89 if (aghead(f) == aghead(e))
90 break;
91 if (f == NULL)
92 f = virtual_edge(left, aghead(e), e);
93 while ((e0 = ND_in(right).list[0])) {
94 merge_oneway(e0, f);
95 /*ED_weight(f) += ED_weight(e0); */
97 }
99 }
100 } else {
101 while ((e = ND_in(right).list[0])) {
102 for (k = 0; (f = ND_in(left).list[k]); k++)
103 if (agtail(f) == agtail(e))
104 break;
105 if (f == NULL)
106 f = virtual_edge(agtail(e), left, e);
107 while ((e0 = ND_out(right).list[0])) {
108 merge_oneway(e0, f);
110 }
112 }
113 }
114 assert(ND_in(right).size + ND_out(right).size == 0);
116 }
117 k = lpos + 1;
118 i = rpos + 1;
119 while (i < GD_rank(g)[r].n) {
120 node_t *n;
121 n = GD_rank(g)[r].v[k] = GD_rank(g)[r].v[i];
122 ND_order(n) = k;
123 k++;
124 i++;
125 }
126 GD_rank(g)[r].n = k;
127 GD_rank(g)[r].v[k] = NULL;
128}
129
130static void infuse(graph_t * g, node_t * n)
131{
132 node_t *lead;
133
134 lead = GD_rankleader(g)[ND_rank(n)];
135 if (lead == NULL || ND_order(lead) > ND_order(n))
136 GD_rankleader(g)[ND_rank(n)] = n;
137}
138
139static int rebuild_vlists(graph_t * g)
140{
141 int c, i, r, maxi;
142 node_t *n, *lead;
143 edge_t *rep;
144
145 for (r = GD_minrank(g); r <= GD_maxrank(g); r++)
146 GD_rankleader(g)[r] = NULL;
148 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
149 infuse(g, n);
150 for (edge_t *e = agfstout(g, n); e; e = agnxtout(g, e)) {
151 for (rep = e; ED_to_virt(rep); rep = ED_to_virt(rep));
152 while (rep != NULL && ND_rank(aghead(rep)) < ND_rank(aghead(e))) {
153 infuse(g, aghead(rep));
154 rep = ND_out(aghead(rep)).list[0];
155 }
156 }
157 }
158
159 for (r = GD_minrank(g); r <= GD_maxrank(g); r++) {
160 lead = GD_rankleader(g)[r];
161 if (lead == NULL) {
162 agerrorf("rebuild_vlists: lead is null for rank %d\n", r);
163 return -1;
164 }
165 else if (GD_rank(dot_root(g))[r].v[ND_order(lead)] != lead) {
166 agerrorf("rebuild_vlists: rank lead %s not in order %d of rank %d\n",
167 agnameof(lead), ND_order(lead), r);
168 return -1;
169 }
170 GD_rank(g)[r].v =
171 GD_rank(dot_root(g))[r].v + ND_order((GD_rankleader(g)[r]));
172 maxi = -1;
173 for (i = 0; i < GD_rank(g)[r].n; i++) {
174 if ((n = GD_rank(g)[r].v[i]) == NULL)
175 break;
176 if (ND_node_type(n) == NORMAL) {
177 if (agcontains(g, n))
178 maxi = i;
179 else
180 break;
181 } else {
182 edge_t *e;
183 for (e = ND_in(n).list[0]; e && ED_to_orig(e);
184 e = ED_to_orig(e));
185 if (e && agcontains(g, agtail(e))
186 && agcontains(g, aghead(e)))
187 maxi = i;
188 }
189 }
190 if (maxi == -1)
191 agwarningf("degenerate concentrated rank %s,%d\n", agnameof(g),
192 r);
193 GD_rank(g)[r].n = maxi + 1;
194 }
195
196 for (c = 1; c <= GD_n_cluster(g); c++) {
197 int ret = rebuild_vlists(GD_clust(g)[c]);
198 if (ret != 0) {
199 return ret;
200 }
201 }
202 return 0;
203}
204
206{
207 int c, r, leftpos, rightpos;
208 node_t *left, *right;
209
210 if (GD_maxrank(g) - GD_minrank(g) <= 1)
211 return;
212 /* this is the downward looking pass. r is a candidate rank. */
213 for (r = 1; GD_rank(g)[r + 1].n; r++) {
214 for (leftpos = 0; leftpos < GD_rank(g)[r].n; leftpos++) {
215 left = GD_rank(g)[r].v[leftpos];
216 if (!downcandidate(left))
217 continue;
218 for (rightpos = leftpos + 1; rightpos < GD_rank(g)[r].n;
219 rightpos++) {
220 right = GD_rank(g)[r].v[rightpos];
222 break;
223 }
224 if (rightpos - leftpos > 1)
225 mergevirtual(g, r, leftpos, rightpos - 1, DOWN);
226 }
227 }
228 /* this is the corresponding upward pass */
229 while (r > 0) {
230 for (leftpos = 0; leftpos < GD_rank(g)[r].n; leftpos++) {
231 left = GD_rank(g)[r].v[leftpos];
232 if (!upcandidate(left))
233 continue;
234 for (rightpos = leftpos + 1; rightpos < GD_rank(g)[r].n;
235 rightpos++) {
236 right = GD_rank(g)[r].v[rightpos];
238 break;
239 }
240 if (rightpos - leftpos > 1)
241 mergevirtual(g, r, leftpos, rightpos - 1, UP);
242 }
243 r--;
244 }
245 for (c = 1; c <= GD_n_cluster(g); c++) {
246 if (rebuild_vlists(GD_clust(g)[c]) != 0) {
247 agerr(AGPREV, "concentrate=true may not work correctly.\n");
248 return;
249 }
250 }
251}
#define right(i)
Definition closest.c:79
#define UP
Definition conc.c:19
static int rebuild_vlists(graph_t *g)
Definition conc.c:139
#define DOWN
Definition conc.c:20
static bool bothdowncandidates(node_t *u, node_t *v)
Definition conc.c:46
static bool downcandidate(node_t *v)
Definition conc.c:40
static bool upcandidate(node_t *v)
Definition conc.c:58
static bool bothupcandidates(node_t *u, node_t *v)
Definition conc.c:64
static void infuse(graph_t *g, node_t *n)
Definition conc.c:130
void dot_concentrate(graph_t *g)
Definition conc.c:205
static bool samedir(edge_t *e, edge_t *f)
Definition conc.c:22
static void mergevirtual(graph_t *g, int r, int lpos, int rpos, int dir)
Definition conc.c:76
#define NORMAL
Definition const.h:24
#define VIRTUAL
Definition const.h:25
Agraph_t * dot_root(void *p)
Definition dotinit.c:496
void delete_fast_node(Agraph_t *, Agnode_t *)
Definition fastgr.c:191
void dot_scan_ranks(graph_t *g)
Definition rank.c:202
Agedge_t * virtual_edge(Agnode_t *, Agnode_t *, Agedge_t *)
Definition fastgr.c:172
int portcmp(port p0, port p1)
Definition dotsplines.c:130
void delete_fast_edge(Agedge_t *)
Definition fastgr.c:108
void merge_oneway(Agedge_t *, Agedge_t *)
Definition fastgr.c:291
#define left
Definition dthdr.h:12
node NULL
Definition grammar.y:163
#define ED_to_orig(e)
Definition types.h:598
#define ED_conc_opp_flag(e)
Definition types.h:579
Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
Definition edge.c:24
#define agtail(e)
Definition cgraph.h:880
#define ED_edge_type(e)
Definition types.h:582
#define aghead(e)
Definition cgraph.h:881
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition edge.c:39
#define ED_head_port(e)
Definition types.h:588
#define ED_tail_port(e)
Definition types.h:597
#define ED_to_virt(e)
Definition types.h:599
void agwarningf(const char *fmt,...)
Definition agerror.c:173
void agerrorf(const char *fmt,...)
Definition agerror.c:165
int agerr(agerrlevel_t level, const char *fmt,...)
Definition agerror.c:155
@ AGPREV
Definition cgraph.h:849
#define GD_minrank(g)
Definition types.h:384
#define GD_maxrank(g)
Definition types.h:382
#define GD_clust(g)
Definition types.h:360
#define GD_rank(g)
Definition types.h:395
#define GD_n_cluster(g)
Definition types.h:389
#define GD_rankleader(g)
Definition types.h:396
#define ND_rank(n)
Definition types.h:523
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:47
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:40
#define ND_label(n)
Definition types.h:502
#define ND_node_type(n)
Definition types.h:511
#define ND_order(n)
Definition types.h:513
#define ND_in(n)
Definition types.h:501
#define ND_out(n)
Definition types.h:515
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:158
int agcontains(Agraph_t *, void *obj)
returns non-zero if obj is a member of (sub)graph
Definition obj.c:233
graph or subgraph
Definition cgraph.h:425