Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
apply.c
Go to the documentation of this file.
1
5/*************************************************************************
6 * Copyright (c) 2011 AT&T Intellectual Property
7 * All rights reserved. This program and the accompanying materials
8 * are made available under the terms of the Eclipse Public License v1.0
9 * which accompanies this distribution, and is available at
10 * https://www.eclipse.org/legal/epl-v10.html
11 *
12 * Contributors: Details at https://graphviz.org
13 *************************************************************************/
14
15#include <cgraph/cghdr.h>
16#include <stdbool.h>
17
18/* The following functions take a graph and a template (node/edge/graph)
19 * and return the object representing the template within the local graph.
20 */
22{
23 if (agraphof(n) == sub)
24 return n;
25 return (Agobj_t *) agsubnode(sub, (Agnode_t *) n, 0);
26}
27
29{
30 if (agraphof(e) == sub)
31 return e;
32 return (Agobj_t *) agsubedge(sub, (Agedge_t *) e, 0);
33}
34
36{
37 (void)g;
38 return (Agobj_t *) sub;
39}
40
41/* recursively apply objfn within the hierarchy of a graph.
42 * if obj is a node or edge, it and its images in every subg are visited.
43 * if obj is a graph, then it and its subgs are visited.
44 */
45static void rec_apply(Agraph_t * g, Agobj_t * obj, agobjfn_t fn, void *arg,
46 agobjsearchfn_t objsearch, bool preorder) {
48 Agobj_t *subobj;
49
50 if (preorder)
51 fn(g, obj, arg);
52 for (sub = agfstsubg(g); sub; sub = agnxtsubg(sub)) {
53 if ((subobj = objsearch(sub, obj)))
54 rec_apply(sub, subobj, fn, arg, objsearch, preorder);
55 }
56 if (!preorder)
57 fn(g, obj, arg);
58}
59
60int agapply(Agraph_t * g, Agobj_t * obj, agobjfn_t fn, void *arg,
61 int preorder)
62{
63 Agobj_t *subobj;
64
65 agobjsearchfn_t objsearch;
66 switch (AGTYPE(obj)) {
67 case AGRAPH:
68 objsearch = subgraph_search;
69 break;
70 case AGNODE:
71 objsearch = subnode_search;
72 break;
73 case AGOUTEDGE:
74 case AGINEDGE:
75 objsearch = subedge_search;
76 break;
77 default:
78 agerrorf("agapply: unknown object type %d\n", AGTYPE(obj));
79 return FAILURE;
80 }
81 if ((subobj = objsearch(g, obj))) {
82 rec_apply(g, subobj, fn, arg, objsearch, preorder != 0);
83 return SUCCESS;
84 } else
85 return FAILURE;
86}
static void rec_apply(Agraph_t *g, Agobj_t *obj, agobjfn_t fn, void *arg, agobjsearchfn_t objsearch, bool preorder)
Definition apply.c:45
int agapply(Agraph_t *g, Agobj_t *obj, agobjfn_t fn, void *arg, int preorder)
Definition apply.c:60
static Agobj_t * subgraph_search(Agraph_t *sub, Agobj_t *g)
Definition apply.c:35
static Agobj_t * subedge_search(Agraph_t *sub, Agobj_t *e)
Definition apply.c:28
static Agobj_t * subnode_search(Agraph_t *sub, Agobj_t *n)
Definition apply.c:21
cgraph.h additions
#define FAILURE
Definition cghdr.h:45
#define SUCCESS
Definition cghdr.h:44
Agobj_t *(* agobjsearchfn_t)(Agraph_t *g, Agobj_t *obj)
Definition cghdr.h:52
#define sub(h, i)
Definition closest.c:67
Agedge_t * agsubedge(Agraph_t *g, Agedge_t *e, int createflag)
Definition edge.c:355
void agerrorf(const char *fmt,...)
Definition agerror.c:165
void(* agobjfn_t)(Agraph_t *g, Agobj_t *obj, void *arg)
Definition cgraph.h:369
Agnode_t * agsubnode(Agraph_t *g, Agnode_t *n, int createflag)
Definition node.c:259
Agraph_t * agraphof(void *obj)
Definition obj.c:185
#define AGTYPE(obj)
returns AGRAPH, AGNODE, or AGEDGE depending on the type of the object
Definition cgraph.h:216
@ AGOUTEDGE
Definition cgraph.h:207
@ AGNODE
Definition cgraph.h:207
@ AGINEDGE
Definition cgraph.h:207
@ AGRAPH
Definition cgraph.h:207
Agraph_t * agfstsubg(Agraph_t *g)
Definition subg.c:78
Agraph_t * agnxtsubg(Agraph_t *subg)
Definition subg.c:83
a generic header of Agraph_s, Agnode_s and Agedge_s
Definition cgraph.h:210
graph or subgraph
Definition cgraph.h:425