iRoCS Toolbox  1.1.0
ATBBasicTree.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2015 Thorsten Falk
4  *
5  * Image Analysis Lab, University of Freiburg, Germany
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  **************************************************************************/
22 
23 #ifndef ATBBASICTREE_HH
24 #define ATBBASICTREE_HH
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.hh>
28 #endif
29 
30 #include <iostream>
31 #include <sstream>
32 #include <list>
33 
34 namespace atb
35 {
36 
37  template<typename KeyT, typename ContentT>
39  {
40 
41  public:
42 
43  BasicTreeNode(KeyT const &key, ContentT const &content = ContentT())
44  : _key(key), _content(content), p_parent(NULL), _children(),
45  p_root(this)
46  {}
47 
49  {
50  while (_children.size() > 0)
51  _children.front()->reparent(p_parent);
52  reparent(NULL);
53  }
54 
55  KeyT const key() const
56  {
57  return _key;
58  }
59 
60  void setKey(const KeyT& key)
61  {
62  _key = key;
63  }
64 
65  ContentT const content() const
66  {
67  return _content;
68  }
69 
70  void setContent(const ContentT& content)
71  {
72  _content = content;
73  }
74 
76  {
77  return p_parent;
78  }
79 
81  {
82  return p_parent;
83  }
84 
85  std::list<BasicTreeNode<KeyT, ContentT>*> const &children() const
86  {
87  return _children;
88  }
89 
90  std::list<BasicTreeNode<KeyT, ContentT>*> &children()
91  {
92  return _children;
93  }
94 
96  {
97  return p_root;
98  }
99 
101  {
102  return p_root;
103  }
104 
106  {
107  if (p_parent == parent) return;
108  if (p_parent != NULL) p_parent->_removeChild(this);
109  p_parent = parent;
110  if (parent != NULL) parent->_addChild(this);
111  _updateRoot();
112  }
113 
115  {
116  child->reparent(this);
117  }
118 
120  {
121  child->reparent(NULL);
122  }
123 
124  private:
125 
126  void _removeChild(BasicTreeNode<KeyT, ContentT>* child)
127  {
128  _children.remove(child);
129  }
130 
131  void _addChild(BasicTreeNode<KeyT, ContentT>* child)
132  {
133  _children.push_back(child);
134  }
135 
136  void _updateRoot()
137  {
138  if (p_parent == NULL) p_root = this;
139  else p_root = p_parent->p_root;
140  for (typename std::list<BasicTreeNode<KeyT, ContentT>*>::iterator
141  it = _children.begin(); it != _children.end(); ++it)
142  (*it)->_updateRoot();
143  }
144 
145  KeyT _key;
146  ContentT _content;
147 
149  std::list<BasicTreeNode<KeyT, ContentT>*> _children;
150 
152 
153  };
154 
155  template<typename KeyT, typename ContentT>
156  std::ostream &operator<<(
157  std::ostream& os, BasicTreeNode<KeyT,ContentT> const &n)
158  {
159  os << n.key() << " ( ";
160  typename std::list<BasicTreeNode<KeyT, ContentT>*>::const_iterator it;
161  for (it = n.children().begin(); it != n.children().end(); ++it)
162  {
163  os << **it << " ";
164  }
165  os << ")";
166  return os;
167  }
168 
169 }
170 
171 #endif
void setContent(const ContentT &content)
Definition: ATBBasicTree.hh:70
BasicTreeNode< KeyT, ContentT > const * parent() const
Definition: ATBBasicTree.hh:75
KeyT const key() const
Definition: ATBBasicTree.hh:55
std::ostream & operator<<(std::ostream &os, BasicTreeNode< KeyT, ContentT > const &n)
void reparent(BasicTreeNode< KeyT, ContentT > *parent)
BasicTreeNode(KeyT const &key, ContentT const &content=ContentT())
Definition: ATBBasicTree.hh:43
BasicTreeNode< KeyT, ContentT > const * root() const
Definition: ATBBasicTree.hh:95
BasicTreeNode< KeyT, ContentT > * parent()
Definition: ATBBasicTree.hh:80
std::list< BasicTreeNode< KeyT, ContentT > * > & children()
Definition: ATBBasicTree.hh:90
std::list< BasicTreeNode< KeyT, ContentT > * > const & children() const
Definition: ATBBasicTree.hh:85
void addChild(BasicTreeNode< KeyT, ContentT > *child)
BasicTreeNode< KeyT, ContentT > * root()
void setKey(const KeyT &key)
Definition: ATBBasicTree.hh:60
ContentT const content() const
Definition: ATBBasicTree.hh:65
void removeChild(BasicTreeNode< KeyT, ContentT > *child)