diff options
| author | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
|---|---|---|
| committer | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
| commit | 3bf9df6b2785fa6d951086978a3e66f49427166a (patch) | |
| tree | 2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /hammer/blockarray.h | |
| download | archived-source-engine-2018-hl2-src-master.tar.xz archived-source-engine-2018-hl2-src-master.zip | |
Diffstat (limited to 'hammer/blockarray.h')
| -rw-r--r-- | hammer/blockarray.h | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/hammer/blockarray.h b/hammer/blockarray.h new file mode 100644 index 0000000..9717c6e --- /dev/null +++ b/hammer/blockarray.h @@ -0,0 +1,101 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $NoKeywords: $ +// +//=============================================================================// + +#ifndef _BLOCKARRAY_H +#define _BLOCKARRAY_H + +#include "tier0/dbg.h" + +// memdbgon must be the last include file in a .cpp file!!! +#include <tier0/memdbgon.h> + +template <class T, int nBlockSize, int nMaxBlocks> +class BlockArray +{ +public: + BlockArray() + { + nCount = nBlocks = 0; + } + ~BlockArray() + { + GetBlocks(0); + } + + T& operator[] (int iIndex); + + void SetCount(int nObjects); + int GetCount() { return nCount; } + +private: + T * Blocks[nMaxBlocks+1]; + short nCount; + short nBlocks; + void GetBlocks(int nNewBlocks); +}; + +template <class T, int nBlockSize, int nMaxBlocks> +void BlockArray<T,nBlockSize,nMaxBlocks>:: + GetBlocks(int nNewBlocks) +{ + for(int i = nBlocks; i < nNewBlocks; i++) + { + Blocks[i] = new T[nBlockSize]; + } + for(int i = nNewBlocks; i < nBlocks; i++) + { + delete[] Blocks[i]; + } + + nBlocks = nNewBlocks; +} + +template <class T, int nBlockSize, int nMaxBlocks> +void BlockArray<T,nBlockSize,nMaxBlocks>:: + SetCount(int nObjects) +{ + if(nObjects == nCount) + return; + + // find the number of blocks required by nObjects, checking for + // integer rounding error + int nNewBlocks = (nObjects / nBlockSize); + if ((nNewBlocks * nBlockSize) < nObjects) + { + nNewBlocks++; + } + + if(nNewBlocks != nBlocks) + { + // Make sure we don't get an overrun. + if ( nNewBlocks > ARRAYSIZE( Blocks ) ) + { + Error( "BlockArray< ?, %d, %d > - too many blocks needed.", nBlockSize, nMaxBlocks ); + } + + GetBlocks(nNewBlocks); + } + nCount = nObjects; +} + +template <class T, int nBlockSize, int nMaxBlocks> +T& BlockArray<T,nBlockSize,nMaxBlocks>::operator[] (int iIndex) +{ + // Cast to unsigned so that this check will reject negative values as + // well as overly large values. + if((unsigned)iIndex >= (unsigned)nCount) + { + Error( "BlockArray< %d, %d > - invalid block index.", iIndex, nCount ); + SetCount(iIndex+1); + } + return Blocks[iIndex / nBlockSize][iIndex % nBlockSize]; +} + +#include <tier0/memdbgoff.h> + +#endif // _BLOCKARRAY_H
\ No newline at end of file |