ITK  5.1.0
Insight Toolkit
itkTreeIteratorBase.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 itkTreeIteratorBase_h
19 #define itkTreeIteratorBase_h
20 
21 #include "itkTreeNode.h"
22 
23 namespace itk
24 {
30 {
31  UNDEFIND = 0,
32  PREORDER = 1,
33  INORDER = 2,
34  POSTORDER = 3,
35  LEVELORDER = 4,
36  CHILD = 5,
37  ROOT = 6,
38  LEAF = 7
39 };
40 
57 template <typename TTreeType>
58 class ITK_TEMPLATE_EXPORT TreeIteratorBase
59 {
60 public:
63  using ValueType = typename TTreeType::ValueType;
64  using TreeNodeType = typename TTreeType::TreeNodeType;
65  using ChildIdentifier = typename TreeNodeType::ChildIdentifier;
66 
69 #if !defined(ITK_LEGACY_REMOVE)
70  // We need to expose the enum values at the class level
71  // for backwards compatibility
72  static constexpr NodeType UNDEFIND = NodeType::UNDEFIND;
73  static constexpr NodeType PREORDER = NodeType::PREORDER;
74  static constexpr NodeType INORDER = NodeType::INORDER;
75  static constexpr NodeType POSTORDER = NodeType::POSTORDER;
76  static constexpr NodeType LEVELORDER = NodeType::LEVELORDER;
77  static constexpr NodeType CHILD = NodeType::CHILD;
78  static constexpr NodeType ROOT = NodeType::ROOT;
79  static constexpr NodeType LEAF = NodeType::LEAF;
80 #endif
81 
83  virtual bool
84  Add(ValueType element);
85 
87  virtual bool
88  Add(int position, ValueType element);
89 
91  virtual bool
92  Add(TTreeType & subTree);
93 
95  virtual const ValueType &
96  Get() const;
97 
99  virtual TTreeType *
100  GetSubTree() const;
101 
103  virtual bool
104  IsLeaf() const;
105 
107  virtual bool
108  IsRoot() const;
109 
111  virtual NodeType
112  GetType() const = 0;
113 
115  virtual bool
116  GoToChild(ChildIdentifier number = 0);
117 
119  virtual bool
120  GoToParent();
121 
123  void
124  Set(ValueType element);
125 
127  virtual bool
128  HasChild(int number = 0) const;
129 
131  virtual int
132  ChildPosition(ValueType element) const;
133 
135  virtual bool
136  RemoveChild(int number);
137 
139  virtual int
140  CountChildren() const;
141 
143  virtual bool
144  HasParent() const;
145 
147  virtual bool
148  Disconnect();
149 
152  Children();
153 
156  Parents();
157 
160  GetChild(int number) const;
161 
163  virtual int
164  Count();
165 
167  bool
168  Remove();
169 
171  virtual TreeNodeType *
172  GetNode();
173 
174  virtual const TreeNodeType *
175  GetNode() const;
176 
178  TreeNodeType *
179  GetRoot();
180 
181  const TreeNodeType *
182  GetRoot() const;
183 
185  TTreeType *
186  GetTree() const;
187 
189  const TreeNodeType *
190  GetParent() const;
191 
193  void
195  {
196  m_Position = m_Begin;
197  }
198 
200  void
202  {
203  m_Position = nullptr;
204  }
205 
207  bool
208  IsAtBegin() const
209  {
210  return (m_Position == m_Begin);
211  }
212 
214  bool
215  IsAtEnd() const
216  {
217  return (m_Position == nullptr);
218  }
219 
222  Clone() = 0;
223 
225  Self &
227  {
228  this->Next();
229  return *this;
230  }
232 
234  void
236  {
237  this->Next();
238  }
239 
241  Self &
242  operator=(const Self & iterator)
243  {
244  if (this != &iterator)
245  {
246  m_Position = iterator.m_Position;
247  m_Begin = iterator.m_Begin;
248  m_Root = iterator.m_Root;
249  m_Tree = iterator.m_Tree;
250  }
251  return *this;
252  }
254 
255  virtual ~TreeIteratorBase() = default;
256 
257 protected:
259  TreeIteratorBase(TTreeType * tree, const TreeNodeType * start);
260  TreeIteratorBase(const TTreeType * tree, const TreeNodeType * start);
262 
263  mutable TreeNodeType * m_Position; // Current position of the iterator
266  TTreeType * m_Tree;
267 
268  virtual bool
269  HasNext() const = 0;
270 
271  virtual const ValueType &
272  Next() = 0;
273 };
274 } // end namespace itk
275 
276 #ifndef ITK_MANUAL_INSTANTIATION
277 # include "itkTreeIteratorBase.hxx"
278 #endif
279 
280 #endif
itk::uint8_t
::uint8_t uint8_t
Definition: itkIntTypes.h:29
itk::TreeIteratorBaseNodeEnum::INORDER
itk::TreeIteratorBase::m_Begin
TreeNodeType * m_Begin
Definition: itkTreeIteratorBase.h:264
itk::TreeIteratorBaseNodeEnum::LEVELORDER
itk::TreeIteratorBaseNodeEnum::POSTORDER
TreeIteratorBaseNodeEnum
itk::TreeIteratorBaseNodeEnum::LEAF
itk::TreeIteratorBase::ValueType
typename TTreeType::ValueType ValueType
Definition: itkTreeIteratorBase.h:63
itk::TreeIteratorBase::ChildIdentifier
typename TreeNodeType::ChildIdentifier ChildIdentifier
Definition: itkTreeIteratorBase.h:65
itk::TreeIteratorBaseNodeEnum::UNDEFIND
itk::TreeIteratorBase
This class provides the base implementation for tree iterators.
Definition: itkTreeIteratorBase.h:58
itk::TreeIteratorBaseNodeEnum::CHILD
itk::TreeIteratorBaseNodeEnum::ROOT
itk::TreeIteratorBaseNodeEnum
TreeIteratorBaseNodeEnum
Definition: itkTreeIteratorBase.h:29
itk::TreeIteratorBase::IsAtEnd
bool IsAtEnd() const
Definition: itkTreeIteratorBase.h:215
itk::TreeIteratorBase::GoToBegin
void GoToBegin()
Definition: itkTreeIteratorBase.h:194
itk::TreeIteratorBase::TreeNodeType
typename TTreeType::TreeNodeType TreeNodeType
Definition: itkTreeIteratorBase.h:64
itk::TreeIteratorBase::operator=
Self & operator=(const Self &iterator)
Definition: itkTreeIteratorBase.h:242
itk::TreeIteratorBase::IsAtBegin
bool IsAtBegin() const
Definition: itkTreeIteratorBase.h:208
itk::TreeIteratorBase::m_Tree
TTreeType * m_Tree
Definition: itkTreeIteratorBase.h:266
itk::TreeIteratorBase::operator++
Self & operator++()
Definition: itkTreeIteratorBase.h:226
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
itk::TreeIteratorBaseNodeEnum::PREORDER
itk::TreeIteratorBase::m_Position
TreeNodeType * m_Position
Definition: itkTreeIteratorBase.h:263
itkTreeNode.h
itk::TreeIteratorBase::GoToEnd
void GoToEnd()
Definition: itkTreeIteratorBase.h:201
itk::TreeIteratorBase::m_Root
const TreeNodeType * m_Root
Definition: itkTreeIteratorBase.h:265
itk::TreeIteratorBase::operator++
void operator++(int)
Definition: itkTreeIteratorBase.h:235