Main MRPT website > C++ reference for MRPT 1.9.9
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-2017, 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 #include <list>
14 
15 namespace mrpt
16 {
17 namespace graphs
18 {
19 /** Make available this typedef in this namespace too */
21 
22 /** A special kind of graph in the form of a tree with directed edges and
23  *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
27  *children.
28  *
29  * Note that nodes are *not* explicitly listed anywhere: their existence is
30  *only inferred from their ID numbers in the list
31  * of edges in the \a edges_to_children data structure. If you want to include
32  *information for each node, derive from this class
33  * and create a separate container for that data.
34  *
35  * This class is less general than CDirectedGraph but more efficient to
36  *traverse (see \a visitDepthFirst and \a visitBreadthFirst).
37  *
38  * If annotations in edges are not required, you can leave TYPE_EDGES to its
39  *default type "uint8_t".
40  *
41  * Example of insertion of a new edge:
42  * \code
43  * typedef CDirectedTree<edge_t> my_tree_t;
44  * my_tree_t tree;
45  * TNodeID id_root = XXX;
46  * TNodeID id_child = XXX;
47  * my_tree_t::TListEdges & edges_of_root = tree.edges_to_children[id_root];
48  * edges_of_root.push_back( my_tree_t::TEdgeInfo(id_child,false, edge_t(...) )
49  *);
50  * \endcode
51  *
52  * \sa CDirectedGraph, CDijkstra, mrpt::graphs::CNetworkOfPoses
53  * \ingroup mrpt_graphs_grp
54  */
55 template <class TYPE_EDGES = uint8_t>
57 {
58  public:
59  struct TEdgeInfo
60  {
61  /** The ID of the child node. */
63  /** True if edge direction is child->parent, false if it's
64  * parent->child. */
65  bool reverse;
66  /** User data for this edge. */
67  TYPE_EDGES data;
68 
69  /** Edge constructor from data */
71  TNodeID child_id_, bool direction_child_to_parent = false,
72  const TYPE_EDGES& edge_data = TYPE_EDGES())
73  : id(child_id_), reverse(direction_child_to_parent), data(edge_data)
74  {
75  }
76  };
77 
78  typedef std::list<TEdgeInfo> TListEdges;
79  typedef std::map<TNodeID, TListEdges> TMapNode2ListEdges;
80 
81  /** @name Data
82  @{ */
83  /** The root of the tree */
85  /** The edges of each node */
87  /** @} */
88 
89  /** @name Utilities
90  @{ */
91 
92  /** Empty all edge data and set "root" to INVALID_NODEID */
93  void clear()
94  {
95  edges_to_children.clear();
97  }
98 
99  /** Virtual base class for user-defined visitors */
100  struct Visitor
101  {
103 
104  /** Virtual method to be implemented by the user and which will be
105  * called during the visit to a graph with visitDepthFirst or
106  * visitBreadthFirst
107  * Specifically, the method will be called once for each <b>edge</b>
108  * in the tree.
109  * \param parent [IN] The ID of the parent node.
110  * \param edge_to_child [IN] The edge information from the parent to
111  * "edge_to_child.id"
112  * \param depth_level [IN] The "depth level" of the child node
113  * "edge_to_child.id" (root node is at 0, its children are at 1, etc.).
114  */
115  virtual void OnVisitNode(
116  const TNodeID parent,
117  const typename tree_t::TEdgeInfo& edge_to_child,
118  const size_t depth_level) = 0;
119  };
120 
121  /** Depth-first visit of all children nodes of a given root (itself excluded
122  * from the visit), invoking a user-provided function for each node/edge.
123  * \sa visitBreadthFirst */
125  const TNodeID root, Visitor& user_visitor,
126  const size_t root_depth_level = 0) const
127  {
128  const size_t next_depth_level = root_depth_level + 1;
129  typename TMapNode2ListEdges::const_iterator itChildren =
130  edges_to_children.find(root);
131  if (itChildren == edges_to_children.end()) return; // No children
132  const TListEdges& children = itChildren->second;
133  for (typename TListEdges::const_iterator itEdge = children.begin();
134  itEdge != children.end(); ++itEdge)
135  {
136  user_visitor.OnVisitNode(root, *itEdge, next_depth_level);
138  itEdge->id, user_visitor,
139  next_depth_level); // Recursive depth-first call.
140  }
141  }
142 
143  /** Breadth-first visit of all children nodes of a given root (itself
144  * excluded from the visit), invoking a user-provided function for each
145  * node/edge. \sa visitDepthFirst */
147  const TNodeID root, Visitor& user_visitor,
148  const size_t root_depth_level = 0) const
149  {
150  const size_t next_depth_level = root_depth_level + 1;
151  typename TMapNode2ListEdges::const_iterator itChildren =
152  edges_to_children.find(root);
153  if (itChildren == edges_to_children.end()) return; // No children
154  const TListEdges& children = itChildren->second;
155  for (typename TListEdges::const_iterator itEdge = children.begin();
156  itEdge != children.end(); ++itEdge)
157  user_visitor.OnVisitNode(root, *itEdge, next_depth_level);
158  for (typename TListEdges::const_iterator itEdge = children.begin();
159  itEdge != children.end(); ++itEdge)
161  itEdge->id, user_visitor,
162  next_depth_level); // Recursive breath-first call.
163  }
164 
165  /** Return a text representation of the tree spanned in a depth-first view,
166  * as in this example:
167  * \code
168  * 0
169  * -> 1
170  * -> 2
171  * -> 4
172  * -> 5
173  * -> 3
174  * \endcode
175  */
177  {
178  std::ostringstream s;
179  struct CMyVisitor
180  : public mrpt::graphs::CDirectedTree<TYPE_EDGES>::Visitor
181  {
182  std::ostringstream& m_s;
183  CMyVisitor(std::ostringstream& s) : m_s(s) {}
184  virtual void OnVisitNode(
185  const TNodeID parent,
186  const typename mrpt::graphs::CDirectedTree<
187  TYPE_EDGES>::Visitor::tree_t::TEdgeInfo& edge_to_child,
188  const size_t depth_level) override
189  {
190  m_s << std::string(depth_level * 5, ' ')
191  << (edge_to_child.reverse ? "<-" : "->") //;
192  << edge_to_child.id << std::endl;
193  }
194  };
195  CMyVisitor myVisitor(s);
196  s << root << std::endl;
197  visitDepthFirst(root, myVisitor);
198  return s.str();
199  }
200 };
201 
202 /** @} */
203 } // End of namespace
204 } // End of namespace
205 #endif
std::map< TNodeID, TListEdges > TMapNode2ListEdges
Definition: CDirectedTree.h:79
TMapNode2ListEdges edges_to_children
The edges of each node.
Definition: CDirectedTree.h:86
A special kind of graph in the form of a tree with directed edges and optional edge annotations of te...
Definition: CDirectedTree.h:56
std::list< TEdgeInfo > TListEdges
Definition: CDirectedTree.h:78
#define INVALID_NODEID
const Scalar * const_iterator
Definition: eigen_plugins.h:27
CDirectedTree< TYPE_EDGES > tree_t
GLdouble s
Definition: glext.h:3676
Virtual base class for user-defined visitors.
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)...
bool reverse
True if edge direction is child->parent, false if it&#39;s parent->child.
Definition: CDirectedTree.h:65
uint64_t TNodeID
The type for node IDs in graphs of different types.
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...
uint64_t TNodeID
The type for node IDs in graphs of different types.
Definition: types_simple.h:49
TNodeID root
The root of the tree.
Definition: CDirectedTree.h:84
TNodeID id
The ID of the child node.
Definition: CDirectedTree.h:62
GLsizei const GLchar ** string
Definition: glext.h:4101
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)...
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:70
GLuint id
Definition: glext.h:3909
TYPE_EDGES data
User data for this edge.
Definition: CDirectedTree.h:67
void clear()
Empty all edge data and set "root" to INVALID_NODEID.
Definition: CDirectedTree.h:93
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3546
std::string getAsTextDescription() const
Return a text representation of the tree spanned in a depth-first view, as in this example: ...



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019