StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StiCompositeTreeNode.h
1 //StiCompositeTreeNode
2 //M.L. Miller (Yale Software)
3 //07/01
4 
59 #ifndef StiCompositeTreeNode_H
60 #define StiCompositeTreeNode_H
61 
62 #include "Stiostream.h"
63 #include <iterator>
64 #include <vector>
65 #include <string>
66 #include <algorithm>
67 
68 using namespace std;
69 
70 /*
71  using std::find;
72  using std::vector;
73  using std::string;
74  using std::cout;
75  using std::ostream;
76  using std::endl;
77 */
78 
83 {
84  StiOrderKey()
85  : key(0.), index(0) {};
86  StiOrderKey(double k, unsigned int i)
87  : key(k), index(i) {};
88 
89  double key;
90  unsigned int index;
91 };
92 
93 template <class T>
95 {
96 public:
97 
100 
102  virtual ~StiCompositeTreeNode();
103 
104  //Sets
105  void reset(){;}
106  void unset(){;}
107 
109  void setName(const string&);
110 
112  void setOrderKey(const StiOrderKey&);
113 
115  void setData(T*);
116 
117  //Gets
118 
120  const string& getName() const;
121 
123  unsigned int getChildCount() const;
124 
126  StiCompositeTreeNode* getParent() const;
127 
129  const StiOrderKey& getOrderKey() const;
130 
132  T* getData() const;
133 
134  //Action
135 
137  virtual void add(StiCompositeTreeNode*);
138 
139 public:
140  //provide random access iterators to daughters
141 
143  typedef vector<StiCompositeTreeNode *> vec_type;
144 
146  typedef vector<StiCompositeTreeNode *> StiCompositeTreeNodeVector;
147 
149  typename vec_type::iterator whereInParent() {
150  return (mparent) ? (mparent->begin()+mkey.index) : mVec.end();
151  }
152 
154  typename vec_type::iterator begin() {return mVec.begin();}
155 
157  typename vec_type::iterator end() {return mVec.end();}
158 
160  typename vec_type::const_iterator begin() const {return mVec.begin();}
161 
163  typename vec_type::const_iterator end() const {return mVec.end();}
164 
166  typename vec_type::reverse_iterator rbegin() {return mVec.rbegin();}
167 
169  typename vec_type::reverse_iterator rend() {return mVec.rend();}
170 
172  typename vec_type::const_reverse_iterator rbegin() const {return mVec.rbegin();}
173 
175  typename vec_type::const_reverse_iterator rend() const {return mVec.rend();}
176 
177 private:
178 
181  void setParent(StiCompositeTreeNode* val);
182 
184  vec_type mVec;
185 
187  StiCompositeTreeNode* mparent;
188 
190  T* mdata;
191 
193  StiOrderKey mkey;
194 
196  string mname;
197 };
198 
199 //Implementation
200 
204 template <class T>
206  : mparent(0), mdata(0)
207 {
208  mkey.key=0.;
209  mkey.index=0;
210 }
211 
212 template <class T>
214 {
215 }
216 
217 template <class T>
218 inline void StiCompositeTreeNode<T>::setName(const string& val)
219 {
220  mname = val;
221 }
222 
223 template <class T>
225 {
226  mkey=val;
227 }
228 
232 template <class T>
234 {
235  assert(!mdata);
236  mdata=val;
237 }
238 
239 template <class T>
240 inline const string& StiCompositeTreeNode<T>::getName() const
241 {
242  return mname;
243 }
244 
245 template <class T>
246 inline unsigned int StiCompositeTreeNode<T>::getChildCount() const
247 {
248  return mVec.size();
249 }
250 
251 template <class T>
254 {
255  return mparent;
256 }
257 
258 template <class T>
260 {
261  return mkey;
262 }
263 
264 template <class T>
266 {
267  return mdata;
268 }
269 
280 template <class T>
282 {
283  if (!newChild) return;
284  typename StiCompositeTreeNodeVector::iterator where = find(mVec.begin(), mVec.end(),
285  newChild);
286  //Remove if we see an efficiency penalty
287  if (where!=end()) {
288  cout <<"StiCompositeTreeNode::add(). ERROR:\t";
289  cout <<"Child Already exists. Abort"<<endl;
290  return;
291  }
292  //else add to list, set parent
293  newChild->setParent(this);
294  mVec.push_back(newChild);
295 }
296 
300 template <class T>
302 {
303  if (mparent) {
304  cout <<"StiCompositeTreeNode::setParent()\tError:\t";
305  cout <<"parent already exists"<<endl;
306  return;
307  }
308  mparent=val;
309  return;
310 }
311 
312 // \warning This iterator makes no sense for nodes with no parent. In
313 
314 /* \warning This iterator makes no sense for nodes with no parent. In
315  that case whereInParent() returns end(). Therefore, a safe call to whereInParent()
316  would be as follows: <code> \n
317  StiCompositeTreeNode<T>::vec_type::iterator where = node->whereInParent(); \n
318  if (where!=node->end()) {\\you're ok \n
319  }
320  <\code>
321 
322  template <class T>
323  StiCompositeTreeNode<T>::vec_type::iterator //return type
324  StiCompositeTreeNode<T>::whereInParent()
325  {
326  return (mparent) ? (mparent->begin()+mkey.index) : mVec.end();
327  }
328 
329  template <class T>
330  StiCompositeTreeNode<T>::vec_type::iterator //return type
331  StiCompositeTreeNode<T>::begin()
332  {
333  return mVec.begin();
334  }
335 
336  template <class T>
337  StiCompositeTreeNode<T>::vec_type::iterator //return type
338  StiCompositeTreeNode<T>::end()
339  {
340  return mVec.end();
341  }
342 
343  template <class T>
344  StiCompositeTreeNode<T>::vec_type::const_iterator //return type
345  StiCompositeTreeNode<T>::begin() const
346  {
347  return mVec.begin();
348  }
349 
350  template <class T>
351  StiCompositeTreeNode<T>::vec_type::const_iterator //return type
352  StiCompositeTreeNode<T>::end() const
353  {
354  return mVec.end();
355  }
356 
357  template <class T>
358  StiCompositeTreeNode<T>::vec_type::reverse_iterator //return type
359  StiCompositeTreeNode<T>::rbegin()
360  {
361  return mVec.rbegin();
362  }
363 
364  template <class T>
365  StiCompositeTreeNode<T>::vec_type::reverse_iterator //return type
366  StiCompositeTreeNode<T>::rend()
367  {
368  return mVec.rend();
369  }
370 
371  template <class T>
372  StiCompositeTreeNode<T>::vec_type::const_reverse_iterator //return type
373  StiCompositeTreeNode<T>::rbegin() const
374  {
375  return mVec.rbegin();
376  }
377 
378  template <class T>
379  StiCompositeTreeNode<T>::vec_type::const_reverse_iterator //return type
380  StiCompositeTreeNode<T>::rend() const
381  {
382  return mVec.rend();
383  }
384 */
385 
386 //For now, include some typdefs that will make for easy user includes
387 #include "StiDetector.h"
388 typedef StiDetector data_t;
389 
390 #ifndef __CINT__
392 #else
393 class StiDetectorNode;
394 #endif
395 typedef vector<StiDetectorNode*> StiDetectorNodeVector;
396 
397 //non-members
398 inline ostream& operator<<(ostream& os, const StiOrderKey& theKey)
399 {
400  return os<<"key: "<<theKey.key<<" index: "<<theKey.index;
401 }
402 
403 #endif
const string & getName() const
Return the name of the node.
virtual ~StiCompositeTreeNode()
Default Destructor.
vec_type::reverse_iterator rbegin()
Provide reverse iterator to the beginning of the vector of children.
T * getData() const
Return a (non-const!) pointer to the data hung on this node.
vector< StiCompositeTreeNode * > vec_type
For internal convenience.
vec_type::reverse_iterator rend()
Provide reverse iterator to the end of the vector of children.
vec_type::iterator whereInParent()
Provide the iterator into the parent that can be dereferenced to get this node.
unsigned int getChildCount() const
Return the number of children that belong to this node.
vec_type::const_reverse_iterator rbegin() const
Provide const_reverse_iterator tot he beginning of the vector of children.
vec_type::const_iterator end() const
Provied const_iterator to the end of the vector of children.
const StiOrderKey & getOrderKey() const
Return a reference to the the orderkey of this node.
vec_type::const_reverse_iterator rend() const
Provide const_reverse_iterator to the end of the vector of children.
vector< StiCompositeTreeNode * > StiCompositeTreeNodeVector
For internal convenience.
StiCompositeTreeNode()
We provide only a default constructor.
virtual void add(StiCompositeTreeNode *)
Add a child to this node.
vec_type::const_iterator begin() const
Provide const_iterator to the beginning of the vector of children.
void setName(const string &)
Set the name of the node.
vec_type::iterator begin()
Provide random access iterator to the beginning of the vector of children.
vec_type::iterator end()
Provide random access iterator to the end of the vector of children.
StiCompositeTreeNode * getParent() const
Return a (non-const!) pointer to the parent of this node.
void setData(T *)
Set the data to be hung on the node.
void setOrderKey(const StiOrderKey &)
Set the order-key for the node.