Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
ntree.h
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : LevelEdit *
24 * *
25 * $Archive:: /Commando/Code/wwlib/ntree.h $*
26 * *
27 * Author:: Patrick Smith *
28 * *
29 * $Modtime:: 4/18/00 7:02p $*
30 * *
31 * $Revision:: 3 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37
38#if defined(_MSC_VER)
39#pragma once
40#endif
41
42#ifndef __NTREE_H
43#define __NTREE_H
44
45
46#include "refcount.h"
47#include "wwstring.h"
48
49
51// Forward declarations
53template<class T> class NTreeLeafClass;
54template<class T> class SortedNTreeLeafClass;
55
56
58//
59// NTreeClass
60//
62template<class T>
64{
65public:
66
68 // Public constructors/destructors
71 : m_Root (NULL) { }
72 virtual ~NTreeClass (void) { Reset (); }
73
75 // Public methods
77 virtual NTreeLeafClass<T> * Add (const T &value);
78
79 NTreeLeafClass<T> * Peek_Root (void) { return m_Root; }
80 virtual void Reset (void);
81
82protected:
83
85 // Protected methods
88
90 // Protected member data
93};
94
95
97// Add
99template<class T>
101{
102 NTreeLeafClass<T> *retval = NULL;
103
104 if (m_Root == NULL) {
105
106 //
107 // Allocate a new root node
108 //
110 m_Root->Set_Value (value);
111 retval = m_Root;
112
113 } else {
114
115 //
116 // Simply add this value to list
117 //
118 retval = m_Root->Add (value);
119 }
120
121 return retval;
122}
123
124
126// Reset
128template<class T>
130{
131 if (m_Root != NULL) {
132
133 //
134 // Find the last leaf in the root
135 //
136 NTreeLeafClass<T> *end_leaf = m_Root;
137 while (end_leaf->Peek_Next () != NULL) {
138 end_leaf = end_leaf->Peek_Next ();
139 }
140
141 //
142 // Remove all the top-level leaves from the tree.
143 // Note: We work backwards from last root-leaf so each
144 // leaf along the way is guarenteed to have at least 1
145 // reference count on it.
146 //
147 for (NTreeLeafClass<T> *leaf = end_leaf; leaf != NULL; ) {
148 NTreeLeafClass<T> *curr_leaf = leaf;
149 leaf = leaf->Peek_Prev ();
150
151 //
152 // Remove this leaf
153 //
154 curr_leaf->Remove ();
155 }
156
158 }
159 return ;
160}
161
162
163
165//
166// SortedNTreeClass
167//
169template<class T>
171{
172public:
173
175 // Public constructors/destructors
178 virtual ~SortedNTreeClass (void) { }
179
181 // Public methods
183 SortedNTreeLeafClass<T> * Add_Sorted (const T &value, const char *name);
184
185protected:
186
188 // Protected methods
191};
192
194// Add_Sorted
196template<class T>
198{
200
201 if (m_Root == NULL) {
202
203 //
204 // Allocate a new root node
205 //
207
209 retval->Set_Value (value);
210 retval->Set_Name (name);
211
212 } else {
213
214 //
215 // Simply add this value to list
216 //
217 retval = ((SortedNTreeLeafClass<T> *)m_Root)->Add_Sorted (value, name);
218
219 //
220 // Make sure our 'root' pointer is the first one in the list
221 //
222 NTreeLeafClass<T> *prev = m_Root->Peek_Prev ();
223 if (prev != NULL) {
224 REF_PTR_SET (m_Root, prev);
225 }
226 }
227
228 return retval;
229}
230
231
233//
234// NTreeLeafClass
235//
237template<class T>
239{
240public:
241
243 // Public constructors/destructors
246 : m_Parent (NULL),
247 m_Child (NULL),
249 m_NextSibling (NULL) { }
250
251 virtual ~NTreeLeafClass (void);
252
254 // Public methods
256
257 //
258 // Tree management
259 //
260 virtual NTreeLeafClass<T> * Add_Child (const T &value);
261 virtual NTreeLeafClass<T> * Add (const T &value);
262 virtual void Remove (void);
263
264 //
265 // Value accessors
266 //
267 virtual const T & Get_Value (void) const { return m_Data; }
268 virtual void Set_Value (const T &data) { m_Data = data; }
269
270 //
271 // Tree traversal methods
272 //
277
278protected:
279
281 // Protected member data
283 void Set_Parent (NTreeLeafClass<T> *parent) { REF_PTR_SET (m_Parent, parent); }
284 void Set_Child (NTreeLeafClass<T> *child) { REF_PTR_SET (m_Child, child); }
287
289 // Protected member data
295
297};
298
299
301// ~NTreeLeafClass
303template<class T>
312
314// Add_Child
316template<class T>
318{
319 //
320 // Allocate a new leaf object
321 //
323 new_child->Set_Value (value);
324 new_child->Set_Parent (this);
325
326 //
327 // Link this new leaf into the hierarchy
328 //
329 if (m_Child != NULL) {
330 m_Child->Set_Prev (new_child);
331 new_child->Set_Next (m_Child);
332 }
333 m_Child = new_child;
334 return m_Child;
335}
336
337
339// Add
341template<class T>
343{
344 //
345 // Allocate a new leaf object
346 //
348 new_leaf->Set_Value (value);
349
350 //
351 // Link this new leaf into the list
352 //
353 new_leaf->Set_Prev (this);
354 new_leaf->Set_Next (m_NextSibling);
355 Set_Next (new_leaf);
356
357 //
358 // Release our hold on the new leaf
359 //
360 new_leaf->Release_Ref ();
361 return new_leaf;
362}
363
364
366// Remove
368template<class T>
370{
371 Add_Ref ();
372
373 //
374 // Fixup the parent's child leaf object
375 //
376 if (m_Parent != NULL && m_Parent->Peek_Child () == this) {
377 m_Parent->Set_Child (m_NextSibling);
378 }
379
380 //
381 // Remove all our children
382 //
383 while (m_Child != NULL) {
384 m_Child->Remove ();
385 }
386
387 //
388 // Unlink ourselves from our siblings
389 //
390 if (m_NextSibling != NULL) {
391 m_NextSibling->Set_Prev (NULL);
392 }
393
394 if (m_PrevSibling != NULL) {
395 m_PrevSibling->Set_Next (NULL);
396 }
397
402
403 Release_Ref ();
404 return ;
405}
406
407
409//
410// SortedNTreeLeafClass
411//
413template<class T>
415{
416public:
417
419 // Public constructors/destructors
423
425 // Public methods
427 SortedNTreeLeafClass<T> * Add_Sorted (const T &value, const char *name);
428 SortedNTreeLeafClass<T> * Add_Child_Sorted (const T &value, const char *name);
429
430 const StringClass & Get_Name (void) const { return m_Name; }
431 void Set_Name (const char *name) { m_Name = name; }
432
433protected:
434
436 // Protected methods
439
441 // Protected member data
444};
445
446
448// Add_Sorted
450template<class T>
452{
453 //
454 // Allocate a new leaf object
455 //
457 new_sibling->Set_Value (value);
458 new_sibling->Set_Name (name);
459
460 //
461 // Find the first-most sibling
462 //
463 SortedNTreeLeafClass<T> *start = this;
464 while (start->Peek_Prev () != NULL) {
465 start = (SortedNTreeLeafClass<T> *)start->Peek_Prev ();
466 }
467
468 //
469 // Insert the new leaf in its sorted position
470 //
471 Insertion_Sort (start, new_sibling);
472
473 //
474 // Release our hold on the object pointer
475 //
476 new_sibling->Release_Ref ();
477 return new_sibling;
478}
479
481// Add_Child_Sorted
483template<class T>
485{
486 //
487 // Allocate a new leaf object
488 //
490 new_child->Set_Value (value);
491 new_child->Set_Name (name);
492 new_child->Set_Parent (this);
493
494 if (m_Child == NULL) {
495 m_Child = new_child;
496 } else {
497
498 //
499 // Insert the new leaf in its sorted position
500 //
502
503 //
504 // Make sure our 'child' pointer is the first one in the list
505 //
506 NTreeLeafClass<T> *prev = m_Child->Peek_Prev ();
507 if (prev != NULL) {
508 REF_PTR_SET (m_Child, prev);
509 }
510
511 //
512 // Release our hold on the object pointer
513 //
514 new_child->Release_Ref ();
515 }
516
517 return new_child;
518}
519
521// Insertion_Sort
523template<class T>
525{
526 const char *name = new_sibling->Get_Name ();
527
528 //
529 // Determine where to insert the new sibling
530 //
531 bool inserted = false;
532 for ( SortedNTreeLeafClass<T> *leaf = start;
533 leaf != NULL && !inserted;
534 leaf = (SortedNTreeLeafClass<T> *)leaf->Peek_Next ())
535 {
536 //
537 // Does the new sibling come before the current leaf?
538 //
539 if (::stricmp (name, leaf->Get_Name ()) < 0) {
540
541 //
542 // Insert this sibling before the leaf
543 //
544 SortedNTreeLeafClass<T> *prev = (SortedNTreeLeafClass<T> *)leaf->Peek_Prev ();
545 new_sibling->Set_Prev (prev);
546 new_sibling->Set_Next (leaf);
547 leaf->Set_Prev (new_sibling);
548 if (prev != NULL) {
549 prev->Set_Next (new_sibling);
550 }
551
552 inserted = true;
553
554 } else if (leaf->Peek_Next () == NULL) {
555
556 //
557 // Put the new sibling on the end of the list
558 //
559 new_sibling->Set_Prev (leaf);
560 leaf->Set_Next (new_sibling);
561 inserted = true;
562 }
563 }
564
565 return ;
566}
567
568#endif //__NTREE_H
#define NULL
Definition BaseType.h:92
void const char * value
#define W3DNEW
Definition always.h:109
virtual void Reset(void)
Definition ntree.h:129
virtual NTreeLeafClass< T > * Add(const T &value)
Definition ntree.h:100
NTreeLeafClass< T > * m_Root
Definition ntree.h:92
virtual NTreeLeafClass< T > * Allocate_Leaf(void)
Definition ntree.h:87
NTreeLeafClass< T > * Peek_Root(void)
Definition ntree.h:79
virtual ~NTreeClass(void)
Definition ntree.h:72
NTreeClass(void)
Definition ntree.h:70
void Set_Prev(NTreeLeafClass< T > *prev)
Definition ntree.h:285
NTreeLeafClass< T > * m_Parent
Definition ntree.h:291
virtual NTreeLeafClass< T > * Add(const T &value)
Definition ntree.h:342
virtual void Set_Value(const T &data)
Definition ntree.h:268
virtual ~NTreeLeafClass(void)
Definition ntree.h:304
NTreeLeafClass(void)
Definition ntree.h:245
NTreeLeafClass< T > * m_PrevSibling
Definition ntree.h:294
void Set_Next(NTreeLeafClass< T > *next)
Definition ntree.h:286
void Set_Parent(NTreeLeafClass< T > *parent)
Definition ntree.h:283
NTreeLeafClass< T > * m_Child
Definition ntree.h:292
void Set_Child(NTreeLeafClass< T > *child)
Definition ntree.h:284
NTreeLeafClass< T > * Peek_Prev(void)
Definition ntree.h:276
NTreeLeafClass< T > * Peek_Next(void)
Definition ntree.h:275
NTreeLeafClass< T > * Peek_Child(void)
Definition ntree.h:274
NTreeLeafClass< T > * Peek_Parent(void)
Definition ntree.h:273
virtual const T & Get_Value(void) const
Definition ntree.h:267
virtual void Remove(void)
Definition ntree.h:369
virtual NTreeLeafClass< T > * Add_Child(const T &value)
Definition ntree.h:317
NTreeLeafClass< T > * m_NextSibling
Definition ntree.h:293
WWINLINE void Release_Ref(void) const
Definition refcount.h:146
void Add_Ref(void) const
Definition refcount.cpp:171
RefCountClass(void)
Definition refcount.h:108
SortedNTreeClass(void)
Definition ntree.h:177
virtual ~SortedNTreeClass(void)
Definition ntree.h:178
NTreeLeafClass< T > * Allocate_Leaf(void)
Definition ntree.h:190
SortedNTreeLeafClass< T > * Add_Sorted(const T &value, const char *name)
Definition ntree.h:197
StringClass m_Name
Definition ntree.h:443
SortedNTreeLeafClass(void)
Definition ntree.h:421
~SortedNTreeLeafClass(void)
Definition ntree.h:422
SortedNTreeLeafClass< T > * Add_Sorted(const T &value, const char *name)
Definition ntree.h:451
SortedNTreeLeafClass< T > * Add_Child_Sorted(const T &value, const char *name)
Definition ntree.h:484
void Set_Name(const char *name)
Definition ntree.h:431
const StringClass & Get_Name(void) const
Definition ntree.h:430
void Insertion_Sort(SortedNTreeLeafClass< T > *start, SortedNTreeLeafClass< T > *new_sibling)
Definition ntree.h:524
else return(RetVal)
#define REF_PTR_RELEASE(x)
Definition refcount.h:80
#define REF_PTR_SET(dst, src)
Definition refcount.h:79
void Insertion_Sort(T *a, int N)