ITK  5.1.0
Insight 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:
32  using TreeType = TTreeType;
33  using ValueType = typename TreeType::ValueType;
35  using NodeType = typename Superclass::NodeType;
36 
38  LeafTreeIterator(const TreeType * tree);
39 
41  LeafTreeIterator(TreeType * tree);
42 
44  ~LeafTreeIterator() override;
45 
47  NodeType
48  GetType() const override;
49 
52  Clone() override;
53 
54 protected:
56  const ValueType &
57  Next() override;
58 
60  bool
61  HasNext() const override;
62 
63 private:
65  const TreeNodeType *
66  FindNextNode() const;
67 };
68 
70 template <typename TTreeType>
72  : TreeIteratorBase<TTreeType>(tree, nullptr)
73 {
74  this->m_Begin = const_cast<TreeNodeType *>(this->FindNextNode()); //
75  //
76  // Position
77  // the
78  //
79  // iterator
80  // to
81  // the
82  // first
83  // leaf;
84 }
86 
88 template <typename TTreeType>
90  : TreeIteratorBase<TTreeType>(tree, nullptr)
91 {
92  this->m_Begin = const_cast<TreeNodeType *>(this->FindNextNode()); //
93  //
94  // Position
95  // the
96  //
97  // iterator
98  // to
99  // the
100  // first
101  // leaf;
102 }
104 
106 template <typename TTreeType>
108 
110 template <typename TTreeType>
113 {
115 }
116 
118 template <typename TTreeType>
119 bool
121 {
122  if (this->m_Position == nullptr)
123  {
124  return false;
125  }
126  if (const_cast<TreeNodeType *>(FindNextNode()) != nullptr)
127  {
128  return true;
129  }
130  return false;
131 }
133 
135 template <typename TTreeType>
138 {
139  this->m_Position = const_cast<TreeNodeType *>(FindNextNode());
140  if (this->m_Position == nullptr)
141  {
142  return this->m_Root->Get(); // value irrelevant, but we have to return something
143  }
144  return this->m_Position->Get();
145 }
147 
149 template <typename TTreeType>
152 {
153  PreOrderTreeIterator<TTreeType> it(this->m_Tree, this->m_Position);
154  it.m_Root = this->m_Root;
155  ++it; // go next
156  if (it.IsAtEnd())
157  {
158  return nullptr;
159  }
161 
162  if (!it.HasChild())
163  {
164  return it.GetNode();
165  }
166 
167  while (!it.IsAtEnd())
168  {
169  if (!it.HasChild())
170  {
171  return it.GetNode();
172  }
173  ++it;
174  }
175 
176  return nullptr;
177 }
178 
180 template <typename TTreeType>
183 {
184  auto * clone = new LeafTreeIterator<TTreeType>(this->m_Tree);
185  *clone = *this;
186  return clone;
187 }
188 } // end namespace itk
190 
191 #endif
itkPreOrderTreeIterator.h
itk::TreeIteratorBase::m_Begin
TreeNodeType * m_Begin
Definition: itkTreeIteratorBase.h:264
itk::LeafTreeIterator::HasNext
bool HasNext() const override
Definition: itkLeafTreeIterator.h:120
itk::TreeIteratorBase::GetNode
virtual TreeNodeType * GetNode()
itk::LeafTreeIterator::LeafTreeIterator
LeafTreeIterator(const TreeType *tree)
Definition: itkLeafTreeIterator.h:71
itk::LeafTreeIterator
class ITK_TEMPLATE_EXPORT LeafTreeIterator
Definition: itkPreOrderTreeIterator.h:27
itk::TreeIteratorBaseNodeEnum::LEAF
itk::LeafTreeIterator
Definition: itkLeafTreeIterator.h:26
itk::TreeIteratorBase::HasChild
virtual bool HasChild(int number=0) const
itk::TreeIteratorBase
This class provides the base implementation for tree iterators.
Definition: itkTreeIteratorBase.h:58
itk::LeafTreeIterator::ValueType
typename TreeType::ValueType ValueType
Definition: itkLeafTreeIterator.h:33
itk::PreOrderTreeIterator
Definition: itkPreOrderTreeIterator.h:30
itk::LeafTreeIterator::Clone
TreeIteratorBase< TTreeType > * Clone() override
Definition: itkLeafTreeIterator.h:182
itk::LeafTreeIterator::Next
const ValueType & Next() override
Definition: itkLeafTreeIterator.h:137
itk::LeafTreeIterator::GetType
NodeType GetType() const override
Definition: itkLeafTreeIterator.h:112
itk::LeafTreeIterator::FindNextNode
const TreeNodeType * FindNextNode() const
Definition: itkLeafTreeIterator.h:151
itk::TreeIteratorBaseNodeEnum
TreeIteratorBaseNodeEnum
Definition: itkTreeIteratorBase.h:29
itk::TreeIteratorBase::IsAtEnd
bool IsAtEnd() const
Definition: itkTreeIteratorBase.h:215
itk::LeafTreeIterator::~LeafTreeIterator
~LeafTreeIterator() override
itk::LeafTreeIterator::TreeNodeType
typename Superclass::TreeNodeType TreeNodeType
Definition: itkLeafTreeIterator.h:34
itk::TreeIteratorBase::TreeNodeType
typename TTreeType::TreeNodeType TreeNodeType
Definition: itkTreeIteratorBase.h:64
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::LeafTreeIterator::NodeType
typename Superclass::NodeType NodeType
Definition: itkLeafTreeIterator.h:35
itk::TreeIteratorBase::m_Root
const TreeNodeType * m_Root
Definition: itkTreeIteratorBase.h:265
itk::LeafTreeIterator::TreeType
TTreeType TreeType
Definition: itkLeafTreeIterator.h:32