1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
/* $Id: apply.c,v 1.3 2009/06/03 01:10:51 ellson Exp $ $Revision: 1.3 $ */
/* vim:set shiftwidth=4 ts=8: */
/**********************************************************
* This software is part of the graphviz package *
* http://www.graphviz.org/ *
* *
* Copyright (c) 1994-2004 AT&T Corp. *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Corp. *
* *
* Information and Software Systems Research *
* AT&T Research, Florham Park NJ *
**********************************************************/
#include <cghdr.h>
/* The following functions take a graph and a template (node/edge/graph)
* and return the object representing the template within the local graph.
*/
static Agobj_t *subnode_search(Agraph_t * sub, Agobj_t * n)
{
if (agraphof(n) == sub)
return n;
return (Agobj_t *) agsubnode(sub, (Agnode_t *) n, FALSE);
}
static Agobj_t *subedge_search(Agraph_t * sub, Agobj_t * e)
{
if (agraphof(e) == sub)
return e;
return (Agobj_t *) agsubedge(sub, (Agedge_t *) e, FALSE);
}
static Agobj_t *subgraph_search(Agraph_t * sub, Agobj_t * g)
{
NOTUSED(g);
return (Agobj_t *) sub;
}
/* recursively apply objfn within the hierarchy of a graph.
* if obj is a node or edge, it and its images in every subg are visited.
* if obj is a graph, then it and its subgs are visited.
*/
static void rec_apply(Agraph_t * g, Agobj_t * obj, agobjfn_t fn, void *arg,
agobjsearchfn_t objsearch, int preorder)
{
Agraph_t *sub;
Agobj_t *subobj;
if (preorder)
fn(g, obj, arg);
for (sub = agfstsubg(g); sub; sub = agnxtsubg(sub)) {
if ((subobj = objsearch(sub, obj)))
rec_apply(sub, subobj, fn, arg, objsearch, preorder);
}
if (NOT(preorder))
fn(g, obj, arg);
}
/* external entry point (this seems to be one of those ineffective
* comments censured in books on programming style) */
int agapply(Agraph_t * g, Agobj_t * obj, agobjfn_t fn, void *arg,
int preorder)
{
Agobj_t *subobj;
agobjsearchfn_t objsearch;
switch (AGTYPE(obj)) {
case AGRAPH:
objsearch = subgraph_search;
break;
case AGNODE:
objsearch = subnode_search;
break;
case AGOUTEDGE:
case AGINEDGE:
objsearch = subedge_search;
break;
default:
abort();
}
if ((subobj = objsearch(g, obj))) {
rec_apply(g, subobj, fn, arg, objsearch, preorder);
return SUCCESS;
} else
return FAILURE;
}
|