Debian Codegraph @

lib/cgraph/cgraph.h

raw
1 /**
2 * @file
3 * @brief abstract graph C library, @ref cgraph_api
4 * @ingroup cgraph_api
5 *
6 * **Libcgraph** supports graph programming by maintaining graphs
7 * in memory and reading and writing graph files.
8 * Graphs are composed of nodes, edges, and nested subgraphs.
9 * These graph objects may be attributed with string name-value pairs
10 * and programmer-defined records (see Attributes).
11 * Most of Libcgraph’s global symbols have the prefix **ag** (case varying).
12 * In the following, if a function has a parameter `int createflag` and
13 * the object does not exist, the function will create the specified object
14 * if `createflag` is non-zero; otherwise, it will return NULL.
15 *
16 * [man 3 cgraph](https://graphviz.org/pdf/cgraph.3.pdf)
17 *
18 */
19
20 /*************************************************************************
21 * Copyright (c) 2011 AT&T Intellectual Property
22 * All rights reserved. This program and the accompanying materials
23 * are made available under the terms of the Eclipse Public License v1.0
24 * which accompanies this distribution, and is available at
25 * https://www.eclipse.org/legal/epl-v10.html
26 *
27 * Contributors: Details at https://graphviz.org
28 *************************************************************************/
29
30 #pragma once
31
32 #include "cdt.h"
33 #include <inttypes.h>
34 #include <stdbool.h>
35 #include <stddef.h>
36 #include <stdint.h>
37 #include <stdio.h>
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /**
44 * @defgroup cgraph_api Cgraph API
45 * @brief Abstract graph C library. API cgraph.h
46 * @ingroup public_apis
47 *
48 * [man 3 cgraph](https://graphviz.org/pdf/cgraph.3.pdf)
49 *
50 * Main types @ref Agraph_t, @ref Agnode_t, @ref Agedge_t.
51 * @{
52 */
53
54 /// @cond
55
56 #ifdef GVDLL
57 #ifdef EXPORT_CGRAPH
58 #define CGRAPH_API __declspec(dllexport)
59 #else
60 #define CGRAPH_API __declspec(dllimport)
61 #endif
62 #endif
63
64 #ifndef CGRAPH_API
65 #define CGRAPH_API /* nothing */
66 #endif
67
68 /// @endcond
69
70 /* forward struct type declarations */
71 /// @addtogroup cgraph_object
72 /// @{
73 typedef uint64_t IDTYPE; ///< unique per main graph ID
74 typedef struct Agtag_s Agtag_t;
75 typedef struct Agobj_s Agobj_t; ///< generic object header
76 /// @}
77 /// @addtogroup cgraph_graph
78 /// @{
79 typedef struct Agraph_s Agraph_t; ///< graph, subgraph (or hyperedge)
80 typedef struct Agdesc_s Agdesc_t; ///< graph descriptor
81 typedef struct Agdstate_s Agdstate_t; ///< client state (closures)
82 typedef struct Agclos_s Agclos_t; ///< common fields for graph/subgs
83 /// @}
84 /// @addtogroup cgraph_node
85 /// @{
86 typedef struct Agnode_s Agnode_t; ///< node (atom)
87 typedef struct Agsubnode_s Agsubnode_t;
88 /// @}
89 /// @addtogroup cgraph_edge
90 /// @{
91 typedef struct Agedge_s Agedge_t; ///< node pair
92 typedef struct Agedgepair_s Agedgepair_t; ///< the edge object
93 /// @}
94 /// @addtogroup cgraph_disc
95 /// @{
96 typedef struct Agiddisc_s Agiddisc_t; ///< object ID allocator
97 typedef struct Agiodisc_s Agiodisc_t; ///< IO services
98 typedef struct Agdisc_s Agdisc_t; ///< union of client discipline methods
99 /// @}
100 /// @addtogroup cgraph_callback
101 /// @{
102 typedef struct Agcbdisc_s Agcbdisc_t; ///< client event callbacks
103 typedef struct Agcbstack_s Agcbstack_t; ///< enclosing state for @ref Agcbdisc_t
104 /// @}
105
106 /** @addtogroup cgraph_attr
107 * @{
108 *
109 * @defgroup cgraph_rec records
110 * @brief These records are attached by client programs dynamically at runtime.
111 * @{
112 *
113 * Uninterpreted records may be attached to graphs, subgraphs, nodes,
114 * and edges for efficient operations on values such as marks, weights,
115 * counts, and pointers needed by algorithms.
116 * Application programmers define the fields of these records,
117 * but they must be declared with a common record header @ref Agrec_t.
118 *
119 * A unique string ID (stored in @ref Agrec_s.name) must be given
120 * to each record attached to the same object.
121 * Cgraph has functions to create, search for, and delete these records.
122 * The records are maintained in a circular list,
123 * with @ref Agobj_s.data pointing somewhere in the list.
124 * The search function @ref aggetrec has an option to lock this pointer on a
125 * given record. The application must be written so only one such lock is
126 * outstanding at a time.
127 *
128 * Records are created and managed by Libcgraph.
129 * A programmer must explicitly attach them to the objects in a graph,
130 * either to individual objects one at a time via @ref agbindrec,
131 * or to all the objects of the same class in a graph via @ref aginit.
132 * The `name` argument of a record distinguishes various types of records,
133 * and is programmer defined.
134 *
135 * Libcgraph reserves the prefix "_AG_" (in
136 * @ref DataDictName,
137 * @ref AgDataRecName,
138 * @ref DRName).
139 *
140 * Internally, records Agrec_s are maintained in circular linked lists
141 * attached to graph objects Agobj_s.
142 * To allow referencing application-dependent data without function calls or
143 * search, Libcgraph allows setting and locking the list pointer of a graph,
144 * node, or edge on a particular record (see @ref Agtag_s.mtflock and @ref
145 * Agobj_s.data). This pointer can be obtained with the macro @ref AGDATA(obj).
146 * A cast, generally within a macro or inline function,
147 * is usually applied to convert the list pointer to
148 * an appropriate programmer-defined type (eg. @ref GD_parent).
149 *
150 * To control the setting of this pointer,
151 * the `move_to_front` flag may be TRUE or FALSE.
152 * If `move_to_front` is TRUE, the record will be
153 * locked @ref Agtag_s.mtflock at the
154 * head of the list @ref Agobj_s.data,
155 * so it can be accessed directly by @ref AGDATA(obj).
156 *
157 * The lock protects the data pointer from being moved.
158 * Function @ref aggetrec reports error when data pointer and lock
159 * are reassigned.
160 *
161 * The lock can be released or reset by a call to @ref agdelrec.
162 *
163 */
164
165 typedef struct Agsym_s Agsym_t; ///< string attribute descriptors
166 typedef struct Agattr_s Agattr_t; ///< string attribute container
167 typedef struct Agdatadict_s Agdatadict_t; ///< set of dictionaries per graph
168 typedef struct Agrec_s Agrec_t;
169 ///< generic header of @ref Agattr_s, @ref Agdatadict_s and user records
170
171 /// implementation of @ref Agrec_t
172 struct Agrec_s {
173 char *name;
174 Agrec_t *next; ///< **circular** linked list of records
175 /* following this would be any programmer-defined data */
176 };
177 /// @}
178 /// @}
179
180 /** @defgroup cgraph_object objects
181 * @brief parent for @ref cgraph_graph, @ref cgraph_node, and @ref cgraph_edge.
182 * @ingroup cgraph_api
183 *
184 * Common parameter for functions **obj** is generic pointer
185 * to @ref Agraph_t, @ref Agnode_t, or @ref Agedge_t
186 *
187 * @ref AGDATA, @ref AGID, @ref AGTYPE, and others are macros returning
188 * the specified fields of the argument object.
189 * @{
190 */
191
192 /** @brief tag in @ref Agobj_s for graphs, nodes, and edges.
193
194 While there may be several structs
195 for a given node or edges, there is only one unique ID (per main graph). */
196
197 struct Agtag_s {
198 /// access with @ref AGTYPE
199 unsigned objtype : 2; /* see enum below */
200 unsigned mtflock : 1; ///< @brief move-to-front lock, guards @ref Agobj_s.data
201 unsigned attrwf : 1; /* attrs written (parity, write.c) */
202 unsigned seq : (sizeof(unsigned) * 8 - 4); /* sequence no. */
203 IDTYPE id; /* client ID */
204 };
205
206 /// Object tags. Can't exceed 2 bits. See Agtag_s.
207 enum { AGRAPH, AGNODE, AGEDGE, AGOUTEDGE = AGEDGE, AGINEDGE };
208
209 /// a generic header of @ref Agraph_s, @ref Agnode_s and @ref Agedge_s
210 struct Agobj_s {
211 Agtag_t tag; ///< access with @ref AGTAG
212 Agrec_t *data; ///< stores programmer-defined data, access with @ref AGDATA
213 };
214
215 #define AGTAG(obj) (((Agobj_t *)(obj))->tag)
216 #define AGTYPE(obj) (AGTAG(obj).objtype)
217 ///< @brief returns @ref AGRAPH, @ref AGNODE, or @ref AGEDGE depending
218 /// on the type of the object
219
220 #define AGID(obj) (AGTAG(obj).id)
221 ///< returns the unique integer ID associated with the object
222
223 #define AGSEQ(obj) (AGTAG(obj).seq)
224 #define AGATTRWF(obj) (AGTAG(obj).attrwf)
225 #define AGDATA(obj) (((Agobj_t *)(obj))->data)
226 ///< returns @ref Agrec_t
227
228 /// @}
229 /// @} cgraph_api
230
231 /** @defgroup cgraph_node nodes
232 * @ingroup cgraph_object
233 *
234 * A node is created by giving a unique string name or programmer
235 * defined integer ID, and is represented by a unique internal object.
236 * (%Node equality can checked by pointer comparison.)
237 *
238 * @{
239 */
240
241 /** @brief This is the node struct allocated per graph (or subgraph).
242
243 It resides in the n_dict of the graph.
244 The node set is maintained by libcdt, but transparently to libcgraph callers.
245 Every node may be given an optional string name at its time of creation,
246 or it is permissible to pass NULL for the name. */
247
248 struct Agsubnode_s { /* the node-per-graph-or-subgraph record */
249 Dtlink_t seq_link; /* must be first */
250 Dtlink_t id_link;
251 Agnode_t *node; /* the object */
252 Dtlink_t *in_id, *out_id; /* by node/ID for random access */
253 Dtlink_t *in_seq, *out_seq; /* by node/sequence for serial access */
254 };
255
256 struct Agnode_s {
257 Agobj_t base;
258 Agraph_t *root;
259 Agsubnode_t mainsub; /* embedded for main graph */
260 };
261 /// @}
262
263 /// @addtogroup cgraph_edge
264 /// @{
265 struct Agedge_s {
266 Agobj_t base;
267 Dtlink_t id_link; /* main graph only */
268 Dtlink_t seq_link;
269 Agnode_t *node; /* the endpoint node */
270 };
271
272 struct Agedgepair_s {
273 Agedge_t out, in;
274 };
275 /// @}
276
277 /// @addtogroup cgraph_graph
278 /// @{
279
280 /// graph descriptor
281 struct Agdesc_s { /* graph descriptor */
282 unsigned directed : 1; /* if edges are asymmetric */
283 unsigned strict : 1; /* if multi-edges forbidden */
284 unsigned no_loop : 1; /* if no loops */
285 unsigned maingraph : 1; /* if this is the top level graph */
286 unsigned no_write : 1; /* if a temporary subgraph */
287 unsigned has_attrs : 1; /* if string attr tables should be initialized */
288 unsigned has_cmpnd : 1; /* if may contain collapsed nodes */
289 };
290 /// @}
291
292 /** @defgroup cgraph_disc disciplines
293 * @ingroup cgraph_misc
294 * @brief disciplines for external resources needed by libcgraph
295 *
296 * (This section is not intended for casual users.)
297 *
298 * Programmer-defined disciplines customize certain resources:
299 * ID namespace and I/O - needed by Libcgraph.
300 * A discipline struct (or NULL) is passed at graph creation time.
301 * @{
302 */
303
304 /**
305 * @brief object ID allocator discipline
306 *
307 * An ID allocator discipline allows a client to control assignment
308 * of IDs (uninterpreted integer values) to objects, and possibly how
309 * they are mapped to and from strings.
310 */
311
312 /// object ID allocator
313 struct Agiddisc_s {
314 void *(*open)(Agraph_t *g, Agdisc_t *); /* associated with a graph */
315 long (*map)(void *state, int objtype, char *str, IDTYPE *id, int createflag);
316 void (*free)(void *state, int objtype, IDTYPE id);
317 char *(*print)(void *state, int objtype, IDTYPE id);
318 void (*close)(void *state);
319 void (*idregister)(void *state, int objtype, void *obj);
320 };
321
322 /// IO services
323 struct Agiodisc_s {
324 int (*afread)(void *chan, char *buf, int bufsize);
325 int (*putstr)(void *chan, const char *str);
326 int (*flush)(void *chan); /* sync */
327 /* error messages? */
328 };
329
330 /// @brief user's discipline
331 ///
332 /// A default discipline is supplied when NULL is given for any of these fields.
333 struct Agdisc_s {
334 Agiddisc_t *id;
335 Agiodisc_t *io;
336 };
337
338 /* default resource disciplines */
339
340 CGRAPH_API extern Agiddisc_t AgIdDisc;
341 CGRAPH_API extern Agiodisc_t AgIoDisc;
342
343 CGRAPH_API extern Agdisc_t AgDefaultDisc;
344 /// @}
345
346 /** @defgroup cgraph_graph graphs
347 * @ingroup cgraph_object
348 *
349 * The functions @ref agisdirected, @ref agisundirected,
350 * @ref agisstrict, and @ref agissimple
351 * can be used to query if a graph is directed, undirected,
352 * strict (at most one edge with a given tail and head),
353 * or simple (strict with no loops), respectively.
354 *
355 * @{
356 */
357
358 /// client state (closures)
359 struct Agdstate_s {
360 void *id;
361 /* IO must be initialized and finalized outside Cgraph,
362 * and channels (FILES) are passed as void* arguments. */
363 };
364
365 typedef void (*agobjfn_t)(Agraph_t *g, Agobj_t *obj, void *arg);
366 typedef void (*agobjupdfn_t)(Agraph_t *g, Agobj_t *obj, void *arg,
367 Agsym_t *sym);
368
369 /** @defgroup cgraph_callback callbacks
370 * @brief virtual methods of initialization, modification, and finalization of
371 * graph objects
372 *
373 * An @ref Agcbdisc_t defines callbacks to be invoked by Libcgraph when
374 * initializing, modifying, or finalizing graph objects.
375 * Disciplines are kept on a stack.
376 * Libcgraph automatically calls the methods on the stack, top-down.
377 * Callbacks are installed with @ref agpushdisc, uninstalled with @ref
378 * agpopdisc.
379 *
380 * @{
381 */
382
383 /// client event callbacks, used in Agcbstack_s
384 struct Agcbdisc_s {
385 struct {
386 agobjfn_t ins;
387 agobjupdfn_t mod;
388 agobjfn_t del;
389 } graph, node, edge;
390 };
391
392 /// object event callbacks
393
394 /// enclosing state for Agcbdisc_s, used in Agclos_s
395 struct Agcbstack_s {
396 Agcbdisc_t *f; /* methods */
397 void *state; /* closure */
398 Agcbstack_t *prev; /* kept in a stack, unlike other disciplines */
399 };
400
401 CGRAPH_API void agpushdisc(Agraph_t *g, Agcbdisc_t *disc, void *state);
402 CGRAPH_API int agpopdisc(Agraph_t *g, Agcbdisc_t *disc);
403
404 /// @}
405
406 /// shared resources for Agraph_s
407 struct Agclos_s {
408 Agdisc_t disc; /* resource discipline functions */
409 Agdstate_t state; /* resource closures */
410 void *strdict; ///< shared string dict
411 uint64_t seq[3]; /* local object sequence number counter */
412 Agcbstack_t *cb; /* user and system callback function stacks */
413 Dict_t *lookup_by_name[3];
414 Dict_t *lookup_by_id[3];
415 };
416
417 /// opaque type; the definition of this is internal to Graphviz
418 struct graphviz_node_set;
419
420 /// graph or subgraph
421 struct Agraph_s {
422 Agobj_t base;
423 Agdesc_t desc;
424 Dtlink_t seq_link;
425 Dtlink_t id_link;
426 Dict_t *n_seq; ///< the node set in sequence
427 struct graphviz_node_set *n_id; ///< the node set indexed by ID
428 Dict_t *e_seq, *e_id; ///< holders for edge sets
429 Dict_t *g_seq, *g_id; ///< subgraphs - descendants
430 Agraph_t *parent, *root; ///< subgraphs - ancestors
431 Agclos_t *clos; ///< shared resources
432 };
433
434 /* graphs */
435 CGRAPH_API Agraph_t *agopen(char *name, Agdesc_t desc, Agdisc_t *disc);
436 /**<
437 * @brief creates a new graph with the given name and kind
438 *
439 * @param desc - graph kind, can be @ref Agdirected, @ref Agundirected,
440 * @ref Agstrictdirected or @ref Agstrictundirected.
441 * A strict graph cannot have multi-edges or self-arcs.
442 *
443 * @param disc - discipline structure which can be used
444 * to tailor I/O and ID allocation. Typically, a NULL
445 * value will be used to indicate the default discipline @ref AgDefaultDisc.
446 */
447
448 CGRAPH_API int agclose(Agraph_t *g);
449 ///< deletes a graph, freeing its associated storage
450 CGRAPH_API Agraph_t *agread(void *chan, Agdisc_t *disc);
451 ///< constructs a new graph
452
453 CGRAPH_API Agraph_t *agmemread(const char *cp);
454 ///< reads a graph from the input string
455
456 CGRAPH_API Agraph_t *agmemconcat(Agraph_t *g, const char *cp);
457
458 CGRAPH_API Agraph_t *agconcat(Agraph_t *g, const char *filename, void *chan,
459 Agdisc_t *disc);
460 /**< @brief merges the file contents with a pre-existing graph
461 *
462 * Though I/O methods may be overridden, the default is that
463 * the channel argument is a stdio FILE pointer.
464 * In that case, if any of the streams are wide-oriented,
465 * the behavior is undefined.
466 *
467 * @param filename Path of the input file source, used only for diagnostics
468 */
469
470 CGRAPH_API int agwrite(Agraph_t *g, void *chan);
471 CGRAPH_API int agisdirected(Agraph_t *g);
472 CGRAPH_API int agisundirected(Agraph_t *g);
473 CGRAPH_API int agisstrict(Agraph_t *g);
474 CGRAPH_API int agissimple(Agraph_t *g);
475 /// @}
476
477 /// @addtogroup cgraph_node
478 /// @{
479 CGRAPH_API Agnode_t *agnode(Agraph_t *g, char *name, int createflag);
480 CGRAPH_API Agnode_t *agidnode(Agraph_t *g, IDTYPE id, int createflag);
481 CGRAPH_API Agnode_t *agsubnode(Agraph_t *g, Agnode_t *n, int createflag);
482 CGRAPH_API Agnode_t *agfstnode(Agraph_t *g);
483 CGRAPH_API Agnode_t *agnxtnode(Agraph_t *g, Agnode_t *n);
484 CGRAPH_API Agnode_t *aglstnode(Agraph_t *g);
485 CGRAPH_API Agnode_t *agprvnode(Agraph_t *g, Agnode_t *n);
486
487 CGRAPH_API Agsubnode_t *agsubrep(Agraph_t *g, Agnode_t *n);
488 CGRAPH_API int agnodebefore(Agnode_t *u, Agnode_t *v); /* we have no shame */
489 CGRAPH_API int agdelnode(Agraph_t *g, Agnode_t *arg_n);
490 ///< removes a node from a graph or subgraph.
491 CGRAPH_API int agrelabel_node(Agnode_t *n, char *newname);
492 /// @}
493
494 /** @defgroup cgraph_edge edges
495 * @ingroup cgraph_object
496 *
497 * An abstract edge has two endpoint nodes called tail and head
498 * where all outedges of the same node have it as the tail
499 * value and similarly all inedges have it as the head.
500 * In an undirected graph, head and tail are interchangeable.
501 * If a graph has multi-edges between the same pair of nodes,
502 * the edge's string name behaves as a secondary key.
503 *
504 * Note that an abstract edge has two distinct concrete
505 * representations: as an in-edge and as an out-edge.
506 * In particular, the pointer as an out-edge is different
507 * from the pointer as an in-edge.
508 * The function @ref ageqedge canonicalizes the pointers before
509 * doing a comparison and so can be used to test edge equality.
510 * The sense of an edge can be flipped using @ref agopp.
511 *
512 * @{
513 */
514
515 CGRAPH_API Agedge_t *agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name,
516 int createflag);
517 CGRAPH_API Agedge_t *agidedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, IDTYPE id,
518 int createflag);
519 CGRAPH_API Agedge_t *agsubedge(Agraph_t *g, Agedge_t *e, int createflag);
520 CGRAPH_API Agedge_t *agfstin(Agraph_t *g, Agnode_t *n);
521 CGRAPH_API Agedge_t *agnxtin(Agraph_t *g, Agedge_t *e);
522 CGRAPH_API Agedge_t *agfstout(Agraph_t *g, Agnode_t *n);
523 CGRAPH_API Agedge_t *agnxtout(Agraph_t *g, Agedge_t *e);
524 CGRAPH_API Agedge_t *agfstedge(Agraph_t *g, Agnode_t *n);
525 CGRAPH_API Agedge_t *agnxtedge(Agraph_t *g, Agedge_t *e, Agnode_t *n);
526 CGRAPH_API int agdeledge(Agraph_t *g, Agedge_t *arg_e);
527 /// @}
528
529 /// @addtogroup cgraph_object
530 /// @{
531
532 // Generic object (graphs, nodes and edges) functions
533
534 CGRAPH_API Agraph_t *agraphof(void *obj);
535 CGRAPH_API Agraph_t *agroot(void *obj);
536 ///< takes any graph object (graph, subgraph, node, edge) and returns the root
537 ///< graph in which it lives
538
539 CGRAPH_API int agcontains(Agraph_t *, void *obj);
540 ///< returns non-zero if **obj** is a member of (sub)graph
541
542 CGRAPH_API char *agnameof(void *);
543 ///< returns a string descriptor for the object.
544
545 CGRAPH_API int agdelete(Agraph_t *g, void *obj);
546 /**< @brief deletes object.
547 * Equivalent to @ref agclose, @ref agdelnode, and @ref agdeledge
548 * for **obj** being a graph, node or edge, respectively.
549 * @returns -1 if **obj** does not belong to graph **g**.
550 */
551
552 CGRAPH_API int agobjkind(void *obj);
553 ///< returns @ref AGRAPH, @ref AGNODE, or @ref AGEDGE depending on the type of
554 ///< the object. Synonym for @ref AGTYPE.
555 /// @}
556
557 /** @defgroup cgraph_string string utilities
558 * @brief reference-counted strings
559 * @ingroup cgraph_misc
560 *
561 * Storage management of strings as reference-counted strings.
562 * The caller does not need to dynamically allocate storage.
563 *
564 * All uses of cgraph strings need to be freed using @ref agstrfree
565 * in order to correctly maintain the reference count.
566 *
567 * @{
568 */
569 CGRAPH_API char *agstrdup(Agraph_t *, const char *);
570 ///< @brief returns a pointer to a reference-counted copy of the argument
571 ///< string, creating one if necessary
572 ///
573 /// Use of this function should be avoided where possible. It is not possible to
574 /// explicitly indicate whether the caller is trying to create a regular text
575 /// string or an HTML-like string. It is better to be explicit with your intent
576 /// and instead call either @ref agstrdup_text or @ref agstrdup_html.
577
578 CGRAPH_API char *agstrdup_text(Agraph_t *, const char *);
579 ///< @brief returns a pointer to a reference-counted regular text copy of the
580 ///< argument string, creating one if necessary
581
582 CGRAPH_API char *agstrdup_html(Agraph_t *, const char *);
583 ///< @brief returns a pointer to a reference-counted HTML-like copy of the
584 ///< argument string, creating one if necessary
585
586 CGRAPH_API int aghtmlstr(const char *);
587 ///< query if a string is an ordinary string or an HTML-like string
588 ///
589 CGRAPH_API char *agstrbind(Agraph_t *g, const char *);
590 ///< returns a pointer to a reference-counted string if it exists, or NULL if
591 ///< not
592 CGRAPH_API char *agstrbind_text(Agraph_t *g, const char *);
593 ///< returns a pointer to a reference-counted regular text string if it exists,
594 ///< or NULL if not
595 CGRAPH_API char *agstrbind_html(Agraph_t *g, const char *);
596 ///< returns a pointer to a reference-counted HTML-like string if it exists, or
597 ///< NULL if not
598
599 CGRAPH_API int agstrfree(Agraph_t *, const char *, bool is_html);
600 ///< @param is_html Is the string being freed an HTML-like string?
601 CGRAPH_API char *agstrcanon(char *, char *);
602 /// @}
603
604 /** @defgroup cgraph_attr attributes
605 * @brief symbols, and @ref cgraph_rec
606 * @ingroup cgraph_api
607 *
608 * Programmer-defined values may be dynamically
609 * attached to graphs, subgraphs, nodes, and edges.
610 * Such values are either character string data (see @ref agattr_text,
611 * @ref agattr_html, and @ref agattr) (for I/O) or uninterpreted binary
612 * @ref cgraph_rec (for implementing algorithms efficiently).
613 *
614 * *String attributes* are handled automatically in reading and writing graph
615 * files. A string attribute is identified by name and by an internal symbol
616 * table entry (@ref Agsym_t) created by Libcgraph. Attributes of nodes, edges,
617 * and graphs (with their subgraphs) have separate namespaces. The contents of
618 * an @ref Agsym_t have a char* *name* for the attribute's name, a char*
619 * *defval* field for the attribute's default value, and an int *id* field
620 * containing the index of the attribute's specific value for an object in the
621 * object's array of attribute values.
622 *
623 * @{
624 */
625
626 // definitions for dynamic string attributes
627
628 /// string attribute container
629 struct Agattr_s { /* dynamic string attributes */
630 Agrec_t h; /* common data header */
631 Dict_t *dict; ///< shared dict of Agsym_s to interpret Agattr_s.str
632 char **str; ///< the attribute string values indexed by Agsym_s.id
633 };
634
635 /// @brief string attribute descriptor
636 /// symbol in Agattr_s.dict
637 struct Agsym_s {
638 Dtlink_t link;
639 char *name; /* attribute's name */
640 char *defval; /* its default value for initialization */
641 int id; ///< index in Agattr_s.str
642 unsigned char kind; /* referent object type */
643 unsigned char fixed; /* immutable value */
644 unsigned char print; /* always print */
645 Agraph_t *owner; ///< graph from whose string pool `name` and `defval` were
646 ///< allocated
647 };
648
649 struct Agdatadict_s { ///< set of dictionaries per graph
650 Agrec_t h; /* installed in list of graph recs */
651 struct {
652 Dict_t *n, *e, *g;
653 } dict;
654 };
655
656 CGRAPH_API Agsym_t *agattr_text(Agraph_t *g, int kind, char *name,
657 const char *value);
658 /**< @brief creates or looks up text attributes of a graph
659 *
660 * HTML-like attributes cannot be created or looked up with this function. See
661 * @ref agattr_html for that.
662 *
663 * @param g graph. When is NULL, the default is set for all graphs created
664 * subsequently.
665 * @param kind may be @ref AGRAPH, @ref AGNODE, or @ref AGEDGE.
666 * @param value default value. When is (char*)0, the request is to search
667 * for an existing attribute of the given kind and name.
668 *
669 * If the attribute already exists, its default
670 * for creating new objects is set to the given **value**;
671 * if it does not exist, a new attribute is created with the
672 * given default **value**, and the default is applied to all pre-existing
673 * objects of the given **kind**
674 */
675
676 CGRAPH_API Agsym_t *agattr_html(Agraph_t *g, int kind, char *name,
677 const char *value);
678 ///< @brief `agattr_text`, but creates HTML-like values
679 ///
680 /// Regular text attributes cannot be created or looked up with this function.
681 /// See @ref agattr_text for that.
682 ///
683 /// @param g Graph. When `g` is `NULL`, the default is set for all graphs
684 /// created subsequently.
685 /// @param kind May be @ref AGRAPH, @ref AGNODE, or @ref AGEDGE.
686 /// @param value Default value. When `value` is `NULL`, the request is to search
687 /// for an existing attribute of the given kind and name.
688 ///
689 /// If the attribute already exists, its default for creating new objects is set
690 /// to the given `value`; if it does not exist, a new attribute is created with
691 /// the given default `value`, and the default is applied to all pre-existing
692 /// objects of the given `kind`.
693
694 CGRAPH_API Agsym_t *agattr(Agraph_t *g, int kind, char *name,
695 const char *value);
696 ///< @brief creates or looks up an attribute, without specifying desired form
697 ///
698 /// Use of this function should be avoided where possible. It is not possible to
699 /// explicitly indicate whether the caller is trying to create/lookup a regular
700 /// text attribute or an HTML-like attribute. It is better to be explicit with
701 /// your intent and instead call either @ref agattr_text or @ref agattr_html.
702 ///
703 /// This function has the following behavior:
704 /// 1. If the `value` passed was obtained from `agstrdup_html`, an HTML-like
705 /// attribute value is created/looked up. That is, the behavior is
706 /// equivalent to a call to @ref agattr_html.
707 /// 2. Otherwise, a regular text attribute value is created/looked up.
708 ///
709 /// @param g graph. When is NULL, the default is set for all graphs created
710 /// subsequently.
711 /// @param kind may be @ref AGRAPH, @ref AGNODE, or @ref AGEDGE.
712 /// @param value default value. When is @ref NULL, the request is to search for
713 /// for an existing attribute of the given kind and name.
714
715 CGRAPH_API Agsym_t *agattrsym(void *obj, char *name);
716 ///< looks up a string attribute for a graph object given as an argument
717
718 CGRAPH_API Agsym_t *agnxtattr(Agraph_t *g, int kind, Agsym_t *attr);
719 ///< @brief permits traversing the list of attributes of a given type
720 /// @param attr if `NULL` the function returns the first attribute
721 /// @returns the next one in succession or `NULL` at the end of the list.
722
723 CGRAPH_API int agcopyattr(void *oldobj, void *newobj);
724 /**< @brief copies all of the attributes from one object to another
725 * @return fails and returns non-zero if argument objects are different kinds,
726 * or if all of the attributes of the source object have not been declared
727 * for the target object
728 */
729
730 /// @addtogroup cgraph_rec
731 /// @{
732 CGRAPH_API void *agbindrec(void *obj, const char *name, unsigned int recsize,
733 int move_to_front);
734 ///< @brief attaches a new record of the given size to the object
735 /// @param recsize if 0, the call to @ref agbindrec is simply a lookup
736 /// @returns pointer to `Agrec_t` and user data
737
738 CGRAPH_API Agrec_t *aggetrec(void *obj, const char *name, int move_to_front);
739 ///< find record in circular list and do optional move-to-front and lock
740
741 CGRAPH_API int agdelrec(void *obj, const char *name);
742 ///< deletes a named record from one object
743
744 CGRAPH_API void aginit(Agraph_t *g, int kind, const char *rec_name,
745 int rec_size, int move_to_front);
746 /**< @brief attach new records to objects of specified kind
747 *
748 * @param kind may be @ref AGRAPH, @ref AGNODE, or @ref AGEDGE
749 * @param rec_size if is negative (of the actual rec_size) for graphs,
750 * @ref aginit is applied recursively to the graph and its subgraphs
751 */
752
753 CGRAPH_API void agclean(Agraph_t *g, int kind, char *rec_name);
754 ///< @brief calls @ref agdelrec for all objects
755 /// of the same class in an entire graph
756
757 /// @}
758
759 CGRAPH_API char *agget(void *obj, char *name);
760 CGRAPH_API char *agxget(void *obj, Agsym_t *sym);
761 CGRAPH_API int agset(void *obj, char *name, const char *value);
762 CGRAPH_API int agset_text(void *obj, char *name, const char *value);
763 CGRAPH_API int agset_html(void *obj, char *name, const char *value);
764 CGRAPH_API int agxset(void *obj, Agsym_t *sym, const char *value);
765 CGRAPH_API int agxset_text(void *obj, Agsym_t *sym, const char *value);
766 CGRAPH_API int agxset_html(void *obj, Agsym_t *sym, const char *value);
767
768 CGRAPH_API int agsafeset_text(void *obj, char *name, const char *value,
769 const char *def);
770 ///< @brief set an attribute’s value and default, ensuring it is declared before
771 /// setting it locally
772 ///
773 /// The attribue set by this function is a regular text attribute. See
774 /// @ref agsafeset_html for the equivalent for an HTML-like attribute.
775 ///
776 /// @param obj Object on which to set the attribute
777 /// @param name Name of the attribute to set
778 /// @param value Value of the attribute to set
779 /// @param def Optional default to declare for the attribute
780
781 CGRAPH_API int agsafeset_html(void *obj, char *name, const char *value,
782 const char *def);
783 ///< @brief set an attribute’s value and default, ensuring it is declared before
784 /// setting it locally
785 ///
786 /// The attribue set by this function is an HTML-like attribute. See
787 /// @ref agsafeset_text for the equivalent for a regular text attribute.
788 ///
789 /// @param obj Object on which to set the attribute
790 /// @param name Name of the attribute to set
791 /// @param value Value of the attribute to set
792 /// @param def Optional default to declare for the attribute
793
794 CGRAPH_API int agsafeset(void *obj, char *name, const char *value,
795 const char *def);
796 ///< @brief set an attribute’s value and default, ensuring it is declared before
797 /// setting it locally
798 ///
799 /// Use of this function should be avoided where possible. It is not possible to
800 /// explicitly indicate whether the caller is trying to create/lookup a regular
801 /// text attribute or an HTML-like attribute. It is better to be explicit with
802 /// your intent and instead call either @ref agsafeset_text or
803 /// @ref agsafeset_html.
804 ///
805 /// This function has the following behavior:
806 /// 1. If the attribute needs to be created (it did not already exist) and
807 /// `def` was obtained from `agstrdup_html`, an HTML-like default attribute
808 /// value is created.
809 /// 2. If the attribute needs to be created (it did not already exist) and
810 /// `def` was not obtained from `agstrdup_html`, a regular text default
811 /// attribute value is created.
812 /// … then …
813 /// 1. If the `value` passed was obtained from `agstrdup_html`, an HTML-like
814 /// attribute value is created/looked up. That is, the behavior is
815 /// equivalent to a call to @ref agsafeset_html.
816 /// 2. Otherwise, a regular text attribute value is created/looked up. That
817 /// is, the behavior is equivalent to a call to @ref agsafeset_text.
818 ///
819 /// @param obj Object on which to set the attribute
820 /// @param name Name of the attribute to set
821 /// @param value Value of the attribute to set
822 /// @param def Optional default to declare for the attribute
823
824 /// @}
825
826 /** @defgroup cgraph_subgraph subgraphs
827 * @ingroup cgraph_graph
828 *
829 * A "main" or "root" graph defines a namespace for a collection of
830 * graph objects (subgraphs, nodes, edges) and their attributes.
831 * Objects may be named by unique strings or by integer IDs.
832 *
833 * @ref agsubg finds or creates a subgraph by name.
834 *
835 * @ref agidsubg allows a programmer to specify the subgraph by a unique integer
836 * ID.
837 *
838 * A new subgraph is initially empty and is of the same kind as its parent.
839 * Nested subgraph trees may be created.
840 * A subgraph's name is only interpreted relative to its parent.
841 *
842 * A program can scan subgraphs under a given graph
843 * using @ref agfstsubg and @ref agnxtsubg.
844 *
845 * A subgraph is deleted with @ref agdelsubg (or @ref agclose).
846 *
847 * The @ref agparent function returns the immediate parent graph of a subgraph,
848 * or itself if the graph is already a root graph.
849 *
850 * @{
851 */
852
853 CGRAPH_API Agraph_t *agsubg(Agraph_t *g, char *name,
854 int cflag); /* constructor */
855 CGRAPH_API Agraph_t *agidsubg(Agraph_t *g, IDTYPE id); ///< constructor
856 CGRAPH_API Agraph_t *agfstsubg(Agraph_t *g);
857 CGRAPH_API Agraph_t *agnxtsubg(Agraph_t *subg);
858 CGRAPH_API Agraph_t *agparent(Agraph_t *g);
859 CGRAPH_API int agdelsubg(Agraph_t *g, Agraph_t *sub); /* could be agclose */
860 /// @}
861
862 /** @defgroup cgraph_misc miscellaneous
863 * @ingroup cgraph_api
864 * @{
865 */
866
867 /** @defgroup card set cardinality
868 *
869 * By default, nodes are stored in ordered sets for
870 * efficient random access to insert, find, and delete nodes.
871 *
872 * @ref agnnodes, @ref agnedges, and @ref agnsubg return the
873 * sizes of node, edge and subgraph sets of a graph.
874 *
875 * The function @ref agdegree returns the size of a node’s edge set,
876 * and takes flags to select in-edges, out-edges, or both.
877 *
878 * The function @ref agcountuniqedges returns
879 * the size of a node’s edge set, and takes flags
880 * to select in-edges, out-edges, or both.
881 * Unlike @ref agdegree, each loop is only counted once.
882 *
883 * @{
884 */
885 CGRAPH_API int agnnodes(Agraph_t *g);
886 CGRAPH_API int agnedges(Agraph_t *g);
887 CGRAPH_API int agnsubg(Agraph_t *g);
888 CGRAPH_API int agdegree(Agraph_t *g, Agnode_t *n, int in, int out);
889 CGRAPH_API int agcountuniqedges(Agraph_t *g, Agnode_t *n, int in, int out);
890 /// @}
891
892 /// @cond
893
894 /* support for extra API misuse warnings if available */
895 #ifdef __GNUC__
896 #define PRINTF_LIKE(index, first) __attribute__((format(printf, index, first)))
897 #else
898 #define PRINTF_LIKE(index, first) /* nothing */
899 #endif
900
901 /// @endcond
902
903 /** @defgroup cgraph_err error handling
904 *
905 * The library provides a variety of mechanisms to control
906 * the reporting of errors and warnings.
907 * A message is only written if its type has higher priority than
908 * a programmer-controlled minimum, which is @ref AGWARN by default.
909 * The programmer can set this value using @ref agseterr,
910 * which returns the previous value.
911 * Calling `agseterr(AGMAX)` turns off the writing of messages.
912 *
913 * The function @ref agerr is the main entry point for reporting an anomaly.
914 * The first argument indicates the type of message.
915 * Usually, the first argument is @ref AGWARN or @ref AGERR
916 * to indicate warnings and errors, respectively.
917 * Sometimes additional context information is only available in functions
918 * calling the function where the error is actually caught.
919 * In this case, the calling function can indicate that it is continuing
920 * the current error by using @ref AGPREV as the first argument.
921 * The remaining arguments to @ref agerr are the same as
922 * the arguments to `printf`.
923 *
924 * The functions @ref agwarningf and @ref agerrorf are shorthand for
925 * `agerr(AGWARN,...)` and `agerr(AGERR,...)`, respectively.
926 *
927 * Some applications desire to directly control the writing of messages.
928 * Such an application can use the function @ref agseterrf to register
929 * the function that the library should call to actually write the message.
930 * The previous error function is returned.
931 * By default, the message is written to `stderr`.
932 *
933 * Errors not written are stored in a log file.
934 * The last recorded error can be retrieved by calling @ref aglasterr.
935 * Unless the printing of error messages has been completely disabled
936 * by a call to `agseterr(AGMAX)`, standard error must not be wide-oriented,
937 * even if a user-provided error printing function is provided.
938 *
939 * The function @ref agerrors returns non-zero if errors have been reported.
940 *
941 * @{
942 */
943 typedef enum { AGWARN, AGERR, AGMAX, AGPREV } agerrlevel_t;
944 typedef int (*agusererrf)(char *);
945 CGRAPH_API agerrlevel_t agseterr(agerrlevel_t);
946 CGRAPH_API char *aglasterr(void);
947 CGRAPH_API int agerr(agerrlevel_t level, const char *fmt, ...)
948 PRINTF_LIKE(2, 3);
949 CGRAPH_API void agerrorf(const char *fmt, ...) PRINTF_LIKE(1, 2);
950 CGRAPH_API void agwarningf(const char *fmt, ...) PRINTF_LIKE(1, 2);
951 CGRAPH_API int agerrors(void);
952 CGRAPH_API int agreseterrors(void);
953 CGRAPH_API agusererrf agseterrf(agusererrf);
954 /// @}
955
956 #undef PRINTF_LIKE
957 /// @}
958
959 /// @addtogroup cgraph_edge
960 /// @{
961 /* data access macros */
962 /* this assumes that e[0] is out and e[1] is inedge, see @ref Agedgepair_s */
963 #define AGIN2OUT(inedge) ((inedge) - 1) ///< Agedgepair_s.in -> Agedgepair_s.out
964 #define AGOUT2IN(outedge) \
965 ((outedge) + 1) ///< Agedgepair_s.out -> Agedgepair_s.in
966 #define AGOPP(e) ((AGTYPE(e) == AGINEDGE) ? AGIN2OUT(e) : AGOUT2IN(e))
967 #define AGMKOUT(e) (AGTYPE(e) == AGOUTEDGE ? (e) : AGIN2OUT(e))
968 #define AGMKIN(e) (AGTYPE(e) == AGINEDGE ? (e) : AGOUT2IN(e))
969 #define AGTAIL(e) (AGMKIN(e)->node)
970 #define AGHEAD(e) (AGMKOUT(e)->node)
971 #define AGEQEDGE(e, f) (AGMKOUT(e) == AGMKOUT(f))
972 /* These macros are also exposed as functions, so they can be linked against. */
973 #define agtail(e) AGTAIL(e)
974 #define aghead(e) AGHEAD(e)
975 #define agopp(e) \
976 AGOPP(e) ///< opposite edge: flip Agedgepair_s.out ⇄ Agedgepair_s.in
977 #define ageqedge(e, f) AGEQEDGE(e, f) ///< edges are equal
978
979 #define TAILPORT_ID "tailport"
980 #define HEADPORT_ID "headport"
981 /// @}
982
983 /// @addtogroup cgraph_graph
984 /// @{
985 CGRAPH_API extern Agdesc_t Agdirected; ///< directed
986 CGRAPH_API extern Agdesc_t Agstrictdirected;
987 ///< strict directed. A strict graph cannot have multi-edges or self-arcs.
988 CGRAPH_API extern Agdesc_t Agundirected; ///< undirected
989 CGRAPH_API extern Agdesc_t Agstrictundirected; ///< strict undirected
990 /// @}
991
992 /** @defgroup cgraph_fast fast graphs
993 * @ingroup cgraph_misc
994 * @{
995 */
996
997 /* this is expedient but a bit slimey because it "knows" that dict entries of
998 both nodes and edges are embedded in main graph objects but allocated separately
999 in subgraphs */
1000 #define AGSNMAIN(sn) ((sn) == (&((sn)->node->mainsub)))
1001 /// @}
1002
1003 /// @addtogroup cgraph_app
1004 /// @{
1005
1006 /// options for passing to `graphviz_acyclic`
1007 typedef struct {
1008 FILE *outFile;
1009 bool doWrite;
1010 bool Verbose;
1011 } graphviz_acyclic_options_t;
1012
1013 /// programmatic access to `acyclic`
1014 ///
1015 /// See `man acyclic` for an explanation of the `acyclic` tool.
1016 ///
1017 /// \param g Graph to operate on
1018 /// \param opts Options to control acyclic algorithm
1019 /// \param num_rev [inout] Running total of reversed edges
1020 /// \return True if a cycle was found, indicating failure
1021 CGRAPH_API bool graphviz_acyclic(Agraph_t *g,
1022 const graphviz_acyclic_options_t *opts,
1023 size_t *num_rev);
1024
1025 /// options for passing to `graphviz_tred`
1026 typedef struct {
1027 bool Verbose;
1028 bool PrintRemovedEdges;
1029 FILE *out; ///< stream to write result(s) to
1030 FILE *err; ///< stream to print warnings to
1031 } graphviz_tred_options_t;
1032
1033 /// @brief programmatic access to `tred` -
1034 /// [transitive reduction](https://en.wikipedia.org/wiki/Transitive_reduction)
1035 ///
1036 /// See `man tred` for an explanation of the `tred` tool.
1037 ///
1038 /// \param g Graph to operate on
1039 /// \param opts Options to control tred algorithm
1040 CGRAPH_API void graphviz_tred(Agraph_t *g, const graphviz_tred_options_t *opts);
1041
1042 /// options for passing to `graphviz_unflatten`
1043 typedef struct {
1044 bool Do_fans;
1045 int MaxMinlen;
1046 int ChainLimit;
1047 } graphviz_unflatten_options_t;
1048
1049 /// programmatic access to `unflatten`
1050 ///
1051 /// See `man unflatten` for an explanation of the `unflatten` tool.
1052 ///
1053 /// \param g Graph to operate on
1054 /// \param opts Options to control unflattening
1055 CGRAPH_API void graphviz_unflatten(Agraph_t *g,
1056 const graphviz_unflatten_options_t *opts);
1057
1058 /** add to a graph any edges with both endpoints within that graph
1059 *
1060 * If `edgeset` is given as `NULL`, edges from the root graph of `g` will be
1061 * considered. In this case if `g` itself is the root graph, this call is a
1062 * no-op.
1063 *
1064 * If `g` is a connected component, the edges added will be all edges attached
1065 * to any node in `g`.
1066 *
1067 * \param g Graph to add edges to
1068 * \param edgeset Graph whose edges to consider
1069 * \return Number of edges added
1070 */
1071 CGRAPH_API size_t graphviz_node_induce(Agraph_t *g, Agraph_t *edgeset);
1072 /// @}
1073
1074 #ifdef __cplusplus
1075 }
1076 #endif