list.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. // list.cpp
  20. //
  21. #include "stdAfx.h"
  22. #include <assert.h>
  23. #include "list.h"
  24. ListNode::ListNode ( void )
  25. {
  26. prev = next = this;
  27. pri = NORMAL_PRIORITY;
  28. item = NULL;
  29. }
  30. void ListNode::Append ( ListNode *new_node )
  31. {
  32. assert ( ! new_node->InList()); /* node is already in a list or was not initialized*/
  33. new_node->prev = this;
  34. new_node->next = next;
  35. next = new_node;
  36. new_node->next->prev = new_node;
  37. }
  38. void ListNode::Prepend ( ListNode *new_node )
  39. {
  40. assert ( !new_node->InList() ); /* node is already in a list or was not initialized*/
  41. new_node->prev = prev;
  42. new_node->next = this;
  43. prev = new_node;
  44. new_node->prev->next = new_node;
  45. }
  46. void ListNode::Link ( ListNode *node)
  47. {
  48. next = node;
  49. node->prev = next;
  50. }
  51. void ListNode::Remove ( void )
  52. {
  53. prev->next = next;
  54. next->prev = prev;
  55. prev = next = this; /* so we know that the node is not in a list */
  56. }
  57. ListNode* ListNode::Next ( void )
  58. {
  59. if ( next->IsHead ( ) )
  60. {
  61. return NULL;
  62. }
  63. return next;
  64. }
  65. ListNode* ListNode::Prev ( void )
  66. {
  67. if ( prev->IsHead () )
  68. {
  69. return NULL;
  70. }
  71. return prev;
  72. }
  73. ListNode* ListNode::NextLoop ( void )
  74. {
  75. ListNode *next_node = next;
  76. if ( next_node->IsHead ( ))
  77. {
  78. /* skip head node */
  79. next_node = next_node->next;
  80. if ( next_node->IsHead ( ))
  81. {
  82. return NULL; /* it is an empty list */
  83. }
  84. }
  85. return next_node;
  86. }
  87. ListNode* ListNode::PrevLoop ( void )
  88. {
  89. ListNode *prev_node = prev;
  90. if ( prev_node->IsHead ( ))
  91. {
  92. /* skip head node */
  93. prev_node = prev_node->prev;
  94. if ( prev_node->IsHead ( ))
  95. {
  96. return NULL; /* it is an empty list */
  97. }
  98. }
  99. return prev_node;
  100. }
  101. void* ListNode::Item ( void )
  102. {
  103. assert ( !IsHead () );
  104. return item;
  105. }
  106. void ListNode::SetItem ( void *new_item )
  107. {
  108. assert ( !IsHead () );
  109. item = new_item ;
  110. }
  111. int ListNode::InList ( void )
  112. {
  113. return prev != this;
  114. }
  115. int ListNode::IsHead ( void )
  116. {
  117. return item == &this->item;
  118. }
  119. int ListNode::Priority ( void )
  120. {
  121. return pri;
  122. }
  123. void ListNode::SetPriority ( int new_pri )
  124. {
  125. assert ( new_pri <= HIGHEST_PRIORITY );
  126. assert ( new_pri >= LOWEST_PRIORITY );
  127. pri = new_pri;
  128. }
  129. List::List ( void )
  130. {
  131. SetItem ( &this->item );
  132. }
  133. void List::AddToTail ( ListNode *node )
  134. {
  135. assert ( IsHead ());
  136. Prepend ( node );
  137. }
  138. void List::AddToHead ( ListNode *node )
  139. {
  140. assert ( IsHead ());
  141. Append ( node );
  142. }
  143. void List::Add ( ListNode *new_node )
  144. {
  145. ListNode* node;
  146. int pri;
  147. assert ( IsHead ());
  148. assert ( !new_node->InList ());
  149. pri = new_node->Priority();
  150. node = FirstNode ( );
  151. while( node )
  152. {
  153. if (node->Priority() <= pri )
  154. {
  155. node->Prepend ( new_node );
  156. return;
  157. }
  158. node = node->Next ();
  159. }
  160. Prepend ( new_node );
  161. }
  162. void List::Merge ( List *list )
  163. {
  164. ListNode *first,
  165. *last,
  166. *node;
  167. assert ( IsHead ());
  168. first = list->Next();
  169. last = list->Prev();
  170. if ( !first || !last )
  171. {
  172. return;
  173. }
  174. node = Prev();
  175. node->Link ( first );
  176. last->Link ( this );
  177. list->Empty ();
  178. }
  179. int List::NumItems ( void )
  180. {
  181. int count = 0;
  182. ListNode *node;
  183. assert ( IsHead ());
  184. node = FirstNode();
  185. while ( node )
  186. {
  187. count++;
  188. node = node->Next ();
  189. }
  190. return count;
  191. }
  192. void* List::Item ( int list_index )
  193. {
  194. ListNode *node;
  195. assert ( IsHead ());
  196. node = FirstNode();
  197. while (list_index && node )
  198. {
  199. list_index--;
  200. node = node->Next ();
  201. }
  202. if ( node )
  203. {
  204. return node->Item();
  205. }
  206. return NULL;
  207. }
  208. ListNode* List::FirstNode ( void )
  209. {
  210. assert ( IsHead ());
  211. return Next ();
  212. }
  213. ListNode* List::LastNode ( void )
  214. {
  215. assert ( IsHead ());
  216. return Prev ();
  217. }
  218. int List::IsEmpty ( void )
  219. {
  220. assert ( IsHead ());
  221. return !InList();
  222. }
  223. void List::Empty ( void )
  224. {
  225. assert ( IsHead ());
  226. Remove ();
  227. }
  228. ListNode* List::Find ( void *item )
  229. {
  230. ListNode *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. }