KWStyle - itkLeafTreeIterator.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkLeafTreeIterator.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:40 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 #ifndef __itkLeafTreeIterator_h
18 #define __itkLeafTreeIterator_h
19
20 #include <itkTreeIteratorBase.h>
21 #include <itkPreOrderTreeIterator.h>
22
23 NMS namespace itk{
24
25 template <class TTreeType>
26 class LeafTreeIterator : public TreeIteratorBase<TTreeType> 
27 {
28 public:
29
30   /** Typedefs*/
31   typedef TreeIteratorBase<TTreeType> Superclass;
32 TDA   typedef TTreeType TreeType;
33 TDA   typedef typename TreeType::ValueType ValueType;
34 TDA   typedef TreeNode<ValueType>  TreeNodeType;
35
36   /** Constructor */
37   LeafTreeIterator( const TreeType* tree );
38   
39   /** Constructor */
40   LeafTreeIterator( TreeType* tree );
41
42   /** Destructor */
43   virtual ~LeafTreeIterator();
44
45   /** Return the type of iterator */
46   int GetType() const;
47
48   /** Clone function */
49   TreeIteratorBase<TTreeType>* Clone();
50
51 protected:
52   
53   /** Return the next value */
54   const ValueType& Next();
55
56   /** Return true if the next value exists */
57   bool HasNext() const;
58
59 private:
60
61   /** Find the next node */
62   const TreeNodeType* FindNextNode() const;
63
64 };
65
66 /** Constructor */
67 template <class TTreeType>
68 LeafTreeIterator<TTreeType>::LeafTreeIterator(  const TTreeType* tree ) 
69 IND **:TreeIteratorBase<TTreeType>(tree,NULL)
70 {
71 LEN   this->m_Begin = const_cast<TreeNodeType* >(this->FindNextNode()); // Position the iterator to the first leaf;
72 }
73
74 /** Constructor */
75 template <class TTreeType>
76 LeafTreeIterator<TTreeType>::LeafTreeIterator( TTreeType* tree ) 
77 IND **:TreeIteratorBase<TTreeType>(tree,NULL)
78 {
79 LEN   this->m_Begin = const_cast<TreeNodeType* >(this->FindNextNode()); // Position the iterator to the first leaf;
80 }
81
82 /** Destructor */
83 template <class TTreeType>
84 LeafTreeIterator<TTreeType>::~LeafTreeIterator() 
85 {
86 }
87
88 /** Return the type of iterator */
89 template <class TTreeType>
90 int LeafTreeIterator<TTreeType>::GetType() const 
91 {
92   return TreeIteratorBase<TTreeType>::LEAF;
93 }
94
95 /** Return true if the next value exists */
96 template <class TTreeType>
97 bool LeafTreeIterator<TTreeType>::HasNext() const
98 {
99   if(this->m_Position == NULL)
100     {
101     return false;
102     }
103   if ( const_cast<TreeNodeType* >(FindNextNode()) != NULL )
104     {
105     return true;
106     }
107   return false;
108 }
109
110 /** Return the next node */
111 template <class TTreeType>
112 const typename LeafTreeIterator<TTreeType>::ValueType&
113 LeafTreeIterator<TTreeType>::Next() 
114 {
115   this->m_Position = const_cast<TreeNodeType* >(FindNextNode());
116   return this->m_Position->Get();
117 }
118
119 /** Find the next node given the position */
120 template <class TTreeType>
121 const typename LeafTreeIterator<TTreeType>::TreeNodeType* 
122 LeafTreeIterator<TTreeType>::FindNextNode() const 
123 {
124   PreOrderTreeIterator<TTreeType> it(this->m_Tree,this->m_Position); 
125   ++it; // go next
126   if(it.IsAtEnd())
127     {
128     return NULL;
129     }
130
131   if(!it.HasChild())
132     {
133     return it.GetNode();
134     }
135
136   while( !it.IsAtEnd() )
137     {
138     if(!it.HasChild())
139       {
140       return it.GetNode();
141       }
142     ++it;
143     }
144   
145   return NULL;
146 }
147
148 /** Clone function */
149 template <class TTreeType>
150 TreeIteratorBase<TTreeType>* LeafTreeIterator<TTreeType>::Clone() 
151 {
152 LEN   LeafTreeIterator<TTreeType>* clone = new LeafTreeIterator<TTreeType>( this->m_Tree );
153   *clone = *this;
154   return clone;
155 }
156
157 // end namespace itk
158
159 #endif
160

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:18PM
© Kitware Inc.