| 1 |
/** |
| 2 |
* @file |
| 3 |
* @ingroup cgraph_core |
| 4 |
*/ |
| 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 |
*/ |
| 23 |
static Agobj_t *subnode_search(Agraph_t * sub, Agobj_t * n) |
| 24 |
{ |
| 25 |
if (agraphof(n) == sub) |
| 26 |
return n; |
| 27 |
return (Agobj_t *) agsubnode(sub, (Agnode_t *) n, 0); |
| 28 |
} |
| 29 |
|
| 30 |
static Agobj_t *subedge_search(Agraph_t * sub, Agobj_t * e) |
| 31 |
{ |
| 32 |
if (agraphof(e) == sub) |
| 33 |
return e; |
| 34 |
return (Agobj_t *) agsubedge(sub, (Agedge_t *) e, 0); |
| 35 |
} |
| 36 |
|
| 37 |
static Agobj_t *subgraph_search(Agraph_t * sub, Agobj_t * g) |
| 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 |
*/ |
| 47 |
static void rec_apply(Agraph_t * g, Agobj_t * obj, agobjfn_t fn, void *arg, |
| 48 |
agobjsearchfn_t objsearch, bool preorder) { |
| 49 |
Agraph_t *sub; |
| 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 |
|
| 62 |
int 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 |
} |