From 37b47a8cfe0718d1cd1042349db66382290bcfe9 Mon Sep 17 00:00:00 2001 From: Justin Marshall Date: Fri, 26 Jun 2020 15:04:01 -0700 Subject: [PATCH] Missing file from previous checkin. --- code/redalert/vector.h | 368 ++++++++++++++++++++--------------------- 1 file changed, 184 insertions(+), 184 deletions(-) diff --git a/code/redalert/vector.h b/code/redalert/vector.h index 2cd4ca2..9085235 100644 --- a/code/redalert/vector.h +++ b/code/redalert/vector.h @@ -52,57 +52,57 @@ #include #include -// ST - 5/8/1029 -//inline void * operator new(size_t , void * pointer) {return(pointer);} -//inline void * operator new[](size_t , void * pointer) {return(pointer);} + // ST - 5/8/1029 + //inline void * operator new(size_t , void * pointer) {return(pointer);} + //inline void * operator new[](size_t , void * pointer) {return(pointer);} -/************************************************************************** -** This is a general purpose vector class. A vector is defined by this -** class, as an array of arbitrary objects where the array can be dynamically -** sized. Because is deals with arbitrary object types, it can handle everything. -** As a result of this, it is not terribly efficient for integral objects (such -** as char or int). It will function correctly, but the copy constructor and -** equality operator could be highly optimized if the integral type were known. -** This efficiency can be implemented by deriving an integral vector template -** from this one in order to supply more efficient routines. -*/ + /************************************************************************** + ** This is a general purpose vector class. A vector is defined by this + ** class, as an array of arbitrary objects where the array can be dynamically + ** sized. Because is deals with arbitrary object types, it can handle everything. + ** As a result of this, it is not terribly efficient for integral objects (such + ** as char or int). It will function correctly, but the copy constructor and + ** equality operator could be highly optimized if the integral type were known. + ** This efficiency can be implemented by deriving an integral vector template + ** from this one in order to supply more efficient routines. + */ template class VectorClass { - public: - VectorClass(NoInitClass const & ) {}; - VectorClass(unsigned size=0, T const * array=0); - VectorClass(VectorClass const &); // Copy constructor. - virtual ~VectorClass(void); +public: + VectorClass(NoInitClass const&) {}; + VectorClass(unsigned size = 0, T const* array = 0); + VectorClass(VectorClass const&); // Copy constructor. + virtual ~VectorClass(void); - T & operator[](unsigned index) {return(Vector[index]);}; - T const & operator[](unsigned index) const {return(Vector[index]);}; - virtual VectorClass & operator =(VectorClass const &); // Assignment operator. - virtual int operator == (VectorClass const &) const; // Equality operator. - virtual int Resize(unsigned newsize, T const * array=0); - virtual void Clear(void); - unsigned Length(void) const {return VectorMax;}; - virtual int ID(T const * ptr); // Pointer based identification. - virtual int ID(T const & ptr); // Value based identification. + T& operator[](unsigned index) { return(Vector[index]); }; + T const& operator[](unsigned index) const { return(Vector[index]); }; + virtual VectorClass& operator =(VectorClass const&); // Assignment operator. + virtual int operator == (VectorClass const&) const; // Equality operator. + virtual int Resize(unsigned newsize, T const* array = 0); + virtual void Clear(void); + unsigned Length(void) const { return VectorMax; }; + virtual int ID(T const* ptr); // Pointer based identification. + virtual int ID(T const& ptr); // Value based identification. - protected: +protected: - /* - ** This is a pointer to the allocated vector array of elements. - */ - T * Vector; + /* + ** This is a pointer to the allocated vector array of elements. + */ + T* Vector; - /* - ** This is the maximum number of elements allowed in this vector. - */ - unsigned VectorMax; + /* + ** This is the maximum number of elements allowed in this vector. + */ + unsigned VectorMax; - /* - ** Does the vector data pointer refer to memory that this class has manually - ** allocated? If so, then this class is responsible for deleting it. - */ - unsigned IsAllocated:1; + /* + ** Does the vector data pointer refer to memory that this class has manually + ** allocated? If so, then this class is responsible for deleting it. + */ + unsigned IsAllocated : 1; }; @@ -118,56 +118,56 @@ class VectorClass template class DynamicVectorClass : public VectorClass { - public: - DynamicVectorClass(unsigned size=0, T const * array=0); +public: + DynamicVectorClass(unsigned size = 0, T const* array = 0); - // Change maximum size of vector. - virtual int Resize(unsigned newsize, T const * array=0); + // Change maximum size of vector. + virtual int Resize(unsigned newsize, T const* array = 0); - // Resets and frees the vector array. - virtual void Clear(void) {ActiveCount = 0;VectorClass::Clear();}; + // Resets and frees the vector array. + virtual void Clear(void) { ActiveCount = 0;VectorClass::Clear(); }; - // Fetch number of "allocated" vector objects. - int Count(void) const {return(ActiveCount);}; + // Fetch number of "allocated" vector objects. + int Count(void) const { return(ActiveCount); }; - // Add object to vector (growing as necessary). - int Add(T const & object); - int Add_Head(T const & object); + // Add object to vector (growing as necessary). + int Add(T const& object); + int Add_Head(T const& object); - // Delete object just like this from vector. - int Delete(T const & object); + // Delete object just like this from vector. + int Delete(T const& object); - // Delete object at this vector index. - int Delete(int index); + // Delete object at this vector index. + int Delete(int index); - // Deletes all objects in the vector. - void Delete_All(void) {ActiveCount = 0;}; + // Deletes all objects in the vector. + void Delete_All(void) { ActiveCount = 0; }; - // Set amount that vector grows by. - int Set_Growth_Step(int step) {return(GrowthStep = step);}; + // Set amount that vector grows by. + int Set_Growth_Step(int step) { return(GrowthStep = step); }; - // Fetch current growth step rate. - int Growth_Step(void) {return GrowthStep;}; + // Fetch current growth step rate. + int Growth_Step(void) { return GrowthStep; }; - virtual int ID(T const * ptr) {return(VectorClass::ID(ptr));}; - virtual int ID(T const & ptr); + virtual int ID(T const* ptr) { return(VectorClass::ID(ptr)); }; + virtual int ID(T const& ptr); - protected: +protected: - /* - ** This is a count of the number of active objects in this - ** vector. The memory array often times is bigger than this - ** value. - */ - int ActiveCount; + /* + ** This is a count of the number of active objects in this + ** vector. The memory array often times is bigger than this + ** value. + */ + int ActiveCount; - /* - ** If there is insufficient room in the vector array for a new - ** object to be added, then the vector will grow by the number - ** of objects specified by this value. This is controlled by - ** the Set_Growth_Step() function. - */ - int GrowthStep; + /* + ** If there is insufficient room in the vector array for a new + ** object to be added, then the vector will grow by the number + ** of objects specified by this value. This is controlled by + ** the Set_Growth_Step() function. + */ + int GrowthStep; }; @@ -210,22 +210,22 @@ public: return Count(Active); } - int Add(T const & object) + int Add(T const& object) { return Add(Active, object); } - int Add_Head(T const & object) + int Add_Head(T const& object) { return Add_Head(Active, object); } - int Delete(T const & object) + int Delete(T const& object) { return Delete(Active, object); } - int Delete_All(T const & object) + int Delete_All(T const& object) { int count = 0; for (int i = FIRST; i < COUNT; ++i) @@ -235,7 +235,7 @@ public: return count; } - int Delete_All_Except(T const & object, int except) + int Delete_All_Except(T const& object, int except) { int count = 0; for (int i = FIRST; i < COUNT; ++i) @@ -253,12 +253,12 @@ public: return Delete(Active, index); } - T & operator[](unsigned index) + T& operator[](unsigned index) { return Collection[Active][index]; } - T const & operator[](unsigned index) const + T const& operator[](unsigned index) const { return Collection[Active][index]; } @@ -273,17 +273,17 @@ public: return Collection[context].Count(); } - int Add(int context, T const & object) + int Add(int context, T const& object) { return Collection[context].Add(object); } - int Add_Head(int context, T const & object) + int Add_Head(int context, T const& object) { return Collection[context].Add(object); } - int Delete(int context, T const & object) + int Delete(int context, T const& object) { return Collection[context].Delete(object); } @@ -293,12 +293,12 @@ public: return Collection[context].Delete(index); } - DynamicVectorClass & Raw() + DynamicVectorClass& Raw() { return Collection[Active]; } - DynamicVectorClass & Raw(int context) + DynamicVectorClass& Raw(int context) { return Collection[context]; } @@ -321,104 +321,104 @@ private: */ class BooleanVectorClass { - public: - BooleanVectorClass(unsigned size=0, unsigned char * array=0); - BooleanVectorClass(BooleanVectorClass const & vector); +public: + BooleanVectorClass(unsigned size = 0, unsigned char* array = 0); + BooleanVectorClass(BooleanVectorClass const& vector); - // Assignment operator. - BooleanVectorClass & operator =(BooleanVectorClass const & vector); + // Assignment operator. + BooleanVectorClass& operator =(BooleanVectorClass const& vector); - // Equivalency operator. - int operator == (BooleanVectorClass const & vector); + // Equivalency operator. + int operator == (BooleanVectorClass const& vector); - // Fetch number of boolean objects in vector. - int Length(void) {return BitCount;}; + // Fetch number of boolean objects in vector. + int Length(void) { return BitCount; }; - // Set all boolean values to false; - void Reset(void); + // Set all boolean values to false; + void Reset(void); - // Set all boolean values to true. - void Set(void); + // Set all boolean values to true. + void Set(void); - // Resets vector to zero length (frees memory). - void Clear(void); + // Resets vector to zero length (frees memory). + void Clear(void); - // Change size of this boolean vector. - int Resize(unsigned size); + // Change size of this boolean vector. + int Resize(unsigned size); - // Fetch reference to specified index. - bool const & operator[](int index) const { - if (LastIndex != index) Fixup(index); - return(Copy); - }; - bool & operator[](int index) { - if (LastIndex != index) Fixup(index); - return(Copy); - }; + // Fetch reference to specified index. + bool const& operator[](int index) const { + if (LastIndex != index) Fixup(index); + return(Copy); + }; + bool& operator[](int index) { + if (LastIndex != index) Fixup(index); + return(Copy); + }; - // Quick check on boolean state. - bool Is_True(int index) const { - if (index == LastIndex) return(Copy); - return(Get_Bit(&BitArray[0], index)); - }; + // Quick check on boolean state. + bool Is_True(int index) const { + if (index == LastIndex) return(Copy); + return(Get_Bit(&BitArray[0], index)); + }; - // Find first index that is false. - int First_False(void) const { - if (LastIndex != -1) Fixup(-1); + // Find first index that is false. + int First_False(void) const { + if (LastIndex != -1) Fixup(-1); - int retval = First_False_Bit(&BitArray[0], BitArray.Length()); - if (retval < BitCount) return(retval); - - /* - ** Failure to find a false boolean value in the vector. Return this - ** fact in the form of an invalid index number. - */ - return(-1); - } - - // Find first index that is true. - int First_True(void) const { - if (LastIndex != -1) Fixup(-1); - - int retval = First_True_Bit(&BitArray[0], BitArray.Length()); - if (retval < BitCount) return(retval); - - /* - ** Failure to find a true boolean value in the vector. Return this - ** fact in the form of an invalid index number. - */ - return(-1); - } - - private: - void Fixup(int index=-1) const; + int retval = First_False_Bit(&BitArray[0], BitArray.Length()); + if (retval < BitCount) return(retval); /* - ** This is the number of boolean values in the vector. This value is - ** not necessarily a multiple of 8, even though the underlying character - ** vector contains a multiple of 8 bits. + ** Failure to find a false boolean value in the vector. Return this + ** fact in the form of an invalid index number. */ - int BitCount; + return(-1); + } + + // Find first index that is true. + int First_True(void) const { + if (LastIndex != -1) Fixup(-1); + + int retval = First_True_Bit(&BitArray[0], BitArray.Length()); + if (retval < BitCount) return(retval); /* - ** This is a referential copy of an element in the bit vector. The - ** purpose of this copy is to allow normal reference access to this - ** object (for speed reasons). This hides the bit packing scheme from - ** the user of this class. + ** Failure to find a true boolean value in the vector. Return this + ** fact in the form of an invalid index number. */ - bool Copy; + return(-1); + } - /* - ** This records the index of the value last fetched into the reference - ** boolean variable. This index is used to properly restore the value - ** when the reference copy needs updating. - */ - int LastIndex; +private: + void Fixup(int index = -1) const; - /* - ** This points to the allocated bitfield array. - */ - VectorClass BitArray; + /* + ** This is the number of boolean values in the vector. This value is + ** not necessarily a multiple of 8, even though the underlying character + ** vector contains a multiple of 8 bits. + */ + int BitCount; + + /* + ** This is a referential copy of an element in the bit vector. The + ** purpose of this copy is to allow normal reference access to this + ** object (for speed reasons). This hides the bit packing scheme from + ** the user of this class. + */ + bool Copy; + + /* + ** This records the index of the value last fetched into the reference + ** boolean variable. This index is used to properly restore the value + ** when the reference copy needs updating. + */ + int LastIndex; + + /* + ** This points to the allocated bitfield array. + */ + VectorClass BitArray; }; @@ -447,7 +447,7 @@ class BooleanVectorClass * 03/10/1995 JLB : Created. * *=============================================================================================*/ template -DynamicVectorClass::DynamicVectorClass(unsigned size, T const * array) +DynamicVectorClass::DynamicVectorClass(unsigned size, T const* array) : VectorClass(size, array) { GrowthStep = 10; @@ -475,7 +475,7 @@ DynamicVectorClass::DynamicVectorClass(unsigned size, T const * array) * 03/10/1995 JLB : Created. * *=============================================================================================*/ template -int DynamicVectorClass::Resize(unsigned newsize, T const * array) +int DynamicVectorClass::Resize(unsigned newsize, T const* array) { if (VectorClass::Resize(newsize, array)) { if (Length() < (unsigned)ActiveCount) ActiveCount = Length(); @@ -504,7 +504,7 @@ int DynamicVectorClass::Resize(unsigned newsize, T const * array) * 03/13/1995 JLB : Created. * *=============================================================================================*/ template -int DynamicVectorClass::ID(T const & object) +int DynamicVectorClass::ID(T const& object) { for (int index = 0; index < Count(); index++) { if ((*this)[index] == object) return(index); @@ -531,7 +531,7 @@ int DynamicVectorClass::ID(T const & object) * 03/10/1995 JLB : Created. * *=============================================================================================*/ template -int DynamicVectorClass::Add(T const & object) +int DynamicVectorClass::Add(T const& object) { if (ActiveCount >= (int)Length()) { if ((IsAllocated || !VectorMax) && GrowthStep > 0) { @@ -578,7 +578,7 @@ int DynamicVectorClass::Add(T const & object) * 09/21/1995 JLB : Created. * *=============================================================================================*/ template -int DynamicVectorClass::Add_Head(T const & object) +int DynamicVectorClass::Add_Head(T const& object) { if (ActiveCount >= (int)Length()) { if ((IsAllocated || !VectorMax) && GrowthStep > 0) { @@ -631,7 +631,7 @@ int DynamicVectorClass::Add_Head(T const & object) * 03/10/1995 JLB : Created. * *=============================================================================================*/ template -int DynamicVectorClass::Delete(T const & object) +int DynamicVectorClass::Delete(T const& object) { return(Delete(ID(object))); } @@ -700,7 +700,7 @@ int DynamicVectorClass::Delete(int index) * 03/10/1995 JLB : Created. * *=============================================================================================*/ template -VectorClass::VectorClass(unsigned size, T const * array) : +VectorClass::VectorClass(unsigned size, T const* array) : Vector(0), VectorMax(size), IsAllocated(false) @@ -759,7 +759,7 @@ VectorClass::~VectorClass(void) * 03/10/1995 JLB : Created. * *=============================================================================================*/ template -VectorClass::VectorClass(VectorClass const & vector) : +VectorClass::VectorClass(VectorClass const& vector) : Vector(0), VectorMax(0), IsAllocated(false) @@ -784,7 +784,7 @@ VectorClass::VectorClass(VectorClass const & vector) : * 03/10/1995 JLB : Created. * *=============================================================================================*/ template -VectorClass & VectorClass::operator =(VectorClass const & vector) +VectorClass& VectorClass::operator =(VectorClass const& vector) { if (this != &vector) { Clear(); @@ -824,7 +824,7 @@ VectorClass & VectorClass::operator =(VectorClass const & vector) * 03/10/1995 JLB : Created. * *=============================================================================================*/ template -int VectorClass::operator == (VectorClass const & vector) const +int VectorClass::operator == (VectorClass const& vector) const { if (VectorMax == vector.Length()) { for (int index = 0; index < (int)VectorMax; index++) { @@ -857,9 +857,9 @@ int VectorClass::operator == (VectorClass const & vector) const * 03/13/1995 JLB : Created. * *=============================================================================================*/ template -inline int VectorClass::ID(T const * ptr) +inline int VectorClass::ID(T const* ptr) { - return(((INT_PTR)ptr - (INT_PTR)&(*this)[0]) / sizeof(T)); + return(((INT_PTR)ptr - (INT_PTR) & (*this)[0]) / sizeof(T)); } @@ -880,7 +880,7 @@ inline int VectorClass::ID(T const * ptr) * 03/13/1995 JLB : Created. * *=============================================================================================*/ template -int VectorClass::ID(T const & object) +int VectorClass::ID(T const& object) { for (int index = 0; index < (int)VectorMax; index++) { if ((*this)[index] == object) { @@ -939,7 +939,7 @@ void VectorClass::Clear(void) * 03/10/1995 JLB : Created. * *=============================================================================================*/ template -int VectorClass::Resize(unsigned newsize, T const * array) +int VectorClass::Resize(unsigned newsize, T const* array) { if (newsize) { @@ -947,7 +947,7 @@ int VectorClass::Resize(unsigned newsize, T const * array) ** Allocate a new vector of the size specified. The default constructor ** will be called for every object in this vector. */ - T * newptr; + T* newptr; if (!array) { newptr = new T[newsize]; }