File: node.c

package info (click to toggle)
graphviz 1.7.16-2
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 11,124 kB
  • ctags: 12,650
  • sloc: ansic: 131,002; sh: 7,483; makefile: 1,954; tcl: 1,760; yacc: 1,758; perl: 253; awk: 150; lex: 96
file content (243 lines) | stat: -rw-r--r-- 5,629 bytes parent folder | download
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
/*
    This software may only be used by you under license from AT&T Corp.
    ("AT&T").  A copy of AT&T's Source Code Agreement is available at
    AT&T's Internet website having the URL:
    <http://www.research.att.com/sw/tools/graphviz/license/source.html>
    If you received this software without first entering into a license
    with AT&T, you have an infringing copy of this software and cannot use
    it without violating AT&T's intellectual property rights.
*/
#pragma prototyped

#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 *localnode(Agraph_t *g, unsigned long id, Agnode_t *rootnode)
{
	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 = localnode(par, id, rootnode);
	else
		npar = NILnode;
	n = newnode(g,id,npar?AGSEQ(npar):agnextseq(g,AGNODE));
	if (npar) AGDATA(n) = AGDATA(npar);
	else {
		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;
}