disjointSet.h

Go to the documentation of this file.
00001 
00016 #ifndef DLR_COMPUTERVISION_DISJOINTSET_H
00017 #define DLR_COMPUTERVISION_DISJOINTSET_H
00018 
00019 
00020 // xxx
00021 #include <iostream>
00022 
00023 namespace dlr {
00024 
00025   namespace computerVision {
00026 
00027     namespace privateCode {
00028       
00054       template <class Type>
00055       class DisjointSet {
00056       public:
00057 
00061         DisjointSet()
00062           : m_parentPtr(this), m_rank(0), m_size(1), m_payload() {
00063           if(m_parentPtr != this) {
00064             std::cout << "Ack!!" << std::endl;
00065           }
00066         }
00067 
00068 
00077         DisjointSet(const Type& payload)
00078           : m_parentPtr(this), m_rank(0), m_size(1), m_payload(payload) {};
00079 
00080 
00084         virtual
00085         ~DisjointSet() {}
00086 
00087 #if 0
00088 
00100         DisjointSet&
00101         find() {
00102           if(m_parentPtr == this) {
00103             return *this;
00104           }
00105           m_parentPtr = &(m_parentPtr->find());
00106           return *m_parentPtr;
00107         }
00108 
00109 #elif 1
00110       
00121         DisjointSet&
00122         find() {
00123           // Follow parent pointers to find the head of the tree.
00124           DisjointSet* headPtr = this;
00125           while(headPtr->m_parentPtr != headPtr) {
00126             headPtr = headPtr->m_parentPtr;
00127           }
00128 
00129           // Now do the same thing again, but this time update all of
00130           // the parent pointers to point directly at the head.
00131           DisjointSet* updatePtr = this;
00132           while(updatePtr->m_parentPtr != updatePtr) {
00133             DisjointSet* tmpPtr = updatePtr->m_parentPtr;
00134             updatePtr->m_parentPtr = headPtr;
00135             updatePtr = tmpPtr;
00136           }
00137           return *m_parentPtr;
00138         }
00139 
00140 #else
00141       
00157         DisjointSet&
00158         find() {
00159           DisjointSet* headPtr = this;
00160           while(headPtr->m_parentPtr != headPtr) {
00161             headPtr = headPtr->m_parentPtr;
00162           }
00163           m_parentPtr = headPtr;
00164           return *m_parentPtr;
00165         }
00166 
00167 #endif
00168       
00179         void
00180         merge(DisjointSet& other) {
00181           DisjointSet& myParent = this->find();
00182           DisjointSet& otherParent = other.find();
00183           if(&myParent == &otherParent) {
00184             return;
00185           }
00186           if(myParent.m_rank > otherParent.m_rank) {
00187             otherParent.m_parentPtr = &myParent;
00188             myParent.m_size += otherParent.m_size;
00189           } else if(myParent.m_rank < otherParent.m_rank) {
00190             myParent.m_parentPtr = &otherParent;
00191             otherParent.m_size += myParent.m_size;
00192           } else {
00193             otherParent.m_parentPtr = &myParent;
00194             myParent.m_size += otherParent.m_size;
00195             ++(myParent.m_rank);
00196           }
00197         }
00198       
00199 
00200         const Type&
00201         getPayload() const {return m_payload;}
00202         
00203 
00211         size_t
00212         getSize() {return (this->find()).m_size;}
00213 
00214 
00215         void
00216         setPayload(const Type& payload) {m_payload = payload;}
00217         
00218       protected:
00219 
00220         DisjointSet* m_parentPtr;
00221         size_t m_rank;
00222         size_t m_size;
00223 
00224         Type m_payload;
00225       };
00226       
00227     } // namespace privateCode
00228     
00229   } // namespace computerVision
00230   
00231 } // namespace dlr
00232 
00233 
00234 /* ============ Definitions of inline & template functions ============ */
00235 
00236 
00237 namespace dlr {
00238 
00239   namespace computerVision {
00240 
00241 
00242   } // namespace computerVision
00243   
00244 } // namespace dlr
00245 
00246 #endif /* #ifndef DLR_COMPUTERVISION_DISJOINTSET_H */

Generated on Wed Nov 25 12:15:04 2009 for dlrComputerVision Utility Library by  doxygen 1.5.8