# HG changeset patch # User alpar # Date 1090944261 0 # Node ID aa700e5c47b552830b6c0c32470214fb3a6dbdcc # Parent 7237eaaf5d84c86b168dc1c572a218357f280ee8 A very flexible bfs function using named parameters and impicit map types. diff -r 7237eaaf5d84 -r aa700e5c47b5 src/work/alpar/bfs-named-param.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/alpar/bfs-named-param.cc Tue Jul 27 16:04:21 2004 +0000 @@ -0,0 +1,159 @@ +// -*- mode:C++ -*- + +#include +#include +#include + +using namespace hugo; + +struct _BFS_DEFAULT_VIS {}; +struct _BFS_CUSTOM_VIS {}; + +template +class _BFS +{ + public: + typedef GT Graph; + typedef VT Visited; + typedef PNT PredNode; + typedef PET PredEdge; + typedef PT Priority; + // typedef QDT QueueData; + + typedef typename Graph::Node Node; + typedef typename Graph::OutEdgeIt OutEdgeIt; + + typedef DVT DefaultVisitedTag; + + const Graph &_graph; + + Node _source; + + Visited *_visited; + PredNode _predNode; + PredEdge _predEdge; + Priority _priority; + + _BFS(const Graph &g, + Node s, + Visited *v, + PredNode &pn, + PredEdge &pe, + Priority &pr) :_graph(g), _source(s), + _visited(v), + _predNode(pn), _predEdge(pe), _priority(pr) { } + + + int run(const _BFS_CUSTOM_VIS &) + { + using namespace std; + + int N=_graph.nodeNum(); + vector Q(N); + int Qh=0; + int Qt=0; + + for(typename Graph::NodeIt n(_graph);_graph.valid(n);_graph.next(n)) + _visited->set(n,false); + + Q[Qh++]=_source; + _visited->set(_source,true); + do { + Node m; + Node n=Q[Qt++]; + for(OutEdgeIt e(_graph,n);_graph.valid(e);_graph.next(e)) + if(!(*_visited)[m=_graph.head(e)]) { + Q[Qh++]=m; + _visited->set(m,true); + _predNode.set(m,n); + _predEdge.set(m,e); + } + } while(Qt!=Qh); + + return 1; //Why return 1? + } + int run(const _BFS_DEFAULT_VIS &) + { + _visited= new Visited(_graph); + int r = run(_BFS_CUSTOM_VIS()); + delete _visited; + return r; + } + int run() { return run(DefaultVisitedTag());} + + template _BFS + setVisitMap(T &t) + { + return _BFS + (_graph,_source,&t,_predNode,_predEdge,_priority); + } + + template + _BFS + setPredNodeMap(T &t) + { + return _BFS + (_graph,_source, + _visited, + t,_predEdge,_priority); + } + + template + _BFS + setPredEdgeMap(T &t) + { + return _BFS + (_graph,_source, + _visited, + _predNode,t,_priority); + } + + _BFS + setNothing() + { + return _BFS + (_graph,_source, + _visited, + _predNode,_predEdge,_priority); + } +}; + + +template +_BFS, + _BFS_DEFAULT_VIS, + NullMap, + NullMap, + NullMap > +bfs(const G &g,typename G::Node s) +{ + // typename G::template NodeMap v(g); + + return _BFS < G, + typename G::template NodeMap, + _BFS_DEFAULT_VIS, + NullMap, + NullMap, + NullMap > + (g,s, + (typename G::template NodeMap*)(NULL), + *((NullMap*)(NULL)), + *((NullMap *)(NULL)), + *((NullMap *)(NULL)) + ); +} + + +int main() +{ + SmartGraph G; + SmartGraph::NodeIt n(G); + SmartGraph::NodeMap m(G); + SmartGraph::NodeMap em(G); + + bfs(G,n).run(); + bfs(G,n).setPredNodeMap(m).run(); + bfs(G,n).setPredNodeMap(m).setPredEdgeMap(em).run(); + +}