Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:41

0001 #ifndef DATA_FORMATS_MATH_GRAPH_UTIL_H
0002 #define DATA_FORMATS_MATH_GRAPH_UTIL_H
0003 
0004 #include "DataFormats/Math/interface/Graph.h"
0005 #include "DataFormats/Math/interface/GraphWalker.h"
0006 #include <iostream>
0007 #include <string>
0008 
0009 template <class N, class E>
0010 void output(const math::Graph<N, E>& g, const N& root) {
0011   math::GraphWalker<N, E> w(g, root);
0012   bool go = true;
0013   while (go) {
0014     std::cout << w.current().first << ' ';
0015     go = w.next();
0016   }
0017   std::cout << std::endl;
0018 }
0019 
0020 template <class N, class E>
0021 void graph_combine(const math::Graph<N, E>& g1,
0022                    const math::Graph<N, E>& g2,
0023                    const N& n1,
0024                    const N& n2,
0025                    const N& root,
0026                    math::Graph<N, E>& result) {
0027   result = g1;
0028   result.replace(n1, n2);
0029   math::GraphWalker<N, E> walker(g2, n2);
0030   while (walker.next()) {
0031     const N& parent = g2.nodeData((++walker.stack().rbegin())->first->first);
0032     result.addEdge(parent, walker.current().first, walker.current().second);
0033   }
0034   result.replace(n2, root);
0035 }
0036 
0037 template <class N, class E>
0038 void graph_tree_output(const math::Graph<N, E>& g, const N& root, std::ostream& os) {
0039   math::GraphWalker<N, E> w(g, root);
0040   bool go = true;
0041   unsigned int depth = 0;
0042   while (go) {
0043     std::string s(2 * depth, ' ');
0044     os << ' ' << s << w.current().first << '(' << w.current().second << ')' << std::endl;
0045     go = w.firstChild();
0046     if (go) {
0047       ++depth;
0048     } else if (w.stack().size() > 1 && w.nextSibling()) {
0049       go = true;
0050     } else {
0051       go = false;
0052       while (w.parent()) {
0053         --depth;
0054         if (w.stack().size() > 1 && w.nextSibling()) {
0055           go = true;
0056           break;
0057         }
0058       }
0059     }
0060   }
0061 }
0062 
0063 #endif