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 itkTreeNode_h
19 #define itkTreeNode_h
20 
21 #include <vector>
22 #include <algorithm>
23 #include <iostream>
24 #include "itkObject.h"
25 #include "itkObjectFactory.h"
26 #include "itkIntTypes.h"
27 
28 namespace itk
29 {
30 /** \class TreeNode
31  *  \brief Represents a node in a tree.
32  *
33  * This class derives from the Object class.
34  *
35  * The class is templated over the type of the elements.
36  *
37  * \tparam TValue = Element type stored in the node
38  *
39  * \ingroup DataRepresentation
40  * \ingroup ITKCommon
41  */
42 template< typename TValue >
43 class ITK_TEMPLATE_EXPORT TreeNode:public Object
44 {
45 public:
46   ITK_DISALLOW_COPY_AND_ASSIGN(TreeNode);
47 
48   /** Standard type alias */
49   using Superclass = Object;
50   using Self = TreeNode< TValue >;
51   using Pointer = SmartPointer< Self >;
52   using ConstPointer = SmartPointer< const Self >;
53   using ChildrenListType = std::vector< Pointer >;
54   using ChildIdentifier = ::itk::OffsetValueType;
55 
56   /** Method for creation through the object factory. */
57   itkNewMacro(Self);
58 
59   /** Run-time type information (and related methods). */
60   itkTypeMacro(TreeNode, Object);
61 
62   /** Get the value of the node */
63   const TValue & Get() const;
64 
65   /** Set the current value of the node */
66   TValue Set(const TValue data);
67 
68   /** Get the child node */
69   Self * GetChild(ChildIdentifier number) const;
70 
71   /** Get the parent node */
72   Self * GetParent() const;
73 
74   /** Return true if the node has children */
75   bool HasChildren() const;
76 
77   /** Return true if the node has a parent */
78   bool HasParent() const;
79 
80   /** Set the parent of the node */
81   void SetParent(Self *n);
82 
83   /** Return the number of children */
84   ChildIdentifier CountChildren() const;
85 
86   /** Remove a node from the node */
87   bool Remove(Self *n);
88 
89   /** Get the number of children given a name and depth */
90   ChildIdentifier GetNumberOfChildren(unsigned int depth = 0, char *name = nullptr) const;
91 
92   /** Replace a given child by a new one */
93   bool ReplaceChild(Self *oldChild, Self *newChild);
94 
95   /** Return the child position given a node */
96   ChildIdentifier ChildPosition(const Self *node) const;
97 
98   /** Return the child position given a value */
99   ChildIdentifier ChildPosition(TValue node) const;
100 
101   /** Add a child to the node */
102   void AddChild(Self *node);
103 
104   /** Add a child to the node and specify the number in the children list */
105   virtual void AddChild(ChildIdentifier number, Self *node);
106 
107   /** Get the children list */
108 #if !defined( ITK_WRAPPING_PARSER )
109   virtual ChildrenListType * GetChildren(unsigned int depth = 0, char *name = nullptr) const;
110 
111 #endif
112 
113   /** Get the internal list of children */
114 #if !defined( ITK_WRAPPING_PARSER )
GetChildrenList()115   virtual ChildrenListType & GetChildrenList() { return m_Children; }
116 #endif
117 
118   /** Set the data of the node */
119   //virtual void SetData(TValue data) {m_Data = data;}
120 
121 protected:
122 
123   TreeNode() = default;
124   ~TreeNode() override;
125 
126   TValue m_Data;
127   Self *m_Parent{nullptr};
128 
129   ChildrenListType m_Children;
130 };
131 } // end namespace itk
132 
133 #ifndef ITK_MANUAL_INSTANTIATION
134 #include "itkTreeNode.hxx"
135 #endif
136 
137 #endif
138