CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SeedingTree.h
Go to the documentation of this file.
1 #ifndef FastSimulation_Tracking_SeedingTree_h
2 #define FastSimulation_Tracking_SeedingTree_h
3 
4 #include <vector>
5 #include <iostream>
6 #include <algorithm>
7 #include <unordered_set>
8 
9 template<class DATA>
11 {
12  protected:
13  const DATA _data;
14  const std::vector<SeedingNode<DATA>*>& _allNodes;
15  unsigned int _index; //the index in allNodes
16  int _parentIndex; //the parent's index in allNodes
17  int _childIndex; //the index of this Node in its parent's children vector
18  unsigned int _depth; //the depth within the tree (for root: depth=0 && parentIndex=-1
19  std::vector<unsigned int> _children;
20 
21  public:
22  SeedingNode(const DATA& data, std::vector<SeedingNode*>& allNodes, int parentIndex=-1):
23  _data(data),
24  _allNodes(allNodes),
25  _index(allNodes.size()),
26  _parentIndex(parentIndex)
27  {
28  allNodes.push_back(this);
29 
30  if (_parentIndex>=0)
31  {
32  SeedingNode* parent = allNodes[_parentIndex];
33  _depth=parent->_depth+1;
34  _childIndex=parent->_children.size();
35  parent->_children.push_back(_index);
36 
37  }
38  else
39  {
40  _depth=0;
41  _childIndex=-1;
42  }
43  }
44 
45  void sort(std::vector<SeedingNode<DATA>*>& allNodes,unsigned int parentIndex)
46  {
47  _parentIndex=parentIndex;
48  _index=allNodes.size();
49  if (_parentIndex>=0)
50  {
51  allNodes[_parentIndex]->_children[_childIndex]=_index;
52  }
53  allNodes.push_back(this);
54  for (unsigned int ichild=0; ichild<_children.size();++ichild)
55  {
56  _allNodes[_children[ichild]]->sort(allNodes,_index);
57  }
58  }
59 
60  bool insert(const std::vector<DATA>& dataList, std::vector<SeedingNode<DATA>*>& allNodes)
61  {
62  if (_depth+1>=dataList.size())
63  {
64  //std::cout<<"\tSeedingNode::insert: return"<<std::endl;
65  return false;
66  }
67  for (unsigned int ichild=0; ichild<_children.size();++ichild)
68  {
69  if (allNodes[_children[ichild]]->getData()==dataList[_depth+1])
70  {
71  //std::cout<<"\tSeedingNode::insert: has child "<<dataList[_depth+1].print()<<std::endl;
72  return allNodes[_children[ichild]]->insert(dataList,allNodes);
73  }
74  }
75  //std::cout<<"\tSeedingNode::insert: create child "<<dataList[_depth+1].print()<<std::endl;
76  SeedingNode<DATA>* node = new SeedingNode<DATA>(dataList[_depth+1],allNodes,_index);
77  if (node->getDepth()+1>=dataList.size())
78  {
79  return true;
80  }
81  return node->insert(dataList,allNodes);
82  }
83 
84  inline unsigned int getDepth() const
85  {
86  return _depth;
87  }
88 
89  inline const SeedingNode<DATA>* next() const
90  {
91  if (_index+1<_allNodes.size())
92  {
93  return _allNodes[_index+1];
94  }
95  return nullptr;
96  }
97 
98  inline const SeedingNode<DATA>* firstChild() const
99  {
100  if (_children.size()>0)
101  {
102  return _allNodes[_children[0]];
103  }
104  return nullptr;
105  }
106 
107  inline unsigned int getIndex() const
108  {
109  return _index;
110  }
111 
112  inline const SeedingNode* getParent() const
113  {
114  if (_parentIndex>=0)
115  {
116  return _allNodes[_parentIndex];
117  }
118  return nullptr;
119  }
120 
121  inline unsigned int getChildrenSize() const
122  {
123  return _children.size();
124  }
125 
126  inline const SeedingNode<DATA>* getChild(unsigned int ichild) const
127  {
128  return _allNodes[_children[ichild]];
129  }
130 
131  inline unsigned int getChildIndex() const
132  {
133  return _childIndex;
134  }
135 
136  inline const DATA& getData() const
137  {
138  return _data;
139  }
140 
141  void print() const
142  {
143 
144  printf("index=%3i, depth=%2i, childIndex=%2i: ",_index,_depth,_childIndex);
145  for (unsigned int i=0;i<_depth;++i)
146  {
147  std::cout<<" -- ";
148  }
149  printf("[%s, %s] \r\n",_data.toString().c_str(),_data.toIdString().c_str());
150 
151  }
152  void printRecursive() const
153  {
154  print();
155  for (unsigned int ichild=0; ichild<_children.size(); ++ichild)
156  {
157  _allNodes[_children[ichild]]->printRecursive();
158  }
159  }
160 };
161 
162 template<class DATA>
164 {
165  public:
166  typedef std::unordered_set<DATA,typename DATA:: hashfct, typename DATA:: eqfct> SingleSet;
167  //typedef std::set<DATA> SingleSet;
168  protected:
169  std::vector<SeedingNode<DATA>*> _roots;
170  std::vector<SeedingNode<DATA>*> _allNodes;
171 
173  public:
175  {
176  }
177 
178  //returns true if successfully inserted into tree
179  bool insert(const std::vector<DATA>& dataList)
180  {
181  for (unsigned int i = 0; i< dataList.size(); ++i)
182  {
183  _singleSet.insert(dataList[i]);
184  }
185 
186  if (dataList.size()==0)
187  {
188  return false;
189  }
190  for (unsigned int iroot=0; iroot<_roots.size();++iroot)
191  {
192  if (_roots[iroot]->getData()==dataList[0])
193  {
194  //std::cout<<"\tfound root: "<<dataList[0].print()<<std::endl;
195  return _roots[iroot]->insert(dataList,_allNodes);
196  }
197  }
198  //std::cout<<"\tnew root: "<<dataList[0].print()<<std::endl;
199  SeedingNode<DATA>* node = new SeedingNode<DATA>(dataList[0],_allNodes);
200  _roots.push_back(node);
201  return node->insert(dataList,_allNodes);
202  }
203 
204  inline const SingleSet& getSingleSet() const
205  {
206  return _singleSet;
207  }
208 
209  void sort()
210  {
211  //this setups depth first ordered indexes.
212  std::vector<SeedingNode<DATA>*> allNodes;
213  for (unsigned int iroot=0; iroot<_roots.size();++iroot)
214  {
215  _roots[iroot]->sort(allNodes,-1);
216  }
217  _allNodes=allNodes;
218  }
219 
220 
221 
222  inline unsigned int numberOfRoots() const
223  {
224  return _roots.size();
225  }
226 
227  inline unsigned int numberOfNodes() const
228  {
229  return _allNodes.size();
230  }
231 
232  inline const SeedingNode<DATA>* getRoot(unsigned int i) const
233  {
234  if (i<_roots.size())
235  {
236  return _roots[i];
237  }
238  return nullptr;
239  }
240 
241  void printRecursive() const
242  {
243  std::cout<<"SeedingTree: n="<<_allNodes.size()<<" [recursive]"<<std::endl;
244  for (unsigned int iroot=0; iroot<_roots.size();++iroot)
245  {
246  _roots[iroot]->printRecursive();
247  }
248  }
249  void printOrdered() const
250  {
251  std::cout<<"SeedingTree: n="<<_allNodes.size()<<" [ordered]"<<std::endl;
252  for (unsigned int inode=0; inode<_allNodes.size();++inode)
253  {
254  _allNodes[inode]->print();
255  }
256  }
257 
258  void print()
259  {
260  std::cout<<"SeedingTree: n="<<_allNodes.size()<<std::endl;
261  for (unsigned int inode=0; inode<_allNodes.size();++inode)
262  {
263  _allNodes[inode]->print();
264  }
265  }
266 
268  {
269  for (unsigned int iroot=0; iroot<_roots.size();++iroot)
270  {
271  delete _roots[iroot];
272  }
273  _roots.clear();
274  }
275 };
276 
277 #endif
278 
int i
Definition: DBlmapReader.cc:9
int _childIndex
Definition: SeedingTree.h:17
const SeedingNode< DATA > * next() const
Definition: SeedingTree.h:89
parent
Definition: confdb.py:1052
const SingleSet & getSingleSet() const
Definition: SeedingTree.h:204
void printRecursive() const
Definition: SeedingTree.h:241
const SeedingNode< DATA > * firstChild() const
Definition: SeedingTree.h:98
SeedingNode(const DATA &data, std::vector< SeedingNode * > &allNodes, int parentIndex=-1)
Definition: SeedingTree.h:22
const DATA & getData() const
Definition: SeedingTree.h:136
unsigned int getChildrenSize() const
Definition: SeedingTree.h:121
unsigned int getDepth() const
Definition: SeedingTree.h:84
const SeedingNode * getParent() const
Definition: SeedingTree.h:112
std::vector< SeedingNode< DATA > * > _roots
Definition: SeedingTree.h:169
bool insert(const std::vector< DATA > &dataList)
Definition: SeedingTree.h:179
unsigned int numberOfNodes() const
Definition: SeedingTree.h:227
void sort()
Definition: SeedingTree.h:209
unsigned int numberOfRoots() const
Definition: SeedingTree.h:222
std::unordered_set< DATA, typename DATA::hashfct, typename DATA::eqfct > SingleSet
Definition: SeedingTree.h:166
void sort(std::vector< SeedingNode< DATA > * > &allNodes, unsigned int parentIndex)
Definition: SeedingTree.h:45
void printRecursive() const
Definition: SeedingTree.h:152
int _parentIndex
Definition: SeedingTree.h:16
unsigned int _depth
Definition: SeedingTree.h:18
const DATA _data
Definition: SeedingTree.h:13
bool insert(const std::vector< DATA > &dataList, std::vector< SeedingNode< DATA > * > &allNodes)
Definition: SeedingTree.h:60
std::vector< unsigned int > _children
Definition: SeedingTree.h:19
std::vector< SeedingNode< DATA > * > _allNodes
Definition: SeedingTree.h:170
void print() const
Definition: SeedingTree.h:141
const SeedingNode< DATA > * getRoot(unsigned int i) const
Definition: SeedingTree.h:232
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void printOrdered() const
Definition: SeedingTree.h:249
const std::vector< SeedingNode< DATA > * > & _allNodes
Definition: SeedingTree.h:14
tuple cout
Definition: gather_cfg.py:145
unsigned int getIndex() const
Definition: SeedingTree.h:107
SingleSet _singleSet
Definition: SeedingTree.h:172
tuple size
Write out results.
unsigned int getChildIndex() const
Definition: SeedingTree.h:131
const SeedingNode< DATA > * getChild(unsigned int ichild) const
Definition: SeedingTree.h:126
void print()
Definition: SeedingTree.h:258
unsigned int _index
Definition: SeedingTree.h:15