ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkLeafTreeIterator.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkLeafTreeIterator_h
00019 #define __itkLeafTreeIterator_h
00020 
00021 #include "itkPreOrderTreeIterator.h"
00022 
00023 namespace itk
00024 {
00025 template< class TTreeType >
00026 class LeafTreeIterator:public TreeIteratorBase< TTreeType >
00027 {
00028 public:
00029 
00031   typedef LeafTreeIterator                    Self;
00032   typedef TreeIteratorBase< TTreeType >       Superclass;
00033   typedef TTreeType                           TreeType;
00034   typedef typename TreeType::ValueType        ValueType;
00035   typedef typename Superclass::TreeNodeType   TreeNodeType;
00036   typedef typename Superclass::NodeType       NodeType;
00037 
00039   LeafTreeIterator(const TreeType *tree);
00040 
00042   LeafTreeIterator(TreeType *tree);
00043 
00045   virtual ~LeafTreeIterator();
00046 
00048   NodeType GetType() const;
00049 
00051   TreeIteratorBase< TTreeType > * Clone();
00052 
00053 protected:
00054 
00056   const ValueType & Next();
00057 
00059   bool HasNext() const;
00060 
00061 private:
00062 
00064   const TreeNodeType * FindNextNode() const;
00065 };
00066 
00068 template< class TTreeType >
00069 LeafTreeIterator< TTreeType >::LeafTreeIterator(const TTreeType *tree):
00070   TreeIteratorBase< TTreeType >(tree, NULL)
00071 {
00072   this->m_Begin = const_cast< TreeNodeType * >( this->FindNextNode() ); //
00073                                                                         //
00074                                                                         // Position
00075                                                                         // the
00076                                                                         //
00077                                                                         // iterator
00078                                                                         // to
00079                                                                         // the
00080                                                                         // first
00081                                                                         // leaf;
00082 }
00084 
00086 template< class TTreeType >
00087 LeafTreeIterator< TTreeType >::LeafTreeIterator(TTreeType *tree):
00088   TreeIteratorBase< TTreeType >(tree, NULL)
00089 {
00090   this->m_Begin = const_cast< TreeNodeType * >( this->FindNextNode() ); //
00091                                                                         //
00092                                                                         // Position
00093                                                                         // the
00094                                                                         //
00095                                                                         // iterator
00096                                                                         // to
00097                                                                         // the
00098                                                                         // first
00099                                                                         // leaf;
00100 }
00102 
00104 template< class TTreeType >
00105 LeafTreeIterator< TTreeType >::~LeafTreeIterator()
00106 {}
00107 
00109 template< class TTreeType >
00110 typename LeafTreeIterator< TTreeType >::NodeType
00111 LeafTreeIterator< TTreeType >::GetType() const
00112 {
00113   return TreeIteratorBase< TTreeType >::LEAF;
00114 }
00115 
00117 template< class TTreeType >
00118 bool LeafTreeIterator< TTreeType >::HasNext() const
00119 {
00120   if ( this->m_Position == NULL )
00121     {
00122     return false;
00123     }
00124   if ( const_cast< TreeNodeType * >( FindNextNode() ) != NULL )
00125     {
00126     return true;
00127     }
00128   return false;
00129 }
00131 
00133 template< class TTreeType >
00134 const typename LeafTreeIterator< TTreeType >::ValueType &
00135 LeafTreeIterator< TTreeType >::Next()
00136 {
00137   this->m_Position = const_cast< TreeNodeType * >( FindNextNode() );
00138   return this->m_Position->Get();
00139 }
00141 
00143 template< class TTreeType >
00144 const typename LeafTreeIterator< TTreeType >::TreeNodeType *
00145 LeafTreeIterator< TTreeType >::FindNextNode() const
00146 {
00147   PreOrderTreeIterator< TTreeType > it(this->m_Tree, this->m_Position);
00148   ++it; // go next
00149   if ( it.IsAtEnd() )
00150     {
00151     return NULL;
00152     }
00154 
00155   if ( !it.HasChild() )
00156     {
00157     return it.GetNode();
00158     }
00159 
00160   while ( !it.IsAtEnd() )
00161     {
00162     if ( !it.HasChild() )
00163       {
00164       return it.GetNode();
00165       }
00166     ++it;
00167     }
00168 
00169   return NULL;
00170 }
00171 
00173 template< class TTreeType >
00174 TreeIteratorBase< TTreeType > *LeafTreeIterator< TTreeType >::Clone()
00175 {
00176   LeafTreeIterator< TTreeType > *clone = new LeafTreeIterator< TTreeType >(this->m_Tree);
00177   *clone = *this;
00178   return clone;
00179 }
00180 } // end namespace itk
00182 
00183 #endif
00184