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