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 
36 #ifndef STRINGTREE_H
37 #define STRINGTREE_H
38 
39 #include <vector>
40 #include <deque>
41 #include <iostream>
42 #include <memory>
43 #include <boost/container/stable_vector.hpp>
44 #include <boost/noncopyable.hpp>
46 
47 namespace RosIntrospection {
48 
49 namespace details{
50 
54 template <typename T> class TreeNode
55 {
56 
57 public:
58 
59  typedef std::vector<TreeNode> ChildrenVector; // dangerous because of pointer invalidation (but faster)
60 
61  TreeNode(const TreeNode* parent );
62 
63  const TreeNode* parent() const { return _parent; }
64 
65  const T& value() const { return _value; }
66  void setValue( const T& value) { _value = value; }
67 
68  const ChildrenVector& children()const { return _children; }
69  ChildrenVector& children() { return _children; }
70 
71  const TreeNode* child(size_t index) const { return &(_children[index]); }
72  TreeNode* child(size_t index) { return &(_children[index]); }
73 
74  TreeNode *addChild(const T& child );
75 
76  bool isLeaf() const { return _children.empty(); }
77 
78 private:
79  const TreeNode* _parent;
80  T _value;
81  ChildrenVector _children;
82 };
83 
84 
85 template <typename T> class Tree
86 {
87 public:
88  Tree(): _root( new TreeNode<T>(nullptr) ) {}
89 
95  template<typename Vect> const TreeNode<T>* find( const Vect& concatenated_values, bool partial_allowed = false);
96 
98  const TreeNode<T>* croot() const { return _root.get(); }
99 
101  TreeNode<T>* root() { return _root.get(); }
102 
103 
104  friend std::ostream& operator<<(std::ostream& os, const Tree& _this){
105  _this.print_impl(os, _this.croot() , 0);
106  return os;
107  }
108 
109 private:
110 
111  void print_impl(std::ostream& os, const TreeNode<T> *node, int indent ) const;
112 
113  std::unique_ptr<TreeNode<T>> _root;
114 };
115 
116 //-----------------------------------------
117 
118 
119 template <typename T> inline
120 std::ostream& operator<<(std::ostream &os, const std::pair<const TreeNode<T>*, const TreeNode<T>* >& tail_head )
121 {
122  const TreeNode<T>* tail = tail_head.first;
123  const TreeNode<T>* head = tail_head.second;
124 
125  if( !head ) return os;
126 
127  const TreeNode<T>* array[64];
128  int index = 0;
129  array[index++] = head;
130 
131  while( !head || head != tail)
132  {
133  head = head->parent();
134  array[index++] = head;
135  };
136  array[index] = nullptr;
137  index--;
138 
139  while ( index >=0)
140  {
141  if( array[index] ){
142  os << array[index]->value();
143  }
144  if( index >0 ) os << ".";
145  index--;
146  }
147  return os;
148 }
149 
150 template <typename T> inline
151 void Tree<T>::print_impl(std::ostream &os, const TreeNode<T>* node, int indent) const
152 {
153  for (int i=0; i<indent; i++) os << " ";
154  os << node->value() << std::endl;
155 
156  for (const auto& child: node->children() )
157  {
158  print_impl(os, &child, indent+3);
159  }
160 }
161 
162 template <typename T> inline
164  _parent(parent)
165 {
166 
167 }
168 
169 template <typename T> inline
171 {
172  assert(_children.capacity() > _children.size() );
173  _children.push_back( TreeNode<T>(this) );
174  _children.back().setValue( value );
175  return &_children.back();
176 }
177 
178 
179 template <typename T> template<typename Vect> inline
180 const TreeNode<T> *Tree<T>::find(const Vect& concatenated_values, bool partial_allowed )
181 {
182  TreeNode<T>* node = &_root;
183 
184  for (const auto& value: concatenated_values)
185  {
186  bool found = false;
187  for (auto& child: (node->children() ) )
188  {
189  if( child.value() == value)
190  {
191  node = &(child);
192  found = true;
193  break;
194  }
195  }
196  if( !found ) return nullptr;
197  }
198 
199  if( partial_allowed || node->children().empty() )
200  {
201  return node;
202  }
203  return nullptr;
204 }
205 
206 }
207 
208 }
209 
210 
211 
212 #endif // STRINGTREE_H
const ChildrenVector & children() const
Definition: tree.hpp:68
TreeNode< T > * root()
Mutable pointer to the root of the tree.
Definition: tree.hpp:101
Element of the tree. it has a single parent and N >= 0 children.
Definition: tree.hpp:54
const TreeNode * parent() const
Definition: tree.hpp:63
const TreeNode< T > * croot() const
Constant pointer to the root of the tree.
Definition: tree.hpp:98
TreeNode * child(size_t index)
Definition: tree.hpp:72
void setValue(const T &value)
Definition: tree.hpp:66
const TreeNode * child(size_t index) const
Definition: tree.hpp:71
const T & value() const
Definition: tree.hpp:65
TreeNode(const TreeNode *parent)
Definition: tree.hpp:163
const TreeNode< T > * find(const Vect &concatenated_values, bool partial_allowed=false)
Definition: tree.hpp:180
friend std::ostream & operator<<(std::ostream &os, const Tree &_this)
Definition: tree.hpp:104
TreeNode * addChild(const T &child)
Definition: tree.hpp:170
std::unique_ptr< TreeNode< T > > _root
Definition: tree.hpp:113
int i
ChildrenVector & children()
Definition: tree.hpp:69
std::vector< TreeNode > ChildrenVector
Definition: tree.hpp:59
void print_impl(std::ostream &os, const TreeNode< T > *node, int indent) const
Definition: tree.hpp:151


ros_type_introspection
Author(s): Davide Faconti
autogenerated on Thu May 16 2019 02:39:09