Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef MRPT_DIRECTED_TREE_H
00029 #define MRPT_DIRECTED_TREE_H
00030
00031
00032 #include <mrpt/utils/utils_defs.h>
00033
00034
00035
00036
00037 namespace mrpt
00038 {
00039 namespace graphs
00040 {
00041 using mrpt::utils::TNodeID;
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 template <class TYPE_EDGES = uint8_t>
00055 class CDirectedTree
00056 {
00057 public:
00058 struct TEdgeInfo
00059 {
00060 TNodeID id;
00061 bool reverse;
00062 TYPE_EDGES data;
00063 };
00064 typedef std::list<TEdgeInfo> TListEdges;
00065 typedef std::map<TNodeID,TListEdges> TMapNode2ListEdges;
00066
00067
00068
00069 TNodeID root;
00070 TMapNode2ListEdges edges_to_children;
00071
00072
00073
00074
00075
00076
00077 void clear() { edges_to_children.clear(); root = INVALID_NODEID; }
00078
00079
00080 struct Visitor
00081 {
00082 typedef CDirectedTree<TYPE_EDGES> tree_t;
00083
00084
00085
00086
00087
00088
00089
00090 virtual void OnVisitNode( const TNodeID parent, const typename tree_t::TEdgeInfo &edge_to_child, const size_t depth_level ) = 0;
00091 };
00092
00093
00094 void visitDepthFirst( const TNodeID root, Visitor & user_visitor, const size_t root_depth_level =0 ) const
00095 {
00096 const size_t next_depth_level = root_depth_level+1;
00097 typename TMapNode2ListEdges::const_iterator itChildren = edges_to_children.find(root);
00098 if (itChildren==edges_to_children.end()) return;
00099 const TListEdges &children = itChildren->second;
00100 for (typename TListEdges::const_iterator itEdge=children.begin();itEdge!=children.end();++itEdge)
00101 {
00102 user_visitor.OnVisitNode(root,*itEdge,next_depth_level);
00103 visitDepthFirst(itEdge->id,user_visitor, next_depth_level);
00104 }
00105 }
00106
00107
00108 void visitBreadthFirst( const TNodeID root, Visitor & user_visitor, const size_t root_depth_level =0 ) const
00109 {
00110 const size_t next_depth_level = root_depth_level+1;
00111 typename TMapNode2ListEdges::const_iterator itChildren = edges_to_children.find(root);
00112 if (itChildren==edges_to_children.end()) return;
00113 const TListEdges &children = itChildren->second;
00114 for (typename TListEdges::const_iterator itEdge=children.begin();itEdge!=children.end();++itEdge)
00115 user_visitor.OnVisitNode(root,*itEdge,next_depth_level);
00116 for (typename TListEdges::const_iterator itEdge=children.begin();itEdge!=children.end();++itEdge)
00117 visitDepthFirst(itEdge->id,user_visitor,next_depth_level);
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 std::string getAsTextDescription() const
00131 {
00132 std::ostringstream s;
00133 struct CMyVisitor : public mrpt::graphs::CDirectedTree<TYPE_EDGES>::Visitor
00134 {
00135 std::ostringstream &m_s;
00136 CMyVisitor(std::ostringstream &s) : m_s(s) { }
00137 virtual void OnVisitNode( const TNodeID parent, const typename mrpt::graphs::CDirectedTree<TYPE_EDGES>::Visitor::tree_t::TEdgeInfo &edge_to_child, const size_t depth_level ) {
00138 m_s << std::string(depth_level*5, ' ') << (edge_to_child.reverse ? "<-" : "->" )
00139 << std::setw(3) << edge_to_child.id << std::endl;
00140 }
00141 };
00142 CMyVisitor myVisitor(s);
00143 s << std::setw(3) << root << std::endl;
00144 visitDepthFirst( root, myVisitor );
00145 return s.str();
00146 }
00147
00148 };
00149
00150
00151 }
00152 }
00153 #endif