00001 /*========================================================================= 00002 00003 Program: Insight Segmentation & Registration Toolkit 00004 Module: $RCSfile: itkChildTreeIterator.h,v $ 00005 Language: C++ 00006 Date: $Date: 2008/01/28 13:52:55 $ 00007 Version: $Revision: 1.8 $ 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 00026 template <class TTreeType> 00027 class ChildTreeIterator : public TreeIteratorBase<TTreeType> 00028 { 00029 public: 00030 00032 typedef ChildTreeIterator Self; 00033 typedef TreeIteratorBase<TTreeType> Superclass; 00034 typedef TTreeType TreeType; 00035 typedef typename TTreeType::ValueType ValueType; 00036 typedef typename Superclass::TreeNodeType TreeNodeType; 00037 00039 ChildTreeIterator( TreeType* tree,const TreeNodeType* start=NULL ); 00040 00042 ChildTreeIterator( const TreeIteratorBase<TTreeType>& iterator ); 00043 00045 int GetType( ) const; 00046 00048 virtual bool GoToChild( int number = 0 ); 00049 00051 virtual bool GoToParent(); 00052 00054 TreeIteratorBase<TTreeType>* Clone(); 00055 00057 Self& operator=(Superclass& iterator) 00058 { 00059 Superclass::operator=(iterator); 00060 ChildTreeIterator<TTreeType>& it = 00061 static_cast<ChildTreeIterator<TTreeType>&>(iterator); 00062 m_ListPosition = it.m_ListPosition; 00063 m_ParentNode = it.m_ParentNode; 00064 return *this; 00065 } 00067 00068 protected: 00069 00071 const ValueType& Next(); 00072 00074 bool HasNext() const; 00075 00076 private: 00077 00078 mutable int m_ListPosition; 00079 TreeNode<ValueType>* m_ParentNode; 00080 }; 00081 00083 template <class TTreeType> 00084 ChildTreeIterator<TTreeType>::ChildTreeIterator(TTreeType* tree, 00085 const TreeNodeType* start) 00086 :TreeIteratorBase<TTreeType>(tree, start) 00087 { 00088 m_ListPosition = 0; 00089 m_ParentNode = this->m_Position; 00090 this->m_Position = m_ParentNode->GetChild( m_ListPosition ); 00091 this->m_Begin = this->m_Position; 00092 } 00094 00095 template <class TTreeType> 00096 ChildTreeIterator<TTreeType>::ChildTreeIterator( 00097 const TreeIteratorBase<TTreeType>& iterator) 00098 :TreeIteratorBase<TTreeType>(iterator.GetTree(), iterator.GetNode()) 00099 { 00100 m_ListPosition = 0; 00101 m_ParentNode = this->m_Position; 00102 this->m_Position = m_ParentNode->GetChild( m_ListPosition ); 00103 } 00104 00106 template <class TTreeType> 00107 bool 00108 ChildTreeIterator<TTreeType>::GoToChild(int number) 00109 { 00110 if ( m_ParentNode->GetChild( number ) == NULL ) 00111 { 00112 return false; 00113 } 00114 00115 m_ListPosition = 0; 00116 m_ParentNode = m_ParentNode->GetChild( number ); 00117 this->m_Position = m_ParentNode->GetChild( m_ListPosition ); 00118 this->m_Begin = this->m_Position; 00119 return true; 00120 } 00121 00123 template <class TTreeType> 00124 bool 00125 ChildTreeIterator<TTreeType>::GoToParent() 00126 { 00127 TreeNode<ValueType>* parent = m_ParentNode->GetParent(); 00128 00129 if ( parent == NULL ) 00130 { 00131 return false; 00132 } 00133 00134 m_ListPosition = 0; 00135 m_ParentNode = parent; 00136 this->m_Position = m_ParentNode->GetChild( m_ListPosition ); 00137 this->m_Begin = this->m_Position; 00138 return true; 00139 } 00140 00142 template <class TTreeType> 00143 int 00144 ChildTreeIterator<TTreeType>::GetType() const 00145 { 00146 return TreeIteratorBase<TTreeType>::CHILD; 00147 } 00148 00150 template <class TTreeType> 00151 bool 00152 ChildTreeIterator<TTreeType>::HasNext() const 00153 { 00154 if( m_ListPosition < m_ParentNode->CountChildren() - 1 ) 00155 { 00156 return true; 00157 } 00158 else 00159 { 00160 return false; 00161 } 00162 } 00164 00166 template <class TTreeType> 00167 const typename ChildTreeIterator<TTreeType>::ValueType& 00168 ChildTreeIterator<TTreeType>::Next() 00169 { 00170 m_ListPosition++; 00171 this->m_Position = m_ParentNode->GetChild( m_ListPosition ); 00172 return this->m_Position->Get(); 00173 } 00175 00177 template <class TTreeType> 00178 TreeIteratorBase<TTreeType>* ChildTreeIterator<TTreeType>::Clone() 00179 { 00180 ChildTreeIterator<TTreeType>* clone = new ChildTreeIterator<TTreeType>( 00181 const_cast<TTreeType*>(this->m_Tree),this->m_Position ); 00182 *clone = *this; 00183 return clone; 00184 } 00186 00187 } // end namespace itk 00188 00189 #endif 00190