DYNAVEC.CPP 17 KB

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