List.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. ** Command & Conquer Generals(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) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. //----------------------------------------------------------------------------
  24. //
  25. // Westwood Studios Pacific.
  26. //
  27. // Confidential Information
  28. // Copyright(C) 2001 - All Rights Reserved
  29. //
  30. //----------------------------------------------------------------------------
  31. //
  32. // Project: WSYS Library
  33. //
  34. // Module: List
  35. //
  36. // File name: WSYS_List.cpp
  37. //
  38. // Created: 10/31/01 TR
  39. //
  40. //----------------------------------------------------------------------------
  41. //----------------------------------------------------------------------------
  42. // Includes
  43. //----------------------------------------------------------------------------
  44. #include "PreRTS.h"
  45. #include "Common/List.h"
  46. // 'assignment within condition expression'.
  47. #pragma warning(disable : 4706)
  48. //----------------------------------------------------------------------------
  49. // Externals
  50. //----------------------------------------------------------------------------
  51. //----------------------------------------------------------------------------
  52. // Defines
  53. //----------------------------------------------------------------------------
  54. //----------------------------------------------------------------------------
  55. // Private Types
  56. //----------------------------------------------------------------------------
  57. //----------------------------------------------------------------------------
  58. // Private Data
  59. //----------------------------------------------------------------------------
  60. //----------------------------------------------------------------------------
  61. // Public Data
  62. //----------------------------------------------------------------------------
  63. //----------------------------------------------------------------------------
  64. // Private Prototypes
  65. //----------------------------------------------------------------------------
  66. //----------------------------------------------------------------------------
  67. // Private Functions
  68. //----------------------------------------------------------------------------
  69. //----------------------------------------------------------------------------
  70. // Public Functions
  71. //----------------------------------------------------------------------------
  72. //============================================================================
  73. // LList::LList
  74. //============================================================================
  75. LList::LList( )
  76. : m_sortMode(DESCENDING)
  77. {
  78. m_head.setItem( &m_head.m_item);
  79. };
  80. //=================================================================
  81. // LList::add
  82. //=================================================================
  83. void LList::add( LListNode* new_node )
  84. {
  85. LListNode* node;
  86. Int pri;
  87. if ( m_addToEndOfGroup )
  88. {
  89. pri = new_node->priority();
  90. node = &m_head;
  91. while( (node = node->prev() ))
  92. {
  93. if( (m_sortMode == ASCENDING && node->priority() >= pri)
  94. || (m_sortMode == DESCENDING && node->priority() <= pri) )
  95. {
  96. node->append( new_node );
  97. return;
  98. }
  99. }
  100. m_head.append( new_node );
  101. }
  102. else
  103. {
  104. pri = new_node->priority();
  105. node = &m_head;
  106. while( (node = node->next() ))
  107. {
  108. if( (m_sortMode == ASCENDING && node->priority() <= pri)
  109. || (m_sortMode == DESCENDING && node->priority() >= pri) )
  110. {
  111. node->insert( new_node );
  112. return;
  113. }
  114. }
  115. m_head.insert( new_node );
  116. }
  117. }
  118. //============================================================================
  119. // LList::addGDFNode
  120. //============================================================================
  121. void LList::addItem( Int pri, void* item )
  122. {
  123. LListNode *node = NEW LListNode(); // poolify
  124. if ( node )
  125. {
  126. node->setPriority( pri );
  127. node->setItem( item );
  128. node->autoDelete();
  129. add( node );
  130. }
  131. }
  132. //============================================================================
  133. // LList::addGDFNodeToHead
  134. //============================================================================
  135. void LList::addItemToHead( void *item )
  136. {
  137. LListNode *node = NEW LListNode();
  138. if ( node )
  139. {
  140. node->setItem( item );
  141. node->autoDelete();
  142. addToHead( node );
  143. }
  144. }
  145. //============================================================================
  146. // LList::addGDFNodeToTail
  147. //============================================================================
  148. void LList::addItemToTail( void *item )
  149. {
  150. LListNode *node = NEW LListNode();
  151. if ( node )
  152. {
  153. node->setItem( item );
  154. node->autoDelete();
  155. addToTail( node );
  156. }
  157. }
  158. //============================================================================
  159. // LList::Clear
  160. //============================================================================
  161. void LList::clear( void )
  162. {
  163. LListNode *node;
  164. while ( (node = firstNode()) != NULL )
  165. {
  166. node->remove();
  167. node->destroy();
  168. }
  169. }
  170. //=================================================================
  171. // LList::nodeCount
  172. //=================================================================
  173. Int LList::nodeCount( void )
  174. {
  175. LListNode* node;
  176. Int count = 0;
  177. node = firstNode();
  178. while(node)
  179. {
  180. count++;
  181. node = node->next();
  182. }
  183. return count;
  184. }
  185. //=================================================================
  186. // LList::getNode
  187. //=================================================================
  188. LListNode* LList::getNode( Int index )
  189. {
  190. LListNode* node;
  191. node = firstNode();
  192. while( node && index >= 0 )
  193. {
  194. if( index-- == 0 )
  195. {
  196. return node;
  197. }
  198. node = node->next();
  199. }
  200. return NULL;
  201. }
  202. //============================================================================
  203. // LList::merge
  204. //============================================================================
  205. void LList::merge( LList *list )
  206. {
  207. if ( list == NULL || list->isEmpty() )
  208. {
  209. return;
  210. }
  211. m_head.m_prev->m_next = list->m_head.m_next;
  212. list->m_head.m_next->m_prev = m_head.m_prev;
  213. list->m_head.m_prev->m_next = &m_head;
  214. m_head.m_prev = list->m_head.m_prev;
  215. list->m_head.m_next = &list->m_head;
  216. list->m_head.m_prev = &list->m_head;
  217. }
  218. //============================================================================
  219. // LList::hasReference
  220. //============================================================================
  221. Bool LList::hasItem( void *item )
  222. {
  223. return findItem( item ) != NULL;
  224. }
  225. //============================================================================
  226. // LList::findReference
  227. //============================================================================
  228. LListNode* LList::findItem( void *item )
  229. {
  230. LListNode* node;
  231. node = firstNode();
  232. while( node )
  233. {
  234. if( node->item() == item )
  235. {
  236. return node;
  237. }
  238. node = node->next();
  239. }
  240. return NULL;
  241. }
  242. //============================================================================
  243. // LListNode::LListNode
  244. //============================================================================
  245. LListNode::LListNode()
  246. : m_pri(0),
  247. m_item(NULL),
  248. m_autoDelete(FALSE)
  249. {
  250. m_next = m_prev = this;
  251. };
  252. //=================================================================
  253. // LListNode::insert
  254. //=================================================================
  255. void LListNode::insert( LListNode* new_node )
  256. {
  257. new_node->m_prev = m_prev;
  258. new_node->m_next = this;
  259. m_prev = new_node;
  260. new_node->m_prev->m_next = new_node;
  261. }
  262. //=================================================================
  263. // LListNode::append
  264. //=================================================================
  265. void LListNode::append( LListNode* new_node )
  266. {
  267. new_node->m_prev = this;
  268. new_node->m_next = m_next;
  269. this->m_next = new_node;
  270. new_node->m_next->m_prev = new_node;
  271. }
  272. //=================================================================
  273. // LListNode::remove
  274. //=================================================================
  275. void LListNode::remove( void )
  276. {
  277. m_prev->m_next = m_next;
  278. m_next->m_prev = m_prev;
  279. m_prev = m_next = this; // so we know that the node is not in a list
  280. }
  281. //=================================================================
  282. // LListNode::next
  283. //=================================================================
  284. LListNode* LListNode::next( void )
  285. {
  286. if( m_next->isHead( ))
  287. {
  288. return NULL;
  289. }
  290. return m_next;
  291. }
  292. //=================================================================
  293. // LListNode::prev
  294. //=================================================================
  295. LListNode* LListNode::prev( void )
  296. {
  297. if( m_prev->isHead())
  298. {
  299. return NULL;
  300. }
  301. return m_prev;
  302. }
  303. //=================================================================
  304. // LListNode::loopNext
  305. //=================================================================
  306. LListNode* LListNode::loopNext( void )
  307. {
  308. LListNode* next;
  309. next = m_next;
  310. if( next->isHead( ))
  311. {
  312. // skip head node
  313. next = next->m_next;
  314. if( next->isHead( ))
  315. {
  316. return NULL; // it is an empty list
  317. }
  318. }
  319. return next;
  320. }
  321. //=================================================================
  322. // LListNode::loopPrev
  323. //=================================================================
  324. LListNode* LListNode::loopPrev( void )
  325. {
  326. LListNode* prev;
  327. prev = m_prev;
  328. if( prev->isHead())
  329. {
  330. // skip head node
  331. prev = prev->m_prev;
  332. if( prev->isHead())
  333. {
  334. return NULL; // it is an empty list
  335. }
  336. }
  337. return prev;
  338. }
  339. //============================================================================
  340. // LListNode::destroy
  341. //============================================================================
  342. void LListNode::destroy( void )
  343. {
  344. if ( m_autoDelete )
  345. {
  346. delete this;
  347. }
  348. }
  349. //============================================================================
  350. // LList::addToEndOfGroup
  351. //============================================================================
  352. void LList::addToEndOfGroup( Bool yes )
  353. {
  354. m_addToEndOfGroup = yes;
  355. }