Debian Codegraph @

lib/dotgen/fastgr.c

raw
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 "config.h"
12
13 #include <dotgen/dot.h>
14 #include <stdbool.h>
15 #include <stddef.h>
16 #include <util/alloc.h>
17 #include <util/unused.h>
18
19 /*
20 * operations on the fast internal graph.
21 */
22
23 static edge_t *ffe(node_t * u, elist uL, node_t * v, elist vL)
24 {
25 int i;
26 edge_t *e;
27
28 if (uL.size > 0 && vL.size > 0) {
29 if (uL.size < vL.size) {
30 for (i = 0; (e = uL.list[i]); i++)
31 if (aghead(e) == v)
32 break;
33 } else {
34 for (i = 0; (e = vL.list[i]); i++)
35 if (agtail(e) == u)
36 break;
37 }
38 } else
39 e = 0;
40 return e;
41 }
42
43 edge_t *find_fast_edge(node_t * u, node_t * v)
44 {
45 return ffe(u, ND_out(u), v, ND_in(v));
46 }
47
48 static UNUSED node_t *find_fast_node(graph_t *g, node_t *n) {
49 node_t *v;
50 for (v = GD_nlist(g); v; v = ND_next(v))
51 if (v == n)
52 break;
53 return v;
54 }
55
56 edge_t *find_flat_edge(node_t * u, node_t * v)
57 {
58 return ffe(u, ND_flat_out(u), v, ND_flat_in(v));
59 }
60
61 /// append e to list L only if e not already a member
62 static void
63 safe_list_append(edge_t * e, elist * L)
64 {
65 for (size_t i = 0; i < L->size; i++)
66 if (e == L->list[i])
67 return;
68 elist_append(e, (*L));
69 }
70
71 edge_t *fast_edge(edge_t * e)
72 {
73 #ifdef DEBUG
74 edge_t *f;
75 for (int i = 0; (f = ND_out(agtail(e)).list[i]); i++) {
76 if (e == f) {
77 fprintf(stderr, "duplicate fast edge\n");
78 return 0;
79 }
80 assert(aghead(e) != aghead(f));
81 }
82 for (int i = 0; (f = ND_in(aghead(e)).list[i]); i++) {
83 if (e == f) {
84 fprintf(stderr, "duplicate fast edge\n");
85 return 0;
86 }
87 assert(agtail(e) != agtail(f));
88 }
89 #endif
90 elist_append(e, ND_out(agtail(e)));
91 elist_append(e, ND_in(aghead(e)));
92 return e;
93 }
94
95 /// remove e from list and fill hole with last member of list
96 void zapinlist(elist * L, edge_t * e)
97 {
98 for (size_t i = 0; i < L->size; i++) {
99 if (L->list[i] == e) {
100 L->size--;
101 L->list[i] = L->list[L->size];
102 L->list[L->size] = NULL;
103 break;
104 }
105 }
106 }
107
108 /* disconnects e from graph */
109 void delete_fast_edge(edge_t * e)
110 {
111 assert(e != NULL);
112 zapinlist(&ND_out(agtail(e)), e);
113 zapinlist(&ND_in(aghead(e)), e);
114 }
115
116 void other_edge(edge_t * e)
117 {
118 elist_append(e, ND_other(agtail(e)));
119 }
120
121 void safe_other_edge(edge_t * e)
122 {
123 safe_list_append(e, &ND_other(agtail(e)));
124 }
125
126 /* Create and return a new virtual edge e attached to orig.
127 * ED_to_orig(e) = orig
128 * ED_to_virt(orig) = e if e is the first virtual edge attached.
129 * orig might be an input edge, reverse of an input edge, or virtual edge
130 */
131 edge_t *new_virtual_edge(node_t * u, node_t * v, edge_t * orig)
132 {
133 Agedgepair_t* e2 = gv_alloc(sizeof(Agedgepair_t));
134 AGTYPE(&e2->in) = AGINEDGE;
135 AGTYPE(&e2->out) = AGOUTEDGE;
136 e2->out.base.data = gv_alloc(sizeof(Agedgeinfo_t));
137 edge_t *e = &e2->out;
138 agtail(e) = u;
139 aghead(e) = v;
140 ED_edge_type(e) = VIRTUAL;
141
142 if (orig) {
143 AGSEQ(e) = AGSEQ(orig);
144 AGSEQ(&(e2->in)) = AGSEQ(orig);
145 ED_count(e) = ED_count(orig);
146 ED_xpenalty(e) = ED_xpenalty(orig);
147 ED_weight(e) = ED_weight(orig);
148 ED_minlen(e) = ED_minlen(orig);
149 if (agtail(e) == agtail(orig))
150 ED_tail_port(e) = ED_tail_port(orig);
151 else if (agtail(e) == aghead(orig))
152 ED_tail_port(e) = ED_head_port(orig);
153 if (aghead(e) == aghead(orig))
154 ED_head_port(e) = ED_head_port(orig);
155 else if (aghead(e) == agtail(orig))
156 ED_head_port(e) = ED_tail_port(orig);
157
158 if (ED_to_virt(orig) == NULL)
159 ED_to_virt(orig) = e;
160 ED_to_orig(e) = orig;
161 } else {
162 ED_weight(e) = 1;
163 ED_xpenalty(e) = 1;
164 ED_count(e) = 1;
165 ED_minlen(e) = 1;
166 }
167 return e;
168 }
169
170 edge_t *virtual_edge(node_t * u, node_t * v, edge_t * orig)
171 {
172 return fast_edge(new_virtual_edge(u, v, orig));
173 }
174
175 void fast_node(graph_t * g, Agnode_t * n)
176 {
177
178 #ifdef DEBUG
179 assert(find_fast_node(g, n) == NULL);
180 #endif
181 ND_next(n) = GD_nlist(g);
182 if (ND_next(n))
183 ND_prev(ND_next(n)) = n;
184 GD_nlist(g) = n;
185 ND_prev(n) = NULL;
186 assert(n != ND_next(n));
187 }
188
189 void delete_fast_node(graph_t * g, node_t * n)
190 {
191 assert(find_fast_node(g, n));
192 if (ND_next(n))
193 ND_prev(ND_next(n)) = ND_prev(n);
194 if (ND_prev(n))
195 ND_next(ND_prev(n)) = ND_next(n);
196 else
197 GD_nlist(g) = ND_next(n);
198 }
199
200 node_t *virtual_node(graph_t *g) {
201 node_t *n = gv_alloc(sizeof(node_t));
202 AGTYPE(n) = AGNODE;
203 n->base.data = gv_alloc(sizeof(Agnodeinfo_t));
204 n->root = agroot(g);
205 ND_node_type(n) = VIRTUAL;
206 ND_lw(n) = ND_rw(n) = 1;
207 ND_ht(n) = 1;
208 ND_UF_size(n) = 1;
209 alloc_elist(4, ND_in(n));
210 alloc_elist(4, ND_out(n));
211 fast_node(g, n);
212 return n;
213 }
214
215 void flat_edge(graph_t * g, edge_t * e)
216 {
217 elist_append(e, ND_flat_out(agtail(e)));
218 elist_append(e, ND_flat_in(aghead(e)));
219 GD_has_flat_edges(dot_root(g)) = GD_has_flat_edges(g) = true;
220 }
221
222 void delete_flat_edge(edge_t * e)
223 {
224 assert(e != NULL);
225 if (ED_to_orig(e) && ED_to_virt(ED_to_orig(e)) == e)
226 ED_to_virt(ED_to_orig(e)) = NULL;
227 zapinlist(&ND_flat_out(agtail(e)), e);
228 zapinlist(&ND_flat_in(aghead(e)), e);
229 }
230
231 #ifdef DEBUG
232 static char *NAME(node_t * n)
233 {
234 static char buf[20];
235 if (ND_node_type(n) == NORMAL)
236 return agnameof(n);
237 snprintf(buf, sizeof(buf), "V%p", n);
238 return buf;
239 }
240
241 void fastgr(graph_t * g)
242 {
243 int i, j;
244 node_t *n, *w;
245 edge_t *e, *f;
246
247 for (n = GD_nlist(g); n; n = ND_next(n)) {
248 fprintf(stderr, "%s %d: (", NAME(n), ND_rank(n));
249 for (i = 0; (e = ND_out(n).list[i]); i++) {
250 fprintf(stderr, " %s:%d", NAME(aghead(e)), ED_count(e));
251 w = aghead(e);
252 if (g == agroot(g)) {
253 for (j = 0; (f = ND_in(w).list[j]); j++)
254 if (e == f)
255 break;
256 assert(f != NULL);
257 }
258 }
259 fprintf(stderr, " ) (");
260 for (i = 0; (e = ND_in(n).list[i]); i++) {
261 fprintf(stderr, " %s:%d", NAME(agtail(e)), ED_count(e));
262 w = agtail(e);
263 if (g == agroot(g)) {
264 for (j = 0; (f = ND_out(w).list[j]); j++)
265 if (e == f)
266 break;
267 assert(f != NULL);
268 }
269 }
270 fprintf(stderr, " )\n");
271 }
272 }
273 #endif
274
275 static void
276 basic_merge(edge_t * e, edge_t * rep)
277 {
278 if (ED_minlen(rep) < ED_minlen(e))
279 ED_minlen(rep) = ED_minlen(e);
280 while (rep) {
281 ED_count(rep) += ED_count(e);
282 ED_xpenalty(rep) += ED_xpenalty(e);
283 ED_weight(rep) += ED_weight(e);
284 rep = ED_to_virt(rep);
285 }
286 }
287
288 void
289 merge_oneway(edge_t * e, edge_t * rep)
290 {
291 if (rep == ED_to_virt(e) || e == ED_to_virt(rep)) {
292 agwarningf("merge_oneway glitch\n");
293 return;
294 }
295 assert(ED_to_virt(e) == NULL);
296 ED_to_virt(e) = rep;
297 basic_merge(e, rep);
298 }