| 1 |
/** |
| 2 |
* @file |
| 3 |
* @ingroup cgraph_edge |
| 4 |
* @ingroup cgraph_core |
| 5 |
*/ |
| 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 "config.h" |
| 17 |
|
| 18 |
#include <assert.h> |
| 19 |
#include <cgraph/cghdr.h> |
| 20 |
#include <cgraph/node_set.h> |
| 21 |
#include <stdbool.h> |
| 22 |
#include <stddef.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <util/alloc.h> |
| 25 |
#include <util/unused.h> |
| 26 |
|
| 27 |
/* return first outedge of <n> */ |
| 28 |
Agedge_t *agfstout(Agraph_t * g, Agnode_t * n) |
| 29 |
{ |
| 30 |
Agsubnode_t *sn; |
| 31 |
Agedge_t *e = NULL; |
| 32 |
|
| 33 |
sn = agsubrep(g, n); |
| 34 |
if (sn) { |
| 35 |
dtrestore(g->e_seq, sn->out_seq); |
| 36 |
e = dtfirst(g->e_seq); |
| 37 |
sn->out_seq = dtextract(g->e_seq); |
| 38 |
} |
| 39 |
return e; |
| 40 |
} |
| 41 |
|
| 42 |
/* return outedge that follows <e> of <n> */ |
| 43 |
Agedge_t *agnxtout(Agraph_t * g, Agedge_t * e) |
| 44 |
{ |
| 45 |
Agnode_t *n; |
| 46 |
Agsubnode_t *sn; |
| 47 |
Agedge_t *f = NULL; |
| 48 |
|
| 49 |
n = AGTAIL(e); |
| 50 |
sn = agsubrep(g, n); |
| 51 |
if (sn) { |
| 52 |
dtrestore(g->e_seq, sn->out_seq); |
| 53 |
f = dtnext(g->e_seq, e); |
| 54 |
sn->out_seq = dtextract(g->e_seq); |
| 55 |
} |
| 56 |
return f; |
| 57 |
} |
| 58 |
|
| 59 |
Agedge_t *agfstin(Agraph_t * g, Agnode_t * n) |
| 60 |
{ |
| 61 |
Agsubnode_t *sn; |
| 62 |
Agedge_t *e = NULL; |
| 63 |
|
| 64 |
sn = agsubrep(g, n); |
| 65 |
if (sn) { |
| 66 |
dtrestore(g->e_seq, sn->in_seq); |
| 67 |
e = dtfirst(g->e_seq); |
| 68 |
sn->in_seq = dtextract(g->e_seq); |
| 69 |
} |
| 70 |
return e; |
| 71 |
} |
| 72 |
|
| 73 |
Agedge_t *agnxtin(Agraph_t * g, Agedge_t * e) |
| 74 |
{ |
| 75 |
Agnode_t *n; |
| 76 |
Agsubnode_t *sn; |
| 77 |
Agedge_t *f = NULL; |
| 78 |
|
| 79 |
n = AGHEAD(e); |
| 80 |
sn = agsubrep(g, n); |
| 81 |
if (sn) { |
| 82 |
dtrestore(g->e_seq, sn->in_seq); |
| 83 |
f = dtnext(g->e_seq, e); |
| 84 |
sn->in_seq = dtextract(g->e_seq); |
| 85 |
} |
| 86 |
return f; |
| 87 |
} |
| 88 |
|
| 89 |
Agedge_t *agfstedge(Agraph_t * g, Agnode_t * n) |
| 90 |
{ |
| 91 |
Agedge_t *rv; |
| 92 |
rv = agfstout(g, n); |
| 93 |
if (rv == NULL) |
| 94 |
rv = agfstin(g, n); |
| 95 |
return rv; |
| 96 |
} |
| 97 |
|
| 98 |
Agedge_t *agnxtedge(Agraph_t * g, Agedge_t * e, Agnode_t * n) |
| 99 |
{ |
| 100 |
Agedge_t *rv; |
| 101 |
|
| 102 |
if (AGTYPE(e) == AGOUTEDGE) { |
| 103 |
rv = agnxtout(g, e); |
| 104 |
if (rv == NULL) { |
| 105 |
do { |
| 106 |
rv = !rv ? agfstin(g, n) : agnxtin(g,rv); |
| 107 |
} while (rv && rv->node == n); |
| 108 |
} |
| 109 |
} else { |
| 110 |
do { |
| 111 |
rv = agnxtin(g, e); /* so that we only see each edge once, */ |
| 112 |
e = rv; |
| 113 |
} while (rv && rv->node == n); /* ignore loops as in-edges */ |
| 114 |
} |
| 115 |
return rv; |
| 116 |
} |
| 117 |
|
| 118 |
/* internal edge set lookup */ |
| 119 |
static Agedge_t *agfindedge_by_key(Agraph_t * g, Agnode_t * t, Agnode_t * h, |
| 120 |
Agtag_t key) |
| 121 |
{ |
| 122 |
Agedge_t *e, template; |
| 123 |
Agsubnode_t *sn; |
| 124 |
|
| 125 |
if (t == NULL || h == NULL) |
| 126 |
return NULL; |
| 127 |
template.base.tag = key; |
| 128 |
template.node = t; /* guess that fan-in < fan-out */ |
| 129 |
sn = agsubrep(g, h); |
| 130 |
if (!sn) e = 0; |
| 131 |
else { |
| 132 |
dtrestore(g->e_id, sn->in_id); |
| 133 |
e = dtsearch(g->e_id, &template); |
| 134 |
sn->in_id = dtextract(g->e_id); |
| 135 |
} |
| 136 |
return e; |
| 137 |
} |
| 138 |
|
| 139 |
static Agedge_t *agfindedge_by_id(Agraph_t * g, Agnode_t * t, Agnode_t * h, |
| 140 |
IDTYPE id) |
| 141 |
{ |
| 142 |
Agtag_t tag = {.objtype = AGEDGE, .id = id}; |
| 143 |
return agfindedge_by_key(g, t, h, tag); |
| 144 |
} |
| 145 |
|
| 146 |
Agsubnode_t *agsubrep(Agraph_t * g, Agnode_t * n) |
| 147 |
{ |
| 148 |
return g == n->root ? &n->mainsub : node_set_find(g->n_id, n->base.tag.id); |
| 149 |
} |
| 150 |
|
| 151 |
static void ins(Dict_t * d, Dtlink_t ** set, Agedge_t * e) |
| 152 |
{ |
| 153 |
dtrestore(d, *set); |
| 154 |
dtinsert(d, e); |
| 155 |
*set = dtextract(d); |
| 156 |
} |
| 157 |
|
| 158 |
static void del(Dict_t * d, Dtlink_t ** set, Agedge_t * e) |
| 159 |
{ |
| 160 |
dtrestore(d, *set); |
| 161 |
void *x UNUSED = dtdelete(d, e); |
| 162 |
assert(x); |
| 163 |
*set = dtextract(d); |
| 164 |
} |
| 165 |
|
| 166 |
static void installedge(Agraph_t * g, Agedge_t * e) |
| 167 |
{ |
| 168 |
Agnode_t *t, *h; |
| 169 |
Agedge_t *out, *in; |
| 170 |
Agsubnode_t *sn; |
| 171 |
|
| 172 |
out = AGMKOUT(e); |
| 173 |
in = AGMKIN(e); |
| 174 |
t = agtail(e); |
| 175 |
h = aghead(e); |
| 176 |
while (g) { |
| 177 |
if (agfindedge_by_key(g, t, h, AGTAG(e))) break; |
| 178 |
sn = agsubrep(g, t); |
| 179 |
ins(g->e_seq, &sn->out_seq, out); |
| 180 |
ins(g->e_id, &sn->out_id, out); |
| 181 |
sn = agsubrep(g, h); |
| 182 |
ins(g->e_seq, &sn->in_seq, in); |
| 183 |
ins(g->e_id, &sn->in_id, in); |
| 184 |
g = agparent(g); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
static void subedge(Agraph_t * g, Agedge_t * e) |
| 189 |
{ |
| 190 |
installedge(g, e); |
| 191 |
/* might an init method call be needed here? */ |
| 192 |
} |
| 193 |
|
| 194 |
static Agedge_t *newedge(Agraph_t * g, Agnode_t * t, Agnode_t * h, |
| 195 |
IDTYPE id) |
| 196 |
{ |
| 197 |
Agedge_t *in, *out; |
| 198 |
|
| 199 |
(void)agsubnode(g, t, 1); |
| 200 |
(void)agsubnode(g, h, 1); |
| 201 |
Agedgepair_t *e2 = gv_alloc(sizeof(Agedgepair_t)); |
| 202 |
in = &e2->in; |
| 203 |
out = &e2->out; |
| 204 |
uint64_t seq = agnextseq(g, AGEDGE); |
| 205 |
assert((seq & SEQ_MASK) == seq && "sequence ID overflow"); |
| 206 |
AGTYPE(in) = AGINEDGE; |
| 207 |
AGTYPE(out) = AGOUTEDGE; |
| 208 |
AGID(in) = AGID(out) = id; |
| 209 |
AGSEQ(in) = AGSEQ(out) = seq & SEQ_MASK; |
| 210 |
in->node = t; |
| 211 |
out->node = h; |
| 212 |
|
| 213 |
installedge(g, out); |
| 214 |
if (g->desc.has_attrs) { |
| 215 |
(void)agbindrec(out, AgDataRecName, sizeof(Agattr_t), false); |
| 216 |
agedgeattr_init(g, out); |
| 217 |
} |
| 218 |
agmethod_init(g, out); |
| 219 |
return out; |
| 220 |
} |
| 221 |
|
| 222 |
/* edge creation predicate */ |
| 223 |
static bool ok_to_make_edge(Agraph_t *g, Agnode_t *t, Agnode_t *h) { |
| 224 |
Agtag_t key = {0}; |
| 225 |
|
| 226 |
/* protect against self, multi-edges in strict graphs */ |
| 227 |
if (agisstrict(g)) { |
| 228 |
key.objtype = 0; /* wild card */ |
| 229 |
if (agfindedge_by_key(g, t, h, key)) |
| 230 |
return false; |
| 231 |
} |
| 232 |
if (g->desc.no_loop && (t == h)) /* simple graphs */ |
| 233 |
return false; |
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
Agedge_t *agidedge(Agraph_t * g, Agnode_t * t, Agnode_t * h, |
| 238 |
IDTYPE id, int cflag) |
| 239 |
{ |
| 240 |
Agraph_t *root; |
| 241 |
Agedge_t *e; |
| 242 |
|
| 243 |
e = agfindedge_by_id(g, t, h, id); |
| 244 |
if (e == NULL && agisundirected(g)) |
| 245 |
e = agfindedge_by_id(g, h, t, id); |
| 246 |
if (e == NULL && cflag && ok_to_make_edge(g, t, h)) { |
| 247 |
root = agroot(g); |
| 248 |
if (g != root && (e = agfindedge_by_id(root, t, h, id))) { |
| 249 |
subedge(g, e); /* old */ |
| 250 |
} |
| 251 |
} |
| 252 |
return e; |
| 253 |
} |
| 254 |
|
| 255 |
Agedge_t *agedge(Agraph_t * g, Agnode_t * t, Agnode_t * h, char *name, |
| 256 |
int cflag) |
| 257 |
{ |
| 258 |
Agedge_t *e; |
| 259 |
IDTYPE my_id; |
| 260 |
|
| 261 |
int have_id = agmapnametoid(g, AGEDGE, name, &my_id, false); |
| 262 |
if (have_id || (name == NULL && (!cflag || agisstrict(g)))) { |
| 263 |
/* probe for pre-existing edge */ |
| 264 |
Agtag_t key = {0}; |
| 265 |
if (have_id) { |
| 266 |
key.id = my_id; |
| 267 |
key.objtype = AGEDGE; |
| 268 |
} else { |
| 269 |
key.id = key.objtype = 0; |
| 270 |
} |
| 271 |
|
| 272 |
/* might already exist locally */ |
| 273 |
e = agfindedge_by_key(g, t, h, key); |
| 274 |
if (e == NULL && agisundirected(g)) |
| 275 |
e = agfindedge_by_key(g, h, t, key); |
| 276 |
if (e) |
| 277 |
return e; |
| 278 |
if (cflag) { |
| 279 |
e = agfindedge_by_key(agroot(g), t, h, key); |
| 280 |
if (e == NULL && agisundirected(g)) |
| 281 |
e = agfindedge_by_key(agroot(g), h, t, key); |
| 282 |
if (e) { |
| 283 |
subedge(g,e); |
| 284 |
return e; |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
if (cflag && ok_to_make_edge(g, t, h) |
| 290 |
&& agmapnametoid(g, AGEDGE, name, &my_id, true)) { /* reserve id */ |
| 291 |
e = newedge(g, t, h, my_id); |
| 292 |
agregister(g, AGEDGE, e); /* register new object in external namespace */ |
| 293 |
} |
| 294 |
else |
| 295 |
e = NULL; |
| 296 |
return e; |
| 297 |
} |
| 298 |
|
| 299 |
void agdeledgeimage(Agraph_t *g, Agobj_t *edge, void *ignored) { |
| 300 |
Agedge_t *e = (Agedge_t *)((char *)edge - offsetof(Agedge_t, base)); |
| 301 |
Agedge_t *in, *out; |
| 302 |
Agnode_t *t, *h; |
| 303 |
Agsubnode_t *sn; |
| 304 |
|
| 305 |
(void)ignored; |
| 306 |
if (AGTYPE(e) == AGINEDGE) { |
| 307 |
in = e; |
| 308 |
out = AGIN2OUT(e); |
| 309 |
} else { |
| 310 |
out = e; |
| 311 |
in = AGOUT2IN(e); |
| 312 |
} |
| 313 |
t = in->node; |
| 314 |
h = out->node; |
| 315 |
sn = agsubrep(g, t); |
| 316 |
del(g->e_seq, &sn->out_seq, out); |
| 317 |
del(g->e_id, &sn->out_id, out); |
| 318 |
sn = agsubrep(g, h); |
| 319 |
del(g->e_seq, &sn->in_seq, in); |
| 320 |
del(g->e_id, &sn->in_id, in); |
| 321 |
#ifdef DEBUG |
| 322 |
for (e = agfstin(g,h); e; e = agnxtin(g,e)) |
| 323 |
assert(e != in); |
| 324 |
for (e = agfstout(g,t); e; e = agnxtout(g,e)) |
| 325 |
assert(e != out); |
| 326 |
#endif |
| 327 |
} |
| 328 |
|
| 329 |
int agdeledge(Agraph_t * g, Agedge_t * e) |
| 330 |
{ |
| 331 |
e = AGMKOUT(e); |
| 332 |
if (agfindedge_by_key(g, agtail(e), aghead(e), AGTAG(e)) == NULL) |
| 333 |
return FAILURE; |
| 334 |
|
| 335 |
if (g == agroot(g)) { |
| 336 |
if (g->desc.has_attrs) |
| 337 |
agedgeattr_delete(e); |
| 338 |
agmethod_delete(g, e); |
| 339 |
agrecclose(&e->base); |
| 340 |
agfreeid(g, AGEDGE, AGID(e)); |
| 341 |
} |
| 342 |
if (agapply(g, &e->base, agdeledgeimage, NULL, false) == SUCCESS) { |
| 343 |
if (g == agroot(g)) |
| 344 |
free(e); |
| 345 |
return SUCCESS; |
| 346 |
} else |
| 347 |
return FAILURE; |
| 348 |
} |
| 349 |
|
| 350 |
Agedge_t *agsubedge(Agraph_t * g, Agedge_t * e, int cflag) |
| 351 |
{ |
| 352 |
Agnode_t *t; |
| 353 |
Agedge_t *rv; |
| 354 |
|
| 355 |
rv = NULL; |
| 356 |
t = agsubnode(g, AGTAIL(e), cflag); |
| 357 |
if (t != NULL || cflag) { |
| 358 |
Agnode_t *const h = agsubnode(g, AGHEAD(e), cflag); |
| 359 |
if (t != NULL && h != NULL) { |
| 360 |
rv = agfindedge_by_key(g, t, h, AGTAG(e)); |
| 361 |
if (cflag && rv == NULL) { |
| 362 |
installedge(g, e); |
| 363 |
rv = e; |
| 364 |
} |
| 365 |
if (rv && AGTYPE(rv) != AGTYPE(e)) |
| 366 |
rv = AGOPP(rv); |
| 367 |
} |
| 368 |
} |
| 369 |
return rv; |
| 370 |
} |
| 371 |
|
| 372 |
/* edge comparison. AGTYPE(e) == 0 means ID is a wildcard. */ |
| 373 |
static int agedgeidcmpf(void *arg_e0, void *arg_e1) { |
| 374 |
Agedge_t *e0 = arg_e0; |
| 375 |
Agedge_t *e1 = arg_e1; |
| 376 |
|
| 377 |
if (AGID(e0->node) < AGID(e1->node)) return -1; |
| 378 |
if (AGID(e0->node) > AGID(e1->node)) return 1; |
| 379 |
/* same node */ |
| 380 |
if (AGTYPE(e0) != 0 && AGTYPE(e1) != 0) { |
| 381 |
if (AGID(e0) < AGID(e1)) return -1; |
| 382 |
if (AGID(e0) > AGID(e1)) return 1; |
| 383 |
} |
| 384 |
return 0; |
| 385 |
} |
| 386 |
|
| 387 |
/* edge comparison. for ordered traversal. */ |
| 388 |
static int agedgeseqcmpf(void *arg_e0, void *arg_e1) { |
| 389 |
Agedge_t *e0 = arg_e0; |
| 390 |
Agedge_t *e1 = arg_e1; |
| 391 |
assert(arg_e0 && arg_e1); |
| 392 |
|
| 393 |
if (e0->node != e1->node) { |
| 394 |
if (AGSEQ(e0->node) < AGSEQ(e1->node)) return -1; |
| 395 |
if (AGSEQ(e0->node) > AGSEQ(e1->node)) return 1; |
| 396 |
} |
| 397 |
else { |
| 398 |
if (AGSEQ(e0) < AGSEQ(e1)) return -1; |
| 399 |
if (AGSEQ(e0) > AGSEQ(e1)) return 1; |
| 400 |
} |
| 401 |
return 0; |
| 402 |
} |
| 403 |
|
| 404 |
/* indexing for ordered traversal */ |
| 405 |
Dtdisc_t Ag_mainedge_seq_disc = { |
| 406 |
.link = offsetof(Agedge_t, seq_link), // use internal links |
| 407 |
.comparf = agedgeseqcmpf, |
| 408 |
}; |
| 409 |
|
| 410 |
Dtdisc_t Ag_subedge_seq_disc = { |
| 411 |
.link = -1, // use external holder objects |
| 412 |
.comparf = agedgeseqcmpf, |
| 413 |
}; |
| 414 |
|
| 415 |
/* indexing for random search */ |
| 416 |
Dtdisc_t Ag_mainedge_id_disc = { |
| 417 |
.link = offsetof(Agedge_t, id_link), // use internal links |
| 418 |
.comparf = agedgeidcmpf, |
| 419 |
}; |
| 420 |
|
| 421 |
Dtdisc_t Ag_subedge_id_disc = { |
| 422 |
.link = -1, // use external holder objects |
| 423 |
.comparf = agedgeidcmpf, |
| 424 |
}; |
| 425 |
|
| 426 |
/* expose macros as functions for ease of debugging |
| 427 |
and to expose them to foreign languages without C preprocessor. */ |
| 428 |
#ifdef ageqedge |
| 429 |
#undef ageqedge |
| 430 |
#endif |
| 431 |
CGRAPH_API int ageqedge(Agedge_t *e, Agedge_t *f); |
| 432 |
CGRAPH_API int ageqedge(Agedge_t * e, Agedge_t * f) |
| 433 |
{ |
| 434 |
return AGEQEDGE(e, f); |
| 435 |
} |
| 436 |
|
| 437 |
#ifdef agmkout |
| 438 |
#undef agmkout |
| 439 |
#endif |
| 440 |
CGRAPH_API Agedge_t *agmkout(Agedge_t *e); |
| 441 |
CGRAPH_API Agedge_t *agmkout(Agedge_t * e) |
| 442 |
{ |
| 443 |
return AGMKOUT(e); |
| 444 |
} |
| 445 |
|
| 446 |
#ifdef agmkin |
| 447 |
#undef agmkin |
| 448 |
#endif |
| 449 |
CGRAPH_API Agedge_t *agmkin(Agedge_t *e); |
| 450 |
CGRAPH_API Agedge_t *agmkin(Agedge_t * e) |
| 451 |
{ |
| 452 |
return AGMKIN(e); |
| 453 |
} |
| 454 |
|
| 455 |
#ifdef agtail |
| 456 |
#undef agtail |
| 457 |
#endif |
| 458 |
CGRAPH_API Agnode_t *agtail(Agedge_t *e); |
| 459 |
CGRAPH_API Agnode_t *agtail(Agedge_t * e) |
| 460 |
{ |
| 461 |
return AGTAIL(e); |
| 462 |
} |
| 463 |
|
| 464 |
#ifdef aghead |
| 465 |
#undef aghead |
| 466 |
#endif |
| 467 |
CGRAPH_API Agnode_t *aghead(Agedge_t *e); |
| 468 |
CGRAPH_API Agnode_t *aghead(Agedge_t * e) |
| 469 |
{ |
| 470 |
return AGHEAD(e); |
| 471 |
} |
| 472 |
|
| 473 |
#ifdef agopp |
| 474 |
#undef agopp |
| 475 |
#endif |
| 476 |
CGRAPH_API Agedge_t *agopp(Agedge_t *e); |
| 477 |
CGRAPH_API Agedge_t *agopp(Agedge_t * e) |
| 478 |
{ |
| 479 |
return AGOPP(e); |
| 480 |
} |