QUEUE.H 16 KB

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