DYNAVEC.CPP 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/DYNAVEC.CPP 1 3/03/97 10:24a Joe_bostic $ */
  19. /***************************************************************************
  20. * *
  21. * Project Name : Red Alert *
  22. * *
  23. * File Name : DYNAVEC.CPP *
  24. * *
  25. * Programmer : Bill R Randolph *
  26. * *
  27. * Start Date : 09/18/96 *
  28. * *
  29. * Last Update : September 18, 1996 [BRR] *
  30. * *
  31. *-------------------------------------------------------------------------*
  32. * Functions: *
  33. * DynamicVectorClass<T>::Add -- Add an element to the vector. *
  34. * DynamicVectorClass<T>::Add_Head -- Adds element to head of the list. *
  35. * DynamicVectorClass<T>::Add_Head -- Adds element to head of the list. *
  36. * DynamicVectorClass<T>::Delete -- Deletes specified index from vector. *
  37. * DynamicVectorClass<T>::Delete -- Remove specified object from vector. *
  38. * DynamicVectorClass<T>::DynamicVectorClass -- Constructor *
  39. * DynamicVectorClass<T>::ID -- Find matching value in dynamic vector. *
  40. * DynamicVectorClass<T>::Resize -- Changes size of a dynamic vector. *
  41. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. #include "function.h"
  43. #include "vector.h"
  44. #ifdef WINSOCK_IPX
  45. #include "WSProto.h"
  46. #include "WSPUDP.h"
  47. #endif //WINSOCK_IPX
  48. #include <mem.h>
  49. #include <stdio.h>
  50. /***********************************************************************************************
  51. * DynamicVectorClass<T>::DynamicVectorClass -- Constructor for dynamic vector. *
  52. * *
  53. * This is the normal constructor for the dynamic vector class. It is similar to the normal *
  54. * vector class constructor. The vector is initialized to contain the number of elements *
  55. * specified in the "size" parameter. The memory is allocated from free store unless the *
  56. * optional array parameter is provided. In this case it will place the vector at the *
  57. * memory location specified. *
  58. * *
  59. * INPUT: size -- The maximum number of objects allowed in this vector. *
  60. * *
  61. * array -- Optional pointer to the memory area to place the vector at. *
  62. * *
  63. * OUTPUT: none *
  64. * *
  65. * WARNINGS: none *
  66. * *
  67. * HISTORY: *
  68. * 03/10/1995 JLB : Created. *
  69. *=============================================================================================*/
  70. template<class T>
  71. DynamicVectorClass<T>::DynamicVectorClass(unsigned size, T const * array)
  72. : VectorClass<T>(size, array)
  73. {
  74. GrowthStep = 10;
  75. ActiveCount = 0;
  76. }
  77. /***********************************************************************************************
  78. * DynamicVectorClass<T>::Resize -- Changes the size of a dynamic vector. *
  79. * *
  80. * Use this routine to change the size of the vector. The size changed is the maximum *
  81. * number of allocated objects within this vector. If a memory buffer is provided, then *
  82. * the vector will be located there. Otherwise, the memory will be allocated out of free *
  83. * store. *
  84. * *
  85. * INPUT: newsize -- The desired maximum size of this vector. *
  86. * *
  87. * array -- Optional pointer to a previously allocated memory array. *
  88. * *
  89. * OUTPUT: bool; Was vector successfully resized according to specifications? *
  90. * *
  91. * WARNINGS: Failure to resize the vector could be the result of lack of free store. *
  92. * *
  93. * HISTORY: *
  94. * 03/10/1995 JLB : Created. *
  95. *=============================================================================================*/
  96. template<class T>
  97. int DynamicVectorClass<T>::Resize(unsigned newsize, T const * array)
  98. {
  99. if (VectorClass<T>::Resize(newsize, array)) {
  100. if (Length() < ActiveCount) ActiveCount = Length();
  101. return(true);
  102. }
  103. return(false);
  104. }
  105. /***********************************************************************************************
  106. * DynamicVectorClass<T>::ID -- Find matching value in the dynamic vector. *
  107. * *
  108. * Use this routine to find a matching object (by value) in the vector. Unlike the base *
  109. * class ID function of similar name, this one restricts the scan to the current number *
  110. * of valid objects. *
  111. * *
  112. * INPUT: object -- A reference to the object that a match is to be found in the *
  113. * vector. *
  114. * *
  115. * OUTPUT: Returns with the index number of the object that is equivalent to the one *
  116. * specified. If no equivalent object could be found then -1 is returned. *
  117. * *
  118. * WARNINGS: none *
  119. * *
  120. * HISTORY: *
  121. * 03/13/1995 JLB : Created. *
  122. *=============================================================================================*/
  123. template<class T>
  124. int DynamicVectorClass<T>::ID(T const & object)
  125. {
  126. for (int index = 0; index < Count(); index++) {
  127. if ((*this)[index] == object) return(index);
  128. }
  129. return(-1);
  130. }
  131. /***********************************************************************************************
  132. * DynamicVectorClass<T>::Add -- Add an element to the vector. *
  133. * *
  134. * Use this routine to add an element to the vector. The vector will automatically be *
  135. * resized to accomodate the new element IF the vector was allocated previously and the *
  136. * growth rate is not zero. *
  137. * *
  138. * INPUT: object -- Reference to the object that will be added to the vector. *
  139. * *
  140. * OUTPUT: bool; Was the object added successfully? If so, the object is added to the end *
  141. * of the vector. *
  142. * *
  143. * WARNINGS: none *
  144. * *
  145. * HISTORY: *
  146. * 03/10/1995 JLB : Created. *
  147. *=============================================================================================*/
  148. template<class T>
  149. int DynamicVectorClass<T>::Add(T const & object)
  150. {
  151. if (ActiveCount >= Length()) {
  152. if ((IsAllocated || !VectorMax) && GrowthStep > 0) {
  153. if (!Resize(Length() + GrowthStep)) {
  154. /*
  155. ** Failure to increase the size of the vector is an error condition.
  156. ** Return with the error flag.
  157. */
  158. return(false);
  159. }
  160. } else {
  161. /*
  162. ** Increasing the size of this vector is not allowed! Bail this
  163. ** routine with the error code.
  164. */
  165. return(false);
  166. }
  167. }
  168. /*
  169. ** There is room for the new object now. Add it to the end of the object vector.
  170. */
  171. (*this)[ActiveCount++] = object;
  172. return(true);
  173. }
  174. /***********************************************************************************************
  175. * DynamicVectorClass<T>::Add_Head -- Adds element to head of the list. *
  176. * *
  177. * This routine will add the specified element to the head of the vector. If necessary, *
  178. * the vector will be expanded accordingly. *
  179. * *
  180. * INPUT: object -- Reference to the object to add to the head of this vector. *
  181. * *
  182. * OUTPUT: bool; Was the object added without error? *
  183. * *
  184. * WARNINGS: none *
  185. * *
  186. * HISTORY: *
  187. * 09/21/1995 JLB : Created. *
  188. *=============================================================================================*/
  189. template<class T>
  190. int DynamicVectorClass<T>::Add_Head(T const & object)
  191. {
  192. if (ActiveCount >= Length()) {
  193. if ((IsAllocated || !VectorMax) && GrowthStep > 0) {
  194. if (!Resize(Length() + GrowthStep)) {
  195. /*
  196. ** Failure to increase the size of the vector is an error condition.
  197. ** Return with the error flag.
  198. */
  199. return(false);
  200. }
  201. } else {
  202. /*
  203. ** Increasing the size of this vector is not allowed! Bail this
  204. ** routine with the error code.
  205. */
  206. return(false);
  207. }
  208. }
  209. /*
  210. ** There is room for the new object now. Add it to the end of the object vector.
  211. */
  212. if (ActiveCount) {
  213. memmove(&(*this)[1], &(*this)[0], ActiveCount * sizeof(T));
  214. }
  215. (*this)[0] = object;
  216. ActiveCount++;
  217. // (*this)[ActiveCount++] = object;
  218. return(true);
  219. }
  220. /***********************************************************************************************
  221. * DynamicVectorClass<T>::Delete -- Remove the specified object from the vector. *
  222. * *
  223. * This routine will delete the object referenced from the vector. All objects in the *
  224. * vector that follow the one deleted will be moved "down" to fill the hole. *
  225. * *
  226. * INPUT: object -- Reference to the object in this vector that is to be deleted. *
  227. * *
  228. * OUTPUT: bool; Was the object deleted successfully? This should always be true. *
  229. * *
  230. * WARNINGS: Do no pass a reference to an object that is NOT part of this vector. The *
  231. * results of this are undefined and probably catastrophic. *
  232. * *
  233. * HISTORY: *
  234. * 03/10/1995 JLB : Created. *
  235. *=============================================================================================*/
  236. template<class T>
  237. int DynamicVectorClass<T>::Delete(T const & object)
  238. {
  239. return(Delete(ID(object)));
  240. }
  241. /***********************************************************************************************
  242. * DynamicVectorClass<T>::Delete -- Deletes the specified index from the vector. *
  243. * *
  244. * Use this routine to delete the object at the specified index from the objects in the *
  245. * vector. This routine will move all the remaining objects "down" in order to fill the *
  246. * hole. *
  247. * *
  248. * INPUT: index -- The index number of the object in the vector that is to be deleted. *
  249. * *
  250. * OUTPUT: bool; Was the object index deleted successfully? Failure might mean that the index *
  251. * specified was out of bounds. *
  252. * *
  253. * WARNINGS: none *
  254. * *
  255. * HISTORY: *
  256. * 03/10/1995 JLB : Created. *
  257. *=============================================================================================*/
  258. template<class T>
  259. int DynamicVectorClass<T>::Delete(int index)
  260. {
  261. if ((unsigned)index < ActiveCount) {
  262. ActiveCount--;
  263. /*
  264. ** If there are any objects past the index that was deleted, copy those
  265. ** objects down in order to fill the hole. A simple memory copy is
  266. ** not sufficient since the vector could contain class objects that
  267. ** need to use the assignment operator for movement.
  268. */
  269. for (int i = index; i < ActiveCount; i++) {
  270. (*this)[i] = (*this)[i+1];
  271. }
  272. return(true);
  273. }
  274. return(false);
  275. }
  276. /************************** end of dynavec.cpp *****************************/