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);
00040 
00041   virtual ~LevelOrderTreeIterator() {};
00042 
00044   int GetType() const ;
00045 
00047   int GetStartLevel() const;
00048   
00050   int GetEndLevel() const;
00051 
00053   int GetLevel() const;
00054 
00056   TreeIteratorBase<TTreeType>* Clone();
00057 
00059   Self& operator=(Superclass& iterator) 
00060     {
00061     Superclass::operator=(iterator);
00062     LevelOrderTreeIterator<TTreeType>& it = static_cast<LevelOrderTreeIterator<TTreeType>&>(iterator);
00063     m_StartLevel = it.m_StartLevel;
00064     m_EndLevel = it.m_EndLevel;
00065     m_Queue = it.m_Queue;
00066     return *this;
00067     }
00068 
00069 
00070 protected:
00071 
00073   const ValueType& Next();
00075   bool HasNext() const;
00076 
00077 private:
00078 
00079   const TreeNodeType* FindNextNode() const;
00080   const TreeNodeType* FindNextNodeHelp() const ;
00081   int GetLevel( const TreeNodeType* node ) const;
00082   int m_StartLevel;
00083   int m_EndLevel;
00084   mutable std::queue<const TreeNodeType*> m_Queue;
00085 
00086 };
00087 
00088 
00090 template <class TTreeType>
00091 LevelOrderTreeIterator<TTreeType>
00092 ::LevelOrderTreeIterator( TTreeType* tree, int endLevel, const TreeNodeType* start)
00093  :TreeIteratorBase<TTreeType>(tree,start)
00094 {
00095   m_StartLevel =  -1;
00096   m_EndLevel = endLevel;
00097   if ( start != NULL )
00098     {
00099     m_Queue.push( start );
00100     this->m_Position = const_cast<TreeNodeType*>(start);
00101     }
00102   else
00103     {
00104     if(tree->GetRoot())
00105       {
00106       m_Queue.push( dynamic_cast<const TreeNodeType*>(tree->GetRoot()) );
00107       this->m_Position = const_cast<TreeNodeType*>(dynamic_cast<const TreeNodeType*>(tree->GetRoot()));
00108       }
00109     }
00110   this->m_Begin = this->m_Position;
00111 }
00112 
00114 template <class TTreeType>
00115 LevelOrderTreeIterator<TTreeType>
00116 ::LevelOrderTreeIterator( TTreeType* tree, int startLevel, int endLevel, const TreeNodeType* start)
00117  :TreeIteratorBase<TTreeType>(tree,start)
00118 {
00119   m_StartLevel = startLevel;
00120   m_EndLevel = endLevel;
00121   if ( start != NULL )
00122     {
00123     m_Queue.push( start );
00124     this->m_Position = const_cast<TreeNodeType*>(start);
00125     }
00126   else
00127     {
00128     if(tree->GetRoot())
00129       {
00130       m_Queue.push( dynamic_cast<const TreeNodeType*>(tree->GetRoot()) );
00131       this->m_Position = const_cast<TreeNodeType*>(dynamic_cast<const TreeNodeType*>(tree->GetRoot()));
00132       }
00133     }
00134   this->m_Begin = this->m_Position;
00135 }
00136 
00138 template <class TTreeType>
00139 int 
00140 LevelOrderTreeIterator<TTreeType>::GetType() const 
00141 {
00142   return TreeIteratorBase<TTreeType>::LEVELORDER;
00143 }
00144 
00146 template <class TTreeType>
00147 bool 
00148 LevelOrderTreeIterator<TTreeType>::HasNext() const
00149 {
00150  if(const_cast<TreeNodeType*>(FindNextNode()))
00151    {
00152    return true;
00153    }
00154   return false;
00155 }
00156 
00158 template <class TTreeType>
00159 const typename LevelOrderTreeIterator<TTreeType>::ValueType&
00160 LevelOrderTreeIterator<TTreeType>::Next() 
00161 {
00162   this->m_Position = const_cast<TreeNodeType* >(FindNextNode());
00163   return this->m_Position->Get();
00164 }
00165 
00167 template <class TTreeType>
00168 int LevelOrderTreeIterator<TTreeType>::GetStartLevel() const
00169 {
00170   return m_StartLevel;
00171 }
00172 
00174 template <class TTreeType>
00175 int 
00176 LevelOrderTreeIterator<TTreeType>::GetEndLevel() const
00177 {
00178   return m_EndLevel;
00179 }
00180 
00182 template <class TTreeType>
00183 const typename LevelOrderTreeIterator<TTreeType>::TreeNodeType*
00184 LevelOrderTreeIterator<TTreeType>::FindNextNode() const
00185 {
00186   int level;
00187   const TreeNodeType* node;
00188 
00189   do{
00190     node = FindNextNodeHelp();
00191     if ( node == NULL )
00192       {
00193       return NULL;
00194       }
00195     level = GetLevel( node );
00196     if ( level > m_EndLevel )
00197       {
00198       return NULL;
00199       }
00200   } while ( level < m_StartLevel );
00201 
00202   return node;
00203 }
00204 
00206 template <class TTreeType>
00207 int 
00208 LevelOrderTreeIterator<TTreeType>::GetLevel() const
00209 {
00210   if( this->m_Position == NULL )
00211     {
00212     return -1;
00213     }
00214   
00215   int level = 0;
00216   TreeNodeType* node = this->m_Position;
00217   while( node->HasParent() && node != this->m_Root ) 
00218     {
00219     node = dynamic_cast<TreeNodeType*>(node->GetParent());
00220     level++;
00221     }
00222   return level;
00223 }
00224 
00226 template <class TTreeType>
00227 int 
00228 LevelOrderTreeIterator<TTreeType>::GetLevel( const TreeNodeType* node ) const
00229 {
00230   if( node == NULL )
00231     {
00232     return -1;
00233     }
00234   int level = 0;
00235 
00236   while( node->HasParent() && node != this->m_Root )
00237     {
00238     node = dynamic_cast<const TreeNodeType*>(node->GetParent());
00239     level++;
00240     }
00241   return level;
00242 }
00243 
00245 template <class TTreeType>
00246 const typename LevelOrderTreeIterator<TTreeType>::TreeNodeType* 
00247 LevelOrderTreeIterator<TTreeType>::FindNextNodeHelp() const
00248 {
00249   if( m_Queue.empty() )
00250     {
00251     return NULL;
00252     }
00253  
00254   const TreeNodeType *currentNode = m_Queue.front();
00255   m_Queue.pop();
00256 
00257   if ( currentNode == NULL)
00258     {
00259     return NULL;
00260     }
00261 
00262   int size = currentNode->CountChildren();
00263 
00264   for ( int i=0; i < size; i++ ) 
00265     {
00266     TreeNodeType *child = dynamic_cast<TreeNodeType*>(currentNode->GetChild( i ));
00267     if ( child != NULL )
00268       {
00269       m_Queue.push(child);
00270       }
00271     }
00272 
00273   // If the current node is the root we try again
00274   if(currentNode == this->m_Root)
00275     {
00276     currentNode = const_cast<TreeNodeType*>(FindNextNodeHelp());
00277     }
00278   return currentNode;
00279 }
00280 
00282 template <class TTreeType>
00283 TreeIteratorBase<TTreeType>* LevelOrderTreeIterator<TTreeType>::Clone() 
00284 {
00285   LevelOrderTreeIterator<TTreeType>* clone = new LevelOrderTreeIterator<TTreeType>( const_cast<TTreeType*>(this->m_Tree), m_StartLevel, m_EndLevel );
00286   *clone = *this;
00287   return clone;
00288 }
00289 
00290 
00291 
00292 } // end namespace itk
00293 
00294 #endif

Generated at Wed May 24 23:33:54 2006 for ITK by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2000