Main MRPT website > C++ reference
MRPT logo
CDirectedTree.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2015, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef MRPT_DIRECTED_TREE_H
10 #define MRPT_DIRECTED_TREE_H
11 
12 #include <mrpt/utils/utils_defs.h>
13 
14 /*---------------------------------------------------------------
15  Class
16  ---------------------------------------------------------------*/
17 namespace mrpt
18 {
19  namespace graphs
20  {
21  using mrpt::utils::TNodeID; //!< Make available this typedef in this namespace too
22 
23  /** A special kind of graph in the form of a tree with directed edges and optional edge annotations of templatized type "TYPE_EDGES".
24  * The tree is represented by means of:
25  * - \a root: The ID of the root node.
26  * - \a edges_to_children: A map from node ID to all the edges to its children.
27  *
28  * Note that nodes are *not* explicitly listed anywhere: their existence is only inferred from their ID numbers in the list
29  * of edges in the \a edges_to_children data structure. If you want to include information for each node, derive from this class
30  * and create a separte container for that data.
31  *
32  * This class is less general than CDirectedGraph but more efficient to traverse (see \a visitDepthFirst and \a visitBreadthFirst).
33  *
34  * If annotations in edges are not required, you can leave TYPE_EDGES to its default type "uint8_t".
35  *
36  * Example of insertion of a new edge:
37  * \code
38  * typedef CDirectedTree<edge_t> my_tree_t;
39  * my_tree_t tree;
40  * TNodeID id_root = XXX;
41  * TNodeID id_child = XXX;
42  * my_tree_t::TListEdges & edges_of_root = tree.edges_to_children[id_root];
43  * edges_of_root.push_back( my_tree_t::TEdgeInfo(id_child,false, edge_t(...) ) );
44  * \endcode
45  *
46  * \sa CDirectedGraph, CDijkstra, mrpt::graphs::CNetworkOfPoses
47  * \ingroup mrpt_graphs_grp
48  */
49  template <class TYPE_EDGES = uint8_t>
51  {
52  public:
53  struct TEdgeInfo
54  {
55  TNodeID id; //!< The ID of the child node.
56  bool reverse; //!< True if edge direction is child->parent, false if it's parent->child.
57  TYPE_EDGES data; //!< User data for this edge.
58 
59  /** Edge constructor from data */
60  TEdgeInfo(TNodeID child_id_, bool direction_child_to_parent=false, const TYPE_EDGES & edge_data = TYPE_EDGES() ) : id(child_id_), reverse(direction_child_to_parent), data(edge_data) { }
61  };
62 
63  typedef std::list<TEdgeInfo> TListEdges;
64  typedef std::map<TNodeID,TListEdges> TMapNode2ListEdges;
65 
66  /** @name Data
67  @{ */
68  TNodeID root; //!< The root of the tree
69  TMapNode2ListEdges edges_to_children; //!< The edges of each node
70  /** @} */
71 
72  /** @name Utilities
73  @{ */
74 
75  /** Empty all edge data and set "root" to INVALID_NODEID */
76  void clear() { edges_to_children.clear(); root = INVALID_NODEID; }
77 
78  /** Virtual base class for user-defined visitors */
79  struct Visitor
80  {
82 
83  /** Virtual method to be implemented by the user and which will be called during the visit to a graph with visitDepthFirst or visitBreadthFirst
84  * Specifically, the method will be called once for each <b>edge</b> in the tree.
85  * \param parent [IN] The ID of the parent node.
86  * \param edge_to_child [IN] The edge information from the parent to "edge_to_child.id"
87  * \param depth_level [IN] The "depth level" of the child node "edge_to_child.id" (root node is at 0, its children are at 1, etc.).
88  */
89  virtual void OnVisitNode( const TNodeID parent, const typename tree_t::TEdgeInfo &edge_to_child, const size_t depth_level ) = 0;
90  };
91 
92  /** Depth-first visit of all children nodes of a given root (itself excluded from the visit), invoking a user-provided function for each node/edge. \sa visitBreadthFirst */
93  void visitDepthFirst( const TNodeID root, Visitor & user_visitor, const size_t root_depth_level =0 ) const
94  {
95  const size_t next_depth_level = root_depth_level+1;
96  typename TMapNode2ListEdges::const_iterator itChildren = edges_to_children.find(root);
97  if (itChildren==edges_to_children.end()) return; // No children
98  const TListEdges &children = itChildren->second;
99  for (typename TListEdges::const_iterator itEdge=children.begin();itEdge!=children.end();++itEdge)
100  {
101  user_visitor.OnVisitNode(root,*itEdge,next_depth_level);
102  visitDepthFirst(itEdge->id,user_visitor, next_depth_level); // Recursive depth-first call.
103  }
104  }
105 
106  /** Breadth-first visit of all children nodes of a given root (itself excluded from the visit), invoking a user-provided function for each node/edge. \sa visitDepthFirst */
107  void visitBreadthFirst( const TNodeID root, Visitor & user_visitor, const size_t root_depth_level =0 ) const
108  {
109  const size_t next_depth_level = root_depth_level+1;
110  typename TMapNode2ListEdges::const_iterator itChildren = edges_to_children.find(root);
111  if (itChildren==edges_to_children.end()) return; // No children
112  const TListEdges &children = itChildren->second;
113  for (typename TListEdges::const_iterator itEdge=children.begin();itEdge!=children.end();++itEdge)
114  user_visitor.OnVisitNode(root,*itEdge,next_depth_level);
115  for (typename TListEdges::const_iterator itEdge=children.begin();itEdge!=children.end();++itEdge)
116  visitDepthFirst(itEdge->id,user_visitor,next_depth_level); // Recursive breath-first call.
117  }
118 
119  /** Return a text representation of the tree spanned in a depth-first view, as in this example:
120  * \code
121  * 0
122  * -> 1
123  * -> 2
124  * -> 4
125  * -> 5
126  * -> 3
127  * \endcode
128  */
129  std::string getAsTextDescription() const
130  {
131  std::ostringstream s;
132  struct CMyVisitor : public mrpt::graphs::CDirectedTree<TYPE_EDGES>::Visitor
133  {
134  std::ostringstream &m_s;
135  CMyVisitor(std::ostringstream &s) : m_s(s) { }
136  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 ) {
137  m_s << std::string(depth_level*5, ' ') << (edge_to_child.reverse ? "<-" : "->" ) //;
138  << edge_to_child.id << std::endl;
139  }
140  };
141  CMyVisitor myVisitor(s);
142  s << root << std::endl;
143  visitDepthFirst( root, myVisitor );
144  return s.str();
145  }
146 
147  };
148 
149  /** @} */
150  } // End of namespace
151 } // End of namespace
152 #endif
void visitBreadthFirst(const TNodeID root, Visitor &user_visitor, const size_t root_depth_level=0) const
Breadth-first visit of all children nodes of a given root (itself excluded from the visit)...
TMapNode2ListEdges edges_to_children
The edges of each node.
Definition: CDirectedTree.h:69
< Make available this typedef in this namespace too
Definition: CDirectedTree.h:50
std::list< TEdgeInfo > TListEdges
Definition: CDirectedTree.h:63
uint64_t TNodeID
The type for node IDs in graphs of different types.
Definition: types_simple.h:39
const Scalar * const_iterator
Definition: eigen_plugins.h:24
CDirectedTree< TYPE_EDGES > tree_t
Definition: CDirectedTree.h:81
Virtual base class for user-defined visitors.
Definition: CDirectedTree.h:79
bool reverse
True if edge direction is child->parent, false if it's parent->child.
Definition: CDirectedTree.h:56
std::string getAsTextDescription() const
Return a text representation of the tree spanned in a depth-first view, as in this example: ...
virtual void OnVisitNode(const TNodeID parent, const typename tree_t::TEdgeInfo &edge_to_child, const size_t depth_level)=0
Virtual method to be implemented by the user and which will be called during the visit to a graph wit...
TNodeID root
The root of the tree.
Definition: CDirectedTree.h:68
TNodeID id
The ID of the child node.
Definition: CDirectedTree.h:55
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
TEdgeInfo(TNodeID child_id_, bool direction_child_to_parent=false, const TYPE_EDGES &edge_data=TYPE_EDGES())
Edge constructor from data.
Definition: CDirectedTree.h:60
uint64_t TNodeID
The type for node IDs in graphs of different types.
TYPE_EDGES data
User data for this edge.
Definition: CDirectedTree.h:57
std::map< TNodeID, TListEdges > TMapNode2ListEdges
Definition: CDirectedTree.h:64
void visitDepthFirst(const TNodeID root, Visitor &user_visitor, const size_t root_depth_level=0) const
Depth-first visit of all children nodes of a given root (itself excluded from the visit)...
Definition: CDirectedTree.h:93
void clear()
Empty all edge data and set "root" to INVALID_NODEID.
Definition: CDirectedTree.h:76
#define INVALID_NODEID



Page generated by Doxygen 1.8.9.1 for MRPT 1.3.0 SVN: at Sun Sep 13 03:55:12 UTC 2015