Debian Codegraph @

lib/cgraph/graph.c

raw
1 /**
2 * @file
3 * @ingroup cgraph_core
4 * @ingroup cgraph_graph
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 <limits.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <util/alloc.h>
25
26 /*
27 * this code sets up the resource management discipline
28 * and returns a new main graph struct.
29 */
30 static Agclos_t *agclos(Agdisc_t * proto)
31 {
32 Agclos_t *rv;
33
34 /* establish an allocation arena */
35 rv = gv_calloc(1, sizeof(Agclos_t));
36 rv->disc.id = ((proto && proto->id) ? proto->id : &AgIdDisc);
37 rv->disc.io = ((proto && proto->io) ? proto->io : &AgIoDisc);
38 return rv;
39 }
40
41 /*
42 * Open a new main graph with the given descriptor (directed, strict, etc.)
43 */
44 Agraph_t *agopen(char *name, Agdesc_t desc, Agdisc_t * arg_disc)
45 {
46 Agraph_t *g;
47 Agclos_t *clos;
48 IDTYPE gid;
49
50 clos = agclos(arg_disc);
51 g = gv_calloc(1, sizeof(Agraph_t));
52 AGTYPE(g) = AGRAPH;
53 g->clos = clos;
54 g->desc = desc;
55 g->desc.maingraph = true;
56 g->root = g;
57 g->clos->state.id = g->clos->disc.id->open(g, arg_disc);
58 if (agmapnametoid(g, AGRAPH, name, &gid, true))
59 AGID(g) = gid;
60 g = agopen1(g);
61 agregister(g, AGRAPH, g);
62 return g;
63 }
64
65 /*
66 * initialize dictionaries, set seq, invoke init method of new graph
67 */
68 Agraph_t *agopen1(Agraph_t * g)
69 {
70 Agraph_t *par;
71
72 g->n_seq = agdtopen(&Ag_subnode_seq_disc, Dttree);
73 g->n_id = node_set_new();
74 g->e_seq = agdtopen(g == agroot(g)? &Ag_mainedge_seq_disc : &Ag_subedge_seq_disc, Dttree);
75 g->e_id = agdtopen(g == agroot(g)? &Ag_mainedge_id_disc : &Ag_subedge_id_disc, Dttree);
76 g->g_seq = agdtopen(&Ag_subgraph_seq_disc, Dttree);
77
78 g->g_id = agdtopen(&Ag_subgraph_id_disc, Dttree);
79
80 par = agparent(g);
81 if (par) {
82 uint64_t seq = agnextseq(par, AGRAPH);
83 assert((seq & SEQ_MASK) == seq && "sequence ID overflow");
84 AGSEQ(g) = seq & SEQ_MASK;
85 dtinsert(par->g_seq, g);
86 dtinsert(par->g_id, g);
87 }
88 if (!par || par->desc.has_attrs)
89 agraphattr_init(g);
90 agmethod_init(g, g);
91 return g;
92 }
93
94 /*
95 * Close a graph or subgraph, freeing its storage.
96 */
97 int agclose(Agraph_t * g)
98 {
99 Agraph_t *subg, *next_subg, *par;
100 Agnode_t *n, *next_n;
101
102 par = agparent(g);
103
104 for (subg = agfstsubg(g); subg; subg = next_subg) {
105 next_subg = agnxtsubg(subg);
106 agclose(subg);
107 }
108
109 for (n = agfstnode(g); n; n = next_n) {
110 next_n = agnxtnode(g, n);
111 agdelnode(g, n);
112 }
113
114 aginternalmapclose(g);
115 agmethod_delete(g, g);
116
117 assert(node_set_is_empty(g->n_id));
118 node_set_free(&g->n_id);
119 assert(dtsize(g->n_seq) == 0);
120 if (agdtclose(g, g->n_seq)) return FAILURE;
121
122 assert(dtsize(g->e_id) == 0);
123 if (agdtclose(g, g->e_id)) return FAILURE;
124 assert(dtsize(g->e_seq) == 0);
125 if (agdtclose(g, g->e_seq)) return FAILURE;
126
127 assert(dtsize(g->g_seq) == 0);
128 if (agdtclose(g, g->g_seq)) return FAILURE;
129
130 assert(dtsize(g->g_id) == 0);
131 if (agdtclose(g, g->g_id)) return FAILURE;
132
133 if (g->desc.has_attrs)
134 if (agraphattr_delete(g)) return FAILURE;
135 agrecclose(&g->base);
136 agfreeid(g, AGRAPH, AGID(g));
137
138 if (par) {
139 agdelsubg(par, g);
140 free(g);
141 } else {
142 while (g->clos->cb)
143 agpopdisc(g, g->clos->cb->f);
144 AGDISC(g, id)->close(AGCLOS(g, id));
145 if (agstrclose(g)) return FAILURE;
146 free(g->clos);
147 free(g);
148 }
149 return SUCCESS;
150 }
151
152 uint64_t agnextseq(Agraph_t * g, int objtype)
153 {
154 return ++(g->clos->seq[objtype]);
155 }
156
157 int agnnodes(Agraph_t * g)
158 {
159 assert(node_set_size(g->n_id) <= INT_MAX);
160 return (int)node_set_size(g->n_id);
161 }
162
163 int agnedges(Agraph_t * g)
164 {
165 Agnode_t *n;
166 int rv = 0;
167
168 for (n = agfstnode(g); n; n = agnxtnode(g, n))
169 rv += agdegree(g, n, 0, 1); /* must use OUT to get self-arcs */
170 return rv;
171 }
172
173 int agnsubg(Agraph_t * g)
174 {
175 return dtsize(g->g_seq);
176 }
177
178 int agisdirected(Agraph_t * g)
179 {
180 return g->desc.directed;
181 }
182
183 int agisundirected(Agraph_t * g)
184 {
185 return !agisdirected(g);
186 }
187
188 int agisstrict(Agraph_t * g)
189 {
190 return g->desc.strict;
191 }
192
193 int agissimple(Agraph_t * g)
194 {
195 return (g->desc.strict && g->desc.no_loop);
196 }
197
198 static int cnt(Dict_t * d, Dtlink_t ** set)
199 {
200 int rv;
201 dtrestore(d, *set);
202 rv = dtsize(d);
203 *set = dtextract(d);
204 return rv;
205 }
206
207 int agcountuniqedges(Agraph_t * g, Agnode_t * n, int want_in, int want_out)
208 {
209 Agedge_t *e;
210 Agsubnode_t *sn;
211 int rv = 0;
212
213 sn = agsubrep(g, n);
214 if (want_out) rv = cnt(g->e_seq,&(sn->out_seq));
215 if (want_in) {
216 if (!want_out) rv += cnt(g->e_seq,&(sn->in_seq)); /* cheap */
217 else { /* less cheap */
218 for (e = agfstin(g, n); e; e = agnxtin(g, e))
219 if (e->node != n) rv++; /* don't double count loops */
220 }
221 }
222 return rv;
223 }
224
225 int agdegree(Agraph_t * g, Agnode_t * n, int want_in, int want_out)
226 {
227 Agsubnode_t *sn;
228 int rv = 0;
229
230 sn = agsubrep(g, n);
231 if (sn) {
232 if (want_out) rv += cnt(g->e_seq,&(sn->out_seq));
233 if (want_in) rv += cnt(g->e_seq,&(sn->in_seq));
234 }
235 return rv;
236 }
237
238 static int agraphseqcmpf(void *arg0, void *arg1) {
239 Agraph_t *sg0 = arg0;
240 Agraph_t *sg1 = arg1;
241 if (AGSEQ(sg0) < AGSEQ(sg1)) {
242 return -1;
243 }
244 if (AGSEQ(sg0) > AGSEQ(sg1)) {
245 return 1;
246 }
247 return 0;
248 }
249
250 static int agraphidcmpf(void *arg0, void *arg1) {
251 Agraph_t *sg0 = arg0;
252 Agraph_t *sg1 = arg1;
253 if (AGID(sg0) < AGID(sg1)) {
254 return -1;
255 }
256 if (AGID(sg0) > AGID(sg1)) {
257 return 1;
258 }
259 return 0;
260 }
261
262 Dtdisc_t Ag_subgraph_seq_disc = {
263 .link = offsetof(Agraph_t, seq_link), // link offset
264 .comparf = agraphseqcmpf,
265 };
266
267 Dtdisc_t Ag_subgraph_id_disc = {
268 .link = offsetof(Agraph_t, id_link), // link offset
269 .comparf = agraphidcmpf,
270 };
271
272 Agdesc_t Agdirected = {.directed = true, .maingraph = true};
273 Agdesc_t Agstrictdirected = {.directed = true, .strict = true, .maingraph = true};
274 Agdesc_t Agundirected = {.maingraph = true};
275 Agdesc_t Agstrictundirected = {.strict = true, .maingraph = true};
276
277 Agdisc_t AgDefaultDisc = { &AgIdDisc, &AgIoDisc };
278
279 /**
280 * @dir lib/cgraph
281 * @brief abstract graph C library, API cgraph.h
282 *
283 * [man 3 cgraph](https://graphviz.org/pdf/cgraph.3.pdf)
284 *
285 * See @ref cgraph
286 *
287 * @defgroup cgraph Cgraph
288 * @brief abstract graph C library, API: @ref cgraph_api
289 *
290 * Public API of the library: @ref cgraph_api
291 *
292 * Layers:
293 *
294 * * top layer: @ref cgraph_app - uncoupled application specific functions
295 * * middle layer: @ref cgraph_core - highly cohesive core
296 * * bottom layer: @ref cgraph_utils
297 *
298 * @{
299 * @defgroup cgraph_core core
300 * @brief highly cohesive core
301 * @}
302 */