cmtkUnionFind.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2010 Torsten Rohlfing
00004 //
00005 //  This file is part of the Computational Morphometry Toolkit.
00006 //
00007 //  http://www.nitrc.org/projects/cmtk/
00008 //
00009 //  The Computational Morphometry Toolkit is free software: you can
00010 //  redistribute it and/or modify it under the terms of the GNU General Public
00011 //  License as published by the Free Software Foundation, either version 3 of
00012 //  the License, or (at your option) any later version.
00013 //
00014 //  The Computational Morphometry Toolkit is distributed in the hope that it
00015 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00016 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 //  GNU General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU General Public License along
00020 //  with the Computational Morphometry Toolkit.  If not, see
00021 //  <http://www.gnu.org/licenses/>.
00022 //
00023 //  $Revision: 2199 $
00024 //
00025 //  $LastChangedDate: 2010-08-12 14:20:39 -0700 (Thu, 12 Aug 2010) $
00026 //
00027 //  $LastChangedBy: torstenrohlfing $
00028 //
00029 */
00030 
00031 #ifndef __cmtkUnionFind_h_included_
00032 #define __cmtkUnionFind_h_included_
00033 
00034 #include <cmtkconfig.h>
00035 
00036 #include <set>
00037 #include <list>
00038 
00039 namespace
00040 cmtk
00041 {
00042 
00045 
00047 template<class T>
00048 class UnionFind
00049 {
00050 public:
00052   typedef std::set<T> SetType;
00053 
00055   typedef std::list<SetType> ListType;
00056 
00058   typedef typename ListType::iterator FindResultType;
00059 
00061   FindResultType Find( const T& key )
00062   {
00063     for ( FindResultType it = this->m_List.begin(); it != this->m_List.end(); ++it )
00064       {
00065       if ( it->find( key ) != it->end() )
00066         return it;
00067       }
00068     return this->End();
00069   }
00070 
00072   const T FindKey( const T& key )
00073   {
00074     return *(this->Find( key )->begin());
00075   }
00076 
00078   FindResultType End()
00079   {
00080     return this->m_List.end();
00081   }
00082 
00084   void Union( const FindResultType& s1, const FindResultType& s2 )
00085   {
00086     if ( s1 != s2 )
00087       {
00088       s1->insert( s2->begin(), s2->end() );
00089       this->m_List.erase( s2 );
00090       }
00091   }
00092 
00094   void Insert( const T& key )
00095   {
00096     SetType newSet;
00097     newSet.insert( key );
00098     this->m_List.push_back( newSet );
00099   }
00100   
00101 private:
00103   ListType m_List;
00104 };
00105 
00107 
00108 } // namespace cmtk
00109 
00110 #endif // #ifndef __cmtkUnionFind_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines