ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkRootTreeIterator.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 __itkRootTreeIterator_h
00019 #define __itkRootTreeIterator_h
00020 
00021 #include "itkTreeIteratorBase.h"
00022 
00023 namespace itk
00024 {
00025 template< class TTreeType >
00026 class RootTreeIterator:public TreeIteratorBase< TTreeType >
00027 {
00028 public:
00029 
00031   typedef TreeIteratorBase< TTreeType >     Superclass;
00032   typedef TTreeType                         TreeType;
00033   typedef typename TTreeType::ValueType     ValueType;
00034   typedef typename Superclass::TreeNodeType TreeNodeType;
00035   typedef typename Superclass::NodeType     NodeType;
00036 
00038   RootTreeIterator(TreeType *tree, const TreeNodeType *start = NULL);
00039 
00041   NodeType GetType() const;
00042 
00044   TreeIteratorBase< TTreeType > * Clone();
00045 
00046 protected:
00047 
00049   const ValueType & Next();
00050 
00052   bool HasNext() const;
00053 
00054 private:
00055 
00057   const TreeNodeType * FindNextNode() const;
00058 };
00059 
00061 template< class TTreeType >
00062 RootTreeIterator< TTreeType >::RootTreeIterator(TTreeType *tree, const TreeNodeType *start):
00063   TreeIteratorBase< TTreeType >(tree, start)
00064 {
00065   if ( start )
00066     {
00067     this->m_Begin = const_cast< TreeNode< ValueType > * >( start );
00068     }
00069   this->m_Root = tree->GetRoot();
00070   this->m_Position = this->m_Begin;
00071 }
00073 
00075 template< class TTreeType >
00076 typename RootTreeIterator< TTreeType >::NodeType
00077 RootTreeIterator< TTreeType >::GetType() const
00078 {
00079   return TreeIteratorBase< TTreeType >::ROOT;
00080 }
00081 
00083 template< class TTreeType >
00084 bool
00085 RootTreeIterator< TTreeType >::HasNext() const
00086 {
00087   if ( const_cast< TreeNodeType * >( FindNextNode() ) != NULL )
00088     {
00089     return true;
00090     }
00091   return false;
00092 }
00094 
00096 template< class TTreeType >
00097 const typename RootTreeIterator< TTreeType >::ValueType &
00098 RootTreeIterator< TTreeType >::Next()
00099 {
00100   this->m_Position = const_cast< TreeNodeType * >( FindNextNode() );
00101   return this->m_Position->Get();
00102 }
00104 
00106 template< class TTreeType >
00107 const typename RootTreeIterator< TTreeType >::TreeNodeType *
00108 RootTreeIterator< TTreeType >::FindNextNode() const
00109 {
00110   if ( this->m_Position == NULL )
00111     {
00112     return NULL;
00113     }
00114   if ( this->m_Position == this->m_Root )
00115     {
00116     return NULL;
00117     }
00118   return this->m_Position->GetParent();
00119 }
00121 
00123 template< class TTreeType >
00124 TreeIteratorBase< TTreeType > *RootTreeIterator< TTreeType >::Clone()
00125 {
00126   RootTreeIterator< TTreeType > *clone = new RootTreeIterator< TTreeType >(const_cast< TTreeType * >( this->m_Tree ),
00127                                                                            this->m_Position);
00128   *clone = *this;
00129   return clone;
00130 }
00131 } // end namespace itk
00133 
00134 #endif
00135