VECTOR.CPP (51626B)
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: F:\projects\c&c\vcs\code\vector.cpv 2.17 16 Oct 1995 16:49:26 JOE_BOSTIC $ */ 17 /*********************************************************************************************** 18 *** 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 *** 19 *********************************************************************************************** 20 * * 21 * Project Name : Command & Conquer * 22 * * 23 * File Name : VECTOR.CPP * 24 * * 25 * Programmer : Joe L. Bostic * 26 * * 27 * Start Date : 02/19/95 * 28 * * 29 * Last Update : July 18, 1995 [JLB] * 30 * * 31 *---------------------------------------------------------------------------------------------* 32 * Functions: * 33 * BooleanVectorClass::BooleanVectorClass -- Copy constructor fo boolean array. * 34 * BooleanVectorClass::BooleanVectorClass -- Explicit data buffer constructor. * 35 * BooleanVectorClass::Clear -- Resets boolean vector to empty state. * 36 * BooleanVectorClass::Fixup -- Updates the boolean vector to a known state. * 37 * BooleanVectorClass::Reset -- Clear all boolean values in array. * 38 * BooleanVectorClass::Resize -- Resizes a boolean vector object. * 39 * BooleanVectorClass::Set -- Forces all boolean elements to true. * 40 * BooleanVectorClass::operator = -- Assignment operator. * 41 * BooleanVectorClass::operator == -- Comparison operator for boolean vector. * 42 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ 43 44 #include "function.h" 45 #include "vector.h" 46 //#include <mem.h> 47 #include <stdio.h> 48 49 /* 50 ** The following template function can be located here ONLY if all the instatiations are 51 ** declared in a header file this module includes. By placing the template functions here, 52 ** it speeds up compiler operation and reduces object module size. 53 */ 54 #if (0) // Moved to header 55 56 /*********************************************************************************************** 57 * VectorClass<T>::VectorClass -- Constructor for vector class. * 58 * * 59 * This constructor for the vector class is passed the initial size of the vector and an * 60 * optional pointer to a preallocated block of memory that the vector will be placed in. * 61 * If this optional pointer is NULL (or not provided), then the vector is allocated out * 62 * of free store (with the "new" operator). * 63 * * 64 * INPUT: size -- The number of elements to initialize this vector to. * 65 * * 66 * array -- Optional pointer to a previously allocated memory block to hold the * 67 * vector. * 68 * * 69 * OUTPUT: none * 70 * * 71 * WARNINGS: none * 72 * * 73 * HISTORY: * 74 * 03/10/1995 JLB : Created. * 75 *=============================================================================================*/ 76 template<class T> 77 VectorClass<T>::VectorClass(unsigned size, T const * array) 78 { 79 Vector = 0; 80 VectorMax = size; 81 IsAllocated = false; 82 83 /* 84 ** Allocate the vector. The default constructor will be called for every 85 ** object in this vector. 86 */ 87 if (size) { 88 if (array) { 89 Vector = new((void*)array) T[size]; 90 } else { 91 Vector = new T[size]; 92 IsAllocated = true; 93 } 94 } 95 } 96 97 98 /*********************************************************************************************** 99 * VectorClass<T>::~VectorClass -- Default destructor for vector class. * 100 * * 101 * This is the default destructor for the vector class. It will deallocate any memory * 102 * that it may have allocated. * 103 * * 104 * INPUT: none * 105 * * 106 * OUTPUT: none * 107 * * 108 * WARNINGS: none * 109 * * 110 * HISTORY: * 111 * 03/10/1995 JLB : Created. * 112 *=============================================================================================*/ 113 template<class T> 114 VectorClass<T>::~VectorClass(void) 115 { 116 VectorClass<T>::Clear(); 117 } 118 119 120 /*********************************************************************************************** 121 * VectorClass<T>::VectorClass -- Copy constructor for vector object. * 122 * * 123 * This is the copy constructor for the vector class. It will duplicate the provided * 124 * vector into the new vector being created. * 125 * * 126 * INPUT: vector -- Reference to the vector to use as a copy. * 127 * * 128 * OUTPUT: none * 129 * * 130 * WARNINGS: none * 131 * * 132 * HISTORY: * 133 * 03/10/1995 JLB : Created. * 134 *=============================================================================================*/ 135 template<class T> 136 VectorClass<T>::VectorClass(VectorClass<T> const & vector) 137 { 138 VectorMax = 0; 139 IsAllocated = false; 140 Vector = 0; 141 *this = vector; 142 } 143 144 145 /*********************************************************************************************** 146 * VectorClass<T>::operator = -- The assignment operator. * 147 * * 148 * This the the assignment operator for vector objects. It will alter the existing lvalue * 149 * vector to duplicate the rvalue one. * 150 * * 151 * INPUT: vector -- The rvalue vector to copy into the lvalue one. * 152 * * 153 * OUTPUT: Returns with reference to the newly copied vector. * 154 * * 155 * WARNINGS: none * 156 * * 157 * HISTORY: * 158 * 03/10/1995 JLB : Created. * 159 *=============================================================================================*/ 160 template<class T> 161 VectorClass<T> & VectorClass<T>::operator =(VectorClass<T> const & vector) 162 { 163 Clear(); 164 VectorMax = vector.Length(); 165 if (VectorMax) { 166 Vector = new T[VectorMax]; 167 if (Vector) { 168 IsAllocated = true; 169 for (int index = 0; index < (int)VectorMax; index++) { 170 Vector[index] = vector[index]; 171 } 172 } 173 } else { 174 Vector = 0; 175 IsAllocated = false; 176 } 177 return(*this); 178 } 179 180 181 /*********************************************************************************************** 182 * VectorClass<T>::operator == -- Equality operator for vector objects. * 183 * * 184 * This operator compares two vectors for equality. It does this by performing an object * 185 * by object comparison between the two vectors. * 186 * * 187 * INPUT: vector -- The right vector expression. * 188 * * 189 * OUTPUT: bool; Are the two vectors essentially equal? (do they contain comparable elements * 190 * in the same order?) * 191 * * 192 * WARNINGS: The equality operator must exist for the objects that this vector contains. * 193 * * 194 * HISTORY: * 195 * 03/10/1995 JLB : Created. * 196 *=============================================================================================*/ 197 template<class T> 198 int VectorClass<T>::operator == (VectorClass<T> const & vector) const 199 { 200 if (VectorMax == vector.Length()) { 201 for (int index = 0; index < (int)VectorMax; index++) { 202 if (Vector[index] != vector[index]) { 203 return(false); 204 } 205 } 206 return(true); 207 } 208 return(false); 209 } 210 211 212 /*********************************************************************************************** 213 * VectorClass<T>::ID -- Pointer based conversion to index number. * 214 * * 215 * Use this routine to convert a pointer to an element in the vector back into the index * 216 * number of that object. This routine ONLY works with actual pointers to object within * 217 * the vector. For "equivalent" object index number (such as with similar integral values) * 218 * then use the "by value" index number ID function. * 219 * * 220 * INPUT: pointer -- Pointer to an actual object in the vector. * 221 * * 222 * OUTPUT: Returns with the index number for the object pointed to by the parameter. * 223 * * 224 * WARNINGS: This routine is only valid for actual pointers to object that exist within * 225 * the vector. All other object pointers will yield undefined results. * 226 * * 227 * HISTORY: * 228 * 03/13/1995 JLB : Created. * 229 *=============================================================================================*/ 230 template<class T> 231 inline int VectorClass<T>::ID(T const * ptr) 232 { 233 return(((unsigned long)ptr - (unsigned long)&(*this)[0]) / sizeof(T)); 234 } 235 236 237 /*********************************************************************************************** 238 * VectorClass<T>::ID -- Finds object ID based on value. * 239 * * 240 * Use this routine to find the index value of an object with equivalent value in the * 241 * vector. Typical use of this would be for integral types. * 242 * * 243 * INPUT: object -- Reference to the object that is to be looked up in the vector. * 244 * * 245 * OUTPUT: Returns with the index number of the object that is equivalent to the one * 246 * specified. If no matching value could be found then -1 is returned. * 247 * * 248 * WARNINGS: none * 249 * * 250 * HISTORY: * 251 * 03/13/1995 JLB : Created. * 252 *=============================================================================================*/ 253 template<class T> 254 int VectorClass<T>::ID(T const & object) 255 { 256 for (int index = 0; index < (int)VectorMax; index++) { 257 if ((*this)[index] == object) { 258 return(index); 259 } 260 } 261 return(-1); 262 } 263 264 265 /*********************************************************************************************** 266 * VectorClass<T>::Clear -- Frees and clears the vector. * 267 * * 268 * Use this routine to reset the vector to an empty (non-allocated) state. A vector will * 269 * free all allocated memory when this routine is called. In order for the vector to be * 270 * useful after this point, the Resize function must be called to give it element space. * 271 * * 272 * INPUT: none * 273 * * 274 * OUTPUT: none * 275 * * 276 * WARNINGS: none * 277 * * 278 * HISTORY: * 279 * 03/10/1995 JLB : Created. * 280 *=============================================================================================*/ 281 template<class T> 282 void VectorClass<T>::Clear(void) 283 { 284 if (Vector && IsAllocated) { 285 delete[] Vector; 286 Vector = 0; 287 } 288 IsAllocated = false; 289 VectorMax = 0; 290 } 291 292 293 /*********************************************************************************************** 294 * VectorClass<T>::Resize -- Changes the size of the vector. * 295 * * 296 * This routine is used to change the size (usually to increase) the size of a vector. This * 297 * is the only way to increase the vector's working room (number of elements). * 298 * * 299 * INPUT: newsize -- The desired size of the vector. * 300 * * 301 * array -- Optional pointer to a previously allocated memory block that the * 302 * array will be located in. If this parameter is not supplied, then * 303 * the array will be allocated from free store. * 304 * * 305 * OUTPUT: bool; Was the array resized successfully? * 306 * * 307 * WARNINGS: Failure to succeed could be the result of running out of memory. * 308 * * 309 * HISTORY: * 310 * 03/10/1995 JLB : Created. * 311 *=============================================================================================*/ 312 template<class T> 313 int VectorClass<T>::Resize(unsigned newsize, T const * array) 314 { 315 if (newsize) { 316 317 /* 318 ** Allocate a new vector of the size specified. The default constructor 319 ** will be called for every object in this vector. 320 */ 321 T * newptr; 322 if (!array) { 323 newptr = new T[newsize]; 324 } else { 325 newptr = new((void*)array) T[newsize]; 326 } 327 if (!newptr) { 328 return(false); 329 } 330 331 /* 332 ** If there is an old vector, then it must be copied (as much as is feasable) 333 ** to the new vector. 334 */ 335 if (Vector) { 336 337 /* 338 ** Copy as much of the old vector into the new vector as possible. This 339 ** presumes that there is a functional assignment operator for each 340 ** of the objects in the vector. 341 */ 342 int copycount = (newsize < VectorMax) ? newsize : VectorMax; 343 for (int index = 0; index < copycount; index++) { 344 newptr[index] = Vector[index]; 345 } 346 347 /* 348 ** Delete the old vector. This might cause the destructors to be called 349 ** for all of the old elements. This makes the implementation of suitable 350 ** assignment operator very important. The default assigment operator will 351 ** only work for the simplist of objects. 352 */ 353 if (IsAllocated) { 354 delete[] Vector; 355 Vector = 0; 356 } 357 } 358 359 /* 360 ** Assign the new vector data to this class. 361 */ 362 Vector = newptr; 363 VectorMax = newsize; 364 IsAllocated = (Vector && !array); 365 366 } else { 367 368 /* 369 ** Resizing to zero is the same as clearing the vector. 370 */ 371 Clear(); 372 } 373 return(true); 374 } 375 376 377 /*********************************************************************************************** 378 * DynamicVectorClass<T>::DynamicVectorClass -- Constructor for dynamic vector. * 379 * * 380 * This is the normal constructor for the dynamic vector class. It is similar to the normal * 381 * vector class constructor. The vector is initialized to contain the number of elements * 382 * specified in the "size" parameter. The memory is allocated from free store unless the * 383 * optional array parameter is provided. In this case it will place the vector at the * 384 * memory location specified. * 385 * * 386 * INPUT: size -- The maximum number of objects allowed in this vector. * 387 * * 388 * array -- Optional pointer to the memory area to place the vector at. * 389 * * 390 * OUTPUT: none * 391 * * 392 * WARNINGS: none * 393 * * 394 * HISTORY: * 395 * 03/10/1995 JLB : Created. * 396 *=============================================================================================*/ 397 template<class T> 398 DynamicVectorClass<T>::DynamicVectorClass(unsigned size, T const * array) 399 : VectorClass<T>(size, array) 400 { 401 GrowthStep = 10; 402 ActiveCount = 0; 403 } 404 405 406 /*********************************************************************************************** 407 * DynamicVectorClass<T>::Resize -- Changes the size of a dynamic vector. * 408 * * 409 * Use this routine to change the size of the vector. The size changed is the maximum * 410 * number of allocated objects within this vector. If a memory buffer is provided, then * 411 * the vector will be located there. Otherwise, the memory will be allocated out of free * 412 * store. * 413 * * 414 * INPUT: newsize -- The desired maximum size of this vector. * 415 * * 416 * array -- Optional pointer to a previosly allocated memory array. * 417 * * 418 * OUTPUT: bool; Was vector successfully resized according to specifications? * 419 * * 420 * WARNINGS: Failure to resize the vector could be the result of lack of free store. * 421 * * 422 * HISTORY: * 423 * 03/10/1995 JLB : Created. * 424 *=============================================================================================*/ 425 template<class T> 426 int DynamicVectorClass<T>::Resize(unsigned newsize, T const * array) 427 { 428 if (VectorClass<T>::Resize(newsize, array)) { 429 if (Length() < (unsigned)ActiveCount) ActiveCount = Length(); 430 return(true); 431 } 432 return(false); 433 } 434 435 436 /*********************************************************************************************** 437 * DynamicVectorClass<T>::ID -- Find matching value in the dynamic vector. * 438 * * 439 * Use this routine to find a matching object (by value) in the vector. Unlike the base * 440 * class ID function of similar name, this one restricts the scan to the current number * 441 * of valid objects. * 442 * * 443 * INPUT: object -- A reference to the object that a match is to be found in the * 444 * vector. * 445 * * 446 * OUTPUT: Returns with the index number of the object that is equivalent to the one * 447 * specified. If no equivalent object could be found then -1 is returned. * 448 * * 449 * WARNINGS: none * 450 * * 451 * HISTORY: * 452 * 03/13/1995 JLB : Created. * 453 *=============================================================================================*/ 454 template<class T> 455 int DynamicVectorClass<T>::ID(T const & object) 456 { 457 for (int index = 0; index < Count(); index++) { 458 if ((*this)[index] == object) return(index); 459 } 460 return(-1); 461 } 462 463 464 /*********************************************************************************************** 465 * DynamicVectorClass<T>::Add -- Add an element to the vector. * 466 * * 467 * Use this routine to add an element to the vector. The vector will automatically be * 468 * resized to accomodate the new element IF the vector was allocated previosly and the * 469 * growth rate is not zero. * 470 * * 471 * INPUT: object -- Reference to the object that will be added to the vector. * 472 * * 473 * OUTPUT: bool; Was the object added successfully? If so, the object is added to the end * 474 * of the vector. * 475 * * 476 * WARNINGS: none * 477 * * 478 * HISTORY: * 479 * 03/10/1995 JLB : Created. * 480 *=============================================================================================*/ 481 template<class T> 482 int DynamicVectorClass<T>::Add(T const & object) 483 { 484 if ((unsigned)ActiveCount >= Length()) { 485 if ((IsAllocated || !VectorMax) && GrowthStep > 0) { 486 if (!Resize(Length() + GrowthStep)) { 487 488 /* 489 ** Failure to increase the size of the vector is an error condition. 490 ** Return with the error flag. 491 */ 492 return(false); 493 } 494 } else { 495 496 /* 497 ** Increasing the size of this vector is not allowed! Bail this 498 ** routine with the error code. 499 */ 500 return(false); 501 } 502 } 503 504 /* 505 ** There is room for the new object now. Add it to the end of the object vector. 506 */ 507 (*this)[ActiveCount++] = object; 508 return(true); 509 } 510 511 512 template<class T> 513 int DynamicVectorClass<T>::Add_Head(T const & object) 514 { 515 if (ActiveCount >= Length()) { 516 if ((IsAllocated || !VectorMax) && GrowthStep > 0) { 517 if (!Resize(Length() + GrowthStep)) { 518 519 /* 520 ** Failure to increase the size of the vector is an error condition. 521 ** Return with the error flag. 522 */ 523 return(false); 524 } 525 } else { 526 527 /* 528 ** Increasing the size of this vector is not allowed! Bail this 529 ** routine with the error code. 530 */ 531 return(false); 532 } 533 } 534 535 /* 536 ** There is room for the new object now. Add it to the end of the object vector. 537 */ 538 if (ActiveCount) { 539 memmove(&(*this)[1], &(*this)[0], ActiveCount * sizeof(T)); 540 } 541 (*this)[0] = object; 542 ActiveCount++; 543 // (*this)[ActiveCount++] = object; 544 return(true); 545 } 546 547 548 /*********************************************************************************************** 549 * DynamicVectorClass<T>::Delete -- Remove the specified object from the vector. * 550 * * 551 * This routine will delete the object referenced from the vector. All objects in the * 552 * vector that follow the one deleted will be moved "down" to fill the hole. * 553 * * 554 * INPUT: object -- Reference to the object in this vector that is to be deleted. * 555 * * 556 * OUTPUT: bool; Was the object deleted successfully? This should always be true. * 557 * * 558 * WARNINGS: Do no pass a reference to an object that is NOT part of this vector. The * 559 * results of this are undefined and probably catastrophic. * 560 * * 561 * HISTORY: * 562 * 03/10/1995 JLB : Created. * 563 *=============================================================================================*/ 564 template<class T> 565 int DynamicVectorClass<T>::Delete(T const & object) 566 { 567 int index = ID(object); 568 if (index != -1){ 569 return(Delete(index)); 570 }else{ 571 return (false); 572 } 573 } 574 575 576 /*********************************************************************************************** 577 * DynamicVectorClass<T>::Delete -- Deletes the specified index from the vector. * 578 * * 579 * Use this routine to delete the object at the specified index from the objects in the * 580 * vector. This routine will move all the remaining objects "down" in order to fill the * 581 * hole. * 582 * * 583 * INPUT: index -- The index number of the object in the vector that is to be deleted. * 584 * * 585 * OUTPUT: bool; Was the object index deleted successfully? Failure might mean that the index * 586 * specified was out of bounds. * 587 * * 588 * WARNINGS: none * 589 * * 590 * HISTORY: * 591 * 03/10/1995 JLB : Created. * 592 *=============================================================================================*/ 593 template<class T> 594 int DynamicVectorClass<T>::Delete(int index) 595 { 596 if ((unsigned)index < ActiveCount) { 597 ActiveCount--; 598 599 /* 600 ** If there are any objects past the index that was deleted, copy those 601 ** objects down in order to fill the hole. A simple memory copy is 602 ** not sufficient since the vector could contain class objects that 603 ** need to use the assignment operator for movement. 604 */ 605 for (int i = index; i < ActiveCount; i++) { 606 (*this)[i] = (*this)[i+1]; 607 } 608 return(true); 609 } 610 return(false); 611 } 612 613 #endif //Moved to header 614 615 616 //---------------------------------------------------------------------------------------------- 617 618 /*********************************************************************************************** 619 * BooleanVectorClass::BooleanVectorClass -- Explicit data buffer constructor. * 620 * * 621 * This is the constructor for a boolean array. This constructor takes the memory pointer * 622 * provided as assigns that as the array data pointer. * 623 * * 624 * INPUT: size -- The size of the array (in bits). * 625 * * 626 * array -- Pointer to the memory that the array is to use. * 627 * * 628 * OUTPUT: none * 629 * * 630 * WARNINGS: You must make sure that the memory specified is large enough to contain the * 631 * bits specified. * 632 * * 633 * HISTORY: * 634 * 07/18/1995 JLB : Created. * 635 *=============================================================================================*/ 636 BooleanVectorClass::BooleanVectorClass(unsigned size, unsigned char * array) 637 { 638 BitArray.Resize(((size + (8-1)) / 8), array); 639 LastIndex = -1; 640 BitCount = size; 641 } 642 643 644 /*********************************************************************************************** 645 * BooleanVectorClass::BooleanVectorClass -- Copy constructor fo boolean array. * 646 * * 647 * This is the copy constructor for a boolean array. It is used to make a duplicate of the * 648 * boolean array. * 649 * * 650 * INPUT: vector -- Reference to the vector to be duplicated. * 651 * * 652 * OUTPUT: none * 653 * * 654 * WARNINGS: none * 655 * * 656 * HISTORY: * 657 * 07/18/1995 JLB : Created. * 658 *=============================================================================================*/ 659 BooleanVectorClass::BooleanVectorClass(BooleanVectorClass const & vector) 660 { 661 LastIndex = -1; 662 *this = vector; 663 } 664 665 666 /*********************************************************************************************** 667 * BooleanVectorClass::operator = -- Assignment operator. * 668 * * 669 * This routine will make a copy of the specifed boolean vector array. The vector is * 670 * copied into an already constructed existing vector. The values from the existing vector * 671 * are destroyed by this copy. * 672 * * 673 * INPUT: vector -- Reference to the vector to make a copy of. * 674 * * 675 * OUTPUT: none * 676 * * 677 * WARNINGS: none * 678 * * 679 * HISTORY: * 680 * 07/18/1995 JLB : Created. * 681 *=============================================================================================*/ 682 BooleanVectorClass & BooleanVectorClass::operator =(BooleanVectorClass const & vector) 683 { 684 Fixup(); 685 Copy = vector.Copy; 686 LastIndex = vector.LastIndex; 687 BitArray = vector.BitArray; 688 BitCount = vector.BitCount; 689 return(*this); 690 } 691 692 693 /*********************************************************************************************** 694 * BooleanVectorClass::operator == -- Comparison operator for boolean vector. * 695 * * 696 * This is the comparison operator for a boolean vector class. Boolean vectors are equal * 697 * if the bit count and bit values are identical. * 698 * * 699 * INPUT: vector -- Reference to the vector to compare to. * 700 * * 701 * OUTPUT: Are the boolean vectors identical? * 702 * * 703 * WARNINGS: none * 704 * * 705 * HISTORY: * 706 * 07/18/1995 JLB : Created. * 707 *=============================================================================================*/ 708 int BooleanVectorClass::operator == (const BooleanVectorClass & vector) 709 { 710 Fixup(LastIndex); 711 return(BitCount == vector.BitCount && BitArray == vector.BitArray); 712 } 713 714 715 /*********************************************************************************************** 716 * BooleanVectorClass::Resize -- Resizes a boolean vector object. * 717 * * 718 * This routine will resize the boolean vector object. An index value used with a boolean * 719 * vector must be less than the value specified in as the new size. * 720 * * 721 * INPUT: size -- The new maximum size of this boolean vector. * 722 * * 723 * OUTPUT: Was the boolean vector sized successfully? * 724 * * 725 * WARNINGS: The boolean array might be reallocated or even deleted by this routine. * 726 * * 727 * HISTORY: * 728 * 07/18/1995 JLB : Created. * 729 *=============================================================================================*/ 730 int BooleanVectorClass::Resize(unsigned size) 731 { 732 Fixup(); 733 734 if (size) { 735 736 /* 737 ** Record the previous bit count of the boolean vector. This is used 738 ** to determine if the array has grown in size and thus clearing is 739 ** necessary. 740 */ 741 int oldsize = BitCount; 742 743 /* 744 ** Actually resize the bit array. Since this is a bit packed array, 745 ** there are 8 elements per byte (rounded up). 746 */ 747 int success = BitArray.Resize(((size + (8-1)) / 8)); 748 749 /* 750 ** Since there is no default constructor for bit packed integers, a manual 751 ** clearing of the bits is required. 752 */ 753 BitCount = size; 754 if (success && oldsize < (int)size) { 755 for (int index = oldsize; index < (int)size; index++) { 756 (*this)[index] = 0; 757 } 758 } 759 760 return(success); 761 } 762 763 /* 764 ** Resizing to zero is the same as clearing and deallocating the array. 765 ** This is always successful. 766 */ 767 Clear(); 768 return(true); 769 } 770 771 772 /*********************************************************************************************** 773 * BooleanVectorClass::Clear -- Resets boolean vector to empty state. * 774 * * 775 * This routine will clear out the boolean array. This will free any allocated memory and * 776 * result in the boolean vector being unusable until the Resize function is subsiquently * 777 * called. * 778 * * 779 * INPUT: none * 780 * * 781 * OUTPUT: none * 782 * * 783 * WARNINGS: The boolean vector cannot be used until it is resized to a non null condition. * 784 * * 785 * HISTORY: * 786 * 07/18/1995 JLB : Created. * 787 *=============================================================================================*/ 788 void BooleanVectorClass::Clear(void) 789 { 790 Fixup(); 791 BitCount = 0; 792 BitArray.Clear(); 793 } 794 795 796 /*********************************************************************************************** 797 * BooleanVectorClass::Reset -- Clear all boolean values in array. * 798 * * 799 * This is the preferred (and quick) method to clear the boolean array to a false condition.* 800 * * 801 * INPUT: none * 802 * * 803 * OUTPUT: none * 804 * * 805 * WARNINGS: none * 806 * * 807 * HISTORY: * 808 * 07/18/1995 JLB : Created. * 809 *=============================================================================================*/ 810 void BooleanVectorClass::Reset(void) 811 { 812 LastIndex = -1; 813 if (BitArray.Length()) { 814 memset(&BitArray[0], '\0', BitArray.Length()); 815 } 816 } 817 818 819 /*********************************************************************************************** 820 * BooleanVectorClass::Set -- Forces all boolean elements to true. * 821 * * 822 * This is the preferred (and fast) way to set all boolean elements to true. * 823 * * 824 * INPUT: none * 825 * * 826 * OUTPUT: none * 827 * * 828 * WARNINGS: none * 829 * * 830 * HISTORY: * 831 * 07/18/1995 JLB : Created. * 832 *=============================================================================================*/ 833 void BooleanVectorClass::Set(void) 834 { 835 LastIndex = -1; 836 if (BitArray.Length()) { 837 memset(&BitArray[0], '\xFF', BitArray.Length()); 838 } 839 } 840 841 842 /*********************************************************************************************** 843 * BooleanVectorClass::Fixup -- Updates the boolean vector to a known state. * 844 * * 845 * Use this routine to set the boolean value copy to match the appropriate bit in the * 846 * boolean array. The boolean array will be updated with any changes from the last time * 847 * a value was fetched from the boolean vector. By using this update method, the boolean * 848 * array can be treated as a normal array even though the elements are composed of * 849 * otherwise inaccessable bits. * 850 * * 851 * INPUT: index -- The index to set the new copy value to. If the index is -1, then the * 852 * previous value will be updated into the vector array, but no new value * 853 * will be fetched from it. * 854 * * 855 * OUTPUT: none * 856 * * 857 * WARNINGS: Always call this routine with "-1" if any direct manipulation of the bit * 858 * array is to occur. This ensures that the bit array is accurate. * 859 * * 860 * HISTORY: * 861 * 07/18/1995 JLB : Created. * 862 *=============================================================================================*/ 863 void BooleanVectorClass::Fixup(int index) const 864 { 865 /* 866 ** If the requested index value is illegal, then force the index 867 ** to be -1. This is the default non-index value. 868 */ 869 if (index >= BitCount) { 870 index = -1; 871 } 872 873 /* 874 ** If the new index is different than the previous index, there might 875 ** be some fixing up required. 876 */ 877 if (index != LastIndex) { 878 879 /* 880 ** If the previously fetched boolean value was changed, then update 881 ** the boolean array accordingly. 882 */ 883 if (LastIndex != -1) { 884 Set_Bit((void*)&BitArray[0], LastIndex, Copy); 885 } 886 887 /* 888 ** If this new current index is valid, then fill in the reference boolean 889 ** value with the approriate data from the bit array. 890 */ 891 if (index != -1) { 892 ((unsigned char&)Copy) = Get_Bit(&BitArray[0], index); 893 } 894 895 ((int &)LastIndex) = index; 896 } 897 } 898