Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkLevelOrderTreeIterator.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkLevelOrderTreeIterator.h,v $
00005   Language:  C++
00006   Date:      $Date: 2004/12/11 20:29:18 $
00007   Version:   $Revision: 1.3 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 #ifndef __itkLevelOrderTreeIterator_h
00018 #define __itkLevelOrderTreeIterator_h
00019 
00020 #include <queue>
00021 #include <itkTreeIteratorBase.h>
00022 
00023 namespace itk{
00024 
00025 template <class TTreeType>
00026 class LevelOrderTreeIterator : public TreeIteratorBase<TTreeType> 
00027 {
00028 public:
00029 
00031   typedef TreeIteratorBase<TTreeType> Superclass; 
00032   typedef typename Superclass::Self Self;
00033   typedef TTreeType TreeType;
00034   typedef typename TTreeType::ValueType ValueType;
00035   typedef typename Superclass::TreeNodeType TreeNodeType;
00036 
00038   LevelOrderTreeIterator( TreeType* tree, int endLevel = INT_MAX, const TreeNodeType* start = NULL);
00039   LevelOrderTreeIterator( TreeType* tree, int startLevel, int endLevel, const TreeNodeType* start = NULL);
00041 
00042   virtual ~LevelOrderTreeIterator() {};
00043 
00045   int GetType() const ;
00046 
00048   int GetStartLevel() const;
00049 
00051   int GetEndLevel() const;
00052 
00054   int GetLevel() const;
00055 
00057   TreeIteratorBase<TTreeType>* Clone();
00058 
00060   Self& operator=(Superclass& iterator) 
00061     {
00062     Superclass::operator=(iterator);
00063     LevelOrderTreeIterator<TTreeType>& it = static_cast<LevelOrderTreeIterator<TTreeType>&>(iterator);
00064     m_StartLevel = it.m_StartLevel;
00065     m_EndLevel = it.m_EndLevel;
00066     m_Queue = it.m_Queue;
00067     return *this;
00068     }
00070 
00071 
00072 protected:
00073 
00075   const ValueType& Next();
00076 
00078   bool HasNext() const;
00079 
00080 private:
00081 
00082   const TreeNodeType* FindNextNode() const;
00083   const TreeNodeType* FindNextNodeHelp() const ;
00084   int GetLevel( const TreeNodeType* node ) const;
00085   int m_StartLevel;
00086   int m_EndLevel;
00087   mutable std::queue<const TreeNodeType*> m_Queue;
00088 
00089 };
00090 
00091 
00093 template <class TTreeType>
00094 LevelOrderTreeIterator<TTreeType>
00095 ::LevelOrderTreeIterator( TTreeType* tree, int endLevel, const TreeNodeType* start)
00096  :TreeIteratorBase<TTreeType>(tree,start)
00097 {
00098   m_StartLevel =  -1;
00099   m_EndLevel = endLevel;
00100   if ( start != NULL )
00101     {
00102     m_Queue.push( start );
00103     this->m_Position = const_cast<TreeNodeType*>(start);
00104     }
00105   else
00106     {
00107     if(tree->GetRoot())
00108       {
00109       m_Queue.push( dynamic_cast<const TreeNodeType*>(tree->GetRoot()) );
00110       this->m_Position = const_cast<TreeNodeType*>(dynamic_cast<const TreeNodeType*>(tree->GetRoot()));
00111       }
00112     }
00113   this->m_Begin = this->m_Position;
00114 }
00116 
00118 template <class TTreeType>
00119 LevelOrderTreeIterator<TTreeType>
00120 ::LevelOrderTreeIterator( TTreeType* tree, int startLevel, int endLevel, const TreeNodeType* start)
00121  :TreeIteratorBase<TTreeType>(tree,start)
00122 {
00123   m_StartLevel = startLevel;
00124   m_EndLevel = endLevel;
00125   if ( start != NULL )
00126     {
00127     m_Queue.push( start );
00128     this->m_Position = const_cast<TreeNodeType*>(start);
00129     }
00130   else
00131     {
00132     if(tree->GetRoot())
00133       {
00134       m_Queue.push( dynamic_cast<const TreeNodeType*>(tree->GetRoot()) );
00135       this->m_Position = const_cast<TreeNodeType*>(dynamic_cast<const TreeNodeType*>(tree->GetRoot()));
00136       }
00137     }
00138   this->m_Begin = this->m_Position;
00139 }
00141 
00143 template <class TTreeType>
00144 int 
00145 LevelOrderTreeIterator<TTreeType>::GetType() const 
00146 {
00147   return TreeIteratorBase<TTreeType>::LEVELORDER;
00148 }
00149 
00151 template <class TTreeType>
00152 bool 
00153 LevelOrderTreeIterator<TTreeType>::HasNext() const
00154 {
00155  if(const_cast<TreeNodeType*>(FindNextNode()))
00156    {
00157    return true;
00158    }
00159   return false;
00160 }
00162 
00164 template <class TTreeType>
00165 const typename LevelOrderTreeIterator<TTreeType>::ValueType&
00166 LevelOrderTreeIterator<TTreeType>::Next() 
00167 {
00168   this->m_Position = const_cast<TreeNodeType* >(FindNextNode());
00169   return this->m_Position->Get();
00170 }
00172 
00174 template <class TTreeType>
00175 int LevelOrderTreeIterator<TTreeType>::GetStartLevel() const
00176 {
00177   return m_StartLevel;
00178 }
00179 
00181 template <class TTreeType>
00182 int 
00183 LevelOrderTreeIterator<TTreeType>::GetEndLevel() const
00184 {
00185   return m_EndLevel;
00186 }
00187 
00189 template <class TTreeType>
00190 const typename LevelOrderTreeIterator<TTreeType>::TreeNodeType*
00191 LevelOrderTreeIterator<TTreeType>::FindNextNode() const
00192 {
00193   int level;
00194   const TreeNodeType* node;
00195 
00196   do{
00197     node = FindNextNodeHelp();
00198     if ( node == NULL )
00199       {
00200       return NULL;
00201       }
00202     level = GetLevel( node );
00203     if ( level > m_EndLevel )
00204       {
00205       return NULL;
00206       }
00207   } while ( level < m_StartLevel );
00208 
00209   return node;
00210 }
00211 
00213 template <class TTreeType>
00214 int 
00215 LevelOrderTreeIterator<TTreeType>::GetLevel() const
00216 {
00217   if( this->m_Position == NULL )
00218     {
00219     return -1;
00220     }
00221 
00222   int level = 0;
00223   TreeNodeType* node = this->m_Position;
00224   while( node->HasParent() && node != this->m_Root ) 
00225     {
00226     node = dynamic_cast<TreeNodeType*>(node->GetParent());
00227     level++;
00228     }
00229   return level;
00230 }
00231 
00233 template <class TTreeType>
00234 int 
00235 LevelOrderTreeIterator<TTreeType>::GetLevel( const TreeNodeType* node ) const
00236 {
00237   if( node == NULL )
00238     {
00239     return -1;
00240     }
00241   int level = 0;
00243 
00244   while( node->HasParent() && node != this->m_Root )
00245     {
00246     node = dynamic_cast<const TreeNodeType*>(node->GetParent());
00247     level++;
00248     }
00249   return level;
00250 }
00251 
00253 template <class TTreeType>
00254 const typename LevelOrderTreeIterator<TTreeType>::TreeNodeType* 
00255 LevelOrderTreeIterator<TTreeType>::FindNextNodeHelp() const
00256 {
00257   if( m_Queue.empty() )
00258     {
00259     return NULL;
00260     }
00261 
00262   const TreeNodeType *currentNode = m_Queue.front();
00263   m_Queue.pop();
00264 
00265   if ( currentNode == NULL)
00266     {
00267     return NULL;
00268     }
00269 
00270   int size = currentNode->CountChildren();
00271 
00272   for ( int i=0; i < size; i++ ) 
00273     {
00274     TreeNodeType *child = dynamic_cast<TreeNodeType*>(currentNode->GetChild( i ));
00275     if ( child != NULL )
00276       {
00277       m_Queue.push(child);
00278       }
00279     }
00280 
00281   // If the current node is the root we try again
00282   if(currentNode == this->m_Root)
00283     {
00284     currentNode = const_cast<TreeNodeType*>(FindNextNodeHelp());
00285     }
00286   return currentNode;
00287 }
00288 
00290 template <class TTreeType>
00291 TreeIteratorBase<TTreeType>* LevelOrderTreeIterator<TTreeType>::Clone() 
00292 {
00293   LevelOrderTreeIterator<TTreeType>* clone = new LevelOrderTreeIterator<TTreeType>( const_cast<TTreeType*>(this->m_Tree), m_StartLevel, m_EndLevel );
00294   *clone = *this;
00295   return clone;
00296 }
00298 
00299 
00300 
00301 } // end namespace itk
00302 
00303 #endif
00304 

Generated at Sun Sep 23 13:23:35 2007 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000