CMS 3D CMS Logo

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  return false;
65  }
66  for (unsigned int ichild=0; ichild<_children.size();++ichild)
67  {
68  if (allNodes[_children[ichild]]->getData()==dataList[_depth+1])
69  {
70  return allNodes[_children[ichild]]->insert(dataList,allNodes);
71  }
72  }
73  SeedingNode<DATA>* node = new SeedingNode<DATA>(dataList[_depth+1],allNodes,_index);
74  if (node->getDepth()+1>=dataList.size())
75  {
76  return true;
77  }
78  return node->insert(dataList,allNodes);
79  }
80 
81  inline unsigned int getDepth() const
82  {
83  return _depth;
84  }
85 
86  inline const SeedingNode<DATA>* next() const
87  {
88  if (_index+1<_allNodes.size())
89  {
90  return _allNodes[_index+1];
91  }
92  return nullptr;
93  }
94 
95  inline const SeedingNode<DATA>* firstChild() const
96  {
97  if (!_children.empty())
98  {
99  return _allNodes[_children[0]];
100  }
101  return nullptr;
102  }
103 
104  inline unsigned int getIndex() const
105  {
106  return _index;
107  }
108 
109  inline const SeedingNode* getParent() const
110  {
111  if (_parentIndex>=0)
112  {
113  return _allNodes[_parentIndex];
114  }
115  return nullptr;
116  }
117 
118  inline unsigned int getChildrenSize() const
119  {
120  return _children.size();
121  }
122 
123  inline const SeedingNode<DATA>* getChild(unsigned int ichild) const
124  {
125  return _allNodes[_children[ichild]];
126  }
127 
128  inline unsigned int getChildIndex() const
129  {
130  return _childIndex;
131  }
132 
133  inline const DATA& getData() const
134  {
135  return _data;
136  }
137 
138  void print() const
139  {
140 
141  printf("index=%3i, depth=%2i, childIndex=%2i: ",_index,_depth,_childIndex);
142  for (unsigned int i=0;i<_depth;++i)
143  {
144  std::cout<<" -- ";
145  }
146  printf("[%s, %s] \r\n",_data.toString().c_str(),_data.toIdString().c_str());
147 
148  }
149  void printRecursive() const
150  {
151  print();
152  for (unsigned int ichild=0; ichild<_children.size(); ++ichild)
153  {
154  _allNodes[_children[ichild]]->printRecursive();
155  }
156  }
157 };
158 
159 template<class DATA>
161 {
162  public:
163  typedef std::unordered_set<DATA,typename DATA:: hashfct, typename DATA:: eqfct> SingleSet;
164  protected:
165  std::vector<SeedingNode<DATA>*> _roots;
166  std::vector<SeedingNode<DATA>*> _allNodes;
167 
168  SingleSet _singleSet;
169  public:
170 
171  //returns true if successfully inserted into tree
172  bool insert(const std::vector<DATA>& dataList)
173  {
174  for (unsigned int i = 0; i< dataList.size(); ++i)
175  {
176  _singleSet.insert(dataList[i]);
177  }
178 
179  if (dataList.empty())
180  {
181  return false;
182  }
183  for (unsigned int iroot=0; iroot<_roots.size();++iroot)
184  {
185  if (_roots[iroot]->getData()==dataList[0])
186  {
187  return _roots[iroot]->insert(dataList,_allNodes);
188  }
189  }
190  SeedingNode<DATA>* node = new SeedingNode<DATA>(dataList[0],_allNodes);
191  _roots.push_back(node);
192  return node->insert(dataList,_allNodes);
193  }
194 
195  inline const SingleSet& getSingleSet() const
196  {
197  return _singleSet;
198  }
199 
200  void sort()
201  {
202  //this setups depth first ordered indexes.
203  std::vector<SeedingNode<DATA>*> allNodes;
204  for (unsigned int iroot=0; iroot<_roots.size();++iroot)
205  {
206  _roots[iroot]->sort(allNodes,-1);
207  }
208  _allNodes=allNodes;
209  }
210 
211 
212 
213  inline unsigned int numberOfRoots() const
214  {
215  return _roots.size();
216  }
217 
218  inline unsigned int numberOfNodes() const
219  {
220  return _allNodes.size();
221  }
222 
223  inline const SeedingNode<DATA>* getRoot(unsigned int i) const
224  {
225  if (i<_roots.size())
226  {
227  return _roots[i];
228  }
229  return nullptr;
230  }
231 
232  void printRecursive() const
233  {
234  std::cout<<"SeedingTree: n="<<_allNodes.size()<<" [recursive]"<<std::endl;
235  for (unsigned int iroot=0; iroot<_roots.size();++iroot)
236  {
237  _roots[iroot]->printRecursive();
238  }
239  }
240  void printOrdered() const
241  {
242  std::cout<<"SeedingTree: n="<<_allNodes.size()<<" [ordered]"<<std::endl;
243  for (unsigned int inode=0; inode<_allNodes.size();++inode)
244  {
245  _allNodes[inode]->print();
246  }
247  }
248 
249  void print() const
250  {
251  std::cout<<"SeedingTree: n="<<_allNodes.size()<<std::endl;
252  for (unsigned int inode=0; inode<_allNodes.size();++inode)
253  {
254  _allNodes[inode]->print();
255  }
256  }
257 
259  {
260  for (unsigned int iroot=0; iroot<_roots.size();++iroot)
261  {
262  delete _roots[iroot];
263  }
264  _roots.clear();
265  }
266 };
267 
268 #endif
269 
size
Write out results.
int _childIndex
Definition: SeedingTree.h:17
const SeedingNode< DATA > * next() const
Definition: SeedingTree.h:86
const SingleSet & getSingleSet() const
Definition: SeedingTree.h:195
void printRecursive() const
Definition: SeedingTree.h:232
const SeedingNode< DATA > * firstChild() const
Definition: SeedingTree.h:95
SeedingNode(const DATA &data, std::vector< SeedingNode * > &allNodes, int parentIndex=-1)
Definition: SeedingTree.h:22
const DATA & getData() const
Definition: SeedingTree.h:133
unsigned int getChildrenSize() const
Definition: SeedingTree.h:118
unsigned int getDepth() const
Definition: SeedingTree.h:81
const SeedingNode * getParent() const
Definition: SeedingTree.h:109
std::vector< SeedingNode< DATA > * > _roots
Definition: SeedingTree.h:165
bool insert(const std::vector< DATA > &dataList)
Definition: SeedingTree.h:172
unsigned int numberOfNodes() const
Definition: SeedingTree.h:218
void sort()
Definition: SeedingTree.h:200
unsigned int numberOfRoots() const
Definition: SeedingTree.h:213
std::unordered_set< DATA, typename DATA::hashfct, typename DATA::eqfct > SingleSet
Definition: SeedingTree.h:163
void sort(std::vector< SeedingNode< DATA > * > &allNodes, unsigned int parentIndex)
Definition: SeedingTree.h:45
void printRecursive() const
Definition: SeedingTree.h:149
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:166
void print() const
Definition: SeedingTree.h:249
void print() const
Definition: SeedingTree.h:138
const SeedingNode< DATA > * getRoot(unsigned int i) const
Definition: SeedingTree.h:223
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void printOrdered() const
Definition: SeedingTree.h:240
const std::vector< SeedingNode< DATA > * > & _allNodes
Definition: SeedingTree.h:14
unsigned int getIndex() const
Definition: SeedingTree.h:104
SingleSet _singleSet
Definition: SeedingTree.h:168
unsigned int getChildIndex() const
Definition: SeedingTree.h:128
const SeedingNode< DATA > * getChild(unsigned int ichild) const
Definition: SeedingTree.h:123
unsigned int _index
Definition: SeedingTree.h:15