Graphviz 13.0.0~dev.20250121.0651
Loading...
Searching...
No Matches
edge.c
Go to the documentation of this file.
1
6/*************************************************************************
7 * Copyright (c) 2011 AT&T Intellectual Property
8 * All rights reserved. This program and the accompanying materials
9 * are made available under the terms of the Eclipse Public License v1.0
10 * which accompanies this distribution, and is available at
11 * https://www.eclipse.org/legal/epl-v10.html
12 *
13 * Contributors: Details at https://graphviz.org
14 *************************************************************************/
15
16#include <assert.h>
17#include <cgraph/cghdr.h>
18#include <cgraph/node_set.h>
19#include <stdbool.h>
20#include <stdlib.h>
21#include <util/alloc.h>
22
23/* return first outedge of <n> */
25{
26 Agsubnode_t *sn;
27 Agedge_t *e = NULL;
28
29 sn = agsubrep(g, n);
30 if (sn) {
31 dtrestore(g->e_seq, sn->out_seq);
32 e = dtfirst(g->e_seq);
33 sn->out_seq = dtextract(g->e_seq);
34 }
35 return e;
36}
37
38/* return outedge that follows <e> of <n> */
40{
41 Agnode_t *n;
42 Agsubnode_t *sn;
43 Agedge_t *f = NULL;
44
45 n = AGTAIL(e);
46 sn = agsubrep(g, n);
47 if (sn) {
48 dtrestore(g->e_seq, sn->out_seq);
49 f = dtnext(g->e_seq, e);
50 sn->out_seq = dtextract(g->e_seq);
51 }
52 return f;
53}
54
56{
57 Agsubnode_t *sn;
58 Agedge_t *e = NULL;
59
60 sn = agsubrep(g, n);
61 if (sn) {
62 dtrestore(g->e_seq, sn->in_seq);
63 e = dtfirst(g->e_seq);
64 sn->in_seq = dtextract(g->e_seq);
65 }
66 return e;
67}
68
70{
71 Agnode_t *n;
72 Agsubnode_t *sn;
73 Agedge_t *f = NULL;
74
75 n = AGHEAD(e);
76 sn = agsubrep(g, n);
77 if (sn) {
78 dtrestore(g->e_seq, sn->in_seq);
79 f = dtnext(g->e_seq, e);
80 sn->in_seq = dtextract(g->e_seq);
81 }
82 return f;
83}
84
86{
87 Agedge_t *rv;
88 rv = agfstout(g, n);
89 if (rv == NULL)
90 rv = agfstin(g, n);
91 return rv;
92}
93
95{
96 Agedge_t *rv;
97
98 if (AGTYPE(e) == AGOUTEDGE) {
99 rv = agnxtout(g, e);
100 if (rv == NULL) {
101 do {
102 rv = !rv ? agfstin(g, n) : agnxtin(g,rv);
103 } while (rv && rv->node == n);
104 }
105 } else {
106 do {
107 rv = agnxtin(g, e); /* so that we only see each edge once, */
108 e = rv;
109 } while (rv && rv->node == n); /* ignore loops as in-edges */
110 }
111 return rv;
112}
113
114/* internal edge set lookup */
116 Agtag_t key)
117{
118 Agedge_t *e, template;
119 Agsubnode_t *sn;
120
121 if (t == NULL || h == NULL)
122 return NULL;
123 template.base.tag = key;
124 template.node = t; /* guess that fan-in < fan-out */
125 sn = agsubrep(g, h);
126 if (!sn) e = 0;
127 else {
128 dtrestore(g->e_id, sn->in_id);
129 e = dtsearch(g->e_id, &template);
130 sn->in_id = dtextract(g->e_id);
131 }
132 return e;
133}
134
136 IDTYPE id)
137{
138 Agtag_t tag = {0};
139
140 tag.objtype = AGEDGE;
141 tag.id = id;
142 return agfindedge_by_key(g, t, h, tag);
143}
144
146{
147 return g == n->root ? &n->mainsub : node_set_find(g->n_id, n->base.tag.id);
148}
149
150static void ins(Dict_t * d, Dtlink_t ** set, Agedge_t * e)
151{
152 dtrestore(d, *set);
153 dtinsert(d, e);
154 *set = dtextract(d);
155}
156
157static void del(Dict_t * d, Dtlink_t ** set, Agedge_t * e)
158{
159 void *x;
160 (void)x;
161 dtrestore(d, *set);
162 x = dtdelete(d, e);
163 assert(x);
164 *set = dtextract(d);
165}
166
167static void installedge(Agraph_t * g, Agedge_t * e)
168{
169 Agnode_t *t, *h;
170 Agedge_t *out, *in;
171 Agsubnode_t *sn;
172
173 out = AGMKOUT(e);
174 in = AGMKIN(e);
175 t = agtail(e);
176 h = aghead(e);
177 while (g) {
178 if (agfindedge_by_key(g, t, h, AGTAG(e))) break;
179 sn = agsubrep(g, t);
180 ins(g->e_seq, &sn->out_seq, out);
181 ins(g->e_id, &sn->out_id, out);
182 sn = agsubrep(g, h);
183 ins(g->e_seq, &sn->in_seq, in);
184 ins(g->e_id, &sn->in_id, in);
185 g = agparent(g);
186 }
187}
188
189static void subedge(Agraph_t * g, Agedge_t * e)
190{
191 installedge(g, e);
192 /* might an init method call be needed here? */
193}
194
196 IDTYPE id)
197{
198 Agedge_t *in, *out;
199
200 (void)agsubnode(g, t, 1);
201 (void)agsubnode(g, h, 1);
202 Agedgepair_t *e2 = gv_alloc(sizeof(Agedgepair_t));
203 in = &e2->in;
204 out = &e2->out;
205 uint64_t seq = agnextseq(g, AGEDGE);
206 assert((seq & SEQ_MASK) == seq && "sequence ID overflow");
207 AGTYPE(in) = AGINEDGE;
209 AGID(in) = AGID(out) = id;
210 AGSEQ(in) = AGSEQ(out) = seq & SEQ_MASK;
211 in->node = t;
212 out->node = h;
213
214 installedge(g, out);
215 if (g->desc.has_attrs) {
216 (void)agbindrec(out, AgDataRecName, sizeof(Agattr_t), false);
218 }
219 agmethod_init(g, out);
220 return out;
221}
222
223/* edge creation predicate */
224static bool ok_to_make_edge(Agraph_t *g, Agnode_t *t, Agnode_t *h) {
225 Agtag_t key = {0};
226
227 /* protect against self, multi-edges in strict graphs */
228 if (agisstrict(g)) {
229 key.objtype = 0; /* wild card */
230 if (agfindedge_by_key(g, t, h, key))
231 return false;
232 }
233 if (g->desc.no_loop && (t == h)) /* simple graphs */
234 return false;
235 return true;
236}
237
239 IDTYPE id, int cflag)
240{
241 Agraph_t *root;
242 Agedge_t *e;
243
244 e = agfindedge_by_id(g, t, h, id);
245 if (e == NULL && agisundirected(g))
246 e = agfindedge_by_id(g, h, t, id);
247 if (e == NULL && cflag && ok_to_make_edge(g, t, h)) {
248 root = agroot(g);
249 if (g != root && (e = agfindedge_by_id(root, t, h, id))) {
250 subedge(g, e); /* old */
251 }
252 }
253 return e;
254}
255
256Agedge_t *agedge(Agraph_t * g, Agnode_t * t, Agnode_t * h, char *name,
257 int cflag)
258{
259 Agedge_t *e;
260 IDTYPE my_id;
261
262 int have_id = agmapnametoid(g, AGEDGE, name, &my_id, false);
263 if (have_id || (name == NULL && (!cflag || agisstrict(g)))) {
264 /* probe for pre-existing edge */
265 Agtag_t key = {0};
266 if (have_id) {
267 key.id = my_id;
268 key.objtype = AGEDGE;
269 } else {
270 key.id = key.objtype = 0;
271 }
272
273 /* might already exist locally */
274 e = agfindedge_by_key(g, t, h, key);
275 if (e == NULL && agisundirected(g))
276 e = agfindedge_by_key(g, h, t, key);
277 if (e)
278 return e;
279 if (cflag) {
280 e = agfindedge_by_key(agroot(g), t, h, key);
281 if (e == NULL && agisundirected(g))
282 e = agfindedge_by_key(agroot(g), h, t, key);
283 if (e) {
284 subedge(g,e);
285 return e;
286 }
287 }
288 }
289
290 if (cflag && ok_to_make_edge(g, t, h)
291 && agmapnametoid(g, AGEDGE, name, &my_id, true)) { /* reserve id */
292 e = newedge(g, t, h, my_id);
293 agregister(g, AGEDGE, e); /* register new object in external namespace */
294 }
295 else
296 e = NULL;
297 return e;
298}
299
300void agdeledgeimage(Agraph_t * g, Agedge_t * e, void *ignored)
301{
302 Agedge_t *in, *out;
303 Agnode_t *t, *h;
304 Agsubnode_t *sn;
305
306 (void)ignored;
307 if (AGTYPE(e) == AGINEDGE) {
308 in = e;
309 out = AGIN2OUT(e);
310 } else {
311 out = e;
312 in = AGOUT2IN(e);
313 }
314 t = in->node;
315 h = out->node;
316 sn = agsubrep(g, t);
317 del(g->e_seq, &sn->out_seq, out);
318 del(g->e_id, &sn->out_id, out);
319 sn = agsubrep(g, h);
320 del(g->e_seq, &sn->in_seq, in);
321 del(g->e_id, &sn->in_id, in);
322#ifdef DEBUG
323 for (e = agfstin(g,h); e; e = agnxtin(g,e))
324 assert(e != in);
325 for (e = agfstout(g,t); e; e = agnxtout(g,e))
326 assert(e != out);
327#endif
328}
329
331{
332 e = AGMKOUT(e);
333 if (agfindedge_by_key(g, agtail(e), aghead(e), AGTAG(e)) == NULL)
334 return FAILURE;
335
336 if (g == agroot(g)) {
337 if (g->desc.has_attrs)
339 agmethod_delete(g, e);
340 agrecclose((Agobj_t *) e);
341 agfreeid(g, AGEDGE, AGID(e));
342 }
343 if (agapply(g, (Agobj_t *)e, (agobjfn_t)agdeledgeimage, NULL, false) == SUCCESS) {
344 if (g == agroot(g))
345 free(e);
346 return SUCCESS;
347 } else
348 return FAILURE;
349}
350
351Agedge_t *agsubedge(Agraph_t * g, Agedge_t * e, int cflag)
352{
353 Agnode_t *t;
354 Agedge_t *rv;
355
356 rv = NULL;
357 t = agsubnode(g, AGTAIL(e), cflag);
358 if (t != NULL || cflag) {
359 Agnode_t *const h = agsubnode(g, AGHEAD(e), cflag);
360 if (t != NULL && h != NULL) {
361 rv = agfindedge_by_key(g, t, h, AGTAG(e));
362 if (cflag && rv == NULL) {
363 installedge(g, e);
364 rv = e;
365 }
366 if (rv && AGTYPE(rv) != AGTYPE(e))
367 rv = AGOPP(rv);
368 }
369 }
370 return rv;
371}
372
373/* edge comparison. AGTYPE(e) == 0 means ID is a wildcard. */
374static int agedgeidcmpf(void *arg_e0, void *arg_e1) {
375 Agedge_t *e0 = arg_e0;
376 Agedge_t *e1 = arg_e1;
377
378 if (AGID(e0->node) < AGID(e1->node)) return -1;
379 if (AGID(e0->node) > AGID(e1->node)) return 1;
380 /* same node */
381 if (AGTYPE(e0) != 0 && AGTYPE(e1) != 0) {
382 if (AGID(e0) < AGID(e1)) return -1;
383 if (AGID(e0) > AGID(e1)) return 1;
384 }
385 return 0;
386}
387
388/* edge comparison. for ordered traversal. */
389static int agedgeseqcmpf(void *arg_e0, void *arg_e1) {
390 Agedge_t *e0 = arg_e0;
391 Agedge_t *e1 = arg_e1;
392 assert(arg_e0 && arg_e1);
393
394 if (e0->node != e1->node) {
395 if (AGSEQ(e0->node) < AGSEQ(e1->node)) return -1;
396 if (AGSEQ(e0->node) > AGSEQ(e1->node)) return 1;
397 }
398 else {
399 if (AGSEQ(e0) < AGSEQ(e1)) return -1;
400 if (AGSEQ(e0) > AGSEQ(e1)) return 1;
401 }
402 return 0;
403}
404
405/* indexing for ordered traversal */
407 .link = offsetof(Agedge_t, seq_link), // use internal links
408 .comparf = agedgeseqcmpf,
409};
410
412 .link = -1, // use external holder objects
413 .comparf = agedgeseqcmpf,
414};
415
416/* indexing for random search */
418 .link = offsetof(Agedge_t, id_link), // use internal links
419 .comparf = agedgeidcmpf,
420};
421
423 .link = -1, // use external holder objects
424 .comparf = agedgeidcmpf,
425};
426
427/* expose macros as functions for ease of debugging
428and to expose them to foreign languages without C preprocessor. */
429#ifdef ageqedge
430#undef ageqedge
431#endif
432CGRAPH_API int ageqedge(Agedge_t *e, Agedge_t *f);
433CGRAPH_API int ageqedge(Agedge_t * e, Agedge_t * f)
434{
435 return AGEQEDGE(e, f);
436}
437
438#ifdef agmkout
439#undef agmkout
440#endif
441CGRAPH_API Agedge_t *agmkout(Agedge_t *e);
442CGRAPH_API Agedge_t *agmkout(Agedge_t * e)
443{
444 return AGMKOUT(e);
445}
446
447#ifdef agmkin
448#undef agmkin
449#endif
450CGRAPH_API Agedge_t *agmkin(Agedge_t *e);
451CGRAPH_API Agedge_t *agmkin(Agedge_t * e)
452{
453 return AGMKIN(e);
454}
455
456#ifdef agtail
457#undef agtail
458#endif
459CGRAPH_API Agnode_t *agtail(Agedge_t *e);
460CGRAPH_API Agnode_t *agtail(Agedge_t * e)
461{
462 return AGTAIL(e);
463}
464
465#ifdef aghead
466#undef aghead
467#endif
468CGRAPH_API Agnode_t *aghead(Agedge_t *e);
469CGRAPH_API Agnode_t *aghead(Agedge_t * e)
470{
471 return AGHEAD(e);
472}
473
474#ifdef agopp
475#undef agopp
476#endif
477CGRAPH_API Agedge_t *agopp(Agedge_t *e);
478CGRAPH_API Agedge_t *agopp(Agedge_t * e)
479{
480 return AGOPP(e);
481}
static void out(agerrlevel_t level, const char *fmt, va_list args)
Report messages using a user-supplied or default write function.
Definition agerror.c:84
Memory allocation wrappers that exit on failure.
static void * gv_alloc(size_t size)
Definition alloc.h:47
int agapply(Agraph_t *g, Agobj_t *obj, agobjfn_t fn, void *arg, int preorder)
Definition apply.c:60
char * AgDataRecName
Definition attr.c:173
CDT_API Dtlink_t * dtextract(Dt_t *)
Definition dtextract.c:9
#define dtsearch(d, o)
Definition cdt.h:183
#define dtinsert(d, o)
Definition cdt.h:185
#define dtdelete(d, o)
Definition cdt.h:186
CDT_API int dtrestore(Dt_t *, Dtlink_t *)
Definition dtrestore.c:11
#define dtnext(d, o)
Definition cdt.h:180
#define dtfirst(d)
Definition cdt.h:179
cgraph.h additions
void agfreeid(Agraph_t *g, int objtype, IDTYPE id)
Definition id.c:131
void agrecclose(Agobj_t *obj)
Definition rec.c:227
int agmapnametoid(Agraph_t *g, int objtype, char *str, IDTYPE *result, bool createflag)
Definition id.c:102
uint64_t agnextseq(Agraph_t *g, int objtype)
Definition graph.c:160
#define FAILURE
Definition cghdr.h:45
void agregister(Agraph_t *g, int objtype, void *obj)
Definition id.c:170
#define SUCCESS
Definition cghdr.h:44
@ SEQ_MASK
Definition cghdr.h:74
Agsubnode_t * node_set_find(node_set_t *self, IDTYPE key)
Definition node.c:484
static bool ok_to_make_edge(Agraph_t *g, Agnode_t *t, Agnode_t *h)
Definition edge.c:224
static int agedgeseqcmpf(void *arg_e0, void *arg_e1)
Definition edge.c:389
static Agedge_t * agfindedge_by_id(Agraph_t *g, Agnode_t *t, Agnode_t *h, IDTYPE id)
Definition edge.c:135
static Agedge_t * newedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, IDTYPE id)
Definition edge.c:195
void agdeledgeimage(Agraph_t *g, Agedge_t *e, void *ignored)
Definition edge.c:300
Agedge_t * agmkout(Agedge_t *e)
Definition edge.c:442
Agedge_t * agmkin(Agedge_t *e)
Definition edge.c:451
static void subedge(Agraph_t *g, Agedge_t *e)
Definition edge.c:189
Dtdisc_t Ag_subedge_seq_disc
Definition edge.c:411
static Agedge_t * agfindedge_by_key(Agraph_t *g, Agnode_t *t, Agnode_t *h, Agtag_t key)
Definition edge.c:115
static int agedgeidcmpf(void *arg_e0, void *arg_e1)
Definition edge.c:374
static void ins(Dict_t *d, Dtlink_t **set, Agedge_t *e)
Definition edge.c:150
Dtdisc_t Ag_mainedge_seq_disc
Definition edge.c:406
static void del(Dict_t *d, Dtlink_t **set, Agedge_t *e)
Definition edge.c:157
Dtdisc_t Ag_subedge_id_disc
Definition edge.c:422
Dtdisc_t Ag_mainedge_id_disc
Definition edge.c:417
static void installedge(Agraph_t *g, Agedge_t *e)
Definition edge.c:167
void free(void *)
node NULL
Definition grammar.y:163
void agedgeattr_init(Agraph_t *g, Agedge_t *e)
Definition attr.c:446
void agedgeattr_delete(Agedge_t *e)
Definition attr.c:455
void agmethod_delete(Agraph_t *g, void *obj)
Definition obj.c:138
void agmethod_init(Agraph_t *g, void *obj)
Definition obj.c:78
Agedge_t * agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int cflag)
Definition edge.c:256
Agedge_t * agidedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, IDTYPE id, int cflag)
Definition edge.c:238
#define agopp(e)
opposite edge: flip Agedgepair_s.out ⇄ Agedgepair_s.in/*#end#*‍/
Definition cgraph.h:890
#define AGMKOUT(e)
Definition cgraph.h:882
Agedge_t * agsubedge(Agraph_t *g, Agedge_t *e, int cflag)
Definition edge.c:351
#define AGIN2OUT(inedge)
Agedgepair_s.in -> Agedgepair_s.out.
Definition cgraph.h:877
int agdeledge(Agraph_t *g, Agedge_t *e)
Definition edge.c:330
Agedge_t * agnxtin(Agraph_t *g, Agedge_t *e)
Definition edge.c:69
#define AGEQEDGE(e, f)
Definition cgraph.h:886
Agedge_t * agfstout(Agraph_t *g, Agnode_t *n)
Definition edge.c:24
#define ageqedge(e, f)
edges are equal
Definition cgraph.h:893
#define AGMKIN(e)
Definition cgraph.h:883
#define agtail(e)
Definition cgraph.h:888
Agedge_t * agnxtedge(Agraph_t *g, Agedge_t *e, Agnode_t *n)
Definition edge.c:94
#define aghead(e)
Definition cgraph.h:889
Agedge_t * agnxtout(Agraph_t *g, Agedge_t *e)
Definition edge.c:39
Agedge_t * agfstedge(Agraph_t *g, Agnode_t *n)
Definition edge.c:85
#define AGTAIL(e)
Definition cgraph.h:884
Agedge_t * agfstin(Agraph_t *g, Agnode_t *n)
Definition edge.c:55
#define AGOPP(e)
Definition cgraph.h:881
#define AGHEAD(e)
Definition cgraph.h:885
#define AGOUT2IN(outedge)
Agedgepair_s.out -> Agedgepair_s.in/*#end#*‍/.
Definition cgraph.h:878
void(* agobjfn_t)(Agraph_t *g, Agobj_t *obj, void *arg)
Definition cgraph.h:368
int agisstrict(Agraph_t *g)
Definition graph.c:196
int agisundirected(Agraph_t *g)
Definition graph.c:191
Agnode_t * agsubnode(Agraph_t *g, Agnode_t *n, int createflag)
Definition node.c:254
Agsubnode_t * agsubrep(Agraph_t *g, Agnode_t *n)
Definition edge.c:145
#define AGID(obj)
returns the unique integer ID associated with the object
Definition cgraph.h:221
uint64_t IDTYPE
unique per main graph ID
Definition cgraph.h:73
#define AGTYPE(obj)
returns AGRAPH, AGNODE, or AGEDGE depending on the type of the object
Definition cgraph.h:216
#define AGTAG(obj)
Definition cgraph.h:215
Agraph_t * agroot(void *obj)
Definition obj.c:168
#define AGSEQ(obj)
Definition cgraph.h:225
@ AGOUTEDGE
Definition cgraph.h:207
@ AGEDGE
Definition cgraph.h:207
@ AGINEDGE
Definition cgraph.h:207
void * agbindrec(void *obj, const char *name, unsigned int recsize, int move_to_front)
attaches a new record of the given size to the object
Definition rec.c:89
Agraph_t * agparent(Agraph_t *g)
Definition subg.c:88
static uint64_t id
Definition gv2gml.c:42
unordered set of Agsubnode_t *
string attribute container
Definition cgraph.h:633
unsigned has_attrs
Definition cgraph.h:290
unsigned no_loop
Definition cgraph.h:287
Agnode_t * node
Definition cgraph.h:272
Agedge_t in
Definition cgraph.h:276
Agedge_t out
Definition cgraph.h:276
Agsubnode_t mainsub
Definition cgraph.h:262
Agraph_t * root
Definition cgraph.h:261
Agobj_t base
Definition cgraph.h:260
a generic header of Agraph_s, Agnode_s and Agedge_s
Definition cgraph.h:210
Agtag_t tag
access with AGTAG
Definition cgraph.h:211
graph or subgraph
Definition cgraph.h:424
struct graphviz_node_set * n_id
the node set indexed by ID
Definition cgraph.h:430
Dict_t * e_seq
Definition cgraph.h:431
Agdesc_t desc
Definition cgraph.h:426
Dict_t * e_id
holders for edge sets
Definition cgraph.h:431
This is the node struct allocated per graph (or subgraph).
Definition cgraph.h:251
Dtlink_t * out_seq
Definition cgraph.h:256
Dtlink_t * out_id
Definition cgraph.h:255
Dtlink_t * in_id
Definition cgraph.h:255
Dtlink_t * in_seq
Definition cgraph.h:256
tag in Agobj_s for graphs, nodes, and edges.
Definition cgraph.h:197
unsigned objtype
access with AGTYPE
Definition cgraph.h:199
IDTYPE id
Definition cgraph.h:203
Definition cdt.h:100
int link
Definition cdt.h:87