ITK  5.1.0
Insight 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:
34  using ValueType = typename TTreeType::ValueType;
37  using NodeType = typename Superclass::NodeType;
38 
40  PreOrderTreeIterator(const TTreeType * tree, const TreeNodeType * start = nullptr);
41 
43  NodeType
44  GetType() const override;
45 
48  Clone() override;
49 
50 protected:
52  const ValueType &
53  Next() override;
54 
56  bool
57  HasNext() const override;
58 
59 private:
61  const TreeNodeType *
62  FindNextNode() const;
63 
67  friend class LeafTreeIterator<TTreeType>;
68 };
69 
71 template <typename TTreeType>
73  : TreeIteratorBase<TTreeType>(tree, start)
74 {}
75 
77 template <typename TTreeType>
80 {
82 }
83 
85 template <typename TTreeType>
86 bool
88 {
89  if (const_cast<TreeNodeType *>(FindNextNode()) != nullptr)
90  {
91  return true;
92  }
93  return false;
94 }
96 
98 template <typename TTreeType>
101 {
102  this->m_Position = const_cast<TreeNodeType *>(FindNextNode());
103  if (this->m_Position == nullptr)
104  {
105  return this->m_Root->Get(); // value irrelevant, but we have to return something
106  }
107  return this->m_Position->Get();
108 }
110 
112 template <typename TTreeType>
115 {
116  if (this->m_Position == nullptr)
117  {
118  return nullptr;
119  }
120  if (this->m_Position->HasChildren())
121  {
122  return dynamic_cast<const TreeNodeType *>(this->m_Position->GetChild(0));
123  }
125 
126  if (!this->m_Position->HasParent())
127  {
128  return nullptr;
129  }
130 
131  TreeNodeType * child = this->m_Position;
132  TreeNodeType * parent = this->m_Position;
133 
134  while (parent->HasParent())
135  {
136  child = parent;
137  parent = dynamic_cast<TreeNodeType *>(parent->GetParent());
138 
139  // Subtree
140  if (parent->ChildPosition(this->m_Root) >= 0)
141  {
142  return nullptr;
143  }
144 
145  int childPosition = parent->ChildPosition(child);
146  int lastChildPosition = parent->CountChildren() - 1;
147 
148  while (childPosition < lastChildPosition)
149  {
150  auto * help = dynamic_cast<TreeNodeType *>(parent->GetChild(childPosition + 1));
151 
152  if (help != nullptr)
153  {
154  return help;
155  }
156  childPosition++;
157  }
158  }
159  return nullptr;
160 }
161 
163 template <typename TTreeType>
166 {
167  auto * clone = new PreOrderTreeIterator<TTreeType>(this->m_Tree, this->m_Position);
168  *clone = *this;
169  return clone;
170 }
171 } // end namespace itk
173 
174 #endif
itkTreeIteratorBase.h
itk::PreOrderTreeIterator::FindNextNode
const TreeNodeType * FindNextNode() const
Definition: itkPreOrderTreeIterator.h:114
itk::PreOrderTreeIterator::TreeNodeType
typename Superclass::TreeNodeType TreeNodeType
Definition: itkPreOrderTreeIterator.h:36
itk::PreOrderTreeIterator::Clone
TreeIteratorBase< TTreeType > * Clone() override
Definition: itkPreOrderTreeIterator.h:165
itk::PreOrderTreeIterator::Next
const ValueType & Next() override
Definition: itkPreOrderTreeIterator.h:100
itk::PreOrderTreeIterator::ValueType
typename TTreeType::ValueType ValueType
Definition: itkPreOrderTreeIterator.h:34
itk::LeafTreeIterator
Definition: itkLeafTreeIterator.h:26
itk::TreeIteratorBase
This class provides the base implementation for tree iterators.
Definition: itkTreeIteratorBase.h:58
itk::PreOrderTreeIterator::GetType
NodeType GetType() const override
Definition: itkPreOrderTreeIterator.h:79
itk::PreOrderTreeIterator
Definition: itkPreOrderTreeIterator.h:30
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::TreeIteratorBaseNodeEnum::PREORDER
itk::PreOrderTreeIterator::HasNext
bool HasNext() const override
Definition: itkPreOrderTreeIterator.h:87
itk::PreOrderTreeIterator::NodeType
typename Superclass::NodeType NodeType
Definition: itkPreOrderTreeIterator.h:37
itk::PreOrderTreeIterator::PreOrderTreeIterator
PreOrderTreeIterator(const TTreeType *tree, const TreeNodeType *start=nullptr)
Definition: itkPreOrderTreeIterator.h:72
itk::TreeIteratorBase::NodeType
TreeIteratorBaseNodeEnum NodeType
Definition: itkTreeIteratorBase.h:68