1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/* $Id: node.c,v 1.5 2009/06/03 01:10:51 ellson Exp $ $Revision: 1.5 $ */
/* vim:set shiftwidth=4 ts=8: */
/**********************************************************
* This software is part of the graphviz package *
* http://www.graphviz.org/ *
* *
* Copyright (c) 1994-2004 AT&T Corp. *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Corp. *
* *
* Information and Software Systems Research *
* AT&T Research, Florham Park NJ *
**********************************************************/
#include <cghdr.h>
Agnode_t *agfindnode_by_id(Agraph_t * g, unsigned long id)
{
Agsubnode_t *sn;
static Agsubnode_t template;
static Agnode_t dummy;
dummy.base.tag.id = id;
template.node = &dummy;
sn = (Agsubnode_t *) dtsearch(g->n_id, &template);
return sn ? sn->node : NILnode;
}
Agnode_t *agfindnode_by_name(Agraph_t * g, char *name)
{
unsigned long id;
if (agmapnametoid(g, AGNODE, name, &id, FALSE))
return agfindnode_by_id(g, id);
else
return NILnode;
}
Agnode_t *agfstnode(Agraph_t * g)
{
Agsubnode_t *sn;
sn = (Agsubnode_t *) dtfirst(g->n_seq);
return sn ? sn->node : NILnode;
}
Agnode_t *agnxtnode(Agraph_t * g, Agnode_t * n)
{
Agsubnode_t *sn;
sn = agsubrep(g, n);
sn = ((Agsubnode_t *) dtnext(g->n_seq, sn));
return sn ? sn->node : NILnode;
}
Agnode_t *aglstnode(Agraph_t * g)
{
Agsubnode_t *sn;
sn = (Agsubnode_t *) dtlast(g->n_seq);
return sn ? sn->node : NILnode;
}
Agnode_t *agprvnode(Agraph_t * g, Agnode_t * n)
{
Agsubnode_t *sn;
sn = agsubrep(g, n);
sn = ((Agsubnode_t *) dtprev(g->n_seq, sn));
return sn ? sn->node : NILnode;
}
/* internal node constructor */
static Agnode_t *newnode(Agraph_t * g, unsigned long id, unsigned long seq)
{
Agnode_t *n;
n = agalloc(g, sizeof(Agnode_t));
AGTYPE(n) = AGNODE;
AGID(n) = id;
AGSEQ(n) = seq;
n->root = agroot(g);
if (agroot(g)->desc.has_attrs)
(void) agbindrec(n, AgDataRecName, sizeof(Agattr_t), FALSE);
/* nodeattr_init and method_init will be called later, from the
* subgraph where the node was actually created, but first it has
* to be installed in all the (sub)graphs up to root. */
return n;
}
static void installnode(Agraph_t * g, Agnode_t * n)
{
Agsubnode_t *sn;
if (g == agroot(g)) sn = &(n->mainsub);
else sn = agalloc(g, sizeof(Agsubnode_t));
sn->node = n;
dtinsert(g->n_id, sn);
dtinsert(g->n_seq, sn);
}
static void installnodetoroot(Agraph_t * g, Agnode_t * n)
{
Agraph_t *par;
installnode(g, n);
if ((par = agparent(g)))
installnodetoroot(par, n);
}
static void initnode(Agraph_t * g, Agnode_t * n)
{
if (agroot(g)->desc.has_attrs)
agnodeattr_init(g,n);
agmethod_init(g, n);
}
/* external node constructor - create by id */
Agnode_t *agidnode(Agraph_t * g, unsigned long id, int cflag)
{
Agraph_t *root;
Agnode_t *n;
n = agfindnode_by_id(g, id);
if ((n == NILnode) && cflag) {
root = agroot(g);
if ((g != root) && ((n = agfindnode_by_id(root, id)))) /*old */
agsubnode(g, n, TRUE); /* insert locally */
else {
if (agallocid(g, AGNODE, id)) { /* new */
n = newnode(g, id, agnextseq(g, AGNODE));
installnodetoroot(g, n);
initnode(g, n);
} else
n = NILnode; /* allocid for new node failed */
}
}
/* else return probe result */
return n;
}
Agnode_t *agnode(Agraph_t * g, char *name, int cflag)
{
Agraph_t *root;
Agnode_t *n;
unsigned long id;
root = agroot(g);
/* probe for existing node */
if (agmapnametoid(g, AGNODE, name, &id, FALSE)) {
if ((n = agfindnode_by_id(g, id)))
return n;
/* might already exist globally, but need to insert locally */
if (cflag && (g != root) && ((n = agfindnode_by_id(root, id)))) {
return agsubnode(g, n, TRUE);
}
}
if (cflag && agmapnametoid(g, AGNODE, name, &id, TRUE)) { /* reserve id */
n = newnode(g, id, agnextseq(g, AGNODE));
installnodetoroot(g, n);
initnode(g, n);
return n;
}
return NILnode;
}
/* removes image of node and its edges from graph.
caller must ensure n belongs to g. */
void agdelnodeimage(Agraph_t * g, Agnode_t * n, void *ignored)
{
Agedge_t *e, *f;
static Agsubnode_t template;
template.node = n;
NOTUSED(ignored);
for (e = agfstedge(g, n); e; e = f) {
f = agnxtedge(g, e, n);
agdeledgeimage(g, e, 0);
}
dtdelete(g->n_id, &template);
dtdelete(g->n_seq, &template);
}
int agdelnode(Agraph_t * g, Agnode_t * n)
{
Agedge_t *e, *f;
if (!agfindnode_by_id(g, AGID(n)))
return FAILURE; /* bad arg */
if (g == agroot(g)) {
for (e = agfstedge(g, n); e; e = f) {
f = agnxtedge(g, e, n);
agdeledge(g, e);
}
if (g->desc.has_attrs)
agnodeattr_delete(n);
agmethod_delete(g, n);
agrecclose((Agobj_t *) n);
agfreeid(g, AGNODE, AGID(n));
}
if (agapply (g, (Agobj_t *) n, (agobjfn_t) agdelnodeimage, NILnode, FALSE) == SUCCESS) {
if (g == agroot(g))
agfree(g, n);
return SUCCESS;
} else
return FAILURE;
}
static void dict_relabel(Agnode_t * n, void *arg)
{
Agraph_t *g;
unsigned long new_id;
g = agraphof(n);
new_id = *(unsigned long *) arg;
dtdelete(g->n_id, n); /* wrong, should be subrep */
AGID(n) = new_id;
dtinsert(g->n_id, n); /* also wrong */
/* because all the subgraphs share the same node now, this
now requires a separate deletion and insertion phase */
}
int agrelabel_node(Agnode_t * n, char *newname)
{
Agraph_t *g;
unsigned long new_id;
g = agroot(agraphof(n));
if (agfindnode_by_name(g, newname))
return FAILURE;
if (agmapnametoid(g, AGNODE, newname, &new_id, TRUE)) {
if (agfindnode_by_id(agroot(g), new_id) == NILnode) {
agfreeid(g, AGNODE, AGID(n));
agapply(g, (Agobj_t *) n, (agobjfn_t) dict_relabel,
(void *) &new_id, FALSE);
return SUCCESS;
} else {
agfreeid(g, AGNODE, new_id); /* couldn't use it after all */
}
}
return FAILURE;
}
/* lookup or insert <n> in <g> */
Agnode_t *agsubnode(Agraph_t * g, Agnode_t * n0, int cflag)
{
Agraph_t *par;
Agnode_t *n;
if (agroot(g) != n0->root)
return NILnode;
n = agfindnode_by_id(g, AGID(n0));
if ((n == NILnode) && cflag) {
if ((par = agparent(g))) {
n = agsubnode(par, n0, cflag);
installnode(g, n);
/* no callback for existing node insertion in subgraph (?) */
}
/* else impossible that <n> doesn't belong to <g> */
}
/* else lookup succeeded */
return n;
}
int agsubnodeidcmpf(Dict_t * d, void *arg0, void *arg1, Dtdisc_t * disc)
{
Agsubnode_t *sn0, *sn1;
sn0 = (Agsubnode_t *) arg0;
sn1 = (Agsubnode_t *) arg1;
return (AGID(sn0->node) - AGID(sn1->node));
}
int agsubnodeseqcmpf(Dict_t * d, void *arg0, void *arg1, Dtdisc_t * disc)
{
Agsubnode_t *sn0, *sn1;
sn0 = (Agsubnode_t *) arg0;
sn1 = (Agsubnode_t *) arg1;
return (AGSEQ(sn0->node) - AGSEQ(sn1->node));
}
Dtdisc_t Ag_subnode_id_disc = {
0, /* pass object ptr */
0, /* size (ignored) */
offsetof(Agsubnode_t, id_link), /* link offset */
NIL(Dtmake_f),
NIL(Dtfree_f),
agsubnodeidcmpf,
NIL(Dthash_f),
agdictobjmem,
NIL(Dtevent_f)
};
Dtdisc_t Ag_subnode_seq_disc = {
0, /* pass object ptr */
0, /* size (ignored) */
offsetof(Agsubnode_t, seq_link), /* link offset */
NIL(Dtmake_f),
NIL(Dtfree_f),
agsubnodeseqcmpf,
NIL(Dthash_f),
agdictobjmem,
NIL(Dtevent_f)
};
|