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

itkChildTreeIterator.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkChildTreeIterator.h,v $
00005   Language:  C++
00006   Date:      $Date: 2005/11/03 20:09:08 $
00007   Version:   $Revision: 1.4 $
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 __itkChildTreeIterator_h
00018 #define __itkChildTreeIterator_h
00019 
00020 #include <itkTreeIteratorBase.h>
00021 
00022 
00023 namespace itk{
00024 
00025 template <class TTreeType>
00026 class ChildTreeIterator : public TreeIteratorBase<TTreeType> 
00027 {
00028 public:
00029   
00031   typedef TreeIteratorBase<TTreeType>  Superclass;
00032   typedef TTreeType TreeType;
00033   typedef typename Superclass::Self Self;
00034   typedef typename TTreeType::ValueType ValueType;
00035   typedef typename Superclass::TreeNodeType TreeNodeType;
00036 
00038   ChildTreeIterator( TreeType* tree,const  TreeNodeType* start=NULL );
00039 
00041   ChildTreeIterator( const TreeIteratorBase<TTreeType>& iterator );
00042 
00044   int GetType( ) const;
00045 
00047   virtual bool GoToChild( int number = 0 );
00048 
00050   virtual bool GoToParent();
00051 
00053   TreeIteratorBase<TTreeType>* Clone();
00054 
00056   Self& operator=(Superclass& iterator) 
00057     {
00058     Superclass::operator=(iterator);
00059     ChildTreeIterator<TTreeType>& it = static_cast<ChildTreeIterator<TTreeType>&>(iterator);
00060     m_ListPosition = it.m_ListPosition;
00061     m_ParentNode = it.m_ParentNode;
00062     return *this;
00063     }
00064 
00065 protected:
00066 
00068   const ValueType& Next();
00069 
00071   bool HasNext() const;
00072 
00073 private:
00074 
00075   mutable int m_ListPosition;
00076   TreeNode<ValueType>* m_ParentNode;
00077 };
00078 
00080 template <class TTreeType>
00081 ChildTreeIterator<TTreeType>::ChildTreeIterator(TTreeType* tree, const TreeNodeType* start)  
00082   :TreeIteratorBase<TTreeType>(tree, start)
00083 {
00084   m_ListPosition = 0;
00085   m_ParentNode = this->m_Position;
00086   this->m_Position = m_ParentNode->GetChild( m_ListPosition );
00087   this->m_Begin = this->m_Position;
00088 }
00089 
00090 template <class TTreeType>
00091 ChildTreeIterator<TTreeType>::ChildTreeIterator(const TreeIteratorBase<TTreeType>& iterator)
00092   :TreeIteratorBase<TTreeType>(iterator.GetTree(), iterator.GetNode())
00093 {
00094   m_ListPosition = 0;
00095   m_ParentNode = this->m_Position;
00096   this->m_Position = m_ParentNode->GetChild( m_ListPosition );
00097 }
00098 
00100 template <class TTreeType>
00101 bool 
00102 ChildTreeIterator<TTreeType>::GoToChild(int number)
00103 {
00104   if ( m_ParentNode->GetChild( number ) == NULL )
00105     {
00106     return false;
00107     }
00108 
00109   m_ListPosition = 0;
00110   m_ParentNode = m_ParentNode->GetChild( number );
00111   this->m_Position = m_ParentNode->GetChild( m_ListPosition );
00112   this->m_Begin = this->m_Position;
00113   return true;
00114 }
00115 
00117 template <class TTreeType>
00118 bool 
00119 ChildTreeIterator<TTreeType>::GoToParent()
00120 {
00121   TreeNode<ValueType>* parent =  m_ParentNode->GetParent();
00122     
00123   if ( parent == NULL )
00124     {
00125     return false;
00126     }
00127 
00128   m_ListPosition = 0;
00129   m_ParentNode = parent;
00130   this->m_Position = m_ParentNode->GetChild( m_ListPosition );
00131   this->m_Begin = this->m_Position;
00132   return true;
00133 }
00134 
00136 template <class TTreeType>
00137 int 
00138 ChildTreeIterator<TTreeType>::GetType() const
00139 {
00140   return TreeIteratorBase<TTreeType>::CHILD;
00141 }
00142 
00144 template <class TTreeType>
00145 bool 
00146 ChildTreeIterator<TTreeType>::HasNext() const
00147 {
00148   if( m_ListPosition < m_ParentNode->CountChildren() - 1 )
00149     {
00150     return true;
00151     }
00152   else
00153     {
00154     return false;
00155     }
00156 }
00157 
00159 template <class TTreeType>
00160 const typename ChildTreeIterator<TTreeType>::ValueType&
00161 ChildTreeIterator<TTreeType>::Next() 
00162 {
00163   m_ListPosition++;
00164   this->m_Position = m_ParentNode->GetChild( m_ListPosition );
00165   return this->m_Position->Get();
00166 }
00167 
00169 template <class TTreeType>
00170 TreeIteratorBase<TTreeType>* ChildTreeIterator<TTreeType>::Clone() 
00171 {
00172   ChildTreeIterator<TTreeType>* clone = new ChildTreeIterator<TTreeType>( const_cast<TTreeType*>(this->m_Tree), this->m_Position );   
00173   *clone = *this;
00174   return clone;
00175 }
00176 
00177 } // end namespace itk
00178 
00179 #endif

Generated at Wed May 24 22:56:55 2006 for ITK by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2000