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

itkPreOrderTreeIterator.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkPreOrderTreeIterator.h,v $
00005   Language:  C++
00006   Date:      $Date: 2004/12/11 20:29:19 $
00007   Version:   $Revision: 1.2 $
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 __itkPreOrderTreeIterator_h
00018 #define __itkPreOrderTreeIterator_h
00019 
00020 #include <itkTreeIteratorBase.h>
00021 
00022 namespace itk{
00023 
00024 template <class TTreeType>
00025 class PreOrderTreeIterator : public TreeIteratorBase<TTreeType> 
00026 {
00027 public:
00028 
00030   typedef typename TTreeType::ValueType  ValueType;
00031   typedef TreeIteratorBase<TTreeType> Superclass;
00032   typedef typename Superclass::TreeNodeType TreeNodeType;
00033  
00035   PreOrderTreeIterator( const TTreeType* tree, TreeNodeType* start = NULL );
00036 
00038   int GetType() const;
00040   TreeIteratorBase<TTreeType>* Clone();
00041 
00042 protected:
00044   const ValueType& Next();
00046   bool HasNext() const;
00047   
00048 private:  
00049 
00051   const TreeNodeType* FindNextNode() const;
00052 
00053 };
00054 
00055 
00057 template <class TTreeType>
00058 PreOrderTreeIterator<TTreeType>::PreOrderTreeIterator( const TTreeType* tree, TreeNodeType* start )
00059   :TreeIteratorBase<TTreeType>(tree,start) 
00060 {
00061 
00062 }
00063 
00065 template <class TTreeType>
00066 int 
00067 PreOrderTreeIterator<TTreeType>::GetType() const 
00068 {
00069   return TreeIteratorBase<TTreeType>::PREORDER; 
00070 }
00071 
00073 template <class TTreeType>
00074 bool 
00075 PreOrderTreeIterator<TTreeType>::HasNext() const
00076 {
00077   if ( const_cast<TreeNodeType* >(FindNextNode()) != NULL )
00078     {
00079     return true;
00080     }
00081   return false;
00082 }
00083 
00085 template <class TTreeType>
00086 const typename PreOrderTreeIterator<TTreeType>::ValueType&
00087 PreOrderTreeIterator<TTreeType>::Next() 
00088 {
00089   this->m_Position = const_cast<TreeNodeType* >(FindNextNode());
00090   return this->m_Position->Get();
00091 }
00092 
00094 template <class TTreeType>
00095 const typename PreOrderTreeIterator<TTreeType>::TreeNodeType* 
00096 PreOrderTreeIterator<TTreeType>::FindNextNode() const
00097 { 
00098   if ( this->m_Position == NULL )
00099     {
00100     return NULL;
00101     }
00102   if ( this->m_Position->HasChildren() )
00103     {
00104     return dynamic_cast<const TreeNodeType*>(this->m_Position->GetChild(0));
00105     }
00106 
00107   if ( !this->m_Position->HasParent() )
00108     {
00109     return NULL;
00110     }
00111 
00112   TreeNodeType* child = this->m_Position;
00113   TreeNodeType* parent = dynamic_cast<TreeNodeType*>(this->m_Position->GetParent());
00114 
00115   int childPosition = parent->ChildPosition( child ); 
00116   int lastChildPosition = parent->CountChildren() - 1;
00117 
00118   while ( childPosition < lastChildPosition ) 
00119     {
00120     TreeNodeType* help = dynamic_cast<TreeNodeType*>(parent->GetChild( childPosition + 1 ));
00121 
00122     if ( help != NULL )
00123       {
00124       return help;
00125       }
00126     childPosition++;
00127     }
00128   
00129   while ( parent->HasParent() ) 
00130     {
00131     child = parent;
00132     parent = dynamic_cast<TreeNodeType*>(parent->GetParent());
00133 
00134     // Subtree
00135     if( parent->ChildPosition( this->m_Root ) >= 0 )
00136       {
00137       return NULL;
00138       }
00139 
00140     childPosition = parent->ChildPosition(child);
00141     lastChildPosition = parent->CountChildren() - 1;
00142 
00143     while ( childPosition < lastChildPosition ) 
00144       {
00145       TreeNodeType* help = dynamic_cast<TreeNodeType*>(parent->GetChild( childPosition + 1 ));
00146 
00147       if ( help != NULL )
00148         {
00149         return help;
00150         }
00151       }
00152     }
00153   return NULL;
00154 }
00155 
00157 template <class TTreeType>
00158 TreeIteratorBase<TTreeType>* PreOrderTreeIterator<TTreeType>::Clone() 
00159 {
00160   PreOrderTreeIterator<TTreeType>* clone = new PreOrderTreeIterator<TTreeType>(this-> m_Tree, this->m_Position );
00161   *clone = *this;
00162   return clone;
00163 }
00164 
00165 } // end namespace itk
00166 
00167 #endif

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