CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/DetectorDescription/Core/interface/graph_util.h

Go to the documentation of this file.
00001 #ifndef x_graph_util_h
00002 #define x_graph_util_h
00003 
00004 #include "DetectorDescription/Core/interface/adjgraph.h"
00005 #include "DetectorDescription/Core/interface/graphwalker.h"
00006 #include <iostream>
00007 #include <string>
00008 
00009 
00010 
00011 
00012 
00013 template<class N, class E>
00014 void output(const graph<N,E> & g, const N & root)
00015 {
00016   graphwalker<N,E> w(g,root);
00017   bool go=true;
00018   while(go) {
00019     std::cout << w.current().first << ' ';
00020     go=w.next();
00021   }
00022   std::cout << std::endl;
00023 }
00024 
00025 template<class N, class E>
00026 void graph_combine(const graph<N,E> & g1, const graph<N,E> & g2,
00027                    const N & n1, const N & n2, const N & root,
00028                     graph<N,E> & result)
00029 {
00030   result = g1;
00031   result.replace(n1,n2);
00032   //output(result,n2);
00033   graphwalker<N,E> walker(g2,n2);
00034   while (walker.next()) {
00035     const N & parent = g2.nodeData((++walker.stack().rbegin())->first->first);
00036     /*
00037     N parent = g2.nodeData((++walker.stack().rbegin())->first->first);
00038     N child  = walker.current().first;
00039     E edge   = walker.current().second;
00040     */
00041     //std::cout << parent << ' ' << walker.current().first << ' ' << walker.current().second<< std::endl;
00042     result.addEdge(parent, walker.current().first, walker.current().second);
00043     //result.addEdge(parent,child,edge);
00044     //result.dump_graph();
00045     //output(result,n2);
00046   }
00047   result.replace(n2,root);                      
00048   //output(result,root);
00049 }               
00050 
00051 
00052 template<class N, class E>
00053 void graph_tree_output(const graph<N,E> & g, const N & root, std::ostream & os)
00054 {
00055    graphwalker<N,E> w(g,root);
00056    bool go=true;
00057    unsigned int depth=0;
00058    while (go) {
00059      std::string s(2*depth,' ');
00060      os << ' ' << s << w.current().first << '(' << w.current().second << ')' << std::endl;
00061      if (go=w.firstChild()) {
00062        ++depth;
00063      }
00064      else if(w.stack().size() >1 && w.nextSibling()) {
00065         go=true;
00066      }
00067      else {
00068        go=false;
00069        while(w.parent()) {
00070          --depth;
00071           if (w.stack().size()>1 && w.nextSibling()) {
00072              go=true;
00073              break;
00074           }
00075        }
00076      }
00077      
00078    }  
00079 }
00080 #endif