ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkLeafTreeIterator.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 itkLeafTreeIterator_h
19 #define itkLeafTreeIterator_h
20 
22 
23 namespace itk
24 {
25 template< typename TTreeType >
26 class ITK_TEMPLATE_EXPORT LeafTreeIterator:public TreeIteratorBase< TTreeType >
27 {
28 public:
29 
33  using TreeType = TTreeType;
34  using ValueType = typename TreeType::ValueType;
36  using NodeType = typename Superclass::NodeType;
37 
39  LeafTreeIterator(const TreeType *tree);
40 
43 
45  ~LeafTreeIterator() override;
46 
48  NodeType GetType() const override;
49 
51  TreeIteratorBase< TTreeType > * Clone() override;
52 
53 protected:
54 
56  const ValueType & Next() override;
57 
59  bool HasNext() const override;
60 
61 private:
62 
64  const TreeNodeType * FindNextNode() const;
65 };
66 
68 template< typename TTreeType >
70  TreeIteratorBase< TTreeType >(tree, nullptr)
71 {
72  this->m_Begin = const_cast< TreeNodeType * >( this->FindNextNode() ); //
73  //
74  // Position
75  // the
76  //
77  // iterator
78  // to
79  // the
80  // first
81  // leaf;
82 }
84 
86 template< typename TTreeType >
88  TreeIteratorBase< TTreeType >(tree, nullptr)
89 {
90  this->m_Begin = const_cast< TreeNodeType * >( this->FindNextNode() ); //
91  //
92  // Position
93  // the
94  //
95  // iterator
96  // to
97  // the
98  // first
99  // leaf;
100 }
102 
104 template< typename TTreeType >
106 
108 template< typename TTreeType >
111 {
113 }
114 
116 template< typename TTreeType >
118 {
119  if ( this->m_Position == nullptr )
120  {
121  return false;
122  }
123  if ( const_cast< TreeNodeType * >( FindNextNode() ) != nullptr )
124  {
125  return true;
126  }
127  return false;
128 }
130 
132 template< typename TTreeType >
135 {
136  this->m_Position = const_cast< TreeNodeType * >( FindNextNode() );
137  return this->m_Position->Get();
138 }
140 
142 template< typename TTreeType >
145 {
146  PreOrderTreeIterator< TTreeType > it(this->m_Tree, this->m_Position);
147  it.m_Root = this->m_Root;
148  ++it; // go next
149  if ( it.IsAtEnd() )
150  {
151  return nullptr;
152  }
154 
155  if ( !it.HasChild() )
156  {
157  return it.GetNode();
158  }
159 
160  while ( !it.IsAtEnd() )
161  {
162  if ( !it.HasChild() )
163  {
164  return it.GetNode();
165  }
166  ++it;
167  }
168 
169  return nullptr;
170 }
171 
173 template< typename TTreeType >
175 {
176  auto * clone = new LeafTreeIterator< TTreeType >(this->m_Tree);
177  *clone = *this;
178  return clone;
179 }
180 } // end namespace itk
182 
183 #endif
LeafTreeIterator(const TreeType *tree)
typename Superclass::NodeType NodeType
virtual bool HasChild(int number=0) const
const TreeNodeType * FindNextNode() const
NodeType GetType() const override
typename Superclass::TreeNodeType TreeNodeType
typename TreeType::ValueType ValueType
virtual TreeNodeType * GetNode()
TreeIteratorBase< TTreeType > * Clone() override
This class provides the base implementation for tree iterators.
bool HasNext() const override
~LeafTreeIterator() override
typename TTreeType::TreeNodeType TreeNodeType
class ITK_TEMPLATE_EXPORT LeafTreeIterator
const TreeNodeType * m_Root
const ValueType & Next() override