MINT2
DDTree.h
Go to the documentation of this file.
1 #ifndef BASIC_DDTREE_HH
2 #define BASIC_DDTREE_HH
3 // author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
4 // status: Mon 9 Feb 2009 19:18:02 GMT
5 
6 #include <vector>
7 #include <algorithm>
8 #include <iostream>
9 #include <sstream>
10 
11 #include "Mint/counted_ptr.h"
12 
13 template <typename T>
15  public:
16  bool operator()(const T a, const T b){
17  if(0 == a && 0==b) return false;
18  if(0 == a && 0!=b) return true;
19  if(0 !=a && 0==b) return false;
20  return a->getVal() < b->getVal();
21  }
22 };
23 
24 template <typename T>
26  public:
27  bool operator()(const T a, const T b){
28  if(0 == a && 0==b) return true;
29  if(0 == a && 0!=b) return false;
30  if(0 !=a && 0==b) return false;
31  return a->getVal() == b->getVal();
32  }
33 };
34 
35 
36 template<typename ValueType>
37 class DDTree{
38  protected:
39  // Three data members: value, and list of ptrs to daughters, ptr to parent.
40  typename std::vector< MINT::counted_ptr< DDTree<ValueType> > > daughters;
41  ValueType _thisVal;
42  // private routines to deal with parentny
44  // which this tree node is the daughter of.
45  //...don't use, I think it's dangerous stuff. Left in for debugging.
46 
47  // MINT::counted_ptr<DDTree<ValueType> > Clone(DDTree<ValueType>* parent=0) const{
48  // MINT::counted_ptr<DDTree<ValueType> > top(new DDTree<ValueType>(_thisVal, parent));
49  //
50  // top->daughters.resize(daughters.size());
51  // for(unsigned int i=0; i < daughters.size(); i++){
52  // top->daughters[i] = daughters[i]->Clone(top.get());
53  // }
54  // return top;
55  //}
56 
57  void setParent(const DDTree<ValueType>* parent=0){_parentPtr=parent;}
58  // above private -> only a tree can mess with parentny
59 
61  if(daughters.empty()) return;
62  for(unsigned int i=0; i<daughters.size(); i++){
63  if(0!=daughters[i]) {
64  daughters[i]->setParent(0);
65  // Now using counted pointers - no need
66  // to delete daughters, just "disconnect"
67  //daughters[i]->deleteAllDaughters();
68  //delete daughters[i]; // MINT::counted_ptr, no need to delete
69  }
70  }
71  daughters.clear();
72  }
73  public:
74 
75  template<typename SORTERTYPE> void sortDaughtersBy(){
76  SORTERTYPE sorter;
77  stable_sort(daughters.begin(), daughters.end(), sorter);
78  // lessByPointedTo<ValueType> sorter;
79  // daughters.sort(sorter);
80  }
81 
82  template<typename SORTERTYPE> void sortAllBy(){
83  sortDaughtersBy<SORTERTYPE>();
84  for(unsigned int i=0; i<daughters.size(); i++){
85  daughters[i]->template sortDaughtersBy<SORTERTYPE>();
86  }
87  }
88 
89  void sortDaughters(){
90  return sortDaughtersBy<lessByPointedTo< MINT::counted_ptr<DDTree<ValueType> > > >();
91  }
92 
93  bool isFinalState() const{
94  return nDgtr() == 0;
95  }
96  int nDgtr() const{
97  return daughters.size();
98  }
99  void setVal(const ValueType& m){
100  _thisVal = m;
101  }
102  const ValueType& getVal() const{
103  return _thisVal;
104  }
105  ValueType& getVal(){
106  return _thisVal;
107  }
108  const ValueType& getDgtrVal(int i) const{
109  return daughters[i]->getVal();
110  }
111  ValueType& getDgtrVal(int i){
112  return daughters[i]->getVal();
113  }
115  if(i < 0 || i >= nDgtr()){
117  }
118  return daughters[i];
119  }
121  if(i < 0 || i >= nDgtr()){
123  }
124  return daughters[i];
125  }
126 
127  int setDgtrSize(int i){
128  daughters.resize(i);
129  return daughters.size();
130  }
131  void setDgtr(unsigned int i, const MINT::counted_ptr<DDTree<ValueType> >& tr){
132  if(i >= daughters.size()) daughters.resize(i+0);
133  daughters[i] = tr;
134  }
135 
136  DDTree() : _parentPtr(0){};
137 
138  //DDTree(const std::string& s)
139  // : _parentPtr(0){
140  //std::cout << "called dummy constructor for DDTree from string"
141  // << s
142  //<< std::endl;
143  //}
144  DDTree(const ValueType& pdg_id)
145  : _thisVal(pdg_id)
146  , _parentPtr(0){
147  daughters.clear();
148  }
149  DDTree(const DDTree<ValueType>* parent) : _parentPtr(parent){};
150  DDTree(const ValueType& val, const DDTree<ValueType>* parent)
151  : _thisVal(val)
152  , _parentPtr(parent){};
153 
154  bool hasParent() const{
155  return ! (0 == _parentPtr);
156  }
157  template<typename COMPATIBLE_TYPE>
160  //CloneVariableType(counted_ptr<DDTree<COMPATIBLE_TYPE> > parent=0) const{ // BIG DEBUG
161 
163  top(new DDTree<COMPATIBLE_TYPE>(static_cast<COMPATIBLE_TYPE>(_thisVal)
164  , parent)
165  );
166 
167  top->setDgtrSize(daughters.size());
168  for(unsigned int i=0; i < daughters.size(); i++){
169  top->setDgtr(i, daughters[i]->template CloneVariableType<COMPATIBLE_TYPE>(top.get()));
170  }
171  return top;
172  }
174  Clone(DDTree<ValueType>* parent=0) const{
175  return CloneVariableType<ValueType>(parent);
176  }
177 
178  template<typename COMPATIBLE_ITEM>
179  void Copy(const DDTree<COMPATIBLE_ITEM>& other
180  ){
181  _thisVal = (ValueType) (other.getVal());
182  daughters.resize(other.nDgtr());
183  for(int i=0; i < other.nDgtr(); i++){
184  daughters[i] = other.getDgtrTreePtr(i)->template CloneVariableType<ValueType>(this);
185  }
186  return;
187  }
188 
189  DDTree(const DDTree<ValueType>& other)
190  :_parentPtr(0){
191  this->Copy(other);
192  }
193 
194  template<typename COMPATIBLE_ITEM>
196  : _parentPtr(0){
197  this->Copy(other);
198  }
200  this->Copy(other);
201  return *this;
202  }
203 
204  template<typename COMPATIBLE_ITEM>
206  this->Copy(other);
207  return *this;
208  }
209 
211  addDgtr(const DDTree<ValueType>* treePtr){
212  MINT::counted_ptr< DDTree<ValueType> > newDgtr(treePtr->Clone(this));
213  daughters.push_back(newDgtr);
214  //sortDaughters();
215  return newDgtr;
216  }
217  MINT::counted_ptr<DDTree<ValueType> > addDgtr(const ValueType& pdg_id){
218  MINT::counted_ptr< DDTree<ValueType> > dgtrTree(new DDTree(pdg_id, this));
219  daughters.push_back(dgtrTree);
220  //sortDaughters();
221  return dgtrTree;
222  }
224  addDgtr(const ValueType& pdg_id1, const ValueType& pdg_id2){
225  addDgtr(pdg_id1);
226  //sortDaughters();
227  return addDgtr(pdg_id2);
228  }
229  MINT::counted_ptr<DDTree<ValueType> > addDgtr(const ValueType& pdg_id1
230  , const ValueType& pdg_id2
231  , const ValueType& pdg_id3
232  ){
233  addDgtr(pdg_id1);
234  addDgtr(pdg_id2);
235  return addDgtr(pdg_id3);
236  }
237  MINT::counted_ptr<DDTree<ValueType> > addDgtr(const ValueType& pdg_id1
238  , const ValueType& pdg_id2
239  , const ValueType& pdg_id3
240  , const ValueType& pdg_id4
241  ){
242  addDgtr(pdg_id1);
243  addDgtr(pdg_id2);
244  addDgtr(pdg_id3);
245  return addDgtr(pdg_id4);
246  }
247  void addDgtrArray(ValueType pdg_id[], int n){
248  for(int i=0; i<n; i++){
249  addDgtr(pdg_id[i]);
250  }
251  }
252  void addDgtrArray(DDTree<ValueType>* treePtr[], int n){
253  for(int i=0; i<n; i++){
254  addDgtr(treePtr[i]);
255  }
256  }
257  virtual ~DDTree(){
259  }
260 
261  const DDTree<ValueType>* getParent() const{
262  return _parentPtr;
263  }
264 
265  int nSisters() const{
266  if(0 == _parentPtr) return 0;
267  return _parentPtr->nDgtr() -1;
268  }
269 
270  std::vector<MINT::const_counted_ptr<DDTree<ValueType> > >
271  getSistersTrees() const{
272  // i.e. parent's daughters,except yourself
273  std::vector<MINT::const_counted_ptr< DDTree<ValueType> > > sistersTree;
274  if(0 == _parentPtr) return sistersTree;
275 
276  for(int i=0; i < _parentPtr->nDgtr(); i++){
277  if(_parentPtr->daughters[i].get() != this){
278  sistersTree.push_back(_parentPtr->daughters[i]);
279  }
280  }
281  return sistersTree;
282  }
283  int nGenerations() const{
284  int nMax=0;
285  if(! nDgtr()) return 1;
286  for(int i=0; i<nDgtr(); i++){
287  int n = daughters[i]->nGenerations();
288  if(n > nMax) nMax = n;
289  }
290  return nMax + 1;
291  }
292 
293  std::vector<const ValueType*> finalState() const{
294  std::vector<const ValueType*> listOfStableDaughters;
295  finalState(listOfStableDaughters);
296  return listOfStableDaughters;
297  }
298  std::vector<ValueType*> finalState(){
299  std::vector<ValueType*> listOfStableDaughters;
300  finalState(listOfStableDaughters);
301  return listOfStableDaughters;
302  }
303 
306  listOfStableDaughtersPtr( new std::vector<const ValueType*> );
307  finalState(*listOfStableDaughtersPtr);
308  return listOfStableDaughtersPtr;
309  }
312  listOfStableDaughtersPtr( new std::vector<ValueType*> );
313  finalState(*listOfStableDaughtersPtr);
314  return listOfStableDaughtersPtr;
315  }
316  void finalState(std::vector<const ValueType*>& theList) const{
317  if(daughters.empty()){
318  theList.push_back(&_thisVal);
319  }else{
320  for(int i=0; i<nDgtr(); i++){
321  daughters[i]->finalState(theList);
322  }
323  }
324  }
325  void finalState(std::vector<ValueType*>& theList){
326  if(daughters.empty()){
327  theList.push_back(&_thisVal);
328  }else{
329  for(int i=0; i<nDgtr(); i++){
330  daughters[i]->finalState(theList);
331  }
332  }
333  }
334 
335  template<typename COMPARABLE_CLASS>
336  std::vector<ValueType*>
337  finalStateInThisOrder(const std::vector<COMPARABLE_CLASS>& pattern){
338  std::vector<ValueType*> stableDaughters = finalState();
339 
340  std::vector<ValueType*> sorted;
341 
342  if(stableDaughters.size() != pattern.size()){
343  return sorted;
344  }
345 
346  for(unsigned int i=0; i<pattern.size(); i++){
347  for(typename std::vector<ValueType*>::iterator jt = stableDaughters.begin();
348  jt != stableDaughters.end();
349  jt++){
350  if( *(*jt) == pattern[i] ){
351  sorted.push_back(*jt);
352  stableDaughters.erase(jt);
353  break;
354  }
355  }
356  }
357  return sorted;
358  }
359 
360  void print(std::ostream& os = std::cout
361  , std::string indentString=""
362  ) const{
363  os << " ";
364  os << _thisVal << std::endl;
365  for(int i=0; i<nDgtr(); i++){
366  if(i==nDgtr()-1){
367  os << indentString + " '--";
368  daughters[i]->print(os, indentString + " ");
369  } else{
370  os << indentString + " |--";
371  daughters[i]->print(os, indentString + " | ");
372  }
373  }
374  }
375  void oneLiner(std::stringstream& seam
376  , int generation=0) const{
377  seam << _thisVal;
378  if(nDgtr() <= 0) return;
379  if(generation > 0) seam << "(";
380  seam << "->";
381  for(int i=0; i<nDgtr(); i++){
382  daughters[i]->oneLiner(seam, generation + 1);
383  if(i < nDgtr()-1) seam << ",";
384  }
385  if(generation > 0) seam << ")";
386  }
387  void oneLiner(std::string& ring) const{
388  std::stringstream seam;
389  oneLiner(seam);
390  seam >> ring;
391  }
392  std::string oneLiner() const{
393  std::string ring;
394  oneLiner(ring);
395  return ring;
396  }
397 
398 };
399 
400 template <typename ValueType>
401 std::ostream& operator<<(std::ostream& os
402  , const DDTree<ValueType>& theTree){
403  theTree.print(os);
404  return os;
405 }
406 
407 template <typename ValueType>
408 std::stringstream& operator<<(std::stringstream& seam
409  , const DDTree<ValueType>& theTree){
410  theTree.oneLiner(seam);
411  return seam;
412 }
413 
414 #endif
415 //
ValueType & getVal()
Definition: DDTree.h:105
DDTree(const ValueType &val, const DDTree< ValueType > *parent)
Definition: DDTree.h:150
std::vector< MINT::const_counted_ptr< DDTree< ValueType > > > getSistersTrees() const
Definition: DDTree.h:271
DDTree(const DDTree< ValueType > &other)
Definition: DDTree.h:189
void sortDaughters()
Definition: DDTree.h:89
int setDgtrSize(int i)
Definition: DDTree.h:127
MINT::counted_ptr< std::vector< const ValueType * > > finalStatePtr() const
Definition: DDTree.h:304
DDTree(const DDTree< COMPATIBLE_ITEM > &other)
Definition: DDTree.h:195
MINT::counted_ptr< DDTree< ValueType > > addDgtr(const DDTree< ValueType > *treePtr)
Definition: DDTree.h:211
MINT::counted_ptr< DDTree< ValueType > > addDgtr(const ValueType &pdg_id1, const ValueType &pdg_id2, const ValueType &pdg_id3)
Definition: DDTree.h:229
void oneLiner(std::string &ring) const
Definition: DDTree.h:387
DDTree(const ValueType &pdg_id)
Definition: DDTree.h:144
DDTree< ValueType > operator=(const DDTree< COMPATIBLE_ITEM > &other)
Definition: DDTree.h:205
bool operator()(const T a, const T b)
Definition: DDTree.h:16
void finalState(std::vector< const ValueType * > &theList) const
Definition: DDTree.h:316
void addDgtrArray(DDTree< ValueType > *treePtr[], int n)
Definition: DDTree.h:252
MINT::counted_ptr< std::vector< ValueType * > > finalStatePtr()
Definition: DDTree.h:310
std::vector< MINT::counted_ptr< DDTree< ValueType > > > daughters
Definition: DDTree.h:40
void addDgtrArray(ValueType pdg_id[], int n)
Definition: DDTree.h:247
const ValueType & getVal() const
Definition: DDTree.h:102
void setParent(const DDTree< ValueType > *parent=0)
Definition: DDTree.h:57
void print(std::ostream &os=std::cout, std::string indentString="") const
Definition: DDTree.h:360
bool isFinalState() const
Definition: DDTree.h:93
void setVal(const ValueType &m)
Definition: DDTree.h:99
DDTree< ValueType > operator=(const DDTree< ValueType > &other)
Definition: DDTree.h:199
MINT::counted_ptr< DDTree< ValueType > > addDgtr(const ValueType &pdg_id1, const ValueType &pdg_id2, const ValueType &pdg_id3, const ValueType &pdg_id4)
Definition: DDTree.h:237
void sortAllBy()
Definition: DDTree.h:82
MINT::counted_ptr< DDTree< ValueType > > Clone(DDTree< ValueType > *parent=0) const
Definition: DDTree.h:174
void setDgtr(unsigned int i, const MINT::counted_ptr< DDTree< ValueType > > &tr)
Definition: DDTree.h:131
MINT::const_counted_ptr< DDTree< ValueType > > getDgtrTreePtr(int i) const
Definition: DDTree.h:114
bool hasParent() const
Definition: DDTree.h:154
DDTree()
Definition: DDTree.h:136
MINT::counted_ptr< DDTree< ValueType > > addDgtr(const ValueType &pdg_id)
Definition: DDTree.h:217
void disconnectAllDaughters()
Definition: DDTree.h:60
void Copy(const DDTree< COMPATIBLE_ITEM > &other)
Definition: DDTree.h:179
static const double m
Definition: DDTree.h:37
const DDTree< ValueType > * _parentPtr
Definition: DDTree.h:43
DDTree(const DDTree< ValueType > *parent)
Definition: DDTree.h:149
bool operator()(const T a, const T b)
Definition: DDTree.h:27
int nGenerations() const
Definition: DDTree.h:283
MINT::counted_ptr< DDTree< ValueType > > addDgtr(const ValueType &pdg_id1, const ValueType &pdg_id2)
Definition: DDTree.h:224
std::vector< ValueType * > finalStateInThisOrder(const std::vector< COMPARABLE_CLASS > &pattern)
Definition: DDTree.h:337
int nSisters() const
Definition: DDTree.h:265
std::vector< const ValueType * > finalState() const
Definition: DDTree.h:293
std::ostream & operator<<(std::ostream &os, const DDTree< ValueType > &theTree)
Definition: DDTree.h:401
int nDgtr() const
Definition: DDTree.h:96
void oneLiner(std::stringstream &seam, int generation=0) const
Definition: DDTree.h:375
virtual ~DDTree()
Definition: DDTree.h:257
ValueType _thisVal
Definition: DDTree.h:41
MINT::counted_ptr< DDTree< ValueType > > getDgtrTreePtr(int i)
Definition: DDTree.h:120
std::string oneLiner() const
Definition: DDTree.h:392
const DDTree< ValueType > * getParent() const
Definition: DDTree.h:261
MINT::counted_ptr< DDTree< COMPATIBLE_TYPE > > CloneVariableType(DDTree< COMPATIBLE_TYPE > *parent=0) const
Definition: DDTree.h:159
void finalState(std::vector< ValueType * > &theList)
Definition: DDTree.h:325
void sortDaughtersBy()
Definition: DDTree.h:75
std::vector< ValueType * > finalState()
Definition: DDTree.h:298
const ValueType & getDgtrVal(int i) const
Definition: DDTree.h:108
X * get() const
Definition: counted_ptr.h:123
ValueType & getDgtrVal(int i)
Definition: DDTree.h:111