/* Copyright (c) 2002-2012 Croteam Ltd. All rights reserved. */ #ifndef SE_INCL_STATICSTACKARRAY_CPP #define SE_INCL_STATICSTACKARRAY_CPP #ifdef PRAGMA_ONCE #pragma once #endif #include #include /* * Default constructor. */ template inline CStaticStackArray::CStaticStackArray(void) : CStaticArray() { sa_UsedCount=0; sa_ctAllocationStep = 256; } /* * Destructor. */ template inline CStaticStackArray::~CStaticStackArray(void) { }; /* Destroy all objects, and reset the array to initial (empty) state. */ template inline void CStaticStackArray::Clear(void) { if (CStaticArray::Count()!=0) Delete(); } /* * Set how many elements to allocate when stack overflows. */ template inline void CStaticStackArray::SetAllocationStep(INDEX ctStep) { ASSERT(ctStep>0); sa_ctAllocationStep = ctStep; }; /* * Create a given number of objects. */ template inline void CStaticStackArray::New(INDEX iCount) { CStaticArray::New(iCount); sa_UsedCount = 0; }; /* * Destroy all objects. */ template inline void CStaticStackArray::Delete(void) { CStaticArray::Delete(); sa_UsedCount = 0; } /* * Add new object(s) on top of stack. */ template inline Type &CStaticStackArray::Push(void) { sa_UsedCount++; if (sa_UsedCount>CStaticArray::Count()) { Expand(CStaticArray::Count()+sa_ctAllocationStep); } ASSERT(sa_UsedCount <= CStaticArray::Count()); return CStaticArray::operator[](sa_UsedCount-1); } template inline Type *CStaticStackArray::Push(INDEX ct) { sa_UsedCount+=ct; while(sa_UsedCount>CStaticArray::Count()) { Expand(CStaticArray::Count()+sa_ctAllocationStep); } ASSERT(sa_UsedCount <= CStaticArray::Count()); return &CStaticArray::operator[](sa_UsedCount-ct); } /* Remove one object from top of stack and return it. */ template inline Type &CStaticStackArray::Pop(void) { ASSERT(sa_UsedCount>0); sa_UsedCount--; return CStaticArray::operator[](sa_UsedCount); } /* * Remove objects higher than the given index from stack, but keep stack space. */ template inline void CStaticStackArray::PopUntil(INDEX iNewTop) { ASSERT(iNewTop < sa_UsedCount); sa_UsedCount = iNewTop+1; } /* * Remove all objects from stack, but keep stack space. */ template inline void CStaticStackArray::PopAll(void) { sa_UsedCount = 0; } /* * Random access operator. */ template inline Type &CStaticStackArray::operator[](INDEX i) { ASSERT(this!=NULL); ASSERT(i::operator[](i); } template inline const Type &CStaticStackArray::operator[](INDEX i) const { ASSERT(this!=NULL); ASSERT(i::operator[](i); } /* * Get number of elements in array. */ template INDEX CStaticStackArray::Count(void) const { ASSERT(this!=NULL); return sa_UsedCount; } /* * Get index of a member from it's pointer */ template INDEX CStaticStackArray::Index(Type *ptMember) { ASSERT(this!=NULL); INDEX i = CStaticArray::Index(ptMember); ASSERTMSG(i::Index(): Not a member of this array!"); return i; } /* * Assignment operator. */ template CStaticStackArray &CStaticStackArray::operator=(const CStaticStackArray &arOriginal) { ASSERT(this!=NULL); ASSERT(&arOriginal!=NULL); ASSERT(this!=&arOriginal); // copy stack arrays CStaticArray::operator=(arOriginal); // copy used count sa_UsedCount = arOriginal.sa_UsedCount; return *this; } /* Move all elements of another array into this one. */ template void CStaticStackArray::MoveArray(CStaticStackArray &arOther) { ASSERT(this!=NULL); ASSERT(&arOther!=NULL); ASSERT(this!=&arOther); // clear previous contents Clear(); // if the other array has no elements if (arOther.Count()==0) { // no assignment return; } // move data from the other array into this one and clear the other one CStaticArray::MoveArray(arOther); sa_UsedCount = arOther.sa_UsedCount ; sa_ctAllocationStep = arOther.sa_ctAllocationStep ; arOther.sa_UsedCount = 0; } #endif /* include-once check. */