QUEUE.H 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. ** Command & Conquer Red Alert(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. /* $Header: /CounterStrike/QUEUE.H 1 3/03/97 10:25a Joe_bostic $ */
  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 : Command & Conquer *
  24. * *
  25. * File Name : QUEUE.H *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 12/08/94 *
  30. * *
  31. * Last Update : December 9, 1994 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * QueueClass<T,size>::Add -- Add object to queue. *
  36. * QueueClass<T,size>::First -- Fetches reference to first object in list. *
  37. * QueueClass<T,size>::Init -- Initializes queue to empty state. *
  38. * QueueClass<T,size>::Next -- Throws out the head of the line. *
  39. * QueueClass<T,size>::operator[] -- Fetches reference to sub object in queue. *
  40. * QueueClass<T,size>::QueueClass -- Default constructor for QueueClass objects. *
  41. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. #ifndef QUEUE_H
  43. #define QUEUE_H
  44. #include "mission.h"
  45. #include "target.h"
  46. #include "defines.h"
  47. #pragma warn -inl
  48. /*
  49. ** This class implements a classic FIFO queue (also known as - standing in line). Objects
  50. ** are added to the end (tail) of the line. Objects are removed from the start (first) of
  51. ** the line. A keyboard buffer is a good example of a common computer use of a queue. There
  52. ** is no provision for "taking cuts" or leaving the line once an object has been added.
  53. **
  54. ** The implementation uses a circular list of objects. This allows adding and deleting of
  55. ** elements without any maintenance moves of remaining objects that would otherwise be
  56. ** necessary in a simple array storage method. A side effect of this means that accessing the
  57. ** internal array directly is not allowed. Supporting functions are provided for this purpose.
  58. **
  59. ** WARNING WARNING WARNING WARNING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  60. ** The size parameter MUST be an exact power of two (2, 4, 8, 16, etc.) otherwise the internal
  61. ** indexing algorithm will fail.
  62. */
  63. template<class T, int size>
  64. class QueueClass
  65. {
  66. public:
  67. /*
  68. ** This is the count of the number of objects in the queue. If this count is zero,
  69. ** then the operator[], First(), and Next() functions are undefined. Check this
  70. ** value BEFORE calling these functions.
  71. */
  72. const int Count;
  73. //-------------- Functions --------------------
  74. QueueClass(void); // Default constructor.
  75. /*
  76. ** The bracket subscript operator functions similarly to the way a normal subscript
  77. ** operator works except that entry [0] matches the first-in-line and entry
  78. ** [Count-1] matches the last-in-line. This is ensured regardless of the actual position
  79. ** of the object in the circular internal list.
  80. */
  81. T & operator[](int);
  82. /*
  83. ** This function will return a reference to the "head of the line" object.
  84. */
  85. T & First(void);
  86. /*
  87. ** This function clears the list of objects.
  88. */
  89. void Init(void);
  90. /*
  91. ** This function discards the head-of-the-line object and advances all the remaining
  92. ** objects up by one. Mnemonic: Imagine a broadway audition and the director yells
  93. ** "NEXT!"
  94. */
  95. int Next(void);
  96. /*
  97. ** This will add an object to the tail of the line. If there is no more room to add
  98. ** the object, then false will be returned.
  99. */
  100. int Add(T const &);
  101. int Get_Head(void);
  102. int Get_Tail(void);
  103. T * Get_Array(void);
  104. private:
  105. int Head; // Index of element in list the longest.
  106. int Tail; // Index where next new addition will go.
  107. T Array[size]; // Raw array of objects.
  108. };
  109. /***********************************************************************************************
  110. * QueueClass<T,size>::QueueClass -- Default constructor for QueueClass objects. *
  111. * *
  112. * This default constructor for QueueClass objects initializes the queue to an empty *
  113. * state. *
  114. * *
  115. * INPUT: none *
  116. * *
  117. * OUTPUT: none *
  118. * *
  119. * WARNINGS: none *
  120. * *
  121. * HISTORY: *
  122. * 12/09/1994 JLB : Created. *
  123. *=============================================================================================*/
  124. template<class T, int size>
  125. inline QueueClass<T,size>::QueueClass(void) : Count(0)
  126. {
  127. Init();
  128. }
  129. /***********************************************************************************************
  130. * QueueClass<T,size>::Init -- Initializes queue to empty state. *
  131. * *
  132. * This function resets the queue to an empty state. *
  133. * *
  134. * INPUT: none *
  135. * *
  136. * OUTPUT: none *
  137. * *
  138. * WARNINGS: none *
  139. * *
  140. * HISTORY: *
  141. * 12/09/1994 JLB : Created. *
  142. *=============================================================================================*/
  143. template<class T, int size>
  144. inline void QueueClass<T,size>::Init(void)
  145. {
  146. ((int &)Count) = 0;
  147. Head = 0;
  148. Tail = 0;
  149. }
  150. /***********************************************************************************************
  151. * QueueClass<T,size>::Add -- Add object to queue. *
  152. * *
  153. * This function is used to add an object to the tail of the line. If the queue cannot *
  154. * accept any more entries, then the object won't be added and false will be returned. *
  155. * *
  156. * INPUT: object -- The object that is to be added to the queue. *
  157. * *
  158. * OUTPUT: bool; Was the object added successfully? *
  159. * *
  160. * WARNINGS: If the queue is full, then the object won't be added. Be sure to check for this.*
  161. * *
  162. * HISTORY: *
  163. * 12/09/1994 JLB : Created. *
  164. *=============================================================================================*/
  165. template<class T, int size>
  166. inline int QueueClass<T,size>::Add(T const &q)
  167. {
  168. if (Count < size) {
  169. Array[Tail] = q;
  170. Tail = (Tail + 1) & (size-1);
  171. ((int &)Count) = Count + 1;
  172. return(true);
  173. }
  174. return (false);
  175. }
  176. /***********************************************************************************************
  177. * QueueClass<T,size>::Next -- Throws out the head of the line. *
  178. * *
  179. * This routine is used to discard the object at the head of the line. All remaining *
  180. * objects "move up" one. No actual movement occurs, merely the index is adjusted, but *
  181. * the affect is that the next oldest object in the queue will now be returned with the *
  182. * next call to the First() function. *
  183. * *
  184. * INPUT: none *
  185. * *
  186. * OUTPUT: Returns the number of object remaining in the queue. *
  187. * *
  188. * WARNINGS: none *
  189. * *
  190. * HISTORY: *
  191. * 12/09/1994 JLB : Created. *
  192. *=============================================================================================*/
  193. template<class T, int size>
  194. inline int QueueClass<T,size>::Next(void)
  195. {
  196. if (Count) {
  197. Head = (Head + 1) & (size-1);
  198. ((int &)Count) = Count - 1;
  199. }
  200. return (Count);
  201. }
  202. /***********************************************************************************************
  203. * QueueClass<T,size>::operator[] -- Fetches reference to sub object in queue. *
  204. * *
  205. * Use this routine to examine individual objects within the queue. The oldest object in *
  206. * the queue is referenced by an index value of zero. The newest object in the queue is *
  207. * referenced by a value of Count-1. If there are no objects in the queue, then this *
  208. * operator is undefined. Although this operator allows examination of the queue, there is *
  209. * no corresponding ability to insert or delete objects from the middle of the queue. *
  210. * *
  211. * INPUT: index -- The index into the queue of objects. Valid values range from zero to *
  212. * Count-1. All other values return an undefined reference! *
  213. * *
  214. * OUTPUT: Returns with a reference to the object indicated by the index. *
  215. * *
  216. * WARNINGS: Check to make sure that Count is not zero before using this operator. Failure *
  217. * to do so will return a reference to an undefined object. *
  218. * *
  219. * HISTORY: *
  220. * 12/09/1994 JLB : Created. *
  221. *=============================================================================================*/
  222. template<class T, int size>
  223. inline T & QueueClass<T,size>::operator[](int index)
  224. {
  225. return Array[(Head + index) & (size-1)];
  226. }
  227. /***********************************************************************************************
  228. * QueueClass<T,size>::First -- Fetches reference to first object in list. *
  229. * *
  230. * This routine is used to fetch the first object in the list (head of the line). This *
  231. * object is the oldest in the list. Typical use of this function is to get and process *
  232. * the first object so that it may be discarded with the Next() function in order to bring *
  233. * subsequent objects to the first position. *
  234. * *
  235. * INPUT: none *
  236. * *
  237. * OUTPUT: Returns with a reference to the oldest object in the queue. *
  238. * *
  239. * WARNINGS: If there are no objects in the queue, then this function returns an undefined *
  240. * reference. Be sure to check Count against zero before calling this function. *
  241. * *
  242. * HISTORY: *
  243. * 12/09/1994 JLB : Created. *
  244. *=============================================================================================*/
  245. template<class T, int size>
  246. inline T & QueueClass<T,size>::First(void)
  247. {
  248. return Array[Head];
  249. }
  250. template<class T, int size>
  251. inline int QueueClass<T,size>::Get_Head(void)
  252. {
  253. return Head;
  254. }
  255. template<class T, int size>
  256. inline int QueueClass<T,size>::Get_Tail(void)
  257. {
  258. return Tail;
  259. }
  260. template<class T, int size>
  261. inline T * QueueClass<T,size>::Get_Array(void)
  262. {
  263. return Array;
  264. }
  265. #endif