APRILContent
Algorithm of Particle Reconstruction for ILC - implementation with PandoraSDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations
QuickUnion.h
Go to the documentation of this file.
1 
8 #ifndef LC_QUICK_UNION_H
9 #define LC_QUICK_UNION_H 1
10 
11 #include <vector>
12 
13 namespace april_content
14 {
15 
20 {
21 public:
27  QuickUnion(const unsigned nBranches);
28 
34  int Count() const;
35 
41  unsigned int Find(unsigned p);
42 
51  bool Connected(unsigned p, unsigned q);
52 
59  void Unite(unsigned p, unsigned q);
60 
61 private:
62  std::vector<unsigned> m_id;
63  //std::vector<unsigned> m_size; ///< Stores number of connected indices, used for book-keeping
64  int m_count;
65 };
66 
67 //------------------------------------------------------------------------------------------------------------------------------------------
68 
69 inline QuickUnion::QuickUnion(const unsigned int nBranches)
70 {
71  m_count = nBranches;
72  m_id.resize(nBranches);
73  //m_size.resize(nBranches);
74 
75  for (unsigned int i = 0; i < nBranches; ++i)
76  {
77  m_id[i] = i;
78  //m_size[i] = 1;
79  }
80 }
81 
82 //------------------------------------------------------------------------------------------------------------------------------------------
83 
84 inline int QuickUnion::Count() const
85 {
86  return m_count;
87 }
88 
89 //------------------------------------------------------------------------------------------------------------------------------------------
90 
91 inline unsigned int QuickUnion::Find(unsigned int p)
92 {
93  while (p != m_id[p])
94  {
95  m_id[p] = m_id[m_id[p]];
96  p = m_id[p];
97  }
98 
99  return p;
100 }
101 
102 //------------------------------------------------------------------------------------------------------------------------------------------
103 
104 inline bool QuickUnion::Connected(const unsigned int p, const unsigned int q)
105 {
106  return (this->Find(p) == this->Find(q));
107 }
108 
109 //------------------------------------------------------------------------------------------------------------------------------------------
110 
111 inline void QuickUnion::Unite(const unsigned int p, const unsigned int q)
112 {
113  const unsigned int rootP(this->Find(p));
114  const unsigned int rootQ(this->Find(q));
115 
116  m_id[p] = q;
117 
118  // TODO finalise implementation of Unite function
119 
120  //if (m_size[rootP] < m_size[rootQ])
121  //{
122  m_id[rootP] = rootQ;
123  // m_size[rootQ] += m_size[rootP];
124  //}
125  //else
126  //{
127  // m_id[rootQ] = rootP;
128  // m_size[rootP] += m_size[rootQ];
129  //}
130 
131  --m_count;
132 }
133 
134 } // namespace lc_content
135 
136 #endif // LC_QUICK_UNION_H
int Count() const
Get the current number of target indices.
Definition: QuickUnion.h:84
std::vector< unsigned > m_id
Stores target index for each original index.
Definition: QuickUnion.h:62
bool Connected(unsigned p, unsigned q)
Whether two original indices are now connected.
Definition: QuickUnion.h:104
unsigned int Find(unsigned p)
Find the current target index for provided index p.
Definition: QuickUnion.h:91
QuickUnion class.
Definition: QuickUnion.h:19
void Unite(unsigned p, unsigned q)
Unite two indices.
Definition: QuickUnion.h:111
QuickUnion(const unsigned nBranches)
Constructor.
Definition: QuickUnion.h:69
int m_count
The current number of target indices.
Definition: QuickUnion.h:64