KWStyle - itkPreOrderTreeIterator.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkPreOrderTreeIterator.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:45 $
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 __itkPreOrderTreeIterator_h
18 #define __itkPreOrderTreeIterator_h
19
20 #include <itkTreeIteratorBase.h>
21
22 NMS namespace itk{
23
24 template <class TTreeType>
25 class PreOrderTreeIterator : public TreeIteratorBase<TTreeType> 
26 {
27 public:
28
29   /** Typedefs */
30   typedef typename TTreeType::ValueType  ValueType;
31 TDA   typedef TreeIteratorBase<TTreeType> Superclass;
32 TDA   typedef typename Superclass::TreeNodeType TreeNodeType;
33  
34   /** Constructor */
35   PreOrderTreeIterator( const TTreeType* tree, TreeNodeType* start = NULL );
36
37   /** Get the type of the iterator */
38   int GetType() const;
39   /** Clone function */
40   TreeIteratorBase<TTreeType>* Clone();
41
42 protected:
43   /** Return the next node */
44   const ValueType& Next();
45   /** Return true if the next node exists */
46   bool HasNext() const;
47   
48 private:  
49
50   /** Find the next node */
51   const TreeNodeType* FindNextNode() const;
52
53 };
54
55
56 /** Constructor */
57 template <class TTreeType>
58 LEN PreOrderTreeIterator<TTreeType>::PreOrderTreeIterator( const TTreeType* tree, TreeNodeType* start )
59 IND **:TreeIteratorBase<TTreeType>(tree,start) 
60 {
61
62 }
63
64 /** Return the type of the iterator */
65 template <class TTreeType>
66 int 
67 PreOrderTreeIterator<TTreeType>::GetType() const 
68 {
69   return TreeIteratorBase<TTreeType>::PREORDER; 
70 }
71
72 /** Return true if the next node exists */
73 template <class TTreeType>
74 bool 
75 PreOrderTreeIterator<TTreeType>::HasNext() const
76 {
77   if ( const_cast<TreeNodeType* >(FindNextNode()) != NULL )
78     {
79     return true;
80     }
81   return false;
82 }
83
84 /** Return the next node */
85 template <class TTreeType>
86 const typename PreOrderTreeIterator<TTreeType>::ValueType&
87 PreOrderTreeIterator<TTreeType>::Next() 
88 {
89   this->m_Position = const_cast<TreeNodeType* >(FindNextNode());
90   return this->m_Position->Get();
91 }
92
93 /** Find the next node */
94 template <class TTreeType>
95 const typename PreOrderTreeIterator<TTreeType>::TreeNodeType* 
96 PreOrderTreeIterator<TTreeType>::FindNextNode() const
97
98   if ( this->m_Position == NULL )
99     {
100     return NULL;
101     }
102   if ( this->m_Position->HasChildren() )
103     {
104     return dynamic_cast<const TreeNodeType*>(this->m_Position->GetChild(0));
105     }
106
107   if ( !this->m_Position->HasParent() )
108     {
109     return NULL;
110     }
111
112   TreeNodeType* child = this->m_Position;
113 LEN   TreeNodeType* parent = dynamic_cast<TreeNodeType*>(this->m_Position->GetParent());
114
115   int childPosition = parent->ChildPosition( child ); 
116   int lastChildPosition = parent->CountChildren() - 1;
117
118   while ( childPosition < lastChildPosition ) 
119     {
120 LEN     TreeNodeType* help = dynamic_cast<TreeNodeType*>(parent->GetChild( childPosition + 1 ));
121
122     if ( help != NULL )
123       {
124       return help;
125       }
126     childPosition++;
127     }
128   
129   while ( parent->HasParent() ) 
130     {
131     child = parent;
132     parent = dynamic_cast<TreeNodeType*>(parent->GetParent());
133
134     // Subtree
135     if( parent->ChildPosition( this->m_Root ) >= 0 )
136       {
137       return NULL;
138       }
139
140     childPosition = parent->ChildPosition(child);
141     lastChildPosition = parent->CountChildren() - 1;
142
143     while ( childPosition < lastChildPosition ) 
144       {
145 LEN       TreeNodeType* help = dynamic_cast<TreeNodeType*>(parent->GetChild( childPosition + 1 ));
146
147       if ( help != NULL )
148         {
149         return help;
150         }
151       }
152     }
153   return NULL;
154 }
155
156 /** Clone function */
157 template <class TTreeType>
158 TreeIteratorBase<TTreeType>* PreOrderTreeIterator<TTreeType>::Clone() 
159 {
160 LEN   PreOrderTreeIterator<TTreeType>* clone = new PreOrderTreeIterator<TTreeType>(this-> m_Tree, this->m_Position );
161   *clone = *this;
162   return clone;
163 }
164
165 // end namespace itk
166
167 #endif
168

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