Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
TreeNode.h
Go to the documentation of this file.
1 
12 #ifndef __KOME_CORE_TREE_NODE_H__
13 #define __KOME_CORE_TREE_NODE_H__
14 
15 
16 #include <vector>
17 #include <boost/function.hpp>
18 #include <boost/bind.hpp>
19 #include <algorithm>
20 
21 
22 namespace kome {
23  namespace core {
24 
29  template < typename T > class TreeNode {
30  public:
36  m_parent = NULL;
37  }
38 
43  TreeNode( T& elm ) : m_element( elm ){
44  m_parent = NULL;
45  }
46 
51  virtual ~TreeNode(){
52  clearChildren();
53  }
54 
59  T& getElement(){
60  return m_element;
61  }
62 
67  void setElement( T& elm ){
68  m_element = elm;
69  }
70 
77  return m_parent;
78  }
79 
85  unsigned int getNumberOfChildren(){
86  return m_children.size();
87  }
88 
95  TreeNode* getChild( int index ) {
96  // check parameter
97  if( index < 0 || index > m_children.size() ) {
98  return NULL;
99  }
100 
101  return m_children[index];
102  }
103 
108  void clearChildren() {
109  for( unsigned int i = 0; i < m_children.size(); i++ ){
110  delete m_children[index];
111  }
112  m_children.clear();
113  }
114 
121  TreeNode<T>* child = new TreeNode<T>();
122  m_children.push_back( child );
123  return child;
124  }
125 
132  TreeNode* addChild( T& elm ){
133  TreeNode<T>* child = new TreeNode<T>( elm );
134  m_children.push_back( child );
135  return child;
136  }
137 
147  void sortChildren( boost::function< bool ( TreeNode<T>&, TreeNode<T>& ) > lessFun ) {
148  // check member
149  if( m_children.size() == 0 ) {
150  return;
151  }
152 
153  // sort ( This method is called recursively. )
154  for( unsigned int i = 0; i < m_children.size(); i++ ) {
155  m_children[i]->sortChildre( lessFun );
156  }
157 
158  std::sort( m_children.begin(), m_children.end(), lessFun );
159  }
160 
161  protected:
164 
166  std::vector< TreeNode<T>* > m_children;
167 
170 
176  TreeNode( TreeNode<T>* parent ) : m_element() {
177  m_parent = parent;
178  }
179 
186  TreeNode( TreeNode<T>* parent, T& elm ) : m_element( elm ) {
187  m_parent = parent;
188  }
189  };
190  }
191 }
192 
193 #endif // __KOME_CORE_TREE_NODE_H__
TreeNode(T &elm)
constructor
Definition: TreeNode.h:43
tree structure data management class
Definition: TreeNode.h:29
TreeNode(TreeNode< T > *parent)
constructor
Definition: TreeNode.h:176
TreeNode< T > * m_parent
Definition: TreeNode.h:169
TreeNode * getParentNode()
gets parent node
Definition: TreeNode.h:76
TreeNode * addChild(T &elm)
create child node
Definition: TreeNode.h:132
TreeNode()
constructor
Definition: TreeNode.h:35
void sortChildren(boost::function< bool(TreeNode< T > &, TreeNode< T > &) > lessFun)
sort child nodes
Definition: TreeNode.h:147
virtual ~TreeNode()
destructor
Definition: TreeNode.h:51
#define NULL
Definition: CoreMacros.h:18
void setElement(T &elm)
sets the element that the node has
Definition: TreeNode.h:67
void clearChildren()
remove all child nodes
Definition: TreeNode.h:108
unsigned int getNumberOfChildren()
gets the number of child nodes
Definition: TreeNode.h:85
TreeNode(TreeNode< T > *parent, T &elm)
constructor
Definition: TreeNode.h:186
TreeNode * getChild(int index)
get child node
Definition: TreeNode.h:95
T & getElement()
gets the element that the node has
Definition: TreeNode.h:59
std::vector< TreeNode< T > * > m_children
Definition: TreeNode.h:166
TreeNode * addChild()
create child node
Definition: TreeNode.h:120