ITK  5.0.0
Insight Segmentation and Registration 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 {
41 template< typename TTreeType >
42 class ITK_TEMPLATE_EXPORT TreeIteratorBase
43 {
44 public:
45 
48  using ValueType = typename TTreeType::ValueType;
49  using TreeNodeType = typename TTreeType::TreeNodeType;
50  using ChildIdentifier = typename TreeNodeType::ChildIdentifier;
51 
53  typedef enum
54  {
55  UNDEFIND = 0,
56  PREORDER = 1,
57  INORDER = 2,
58  POSTORDER = 3,
59  LEVELORDER = 4,
60  CHILD = 5,
61  ROOT = 6,
62  LEAF = 7
63  }
64  NodeType;
65 
67  virtual bool Add(ValueType element);
68 
70  virtual bool Add(int position, ValueType element);
71 
73  virtual bool Add(TTreeType & subTree);
74 
76  virtual const ValueType & Get() const;
77 
79  virtual TTreeType * GetSubTree() const;
80 
82  virtual bool IsLeaf() const;
83 
85  virtual bool IsRoot() const;
86 
88  virtual NodeType GetType() const = 0;
89 
91  virtual bool GoToChild(ChildIdentifier number = 0);
92 
94  virtual bool GoToParent();
95 
97  void Set(ValueType element);
98 
100  virtual bool HasChild(int number = 0) const;
101 
103  virtual int ChildPosition(ValueType element) const;
104 
106  virtual bool RemoveChild(int number);
107 
109  virtual int CountChildren() const;
110 
112  virtual bool HasParent() const;
113 
115  virtual bool Disconnect();
116 
118  virtual TreeIteratorBase< TTreeType > * Children();
119 
121  virtual TreeIteratorBase< TTreeType > * Parents();
122 
124  virtual TreeIteratorBase< TTreeType > * GetChild(int number) const;
125 
127  virtual int Count();
128 
130  bool Remove();
131 
133  virtual TreeNodeType * GetNode();
134 
135  virtual const TreeNodeType * GetNode() const;
136 
138  TreeNodeType * GetRoot();
139 
140  const TreeNodeType * GetRoot() const;
141 
143  TTreeType * GetTree() const;
144 
146  const TreeNodeType * GetParent() const;
147 
149  void GoToBegin() { m_Position = m_Begin; }
150 
152  void GoToEnd() { m_Position = m_End; }
153 
155  bool IsAtBegin() const { return ( m_Position == m_Begin ); }
156 
159  bool IsAtEnd() const { return ( m_Position == m_End ); }
160 
162  virtual TreeIteratorBase< TTreeType > * Clone() = 0;
163 
165  Self &
167  {
168  this->Next();
169  return *this;
170  }
172 
174  void
176  {
177  this->Next();
178  }
179 
181  Self & operator=(const Self & iterator)
182  {
183  if(this != &iterator)
184  {
185  m_Position = iterator.m_Position;
186  m_Begin = iterator.m_Begin;
187  m_End = iterator.m_End;
188  m_Root = iterator.m_Root;
189  m_Tree = iterator.m_Tree;
190  }
191  return *this;
192  }
194 
195  virtual ~TreeIteratorBase() = default;
196 
197 protected:
198 
200  TreeIteratorBase(TTreeType *tree, const TreeNodeType *start);
201  TreeIteratorBase(const TTreeType *tree, const TreeNodeType *start);
203 
204  mutable TreeNodeType *m_Position; // Current position of the iterator
206  mutable TreeNodeType *m_End;
208  TTreeType * m_Tree;
209 
210  virtual bool HasNext() const = 0;
211 
212  virtual const ValueType & Next() = 0;
213 };
214 } //end namespace itk
215 
216 #ifndef ITK_MANUAL_INSTANTIATION
217 #include "itkTreeIteratorBase.hxx"
218 #endif
219 
220 #endif
typename TTreeType::ValueType ValueType
typename TreeNodeType::ChildIdentifier ChildIdentifier
Self & operator=(const Self &iterator)
This class provides the base implementation for tree iterators.
typename TTreeType::TreeNodeType TreeNodeType
const TreeNodeType * m_Root