ITK  5.0.0
Insight Segmentation and Registration 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:
29 
32  using TreeType = TTreeType;
33  using ValueType = typename TTreeType::ValueType;
35  using NodeType = typename Superclass::NodeType;
36 
38  RootTreeIterator(TreeType *tree, const TreeNodeType *start = nullptr);
39 
41  NodeType GetType() const override;
42 
45 
46 protected:
47 
49  const ValueType & Next() override;
50 
52  bool HasNext() const override;
53 
54 private:
55 
57  const TreeNodeType * FindNextNode() const;
58 };
59 
61 template< typename TTreeType >
63  TreeIteratorBase< TTreeType >(tree, start)
64 {
65  if ( start )
66  {
67  this->m_Begin = const_cast< TreeNode< ValueType > * >( start );
68  }
69  this->m_Root = tree->GetRoot();
70  this->m_Position = this->m_Begin;
71 }
73 
75 template< typename TTreeType >
78 {
80 }
81 
83 template< typename TTreeType >
84 bool
86 {
87  if ( const_cast< TreeNodeType * >( FindNextNode() ) != nullptr )
88  {
89  return true;
90  }
91  return false;
92 }
94 
96 template< typename TTreeType >
99 {
100  this->m_Position = const_cast< TreeNodeType * >( FindNextNode() );
101  return this->m_Position->Get();
102 }
104 
106 template< typename TTreeType >
109 {
110  if ( this->m_Position == nullptr )
111  {
112  return nullptr;
113  }
114  if ( this->m_Position == this->m_Root )
115  {
116  return nullptr;
117  }
118  return this->m_Position->GetParent();
119 }
121 
123 template< typename TTreeType >
125 {
126  auto * clone = new RootTreeIterator< TTreeType >(const_cast< TTreeType * >( this->m_Tree ),
127  this->m_Position);
128  *clone = *this;
129  return clone;
130 }
131 } // end namespace itk
133 
134 #endif
typename Superclass::TreeNodeType TreeNodeType
Represents a node in a tree.
Definition: itkTreeNode.h:43
typename Superclass::NodeType NodeType
typename TTreeType::ValueType ValueType
RootTreeIterator(TreeType *tree, const TreeNodeType *start=nullptr)
NodeType GetType() const override
const TreeNodeType * FindNextNode() const
This class provides the base implementation for tree iterators.
typename TTreeType::TreeNodeType TreeNodeType
bool HasNext() const override
const TreeNodeType * m_Root
const ValueType & Next() override
TreeIteratorBase< TTreeType > * Clone() override