ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkPreOrderTreeIterator.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkPreOrderTreeIterator_h
19 #define itkPreOrderTreeIterator_h
20 
21 #include "itkTreeIteratorBase.h"
22 
23 namespace itk
24 {
25 // Forward reference because of circular dependencies
26 template< typename TTreeType >
27 class ITK_TEMPLATE_EXPORT LeafTreeIterator;
28 
29 template< typename TTreeType >
30 class PreOrderTreeIterator:public TreeIteratorBase< TTreeType >
31 {
32 public:
33 
35  using ValueType = typename TTreeType::ValueType;
38  using NodeType = typename Superclass::NodeType;
39 
41  PreOrderTreeIterator(const TTreeType *tree, const TreeNodeType *start = nullptr);
42 
44  NodeType GetType() const override;
45 
48 
49 protected:
51  const ValueType & Next() override;
52 
54  bool HasNext() const override;
55 
56 private:
57 
59  const TreeNodeType * FindNextNode() const;
60 
64  friend class LeafTreeIterator< TTreeType >;
65 };
66 
68 template< typename TTreeType >
70  TreeIteratorBase< TTreeType >(tree, start)
71 {}
72 
74 template< typename TTreeType >
77 {
79 }
80 
82 template< typename TTreeType >
83 bool
85 {
86  if ( const_cast< TreeNodeType * >( FindNextNode() ) != nullptr )
87  {
88  return true;
89  }
90  return false;
91 }
93 
95 template< typename TTreeType >
98 {
99  this->m_Position = const_cast< TreeNodeType * >( FindNextNode() );
100  if ( this->m_Position == nullptr )
101  {
102  return this->m_Root->Get(); //value irrelevant, but we have to return something
103  }
104  return this->m_Position->Get();
105 }
107 
109 template< typename TTreeType >
112 {
113  if ( this->m_Position == nullptr )
114  {
115  return nullptr;
116  }
117  if ( this->m_Position->HasChildren() )
118  {
119  return dynamic_cast< const TreeNodeType * >( this->m_Position->GetChild(0) );
120  }
122 
123  if ( !this->m_Position->HasParent() )
124  {
125  return nullptr;
126  }
127 
128  TreeNodeType* child = this->m_Position;
129  TreeNodeType* parent = this->m_Position;
130 
131  while ( parent->HasParent() )
132  {
133  child = parent;
134  parent = dynamic_cast< TreeNodeType * >( parent->GetParent() );
135 
136  // Subtree
137  if ( parent->ChildPosition(this->m_Root) >= 0 )
138  {
139  return nullptr;
140  }
141 
142  int childPosition = parent->ChildPosition(child);
143  int lastChildPosition = parent->CountChildren() - 1;
144 
145  while ( childPosition < lastChildPosition )
146  {
147  auto * help = dynamic_cast< TreeNodeType * >( parent->GetChild(childPosition + 1) );
148 
149  if ( help != nullptr )
150  {
151  return help;
152  }
153  childPosition++;
154  }
155  }
156  return nullptr;
157 }
158 
160 template< typename TTreeType >
162 {
163  auto * clone = new PreOrderTreeIterator< TTreeType >(this->m_Tree, this->m_Position);
164  *clone = *this;
165  return clone;
166 }
167 } // end namespace itk
169 
170 #endif
typename TTreeType::ValueType ValueType
NodeType GetType() const override
const ValueType & Next() override
typename Superclass::TreeNodeType TreeNodeType
PreOrderTreeIterator(const TTreeType *tree, const TreeNodeType *start=nullptr)
const TreeNodeType * FindNextNode() const
typename Superclass::NodeType NodeType
This class provides the base implementation for tree iterators.
typename TTreeType::TreeNodeType TreeNodeType
TreeIteratorBase< TTreeType > * Clone() override