00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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 }
00088
00089 template <class TTreeType>
00090 ChildTreeIterator<TTreeType>::ChildTreeIterator(const TreeIteratorBase<TTreeType>& iterator)
00091 :TreeIteratorBase<TTreeType>(iterator.GetTree(), iterator.GetNode())
00092 {
00093 m_ListPosition = 0;
00094 m_ParentNode = this->m_Position;
00095 this->m_Position = m_ParentNode->GetChild( m_ListPosition );
00096 }
00097
00099 template <class TTreeType>
00100 bool
00101 ChildTreeIterator<TTreeType>::GoToChild(int number)
00102 {
00103 if ( m_ParentNode->GetChild( number ) == NULL )
00104 {
00105 return false;
00106 }
00107
00108 m_ListPosition = 0;
00109 m_ParentNode = m_ParentNode->GetChild( number );
00110 this->m_Position = m_ParentNode->GetChild( m_ListPosition );
00111 this->m_Begin = this->m_Position;
00112 return true;
00113 }
00114
00116 template <class TTreeType>
00117 bool
00118 ChildTreeIterator<TTreeType>::GoToParent()
00119 {
00120 TreeNode<ValueType>* parent = m_ParentNode->GetParent();
00121
00122 if ( parent == NULL )
00123 {
00124 return false;
00125 }
00126
00127 m_ListPosition = 0;
00128 m_ParentNode = parent;
00129 this->m_Position = m_ParentNode->GetChild( m_ListPosition );
00130 this->m_Begin = this->m_Position;
00131 return true;
00132 }
00133
00135 template <class TTreeType>
00136 int
00137 ChildTreeIterator<TTreeType>::GetType() const
00138 {
00139 return TreeIteratorBase<TTreeType>::CHILD;
00140 }
00141
00143 template <class TTreeType>
00144 bool
00145 ChildTreeIterator<TTreeType>::HasNext() const
00146 {
00147 if( m_ListPosition < m_ParentNode->CountChildren() - 1 )
00148 {
00149 return true;
00150 }
00151 else
00152 {
00153 return false;
00154 }
00155 }
00156
00158 template <class TTreeType>
00159 const typename ChildTreeIterator<TTreeType>::ValueType&
00160 ChildTreeIterator<TTreeType>::Next()
00161 {
00162 m_ListPosition++;
00163 this->m_Position = m_ParentNode->GetChild( m_ListPosition );
00164 return this->m_Position->Get();
00165 }
00166
00168 template <class TTreeType>
00169 TreeIteratorBase<TTreeType>* ChildTreeIterator<TTreeType>::Clone()
00170 {
00171 ChildTreeIterator<TTreeType>* clone = new ChildTreeIterator<TTreeType>( const_cast<TTreeType*>(this->m_Tree), this->m_Position );
00172 *clone = *this;
00173 return clone;
00174 }
00175
00176 }
00177
00178 #endif