MultiAgentDecisionProcess  Release 0.2.1
TreeNode.h
Go to the documentation of this file.
1 
28 /* Only include this header file once. */
29 #ifndef _TREENODE_H_
30 #define _TREENODE_H_ 1
31 
32 /* the include directives */
33 #include <iostream>
34 #include <map>
35 #include "Globals.h"
36 #include "E.h"
37 
38 
39 
40 #define DEBUG_TREENODE 0
41 
52 template <class Tcontained >
53 class TreeNode
54 {
55  private:
56  protected:
59  std::map< Index, TreeNode<Tcontained>* > _m_successor;
68  Tcontained* _m_containedElem;
69 
70 
71 
72  public:
73  // Constructor, destructor and copy assignment.
76  {
77  _m_index = 42; // weird number to help track it down in gdb etc
78  _m_indexValid = false;
79  _m_containedElem = 0;
80  _m_pred = 0;
81  }
82 
83  TreeNode(Tcontained *const oh)
84  {
85  _m_index = 0;
86  _m_indexValid = false;
87  _m_containedElem = oh;
88  _m_pred = 0;
89  }
91  TreeNode(const TreeNode& a)
92  {
94  _m_pred=a._m_pred;
97  _m_containedElem=new Tcontained(*a._m_containedElem);
98 
99 #if DEBUG_TREENODE
100  cerr << "Cloning TreeNode. This node ";
101  PrintThisNode();
102  cerr << endl;
103 #endif
104  }
105 
107  virtual ~TreeNode()
108  {
109 #if DEBUG_TREENODE
110  cerr << "Deleting TreeNode. This node ";
111  PrintThisNode();cerr << endl;
112 #endif
113  delete(_m_containedElem);
114 
115  while(!_m_successor.empty())
116  {
117  delete _m_successor.begin()->second; // recursively
118  // delete the
119  // rest of the
120  // tree
121  _m_successor.erase(_m_successor.begin());
122  }
123  }
124 
125  //operators:
126 
127  //data manipulation (set) functions:
129  void SetIndex(Index i){_m_index = i; _m_indexValid = true; };
134  void SetSuccessor(Index sucI, TreeNode<Tcontained>* suc);
140  {_m_pred = pred;}
141 
142  //get (data) functions:
143 
145  TreeNode* GetSuccessor(Index sucI);
148  {return(_m_pred);}
149 
152  Index GetIndex() const
153  {
154  if(_m_indexValid)
155  return(_m_index);
156  else
157  throw E("This TreeNode's index is invalid (not yet set)");
158  }
160  Tcontained* GetContainedElement() const {return(_m_containedElem);};
161 
163  bool ExistsSuccessor(Index sucI);
164 
167  void Print() const;
170  void PrintThisNode() const;
171 
172 };
173 
174 template <class Tcontained>
177 {
178  if( ExistsSuccessor(sucI) )
179  {
180 #if DEBUG_TREENODE
181  cout << "_m_successor["<< sucI<< "] already set: overwriting!\n";
182 #endif
183  _m_successor[sucI] = suc;
184  }
185  else
186  {
187  std::pair< Index, TreeNode<Tcontained>* > arg_pair =
188  std::make_pair(sucI, suc);
189  std::pair< typename std::map< Index, TreeNode<Tcontained>* >::iterator, bool>
190  result_pair;
191  result_pair = _m_successor.insert(arg_pair);
192  if(result_pair.second == false)
193  {
194  std::stringstream ss;
195  ss << "TreeNode<Tcontained>::SetSuccessor insertion failed, "
196  << "but _m_successor["<< sucI<< "] wasn't already set ?!?";
197  throw(E(ss));
198  }
199  }
200 
201  suc->SetPredeccessor(this);
202 }
203 
204 template <class Tcontained>
206 {
207  if(_m_successor.find(sucI)==_m_successor.end())
208  throw EInvalidIndex("TreeNode::GetSuccessor successor not found");
209  else
210  return(_m_successor[sucI]);
211 }
212 
213 template <class Tcontained>
215 {
216  if(_m_successor.find(sucI)==_m_successor.end())
217  return(false);
218  else
219  return(true);
220 }
221 
222 
223 template <class Tcontained>
225 {
226  if(_m_containedElem != 0)
227  {
228  std::cout << "index: "<<_m_index<<" - ";
229  _m_containedElem->Print();
230  }
231 }
232 
233 template <class Tcontained>
235 {
236  if(_m_containedElem != 0)
237  {
238 
239  std::cout << "index: ";
240  if(_m_indexValid)
241  std::cout<< _m_index;
242  else
243  std::cout << "INVALID";
244  std::cout << " - ";
245  _m_containedElem->Print();
246  std::cout << std::endl;
247  //typname dependent on template should be called using typename
248  typename std::map< Index, TreeNode<Tcontained>*>::const_iterator it =
249  _m_successor.begin();
250  while(it != _m_successor.end())
251  {
252  if(it->second != 0) it->second->Print();
253  it++;
254  }
255  }
256 }
257 
258 #endif /* !_TREENODE_H_ */
259 
260 
261 // Local Variables: ***
262 // mode:c++ ***
263 // End: ***