File: node.c

package info (click to toggle)
graphviz 2.8-3%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 20,480 kB
  • ctags: 22,071
  • sloc: ansic: 163,260; cpp: 36,565; sh: 25,024; yacc: 2,358; tcl: 1,808; makefile: 1,745; cs: 805; perl: 801; ml: 649; awk: 160; lex: 153; python: 105; ruby: 32; php: 6
file content (282 lines) | stat: -rw-r--r-- 6,799 bytes parent folder | download | duplicates (2)
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
/* $Id: node.c,v 1.1.1.1 2004/12/23 04:02:34 ellson Exp $ $Revision: 1.1.1.1 $ */
/* 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 <aghdr.h>

#ifdef DMALLOC
#include "dmalloc.h"
#endif

Agnode_t *agfindnode_by_id(Agraph_t * g, unsigned long id)
{
    Agnode_t *n;
    static Agnode_t template;

    template.base.tag.id = id;
    n = (Agnode_t *) dtsearch(g->n_id, &template);
    return n;
}

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)
{
    return (Agnode_t *) dtfirst(g->n_seq);
}

Agnode_t *agnxtnode(Agnode_t * n)
{
    Agraph_t *g;
    g = agraphof(n);
    if (agisflattened(g))
	return AGNEXTNODE(n);
    else {
	return (Agnode_t *) dtnext(g->n_seq, n);
    }
}

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->g = g;
    dtinsert(g->n_seq, n);
    dtinsert(g->n_id, n);
    return n;
}

/* create or bind the given node.  it may already exist in the parent. */
static Agnode_t *mklocalnode(Agraph_t * g, unsigned long id,
			     Agnode_t * rootnode, int *isnew)
{
    Agnode_t *n, *npar;
    Agraph_t *par;

    agnotflat(g);
    if (rootnode) {
	if (g->desc.maingraph)
	    return rootnode;
	if ((n = agfindnode_by_id(g, id)))
	    return n;
    }
    if ((par = agparent(g)))
	npar = mklocalnode(par, id, rootnode, isnew);
    else {
	npar = NILnode;
	*isnew = TRUE;
    }
    n = newnode(g, id, npar ? AGSEQ(npar) : agnextseq(g, AGNODE));
    if (npar)
	AGDATA(n) = AGDATA(npar);
    else {
	if (g->desc.has_attrs)
	    (void) agrealbindrec(n, AgDataRecName, sizeof(Agattr_t), FALSE,
				 TRUE);
	/* nodeattr_init and method_init will be called later */
    }
    return n;
}

static Agnode_t *localnode(Agraph_t * g, unsigned long id,
			   Agnode_t * rootnode)
{
    int isnew = FALSE;
    Agnode_t *n;
    n = mklocalnode(g, id, rootnode, &isnew);
    if (isnew) {
	if (g->desc.has_attrs)
	    agnodeattr_init(n, TRUE);
	agmethod_init(g, n);
    }
    return n;
}

Agnode_t *agidnode(Agraph_t * g, unsigned long id, int cflag)
{
    Agraph_t *root;
    Agnode_t *n, *rootnode;

    rootnode = NILnode;
    n = agfindnode_by_id(g, id);
    if ((n == NILnode) && cflag) {
	root = agroot(g);
	if (((g != root) && ((rootnode = agfindnode_by_id(agroot(g), id))))	/*old */
	    ||agallocid(g, AGNODE, id))	/* new */
	    n = localnode(g, id, rootnode);
    }
    return n;
}

Agnode_t *agnode(Agraph_t * g, char *name, int cflag)
{
    Agraph_t *root;
    Agnode_t *n, *rootnode;
    unsigned long id;

    if (agmapnametoid(g, AGNODE, name, &id, FALSE)) {
	/* might already exist locally */
	if ((n = agfindnode_by_id(g, id)))
	    return n;

	/* might already exist globally */
	root = agroot(g);
	if (cflag && (g != root)
	    && ((rootnode = agfindnode_by_id(root, id))))
	    if ((n = localnode(g, id, rootnode)))
		return n;
    }

    if (cflag && agmapnametoid(g, AGNODE, name, &id, TRUE))	/* reserve id */
	return localnode(g, id, NILnode);

    return NILnode;
}

/* removes image of node and its edges from graph.
   caller must ensure arg_n belongs to g. */
void agdelnodeimage(Agnode_t * n, void *ignored)
{
    Agraph_t *g;
    Agedge_t *e, *f;

    NOTUSED(ignored);
    g = agraphof(n);
    agnotflat(g);
    agflatten_edges(g, n);
#ifndef NOFLATOPT
    if (n->out)
	assert(AGPREV(n->out) == NILedge);
    for (e = AGFSTOUT(n); e; e = f) {
	f = AGNXTE(e);
	if (f)
	    assert(AGPREV(f) == e);
	if (e->node != n)
	    agedgesetop(g, AGOPP(e), FALSE);
	agfree(g, e);		/* Note, assumes edgepair layout in edge.c */
    }
    if (n->in)
	assert(AGPREV(n->in) == NILedge);
    for (e = AGFSTIN(n); e; e = f) {
	f = AGNXTE(e);
	if (f)
	    assert(AGPREV(f) == e);
	if (e->node != n)
	    agedgesetop(g, AGOPP(e), FALSE);
	agfree(g, AGIN2OUT(e));
    }
#else
    for (e = agfstedge(n); e; e = f) {
	f = agnxtedge(e, n);
	agdeledgepair(e);
    }
#endif
    dtdelete(g->n_seq, n);
    dtdelete(g->n_id, n);
    agfree(g, n);
}

int agdelnode(Agnode_t * n)
{
    Agraph_t *g;
    Agedge_t *e, *f;

    g = agraphof(n);
    if ((Agnode_t *) dtsearch(g->n_id, n) == NILnode)
	return FAILURE;		/* bad arg */
    if (agisarootobj(n)) {
	for (e = agfstedge(n); e; e = f) {
	    f = agnxtedge(e, n);
	    agdeledge(e);
	}
	if (g->desc.has_attrs)
	    agnodeattr_delete(n);
	agmethod_delete(g, n);
	agrecclose((Agobj_t *) n);
	agfreeid(g, AGNODE, AGID(n));
    }
    return agapply(g, (Agobj_t *) n, (agobjfn_t) agdelnodeimage, NILnode,
		   FALSE);
}

static void dict_relabel(Agnode_t * n, void *arg)
{
    Agraph_t *g;
    unsigned long new_id;

    g = agraphof(n);
    new_id = *(unsigned long *) arg;
    agnotflat(g);
    dtdelete(g->n_id, n);
    AGID(n) = new_id;
    dtinsert(g->n_id, n);
}

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;
}

/* return a node in <g> that is the image of <arg_n>. */
Agnode_t *agsubnode(Agraph_t * g, Agnode_t * arg_n, int cflag)
{
    Agnode_t *n, *npar;
    Agraph_t *par;

    n = agfindnode_by_id(g, AGID(arg_n));

    if ((n == NILnode) && cflag) {
	if ((par = agparent(g))) {
	    if ((npar = agsubnode(par, arg_n, cflag))) {
		n = newnode(g, AGID(npar), AGSEQ(npar));
		AGDATA(n) = AGDATA(npar);
/* #ifdef MTFHACK */
		n->base.tag.mtflock = npar->base.tag.mtflock;
/* #endif */
	    }			/* else someone handed us a flaky ptr in <n> */
	}
    }
    /* else probe failed */
    return n;
}