ntree.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. ** Command & Conquer Renegade(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. *** 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 ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : LevelEdit *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/ntree.h $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 4/18/00 7:02p $*
  29. * *
  30. * $Revision:: 3 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef __NTREE_H
  39. #define __NTREE_H
  40. #include "refcount.h"
  41. #include "wwstring.h"
  42. //////////////////////////////////////////////////////////////////////////
  43. // Forward declarations
  44. //////////////////////////////////////////////////////////////////////////
  45. template<class T> class NTreeLeafClass;
  46. template<class T> class SortedNTreeLeafClass;
  47. //////////////////////////////////////////////////////////////////////////
  48. //
  49. // NTreeClass
  50. //
  51. //////////////////////////////////////////////////////////////////////////
  52. template<class T>
  53. class NTreeClass
  54. {
  55. public:
  56. //////////////////////////////////////////////////////////////
  57. // Public constructors/destructors
  58. //////////////////////////////////////////////////////////////
  59. NTreeClass (void)
  60. : m_Root (NULL) { }
  61. virtual ~NTreeClass (void) { Reset (); }
  62. //////////////////////////////////////////////////////////////
  63. // Public methods
  64. //////////////////////////////////////////////////////////////
  65. virtual NTreeLeafClass<T> * Add (const T &value);
  66. NTreeLeafClass<T> * Peek_Root (void) { return m_Root; }
  67. virtual void Reset (void);
  68. protected:
  69. //////////////////////////////////////////////////////////////
  70. // Protected methods
  71. //////////////////////////////////////////////////////////////
  72. virtual NTreeLeafClass<T> * Allocate_Leaf (void) { return new NTreeLeafClass<T>; }
  73. //////////////////////////////////////////////////////////////
  74. // Protected member data
  75. //////////////////////////////////////////////////////////////
  76. NTreeLeafClass<T> * m_Root;
  77. };
  78. /////////////////////////////////////////////////////////
  79. // Add
  80. /////////////////////////////////////////////////////////
  81. template<class T>
  82. NTreeLeafClass<T> *NTreeClass<T>::Add (const T &value)
  83. {
  84. NTreeLeafClass<T> *retval = NULL;
  85. if (m_Root == NULL) {
  86. //
  87. // Allocate a new root node
  88. //
  89. m_Root = Allocate_Leaf ();
  90. m_Root->Set_Value (value);
  91. retval = m_Root;
  92. } else {
  93. //
  94. // Simply add this value to list
  95. //
  96. retval = m_Root->Add (value);
  97. }
  98. return retval;
  99. }
  100. /////////////////////////////////////////////////////////
  101. // Reset
  102. /////////////////////////////////////////////////////////
  103. template<class T>
  104. void NTreeClass<T>::Reset (void)
  105. {
  106. if (m_Root != NULL) {
  107. //
  108. // Find the last leaf in the root
  109. //
  110. NTreeLeafClass<T> *end_leaf = m_Root;
  111. while (end_leaf->Peek_Next () != NULL) {
  112. end_leaf = end_leaf->Peek_Next ();
  113. }
  114. //
  115. // Remove all the top-level leaves from the tree.
  116. // Note: We work backwards from last root-leaf so each
  117. // leaf along the way is guarenteed to have at least 1
  118. // reference count on it.
  119. //
  120. for (NTreeLeafClass<T> *leaf = end_leaf; leaf != NULL; ) {
  121. NTreeLeafClass<T> *curr_leaf = leaf;
  122. leaf = leaf->Peek_Prev ();
  123. //
  124. // Remove this leaf
  125. //
  126. curr_leaf->Remove ();
  127. }
  128. REF_PTR_RELEASE (m_Root);
  129. }
  130. return ;
  131. }
  132. //////////////////////////////////////////////////////////////////////////
  133. //
  134. // SortedNTreeClass
  135. //
  136. //////////////////////////////////////////////////////////////////////////
  137. template<class T>
  138. class SortedNTreeClass : public NTreeClass<T>
  139. {
  140. public:
  141. //////////////////////////////////////////////////////////////
  142. // Public constructors/destructors
  143. //////////////////////////////////////////////////////////////
  144. SortedNTreeClass (void) { }
  145. virtual ~SortedNTreeClass (void) { }
  146. //////////////////////////////////////////////////////////////
  147. // Public methods
  148. //////////////////////////////////////////////////////////////
  149. SortedNTreeLeafClass<T> * Add_Sorted (const T &value, const char *name);
  150. protected:
  151. //////////////////////////////////////////////////////////////
  152. // Protected methods
  153. //////////////////////////////////////////////////////////////
  154. NTreeLeafClass<T> * Allocate_Leaf (void) { return new SortedNTreeLeafClass<T>; }
  155. };
  156. /////////////////////////////////////////////////////////
  157. // Add_Sorted
  158. /////////////////////////////////////////////////////////
  159. template<class T>
  160. SortedNTreeLeafClass<T> *SortedNTreeClass<T>::Add_Sorted (const T &value, const char *name)
  161. {
  162. SortedNTreeLeafClass<T> *retval = NULL;
  163. if (m_Root == NULL) {
  164. //
  165. // Allocate a new root node
  166. //
  167. m_Root = Allocate_Leaf ();
  168. retval = (SortedNTreeLeafClass<T> *)m_Root;
  169. retval->Set_Value (value);
  170. retval->Set_Name (name);
  171. } else {
  172. //
  173. // Simply add this value to list
  174. //
  175. retval = ((SortedNTreeLeafClass<T> *)m_Root)->Add_Sorted (value, name);
  176. //
  177. // Make sure our 'root' pointer is the first one in the list
  178. //
  179. NTreeLeafClass<T> *prev = m_Root->Peek_Prev ();
  180. if (prev != NULL) {
  181. REF_PTR_SET (m_Root, prev);
  182. }
  183. }
  184. return retval;
  185. }
  186. //////////////////////////////////////////////////////////////////////////
  187. //
  188. // NTreeLeafClass
  189. //
  190. //////////////////////////////////////////////////////////////////////////
  191. template<class T>
  192. class NTreeLeafClass : public RefCountClass
  193. {
  194. public:
  195. //////////////////////////////////////////////////////////////
  196. // Public constructors/destructors
  197. //////////////////////////////////////////////////////////////
  198. NTreeLeafClass (void)
  199. : m_Parent (NULL),
  200. m_Child (NULL),
  201. m_PrevSibling (NULL),
  202. m_NextSibling (NULL) { }
  203. virtual ~NTreeLeafClass (void);
  204. //////////////////////////////////////////////////////////////
  205. // Public methods
  206. //////////////////////////////////////////////////////////////
  207. //
  208. // Tree management
  209. //
  210. virtual NTreeLeafClass<T> * Add_Child (const T &value);
  211. virtual NTreeLeafClass<T> * Add (const T &value);
  212. virtual void Remove (void);
  213. //
  214. // Value accessors
  215. //
  216. virtual const T & Get_Value (void) const { return m_Data; }
  217. virtual void Set_Value (const T &data) { m_Data = data; }
  218. //
  219. // Tree traversal methods
  220. //
  221. NTreeLeafClass<T> * Peek_Parent (void) { return m_Parent; }
  222. NTreeLeafClass<T> * Peek_Child (void) { return m_Child; }
  223. NTreeLeafClass<T> * Peek_Next (void) { return m_NextSibling; }
  224. NTreeLeafClass<T> * Peek_Prev (void) { return m_PrevSibling; }
  225. protected:
  226. //////////////////////////////////////////////////////////////
  227. // Protected member data
  228. //////////////////////////////////////////////////////////////
  229. void Set_Parent (NTreeLeafClass<T> *parent) { REF_PTR_SET (m_Parent, parent); }
  230. void Set_Child (NTreeLeafClass<T> *child) { REF_PTR_SET (m_Child, child); }
  231. void Set_Prev (NTreeLeafClass<T> *prev) { REF_PTR_SET (m_PrevSibling, prev); }
  232. void Set_Next (NTreeLeafClass<T> *next) { REF_PTR_SET (m_NextSibling, next); }
  233. //////////////////////////////////////////////////////////////
  234. // Protected member data
  235. //////////////////////////////////////////////////////////////
  236. NTreeLeafClass<T> * m_Parent;
  237. NTreeLeafClass<T> * m_Child;
  238. NTreeLeafClass<T> * m_NextSibling;
  239. NTreeLeafClass<T> * m_PrevSibling;
  240. T m_Data;
  241. };
  242. /////////////////////////////////////////////////////////
  243. // ~NTreeLeafClass
  244. /////////////////////////////////////////////////////////
  245. template<class T>
  246. NTreeLeafClass<T>::~NTreeLeafClass (void)
  247. {
  248. REF_PTR_RELEASE (m_Parent);
  249. REF_PTR_RELEASE (m_Child);
  250. REF_PTR_RELEASE (m_NextSibling);
  251. REF_PTR_RELEASE (m_PrevSibling);
  252. return;
  253. }
  254. /////////////////////////////////////////////////////////
  255. // Add_Child
  256. /////////////////////////////////////////////////////////
  257. template<class T>
  258. NTreeLeafClass<T> *NTreeLeafClass<T>::Add_Child (const T &value)
  259. {
  260. //
  261. // Allocate a new leaf object
  262. //
  263. NTreeLeafClass<T> *new_child = new NTreeLeafClass<T>;
  264. new_child->Set_Value (value);
  265. new_child->Set_Parent (this);
  266. //
  267. // Link this new leaf into the hierarchy
  268. //
  269. if (m_Child != NULL) {
  270. m_Child->Set_Prev (new_child);
  271. new_child->Set_Next (m_Child);
  272. }
  273. m_Child = new_child;
  274. return m_Child;
  275. }
  276. /////////////////////////////////////////////////////////
  277. // Add
  278. /////////////////////////////////////////////////////////
  279. template<class T>
  280. NTreeLeafClass<T> *NTreeLeafClass<T>::Add (const T &value)
  281. {
  282. //
  283. // Allocate a new leaf object
  284. //
  285. NTreeLeafClass<T> *new_leaf = new NTreeLeafClass<T>;
  286. new_leaf->Set_Value (value);
  287. //
  288. // Link this new leaf into the list
  289. //
  290. new_leaf->Set_Prev (this);
  291. new_leaf->Set_Next (m_NextSibling);
  292. Set_Next (new_leaf);
  293. //
  294. // Release our hold on the new leaf
  295. //
  296. new_leaf->Release_Ref ();
  297. return new_leaf;
  298. }
  299. /////////////////////////////////////////////////////////
  300. // Remove
  301. /////////////////////////////////////////////////////////
  302. template<class T>
  303. void NTreeLeafClass<T>::Remove (void)
  304. {
  305. Add_Ref ();
  306. //
  307. // Fixup the parent's child leaf object
  308. //
  309. if (m_Parent != NULL && m_Parent->Peek_Child () == this) {
  310. m_Parent->Set_Child (m_NextSibling);
  311. }
  312. //
  313. // Remove all our children
  314. //
  315. while (m_Child != NULL) {
  316. m_Child->Remove ();
  317. }
  318. //
  319. // Unlink ourselves from our siblings
  320. //
  321. if (m_NextSibling != NULL) {
  322. m_NextSibling->Set_Prev (NULL);
  323. }
  324. if (m_PrevSibling != NULL) {
  325. m_PrevSibling->Set_Next (NULL);
  326. }
  327. REF_PTR_RELEASE (m_Parent);
  328. REF_PTR_RELEASE (m_Child);
  329. REF_PTR_RELEASE (m_NextSibling);
  330. REF_PTR_RELEASE (m_PrevSibling);
  331. Release_Ref ();
  332. return ;
  333. }
  334. //////////////////////////////////////////////////////////////////////////
  335. //
  336. // SortedNTreeLeafClass
  337. //
  338. //////////////////////////////////////////////////////////////////////////
  339. template<class T>
  340. class SortedNTreeLeafClass : public NTreeLeafClass<T>
  341. {
  342. public:
  343. //////////////////////////////////////////////////////////////
  344. // Public constructors/destructors
  345. //////////////////////////////////////////////////////////////
  346. SortedNTreeLeafClass (void) { }
  347. ~SortedNTreeLeafClass (void) { }
  348. //////////////////////////////////////////////////////////////
  349. // Public methods
  350. //////////////////////////////////////////////////////////////
  351. SortedNTreeLeafClass<T> * Add_Sorted (const T &value, const char *name);
  352. SortedNTreeLeafClass<T> * Add_Child_Sorted (const T &value, const char *name);
  353. const StringClass & Get_Name (void) const { return m_Name; }
  354. void Set_Name (const char *name) { m_Name = name; }
  355. protected:
  356. //////////////////////////////////////////////////////////////
  357. // Protected methods
  358. //////////////////////////////////////////////////////////////
  359. void Insertion_Sort (SortedNTreeLeafClass<T> *start, SortedNTreeLeafClass<T> *new_sibling);
  360. //////////////////////////////////////////////////////////////
  361. // Protected member data
  362. //////////////////////////////////////////////////////////////
  363. StringClass m_Name;
  364. };
  365. /////////////////////////////////////////////////////////
  366. // Add_Sorted
  367. /////////////////////////////////////////////////////////
  368. template<class T>
  369. SortedNTreeLeafClass<T> *SortedNTreeLeafClass<T>::Add_Sorted (const T &value, const char *name)
  370. {
  371. //
  372. // Allocate a new leaf object
  373. //
  374. SortedNTreeLeafClass<T> *new_sibling = new SortedNTreeLeafClass<T>;
  375. new_sibling->Set_Value (value);
  376. new_sibling->Set_Name (name);
  377. //
  378. // Find the first-most sibling
  379. //
  380. SortedNTreeLeafClass<T> *start = this;
  381. while (start->Peek_Prev () != NULL) {
  382. start = (SortedNTreeLeafClass<T> *)start->Peek_Prev ();
  383. }
  384. //
  385. // Insert the new leaf in its sorted position
  386. //
  387. Insertion_Sort (start, new_sibling);
  388. //
  389. // Release our hold on the object pointer
  390. //
  391. new_sibling->Release_Ref ();
  392. return new_sibling;
  393. }
  394. /////////////////////////////////////////////////////////
  395. // Add_Child_Sorted
  396. /////////////////////////////////////////////////////////
  397. template<class T>
  398. SortedNTreeLeafClass<T> *SortedNTreeLeafClass<T>::Add_Child_Sorted (const T &value, const char *name)
  399. {
  400. //
  401. // Allocate a new leaf object
  402. //
  403. SortedNTreeLeafClass<T> *new_child = new SortedNTreeLeafClass<T>;
  404. new_child->Set_Value (value);
  405. new_child->Set_Name (name);
  406. new_child->Set_Parent (this);
  407. if (m_Child == NULL) {
  408. m_Child = new_child;
  409. } else {
  410. //
  411. // Insert the new leaf in its sorted position
  412. //
  413. Insertion_Sort ((SortedNTreeLeafClass<T> *)m_Child, new_child);
  414. //
  415. // Make sure our 'child' pointer is the first one in the list
  416. //
  417. NTreeLeafClass<T> *prev = m_Child->Peek_Prev ();
  418. if (prev != NULL) {
  419. REF_PTR_SET (m_Child, prev);
  420. }
  421. //
  422. // Release our hold on the object pointer
  423. //
  424. new_child->Release_Ref ();
  425. }
  426. return new_child;
  427. }
  428. /////////////////////////////////////////////////////////
  429. // Insertion_Sort
  430. /////////////////////////////////////////////////////////
  431. template<class T>
  432. void SortedNTreeLeafClass<T>::Insertion_Sort (SortedNTreeLeafClass<T> *start, SortedNTreeLeafClass<T> *new_sibling)
  433. {
  434. const char *name = new_sibling->Get_Name ();
  435. //
  436. // Determine where to insert the new sibling
  437. //
  438. bool inserted = false;
  439. for ( SortedNTreeLeafClass<T> *leaf = start;
  440. leaf != NULL && !inserted;
  441. leaf = (SortedNTreeLeafClass<T> *)leaf->Peek_Next ())
  442. {
  443. //
  444. // Does the new sibling come before the current leaf?
  445. //
  446. if (::stricmp (name, leaf->Get_Name ()) < 0) {
  447. //
  448. // Insert this sibling before the leaf
  449. //
  450. SortedNTreeLeafClass<T> *prev = (SortedNTreeLeafClass<T> *)leaf->Peek_Prev ();
  451. new_sibling->Set_Prev (prev);
  452. new_sibling->Set_Next (leaf);
  453. leaf->Set_Prev (new_sibling);
  454. if (prev != NULL) {
  455. prev->Set_Next (new_sibling);
  456. }
  457. inserted = true;
  458. } else if (leaf->Peek_Next () == NULL) {
  459. //
  460. // Put the new sibling on the end of the list
  461. //
  462. new_sibling->Set_Prev (leaf);
  463. leaf->Set_Next (new_sibling);
  464. inserted = true;
  465. }
  466. }
  467. return ;
  468. }
  469. #endif //__NTREE_H