OpenBarnyard
 
Loading...
Searching...
No Matches
TNodeTree.h
Go to the documentation of this file.
1#pragma once
2#include "Toshi/Typedefs.h"
3
5
6template <class T>
8{
9public:
10 class TNode
11 {
12 public:
13 friend TNodeTree;
14
15 protected:
17 {
18 m_Tree = TNULL;
19 m_Next = (T*)this;
20 m_Prev = (T*)this;
22 m_Child = TNULL;
23 }
24
25 public:
27 {
28 TASSERT( IsLinked() == TTRUE );
29 return m_Parent == (T*)( &Tree()->m_Root );
30 }
31
32 TBOOL IsLinked() const { return m_Tree != TNULL; }
33 T* Parent() const { return m_Parent; }
34 T* Next() const { return m_Next; }
35 T* Prev() const { return m_Prev; }
36 TNodeTree<T>* Tree() const { return m_Tree; }
37 T* Child() const { return m_Child; }
38
39 protected:
45 };
46
47public:
49 {
50 m_Count = 0;
51 }
52
54 {
55 DeleteAll();
56 TASSERT( IsLinked() == TFALSE );
57 }
58
65 void Insert( T* parentNode, T* sourceNode )
66 {
67 // Toshi::TNodeTree<Toshi::TResource>::Insert - 00691aa0
68 TASSERT( sourceNode->IsLinked() == TFALSE, "The source node shouldn't be linked" );
69
70 // Remove the source node from the tree
71 Remove( *sourceNode, TFALSE );
72
73 // Get the first attached to parent node
74 T* firstAttached = parentNode->Child();
75
76 if ( firstAttached != TNULL )
77 {
78 // Attach node to other attached nodes
79 T* lastAttached = firstAttached->Prev();
80
81 lastAttached->m_Next = sourceNode;
82 firstAttached->m_Prev = sourceNode;
83
84 sourceNode->m_Next = firstAttached;
85 sourceNode->m_Prev = lastAttached;
86 }
87 else
88 {
89 // Attach node as the first one
90 parentNode->m_Child = sourceNode;
91 }
92
93 sourceNode->m_Tree = this;
94 sourceNode->m_Parent = parentNode;
95 m_Count += 1;
96 }
97
103 void InsertAtRoot( T* sourceNode )
104 {
105 Insert( GetRoot(), sourceNode );
106 }
107
111 void ReInsert( T* parentNode, T* sourceNode )
112 {
113 Remove( sourceNode, TFALSE );
114
115 if ( parentNode == TNULL )
116 {
117 if ( this != TNULL )
118 {
119 InsertAtRoot( sourceNode );
120 return;
121 }
122 }
123
124 Insert( parentNode, sourceNode );
125 }
126
127 T* Remove( T& node, TBOOL flag = TFALSE )
128 {
129 // Toshi::TNodeTree<Toshi::TResource>::Remove - 00691e70
130 TNodeTree<T>* nodeRoot = node.Tree();
131 T* nodeParent = node.Parent();
132
133 if ( nodeRoot != TNULL )
134 {
135 // Check if the node belongs to the current tree
136 if ( nodeRoot != this )
137 {
138 return &node;
139 }
140
141 m_Count -= 1;
142 }
143
144 if ( flag )
145 {
146 T* attachedNode = node.Child();
147
148 while ( attachedNode != TNULL )
149 {
150 TNodeTree<T>* nodeRoot = node.Tree();
151
152 Remove( *attachedNode, TFALSE );
153 Insert( node.Parent(), attachedNode );
154
155 attachedNode = node.Child();
156 TIMPLEMENT_D( "It seems to be unused and I hope it is. I don't know if it works and what it should do" );
157 }
158 }
159
160 if ( nodeParent != TNULL )
161 {
162 // If it's the first attached to the root node, set it to next or just remove
163 if ( nodeParent->Child() == &node )
164 {
165 nodeParent->m_Child = ( node.Next() != &node ) ? node.Next() : TNULL;
166 }
167
168 node.m_Parent = TNULL;
169 }
170
171 node.m_Prev->m_Next = node.m_Next;
172 node.m_Next->m_Prev = node.m_Prev;
173 node.m_Next = &node;
174 node.m_Prev = &node;
175 node.m_Tree = TNULL;
176 return &node;
177 }
178
179 T* Remove( T* node, TBOOL flag = TFALSE )
180 {
181 return Remove( *node, flag );
182 }
183
184 void DeleteRecurse( T* node )
185 {
186 while ( node != TNULL )
187 {
188 T* next = ( node->Next() != node ) ? node->Next() : TNULL;
189
190 if ( node->Child() != TNULL )
191 {
192 DeleteRecurse( node->Child() );
193 }
194
195 if ( node->Tree() == this )
196 {
197 m_Count -= 1;
198 }
199
200 if ( node->Tree() == TNULL || node->Tree() == this )
201 {
202 T* nodeParent = node->Parent();
203
204 if ( nodeParent != TNULL )
205 {
206 // If it's the first attached to the root node, set it to next or just remove
207 if ( nodeParent->Child() == node )
208 {
209 nodeParent->m_Child = ( node->Next() != node ) ? node->Next() : TNULL;
210 }
211
212 node->m_Parent = TNULL;
213 }
214
215 node->m_Prev->m_Parent = node->m_Next;
216 node->m_Next->m_Child = node->m_Prev;
217 node->m_Next = node;
218 node->m_Prev = node;
219 node->m_Tree = TNULL;
220 }
221
222 delete node;
223 node = next;
224 }
225 }
226
228 {
229 T* node = GetRoot()->Child();
230
231 while ( node != TNULL )
232 {
233 Remove( node, TFALSE );
234 DeleteRecurse( node );
235 node = GetRoot()->Child();
236 }
237
238 TASSERT( Count() == 0 );
239 }
240
242 {
243 return TSTATICCAST( T, &m_Root );
244 }
245
247 {
248 return m_Root.Child();
249 }
250
251 size_t Count() const
252 {
253 return m_Count;
254 }
255
257 {
258 return m_Root.IsLinked();
259 }
260
261protected:
262 TNode m_Root; // 0x0
263 size_t m_Count; // 0x14
264};
265
#define TASSERT(X,...)
Definition Defines.h:138
#define TSTATICCAST(POINTERTYPE, VALUE)
Definition Defines.h:69
#define TIMPLEMENT_D(DESC)
Definition Defines.h:137
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
#define TNULL
Definition Typedefs.h:23
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
void DeleteAll()
Definition TNodeTree.h:227
void DeleteRecurse(T *node)
Definition TNodeTree.h:184
void Insert(T *parentNode, T *sourceNode)
Inserts node as a child of another node.
Definition TNodeTree.h:65
void ReInsert(T *parentNode, T *sourceNode)
Tries to remove sourceNode from the tree and inserts it to the parentNode or to the root.
Definition TNodeTree.h:111
void InsertAtRoot(T *sourceNode)
Inserts node to the default tree.
Definition TNodeTree.h:103
TNode m_Root
Definition TNodeTree.h:262
T * ChildOfRoot()
Definition TNodeTree.h:246
TBOOL IsLinked() const
Definition TNodeTree.h:256
T * GetRoot()
Definition TNodeTree.h:241
T * Remove(T *node, TBOOL flag=false)
Definition TNodeTree.h:179
T * Remove(T &node, TBOOL flag=false)
Definition TNodeTree.h:127
size_t m_Count
Definition TNodeTree.h:263
size_t Count() const
Definition TNodeTree.h:251
TBOOL IsChildOfDefaultRoot() const
Definition TNodeTree.h:26
T * Parent() const
Definition TNodeTree.h:33
T * Prev() const
Definition TNodeTree.h:35
T * Child() const
Definition TNodeTree.h:37
T * Next() const
Definition TNodeTree.h:34
TBOOL IsLinked() const
Definition TNodeTree.h:32
TNodeTree< T > * Tree() const
Definition TNodeTree.h:36
TNodeTree< T > * m_Tree
Definition TNodeTree.h:40