Graphviz 13.0.0~dev.20250607.1528
Loading...
Searching...
No Matches
graph.c
Go to the documentation of this file.
1
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 <assert.h>
17#include <cgraph/cghdr.h>
18#include <cgraph/node_set.h>
19#include <limits.h>
20#include <stdbool.h>
21#include <stdlib.h>
22#include <util/alloc.h>
23
24/*
25 * this code sets up the resource management discipline
26 * and returns a new main graph struct.
27 */
28static Agclos_t *agclos(Agdisc_t * proto)
29{
30 Agclos_t *rv;
31
32 /* establish an allocation arena */
33 rv = gv_calloc(1, sizeof(Agclos_t));
34 rv->disc.id = ((proto && proto->id) ? proto->id : &AgIdDisc);
35 rv->disc.io = ((proto && proto->io) ? proto->io : &AgIoDisc);
36 return rv;
37}
38
39/*
40 * Open a new main graph with the given descriptor (directed, strict, etc.)
41 */
42Agraph_t *agopen(char *name, Agdesc_t desc, Agdisc_t * arg_disc)
43{
44 Agraph_t *g;
45 Agclos_t *clos;
46 IDTYPE gid;
47
48 clos = agclos(arg_disc);
49 g = gv_calloc(1, sizeof(Agraph_t));
50 AGTYPE(g) = AGRAPH;
51 g->clos = clos;
52 g->desc = desc;
53 g->desc.maingraph = true;
54 g->root = g;
55 g->clos->state.id = g->clos->disc.id->open(g, arg_disc);
56 if (agmapnametoid(g, AGRAPH, name, &gid, true))
57 AGID(g) = gid;
58 g = agopen1(g);
59 agregister(g, AGRAPH, g);
60 return g;
61}
62
63/*
64 * initialize dictionaries, set seq, invoke init method of new graph
65 */
67{
68 Agraph_t *par;
69
71 g->n_id = node_set_new();
75
77
78 par = agparent(g);
79 if (par) {
80 uint64_t seq = agnextseq(par, AGRAPH);
81 assert((seq & SEQ_MASK) == seq && "sequence ID overflow");
82 AGSEQ(g) = seq & SEQ_MASK;
83 dtinsert(par->g_seq, g);
84 dtinsert(par->g_id, g);
85 }
86 if (!par || par->desc.has_attrs)
88 agmethod_init(g, g);
89 return g;
90}
91
92/*
93 * Close a graph or subgraph, freeing its storage.
94 */
96{
97 Agraph_t *subg, *next_subg, *par;
98 Agnode_t *n, *next_n;
99
100 par = agparent(g);
101
102 for (subg = agfstsubg(g); subg; subg = next_subg) {
103 next_subg = agnxtsubg(subg);
104 agclose(subg);
105 }
106
107 for (n = agfstnode(g); n; n = next_n) {
108 next_n = agnxtnode(g, n);
109 agdelnode(g, n);
110 }
111
113 agmethod_delete(g, g);
114
115 assert(node_set_is_empty(g->n_id));
116 node_set_free(&g->n_id);
117 assert(dtsize(g->n_seq) == 0);
118 if (agdtclose(g, g->n_seq)) return FAILURE;
119
120 assert(dtsize(g->e_id) == 0);
121 if (agdtclose(g, g->e_id)) return FAILURE;
122 assert(dtsize(g->e_seq) == 0);
123 if (agdtclose(g, g->e_seq)) return FAILURE;
124
125 assert(dtsize(g->g_seq) == 0);
126 if (agdtclose(g, g->g_seq)) return FAILURE;
127
128 assert(dtsize(g->g_id) == 0);
129 if (agdtclose(g, g->g_id)) return FAILURE;
130
131 if (g->desc.has_attrs)
132 if (agraphattr_delete(g)) return FAILURE;
133 agrecclose((Agobj_t *) g);
134 agfreeid(g, AGRAPH, AGID(g));
135
136 if (par) {
137 agdelsubg(par, g);
138 free(g);
139 } else {
140 void *clos;
141 while (g->clos->cb)
142 agpopdisc(g, g->clos->cb->f);
143 AGDISC(g, id)->close(AGCLOS(g, id));
144 if (agstrclose(g)) return FAILURE;
145 clos = g->clos;
146 free(g);
147 free(clos);
148 }
149 return SUCCESS;
150}
151
152uint64_t agnextseq(Agraph_t * g, int objtype)
153{
154 return ++(g->clos->seq[objtype]);
155}
156
158{
159 assert(node_set_size(g->n_id) <= INT_MAX);
160 return (int)node_set_size(g->n_id);
161}
162
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
174{
175 return dtsize(g->g_seq);
176}
177
179{
180 return g->desc.directed;
181}
182
184{
185 return !agisdirected(g);
186}
187
189{
190 return g->desc.strict;
191}
192
194{
195 return (g->desc.strict && g->desc.no_loop);
196}
197
198static 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
207int 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
225int 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
238static 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
250static 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
263 .link = offsetof(Agraph_t, seq_link), // link offset
264 .comparf = agraphseqcmpf,
265};
266
268 .link = offsetof(Agraph_t, id_link), // link offset
269 .comparf = agraphidcmpf,
270};
271
272Agdesc_t Agdirected = {.directed = true, .maingraph = true};
273Agdesc_t Agstrictdirected = {.directed = true, .strict = true, .maingraph = true};
275Agdesc_t Agstrictundirected = {.strict = true, .maingraph = true};
276
278
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
CDT_API Dtlink_t * dtextract(Dt_t *)
Definition dtextract.c:9
CDT_API int dtsize(Dt_t *)
Definition dtsize.c:12
#define dtinsert(d, o)
Definition cdt.h:185
CDT_API Dtmethod_t * Dttree
Definition dttree.c:308
CDT_API int dtrestore(Dt_t *, Dtlink_t *)
Definition dtrestore.c:11
cgraph.h additions
void agfreeid(Agraph_t *g, int objtype, IDTYPE id)
Definition id.c:131
int agstrclose(Agraph_t *g)
Definition refstr.c:324
void aginternalmapclose(Agraph_t *g)
Definition imap.c:195
void agrecclose(Agobj_t *obj)
Definition rec.c:227
Dtdisc_t Ag_subnode_seq_disc
Definition node.c:304
int agmapnametoid(Agraph_t *g, int objtype, char *str, IDTYPE *result, bool createflag)
Definition id.c:102
Dict_t * agdtopen(Dtdisc_t *disc, Dtmethod_t *method)
Definition utils.c:21
@ SEQ_MASK
Definition cghdr.h:74
Dtdisc_t Ag_subedge_seq_disc
Definition edge.c:408
#define FAILURE
Definition cghdr.h:46
#define AGDISC(g, d)
Definition cghdr.h:49
Dtdisc_t Ag_mainedge_seq_disc
Definition edge.c:403
void agregister(Agraph_t *g, int objtype, void *obj)
Definition id.c:170
#define SUCCESS
Definition cghdr.h:45
#define AGCLOS(g, d)
Definition cghdr.h:50
Dtdisc_t Ag_subedge_id_disc
Definition edge.c:419
Dtdisc_t Ag_mainedge_id_disc
Definition edge.c:414
int agdtclose(Agraph_t *g, Dict_t *dict)
Definition utils.c:31
void node_set_free(node_set_t **self)
Definition node.c:549
size_t node_set_size(const node_set_t *self)
Definition node.c:544
node_set_t * node_set_new(void)
Definition node.c:404
void free(void *)
static int agraphidcmpf(void *arg0, void *arg1)
Definition graph.c:250
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:198
static Agclos_t * agclos(Agdisc_t *proto)
Definition graph.c:28
Agraph_t * agopen1(Agraph_t *g)
Definition graph.c:66
uint64_t agnextseq(Agraph_t *g, int objtype)
Definition graph.c:152
static int agraphseqcmpf(void *arg0, void *arg1)
Definition graph.c:238
Dtdisc_t Ag_subgraph_seq_disc
Definition graph.c:262
Dtdisc_t Ag_subgraph_id_disc
Definition graph.c:267
int agnsubg(Agraph_t *g)
Definition graph.c:173
int agcountuniqedges(Agraph_t *g, Agnode_t *n, int want_in, int want_out)
Definition graph.c:207
int agnedges(Agraph_t *g)
Definition graph.c:163
int agdegree(Agraph_t *g, Agnode_t *n, int want_in, int want_out)
Definition graph.c:225
int agnnodes(Agraph_t *g)
Definition graph.c:157
void agraphattr_init(Agraph_t *g)
Definition attr.c:394
int agraphattr_delete(Agraph_t *g)
Definition attr.c:405
int agpopdisc(Agraph_t *g, Agcbdisc_t *disc)
Definition obj.c:211
void agmethod_delete(Agraph_t *g, void *obj)
Definition obj.c:138
void agmethod_init(Agraph_t *g, void *obj)
Definition obj.c:78
Agdisc_t AgDefaultDisc
Definition graph.c:277
Agiddisc_t AgIdDisc
Definition id.c:91
Agiodisc_t AgIoDisc
Definition io.c:39
Agedge_t * agnxtin(Agraph_t *g, Agedge_t *e)
Definition edge.c:71
Agedge_t * agfstin(Agraph_t *g, Agnode_t *n)
Definition edge.c:57
Agdesc_t Agundirected
undirected
Definition graph.c:274
int agisdirected(Agraph_t *g)
Definition graph.c:178
Agdesc_t Agstrictundirected
strict undirected
Definition graph.c:275
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:95
int agisstrict(Agraph_t *g)
Definition graph.c:188
int agissimple(Agraph_t *g)
Definition graph.c:193
Agdesc_t Agstrictdirected
strict directed. A strict graph cannot have multi-edges or self-arcs.
Definition graph.c:273
Agraph_t * agopen(char *name, Agdesc_t desc, Agdisc_t *arg_disc)
creates a new graph with the given name and kind
Definition graph.c:42
int agisundirected(Agraph_t *g)
Definition graph.c:183
Agdesc_t Agdirected
directed
Definition graph.c:272
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:48
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:41
int agdelnode(Agraph_t *g, Agnode_t *arg_n)
removes a node from a graph or subgraph.
Definition node.c:190
Agsubnode_t * agsubrep(Agraph_t *g, Agnode_t *n)
Definition edge.c:144
#define AGID(obj)
returns the unique integer ID associated with the object
Definition cgraph.h:221
uint64_t IDTYPE
unique per main graph ID
Definition cgraph.h:73
#define AGTYPE(obj)
returns AGRAPH, AGNODE, or AGEDGE depending on the type of the object
Definition cgraph.h:216
Agraph_t * agroot(void *obj)
Definition obj.c:168
#define AGSEQ(obj)
Definition cgraph.h:225
@ AGRAPH
Definition cgraph.h:207
Agraph_t * agparent(Agraph_t *g)
Definition subg.c:86
Agraph_t * agfstsubg(Agraph_t *g)
Definition subg.c:73
Agraph_t * agnxtsubg(Agraph_t *subg)
Definition subg.c:78
int agdelsubg(Agraph_t *g, Agraph_t *sub)
Definition subg.c:94
unordered set of Agsubnode_t *
static bool node_set_is_empty(const node_set_t *self)
Definition node_set.h:55
Agcbdisc_t * f
Definition cgraph.h:399
shared resources for Agraph_s
Definition cgraph.h:410
Agdisc_t disc
Definition cgraph.h:411
Agdstate_t state
Definition cgraph.h:412
Agcbstack_t * cb
Definition cgraph.h:415
uint64_t seq[3]
Definition cgraph.h:414
graph descriptor
Definition cgraph.h:284
unsigned has_attrs
Definition cgraph.h:290
unsigned maingraph
Definition cgraph.h:288
unsigned no_loop
Definition cgraph.h:287
unsigned strict
Definition cgraph.h:286
unsigned directed
Definition cgraph.h:285
user's discipline
Definition cgraph.h:336
Agiddisc_t * id
Definition cgraph.h:337
Agiodisc_t * io
Definition cgraph.h:338
void * id
Definition cgraph.h:363
Agnode_t * node
Definition cgraph.h:272
void *(* open)(Agraph_t *g, Agdisc_t *)
Definition cgraph.h:317
a generic header of Agraph_s, Agnode_s and Agedge_s
Definition cgraph.h:210
graph or subgraph
Definition cgraph.h:424
struct graphviz_node_set * n_id
the node set indexed by ID
Definition cgraph.h:430
Dict_t * g_seq
Definition cgraph.h:432
Agraph_t * root
subgraphs - ancestors
Definition cgraph.h:433
Dict_t * g_id
subgraphs - descendants
Definition cgraph.h:432
Dict_t * e_seq
Definition cgraph.h:431
Dict_t * n_seq
the node set in sequence
Definition cgraph.h:429
Agclos_t * clos
shared resources
Definition cgraph.h:434
Agdesc_t desc
Definition cgraph.h:426
Dict_t * e_id
holders for edge sets
Definition cgraph.h:431
This is the node struct allocated per graph (or subgraph).
Definition cgraph.h:251
Dtlink_t * out_seq
Definition cgraph.h:256
Dtlink_t * in_seq
Definition cgraph.h:256
Definition cdt.h:100
int link
Definition cdt.h:87