Graphviz 14.1.3~dev.20260204.1019
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 "config.h"
16
17#include <cgraph/cghdr.h>
18#include <stdbool.h>
19
20/* The following functions take a graph and a template (node/edge/graph)
21 * and return the object representing the template within the local graph.
22 */
24{
25 if (agraphof(n) == sub)
26 return n;
27 return (Agobj_t *) agsubnode(sub, (Agnode_t *) n, 0);
28}
29
31{
32 if (agraphof(e) == sub)
33 return e;
34 return (Agobj_t *) agsubedge(sub, (Agedge_t *) e, 0);
35}
36
38{
39 (void)g;
40 return (Agobj_t *) sub;
41}
42
43/* recursively apply objfn within the hierarchy of a graph.
44 * if obj is a node or edge, it and its images in every subg are visited.
45 * if obj is a graph, then it and its subgs are visited.
46 */
47static void rec_apply(Agraph_t * g, Agobj_t * obj, agobjfn_t fn, void *arg,
48 agobjsearchfn_t objsearch, bool preorder) {
50 Agobj_t *subobj;
51
52 if (preorder)
53 fn(g, obj, arg);
54 for (sub = agfstsubg(g); sub; sub = agnxtsubg(sub)) {
55 if ((subobj = objsearch(sub, obj)))
56 rec_apply(sub, subobj, fn, arg, objsearch, preorder);
57 }
58 if (!preorder)
59 fn(g, obj, arg);
60}
61
62int agapply(Agraph_t * g, Agobj_t * obj, agobjfn_t fn, void *arg,
63 int preorder)
64{
65 Agobj_t *subobj;
66
67 agobjsearchfn_t objsearch;
68 switch (AGTYPE(obj)) {
69 case AGRAPH:
70 objsearch = subgraph_search;
71 break;
72 case AGNODE:
73 objsearch = subnode_search;
74 break;
75 case AGOUTEDGE:
76 case AGINEDGE:
77 objsearch = subedge_search;
78 break;
79 default:
80 agerrorf("agapply: unknown object type %d\n", AGTYPE(obj));
81 return FAILURE;
82 }
83 if ((subobj = objsearch(g, obj))) {
84 rec_apply(g, subobj, fn, arg, objsearch, preorder != 0);
85 return SUCCESS;
86 } else
87 return FAILURE;
88}
static void rec_apply(Agraph_t *g, Agobj_t *obj, agobjfn_t fn, void *arg, agobjsearchfn_t objsearch, bool preorder)
Definition apply.c:47
int agapply(Agraph_t *g, Agobj_t *obj, agobjfn_t fn, void *arg, int preorder)
Definition apply.c:62
static Agobj_t * subgraph_search(Agraph_t *sub, Agobj_t *g)
Definition apply.c:37
static Agobj_t * subedge_search(Agraph_t *sub, Agobj_t *e)
Definition apply.c:30
static Agobj_t * subnode_search(Agraph_t *sub, Agobj_t *n)
Definition apply.c:23
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:62
Agedge_t * agsubedge(Agraph_t *g, Agedge_t *e, int createflag)
Definition edge.c:350
void agerrorf(const char *fmt,...)
Definition agerror.c:167
void(* agobjfn_t)(Agraph_t *g, Agobj_t *obj, void *arg)
Definition cgraph.h:368
Agnode_t * agsubnode(Agraph_t *g, Agnode_t *n, int createflag)
Definition node.c:254
Agraph_t * agraphof(void *obj)
Definition obj.c:187
#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:75
Agraph_t * agnxtsubg(Agraph_t *subg)
Definition subg.c:80
a generic header of Agraph_s, Agnode_s and Agedge_s
Definition cgraph.h:210
graph or subgraph
Definition cgraph.h:424