Graphviz 12.0.1~dev.20240715.2254
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/alloc.h>
18#include <cgraph/cghdr.h>
19#include <cgraph/node_set.h>
20#include <limits.h>
21#include <stdbool.h>
22#include <stdlib.h>
23
25
26/*
27 * this code sets up the resource management discipline
28 * and returns a new main graph struct.
29 */
30static 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 */
44Agraph_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 */
69{
70 Agraph_t *par;
71
73 g->n_id = node_set_new();
78
79 par = agparent(g);
80 if (par) {
81 uint64_t seq = agnextseq(par, AGRAPH);
82 assert((seq & SEQ_MASK) == seq && "sequence ID overflow");
83 AGSEQ(g) = seq & SEQ_MASK;
84 dtinsert(par->g_seq, g);
85 dtinsert(par->g_id, g);
86 }
87 if (!par || par->desc.has_attrs)
89 agmethod_init(g, g);
90 return g;
91}
92
93/*
94 * Close a graph or subgraph, freeing its storage.
95 */
97{
98 Agraph_t *subg, *next_subg, *par;
99 Agnode_t *n, *next_n;
100
101 par = agparent(g);
102
103 for (subg = agfstsubg(g); subg; subg = next_subg) {
104 next_subg = agnxtsubg(subg);
105 agclose(subg);
106 }
107
108 for (n = agfstnode(g); n; n = next_n) {
109 next_n = agnxtnode(g, n);
110 agdelnode(g, n);
111 }
112
114 agmethod_delete(g, g);
115
116 assert(node_set_is_empty(g->n_id));
117 node_set_free(&g->n_id);
118 assert(dtsize(g->n_seq) == 0);
119 if (agdtclose(g, g->n_seq)) return FAILURE;
120
121 assert(dtsize(g->e_id) == 0);
122 if (agdtclose(g, g->e_id)) return FAILURE;
123 assert(dtsize(g->e_seq) == 0);
124 if (agdtclose(g, g->e_seq)) return FAILURE;
125
126 assert(dtsize(g->g_seq) == 0);
127 if (agdtclose(g, g->g_seq)) return FAILURE;
128
129 assert(dtsize(g->g_id) == 0);
130 if (agdtclose(g, g->g_id)) return FAILURE;
131
132 if (g->desc.has_attrs)
133 if (agraphattr_delete(g)) return FAILURE;
134 agrecclose((Agobj_t *) g);
135 agfreeid(g, AGRAPH, AGID(g));
136
137 if (par) {
138 agdelsubg(par, g);
139 agfree(par, g);
140 } else {
141 void *clos;
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 clos = g->clos;
147 free(g);
148 free(clos);
149 }
150 return SUCCESS;
151}
152
153uint64_t agnextseq(Agraph_t * g, int objtype)
154{
155 return ++(g->clos->seq[objtype]);
156}
157
159{
160 assert(node_set_size(g->n_id) <= INT_MAX);
161 return (int)node_set_size(g->n_id);
162}
163
165{
166 Agnode_t *n;
167 int rv = 0;
168
169 for (n = agfstnode(g); n; n = agnxtnode(g, n))
170 rv += agdegree(g, n, 0, 1); /* must use OUT to get self-arcs */
171 return rv;
172}
173
175{
176 return dtsize(g->g_seq);
177}
178
180{
181 return g->desc.directed;
182}
183
185{
186 return !agisdirected(g);
187}
188
190{
191 return g->desc.strict;
192}
193
195{
196 return (g->desc.strict && g->desc.no_loop);
197}
198
199static int cnt(Dict_t * d, Dtlink_t ** set)
200{
201 int rv;
202 dtrestore(d, *set);
203 rv = dtsize(d);
204 *set = dtextract(d);
205 return rv;
206}
207
208int agcountuniqedges(Agraph_t * g, Agnode_t * n, int want_in, int want_out)
209{
210 Agedge_t *e;
211 Agsubnode_t *sn;
212 int rv = 0;
213
214 sn = agsubrep(g, n);
215 if (want_out) rv = cnt(g->e_seq,&(sn->out_seq));
216 if (want_in) {
217 if (!want_out) rv += cnt(g->e_seq,&(sn->in_seq)); /* cheap */
218 else { /* less cheap */
219 for (e = agfstin(g, n); e; e = agnxtin(g, e))
220 if (e->node != n) rv++; /* don't double count loops */
221 }
222 }
223 return rv;
224}
225
226int agdegree(Agraph_t * g, Agnode_t * n, int want_in, int want_out)
227{
228 Agsubnode_t *sn;
229 int rv = 0;
230
231 sn = agsubrep(g, n);
232 if (sn) {
233 if (want_out) rv += cnt(g->e_seq,&(sn->out_seq));
234 if (want_in) rv += cnt(g->e_seq,&(sn->in_seq));
235 }
236 return rv;
237}
238
239static int agraphseqcmpf(void *arg0, void *arg1) {
240 Agraph_t *sg0 = arg0;
241 Agraph_t *sg1 = arg1;
242 if (AGSEQ(sg0) < AGSEQ(sg1)) {
243 return -1;
244 }
245 if (AGSEQ(sg0) > AGSEQ(sg1)) {
246 return 1;
247 }
248 return 0;
249}
250
251static int agraphidcmpf(void *arg0, void *arg1) {
252 Agraph_t *sg0 = arg0;
253 Agraph_t *sg1 = arg1;
254 if (AGID(sg0) < AGID(sg1)) {
255 return -1;
256 }
257 if (AGID(sg0) > AGID(sg1)) {
258 return 1;
259 }
260 return 0;
261}
262
264 .link = offsetof(Agraph_t, seq_link), // link offset
265 .comparf = agraphseqcmpf,
266};
267
269 .link = offsetof(Agraph_t, id_link), // link offset
270 .comparf = agraphidcmpf,
271};
272
273Agdesc_t Agdirected = {.directed = true, .maingraph = true};
274Agdesc_t Agstrictdirected = {.directed = true, .strict = true, .maingraph = true};
276Agdesc_t Agstrictundirected = {.strict = true, .maingraph = true};
277
279
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:193
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:146
int agstrclose(Agraph_t *g)
Definition refstr.c:63
void aginternalmapclose(Agraph_t *g)
Definition imap.c:198
void agrecclose(Agobj_t *obj)
Definition rec.c:226
@ SEQ_MASK
Definition cghdr.h:77
Dtdisc_t Ag_subnode_seq_disc
Definition node.c:313
int agmapnametoid(Agraph_t *g, int objtype, char *str, IDTYPE *result, bool createflag)
Definition id.c:112
Dict_t * agdtopen(Agraph_t *g, Dtdisc_t *disc, Dtmethod_t *method)
Definition utils.c:29
Dtdisc_t Ag_subedge_seq_disc
Definition edge.c:413
#define FAILURE
Definition cghdr.h:44
#define AGDISC(g, d)
Definition cghdr.h:47
Dtdisc_t Ag_mainedge_seq_disc
Definition edge.c:408
void agregister(Agraph_t *g, int objtype, void *obj)
Definition id.c:185
#define SUCCESS
Definition cghdr.h:43
#define AGCLOS(g, d)
Definition cghdr.h:48
Dtdisc_t Ag_subedge_id_disc
Definition edge.c:424
Dtdisc_t Ag_mainedge_id_disc
Definition edge.c:419
int agdtclose(Agraph_t *g, Dict_t *dict)
Definition utils.c:45
void node_set_free(node_set_t **self)
Definition node.c:545
size_t node_set_size(const node_set_t *self)
Definition node.c:540
node_set_t * node_set_new(void)
Definition node.c:391
void free(void *)
static int agraphidcmpf(void *arg0, void *arg1)
Definition graph.c:251
static int cnt(Dict_t *d, Dtlink_t **set)
Definition graph.c:199
static Agclos_t * agclos(Agdisc_t *proto)
Definition graph.c:30
Agraph_t * agopen1(Agraph_t *g)
Definition graph.c:68
Agraph_t * Ag_G_global
Definition graph.c:24
uint64_t agnextseq(Agraph_t *g, int objtype)
Definition graph.c:153
static int agraphseqcmpf(void *arg0, void *arg1)
Definition graph.c:239
Dtdisc_t Ag_subgraph_seq_disc
Definition graph.c:263
Dtdisc_t Ag_subgraph_id_disc
Definition graph.c:268
int agnsubg(Agraph_t *g)
Definition graph.c:174
int agcountuniqedges(Agraph_t *g, Agnode_t *n, int want_in, int want_out)
Definition graph.c:208
int agnedges(Agraph_t *g)
Definition graph.c:164
int agdegree(Agraph_t *g, Agnode_t *n, int want_in, int want_out)
Definition graph.c:226
int agnnodes(Agraph_t *g)
Definition graph.c:158
void agfree(Agraph_t *g, void *ptr)
Definition mem.c:49
void agraphattr_init(Agraph_t *g)
Definition attr.c:373
int agraphattr_delete(Agraph_t *g)
Definition attr.c:384
int agpopdisc(Agraph_t *g, Agcbdisc_t *disc)
Definition obj.c:212
void agmethod_delete(Agraph_t *g, void *obj)
Definition obj.c:137
void agmethod_init(Agraph_t *g, void *obj)
Definition obj.c:77
Agdisc_t AgDefaultDisc
Definition graph.c:278
Agiddisc_t AgIdDisc
Definition id.c:100
Agiodisc_t AgIoDisc
Definition io.c:39
Agedge_t * agnxtin(Agraph_t *g, Agedge_t *e)
Definition edge.c:68
Agedge_t * agfstin(Agraph_t *g, Agnode_t *n)
Definition edge.c:54
Agdesc_t Agundirected
undirected
Definition graph.c:275
int agisdirected(Agraph_t *g)
Definition graph.c:179
Agdesc_t Agstrictundirected
strict undirected
Definition graph.c:276
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:96
int agisstrict(Agraph_t *g)
Definition graph.c:189
int agissimple(Agraph_t *g)
Definition graph.c:194
Agdesc_t Agstrictdirected
strict directed. A strict graph cannot have multi-edges or self-arcs.
Definition graph.c:274
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:44
int agisundirected(Agraph_t *g)
Definition graph.c:184
Agdesc_t Agdirected
directed
Definition graph.c:273
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:47
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:40
int agdelnode(Agraph_t *g, Agnode_t *arg_n)
removes a node from a graph or subgraph.
Definition node.c:197
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:167
#define AGSEQ(obj)
Definition cgraph.h:225
@ AGRAPH
Definition cgraph.h:207
Agraph_t * agparent(Agraph_t *g)
Definition subg.c:90
Agraph_t * agfstsubg(Agraph_t *g)
Definition subg.c:77
Agraph_t * agnxtsubg(Agraph_t *subg)
Definition subg.c:82
int agdelsubg(Agraph_t *g, Agraph_t *sub)
Definition subg.c:98
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:400
shared resources for Agraph_s
Definition cgraph.h:411
Agdisc_t disc
Definition cgraph.h:412
Agdstate_t state
Definition cgraph.h:413
Agcbstack_t * cb
Definition cgraph.h:416
uint64_t seq[3]
Definition cgraph.h:415
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:337
Agiddisc_t * id
Definition cgraph.h:338
Agiodisc_t * io
Definition cgraph.h:339
void * id
Definition cgraph.h:364
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:425
struct graphviz_node_set * n_id
the node set indexed by ID
Definition cgraph.h:431
Dict_t * g_seq
Definition cgraph.h:433
Agraph_t * root
subgraphs - ancestors
Definition cgraph.h:434
Dict_t * g_id
subgraphs - descendants
Definition cgraph.h:433
Dict_t * e_seq
Definition cgraph.h:432
Dict_t * n_seq
the node set in sequence
Definition cgraph.h:430
Agclos_t * clos
shared resources
Definition cgraph.h:435
Agdesc_t desc
Definition cgraph.h:427
Dict_t * e_id
holders for edge sets
Definition cgraph.h:432
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:104
int link
Definition cdt.h:91