tree.hpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright 2016-2017 Davide Faconti
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  * *******************************************************************/
34 
35 #ifndef STRINGTREE_H
36 #define STRINGTREE_H
37 
38 #include <vector>
39 #include <deque>
40 #include <iostream>
41 #include <memory>
42 
44 
45 namespace RosMsgParser
46 {
47 
48 namespace details
49 {
50 
54 template <typename T>
55 class TreeNode
56 {
57 public:
58  typedef std::vector<TreeNode> ChildrenVector; // dangerous because of pointer
59  // invalidation (but faster)
60 
61  TreeNode(const TreeNode* parent);
62 
63  const TreeNode* parent() const
64  {
65  return _parent;
66  }
67 
68  const T& value() const
69  {
70  return _value;
71  }
72  void setValue(const T& value)
73  {
74  _value = value;
75  }
76 
77  const ChildrenVector& children() const
78  {
79  return _children;
80  }
82  {
83  return _children;
84  }
85 
86  const TreeNode* child(size_t index) const
87  {
88  return &(_children[index]);
89  }
90  TreeNode* child(size_t index)
91  {
92  return &(_children[index]);
93  }
94 
95  TreeNode* addChild(const T& child);
96 
97  bool isLeaf() const
98  {
99  return _children.empty();
100  }
101 
102 private:
103  const TreeNode* _parent = nullptr;
104  T _value;
106 };
107 
108 template <typename T>
109 class Tree
110 {
111 public:
112  Tree() : _root(new TreeNode<T>(nullptr))
113  {
114  }
115 
121  template <typename Vect>
122  const TreeNode<T>* find(const Vect& concatenated_values, bool partial_allowed = false);
123 
125  const TreeNode<T>* croot() const
126  {
127  return _root.get();
128  }
129 
131  TreeNode<T>* root()
132  {
133  return _root.get();
134  }
135 
136  friend std::ostream& operator<<(std::ostream& os, const Tree& _this)
137  {
138  _this.print_impl(os, _this.croot(), 0);
139  return os;
140  }
141 
142  template <class Functor>
143  void visit(Functor& func, const TreeNode<T>* node) const
144  {
145  func(node);
146  for (auto child : node->children())
147  {
148  visit(func, &child);
149  }
150  }
151 
152 private:
153  void print_impl(std::ostream& os, const TreeNode<T>* node, int indent) const;
154 
155  std::unique_ptr<TreeNode<T>> _root;
156 };
157 
158 //-----------------------------------------
159 
160 template <typename T>
161 inline std::ostream& operator<<(
162  std::ostream& os, const std::pair<const TreeNode<T>*, const TreeNode<T>*>& tail_head)
163 {
164  const TreeNode<T>* tail = tail_head.first;
165  const TreeNode<T>* head = tail_head.second;
166 
167  if (!head)
168  return os;
169 
170  const TreeNode<T>* array[64];
171  int index = 0;
172  array[index++] = head;
173 
174  while (!head || head != tail)
175  {
176  head = head->parent();
177  array[index++] = head;
178  };
179  array[index] = nullptr;
180  index--;
181 
182  while (index >= 0)
183  {
184  if (array[index])
185  {
186  os << array[index]->value();
187  }
188  if (index > 0)
189  os << ".";
190  index--;
191  }
192  return os;
193 }
194 
195 template <typename T>
196 inline void Tree<T>::print_impl(std::ostream& os, const TreeNode<T>* node,
197  int indent) const
198 {
199  for (int i = 0; i < indent; i++)
200  os << " ";
201  os << node->value() << std::endl;
202 
203  for (const auto& child : node->children())
204  {
205  print_impl(os, &child, indent + 3);
206  }
207 }
208 
209 template <typename T>
210 inline TreeNode<T>::TreeNode(const TreeNode* parent) : _parent(parent)
211 {
212 }
213 
214 template <typename T>
215 inline TreeNode<T>* TreeNode<T>::addChild(const T& value)
216 {
217  assert(_children.capacity() > _children.size());
218  _children.emplace_back(this);
219  _children.back().setValue(value);
220  return &_children.back();
221 }
222 
223 template <typename T>
224 template <typename Vect>
225 inline const TreeNode<T>* Tree<T>::find(const Vect& concatenated_values,
226  bool partial_allowed)
227 {
228  TreeNode<T>* node = &_root;
229 
230  for (const auto& value : concatenated_values)
231  {
232  bool found = false;
233  for (auto& child : (node->children()))
234  {
235  if (child.value() == value)
236  {
237  node = &(child);
238  found = true;
239  break;
240  }
241  }
242  if (!found)
243  return nullptr;
244  }
245 
246  if (partial_allowed || node->children().empty())
247  {
248  return node;
249  }
250  return nullptr;
251 }
252 
253 } // namespace details
254 
255 } // namespace RosMsgParser
256 
257 #endif // STRINGTREE_H
RosMsgParser::details::Tree::find
const TreeNode< T > * find(const Vect &concatenated_values, bool partial_allowed=false)
Definition: tree.hpp:289
RosMsgParser::details::Tree::croot
const TreeNode< T > * croot() const
Constant pointer to the root of the tree.
Definition: tree.hpp:189
RosMsgParser::details::Tree::_root
std::unique_ptr< TreeNode< T > > _root
Definition: tree.hpp:219
RosMsgParser::details::Tree::print_impl
void print_impl(std::ostream &os, const TreeNode< T > *node, int indent) const
Definition: tree.hpp:260
RosMsgParser::details::TreeNode::isLeaf
bool isLeaf() const
Definition: tree.hpp:193
RosMsgParser::details::Tree::Tree
Tree()
Definition: tree.hpp:176
sol::lib::os
@ os
sol::meta_function::index
@ index
detail::find
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2154
RosMsgParser::details::TreeNode::_value
T _value
Definition: tree.hpp:200
RosMsgParser::details::TreeNode::_parent
const TreeNode * _parent
Definition: tree.hpp:199
RosMsgParser::details::Tree::visit
void visit(Functor &func, const TreeNode< T > *node) const
Definition: tree.hpp:207
RosMsgParser
Definition: builtin_types.hpp:34
builtin_types.hpp
RosMsgParser::details::TreeNode::child
const TreeNode * child(size_t index) const
Definition: tree.hpp:182
RosMsgParser::details::TreeNode::setValue
void setValue(const T &value)
Definition: tree.hpp:168
RosMsgParser::details::TreeNode::addChild
TreeNode * addChild(const T &child)
Definition: tree.hpp:279
RosMsgParser::details::TreeNode::children
const ChildrenVector & children() const
Definition: tree.hpp:173
RosMsgParser::details::TreeNode::TreeNode
TreeNode(const TreeNode *parent)
Definition: tree.hpp:274
assert
#define assert(condition)
Definition: lz4.c:271
RosMsgParser::details::operator<<
std::ostream & operator<<(std::ostream &os, const std::pair< const TreeNode< T > *, const TreeNode< T > * > &tail_head)
Definition: tree.hpp:225
RosMsgParser::details::Tree::operator<<
friend std::ostream & operator<<(std::ostream &os, const Tree &_this)
Definition: tree.hpp:200
RosMsgParser::details::TreeNode::ChildrenVector
std::vector< TreeNode > ChildrenVector
Definition: tree.hpp:154
RosMsgParser::details::Tree::root
TreeNode< T > * root()
Mutable pointer to the root of the tree.
Definition: tree.hpp:195
RosMsgParser::details::TreeNode::_children
ChildrenVector _children
Definition: tree.hpp:201
RosMsgParser::details::TreeNode
Element of the tree. it has a single parent and N >= 0 children.
Definition: tree.hpp:119
nullptr
#define nullptr
Definition: backward.hpp:386
RosMsgParser::details::TreeNode::value
const T & value() const
Definition: tree.hpp:164
RosMsgParser::details::TreeNode::parent
const TreeNode * parent() const
Definition: tree.hpp:159


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:26