ITK  5.1.0
Insight Toolkit
itkRootTreeIterator.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 itkRootTreeIterator_h
19 #define itkRootTreeIterator_h
20 
21 #include "itkTreeIteratorBase.h"
22 
23 namespace itk
24 {
25 template <typename TTreeType>
26 class RootTreeIterator : public TreeIteratorBase<TTreeType>
27 {
28 public:
31  using TreeType = TTreeType;
32  using ValueType = typename TTreeType::ValueType;
34  using NodeType = typename Superclass::NodeType;
35 
37  RootTreeIterator(TreeType * tree, const TreeNodeType * start = nullptr);
38 
40  NodeType
41  GetType() const override;
42 
45  Clone() override;
46 
47 protected:
49  const ValueType &
50  Next() override;
51 
53  bool
54  HasNext() const override;
55 
56 private:
58  const TreeNodeType *
59  FindNextNode() const;
60 };
61 
63 template <typename TTreeType>
65  : TreeIteratorBase<TTreeType>(tree, start)
66 {
67  if (start)
68  {
69  this->m_Begin = const_cast<TreeNode<ValueType> *>(start);
70  }
71  this->m_Root = tree->GetRoot();
72  this->m_Position = this->m_Begin;
73 }
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  return this->m_Position->Get();
104 }
106 
108 template <typename TTreeType>
111 {
112  if (this->m_Position == nullptr)
113  {
114  return nullptr;
115  }
116  if (this->m_Position == this->m_Root)
117  {
118  return nullptr;
119  }
120  return this->m_Position->GetParent();
121 }
123 
125 template <typename TTreeType>
128 {
129  auto * clone = new RootTreeIterator<TTreeType>(const_cast<TTreeType *>(this->m_Tree), this->m_Position);
130  *clone = *this;
131  return clone;
132 }
133 } // end namespace itk
135 
136 #endif
itk::TreeNode
Represents a node in a tree.
Definition: itkTreeNode.h:43
itkTreeIteratorBase.h
itk::TreeIteratorBase::m_Begin
TreeNodeType * m_Begin
Definition: itkTreeIteratorBase.h:264
itk::RootTreeIterator::NodeType
typename Superclass::NodeType NodeType
Definition: itkRootTreeIterator.h:34
itk::TreeIteratorBase
This class provides the base implementation for tree iterators.
Definition: itkTreeIteratorBase.h:58
itk::TreeIteratorBaseNodeEnum::ROOT
itk::RootTreeIterator::Next
const ValueType & Next() override
Definition: itkRootTreeIterator.h:100
itk::RootTreeIterator::FindNextNode
const TreeNodeType * FindNextNode() const
Definition: itkRootTreeIterator.h:110
itk::RootTreeIterator::TreeType
TTreeType TreeType
Definition: itkRootTreeIterator.h:31
itk::TreeIteratorBase::TreeNodeType
typename TTreeType::TreeNodeType TreeNodeType
Definition: itkTreeIteratorBase.h:64
itk::RootTreeIterator::Clone
TreeIteratorBase< TTreeType > * Clone() override
Definition: itkRootTreeIterator.h:127
itk::RootTreeIterator::GetType
NodeType GetType() const override
Definition: itkRootTreeIterator.h:79
itk::RootTreeIterator::HasNext
bool HasNext() const override
Definition: itkRootTreeIterator.h:87
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::TreeIteratorBase::m_Position
TreeNodeType * m_Position
Definition: itkTreeIteratorBase.h:263
itk::RootTreeIterator::ValueType
typename TTreeType::ValueType ValueType
Definition: itkRootTreeIterator.h:32
itk::RootTreeIterator::TreeNodeType
typename Superclass::TreeNodeType TreeNodeType
Definition: itkRootTreeIterator.h:33
itk::TreeIteratorBase::m_Root
const TreeNodeType * m_Root
Definition: itkTreeIteratorBase.h:265
itk::RootTreeIterator
Definition: itkRootTreeIterator.h:26
itk::TreeIteratorBase::NodeType
TreeIteratorBaseNodeEnum NodeType
Definition: itkTreeIteratorBase.h:68
itk::RootTreeIterator::RootTreeIterator
RootTreeIterator(TreeType *tree, const TreeNodeType *start=nullptr)
Definition: itkRootTreeIterator.h:64