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.cpp | |
| download | archived-source-engine-2018-hl2-src-3bf9df6b2785fa6d951086978a3e66f49427166a.tar.xz archived-source-engine-2018-hl2-src-3bf9df6b2785fa6d951086978a3e66f49427166a.zip | |
Diffstat (limited to 'hammer/blockarray.cpp')
| -rw-r--r-- | hammer/blockarray.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/hammer/blockarray.cpp b/hammer/blockarray.cpp new file mode 100644 index 0000000..7bd52fb --- /dev/null +++ b/hammer/blockarray.cpp @@ -0,0 +1,111 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $NoKeywords: $ +// +//=============================================================================// + +#include <windows.h> +#include <stdio.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> +BlockArray<T,BlockSize,nMaxBlocks>::BlockArray() +{ + nCount = nBlocks = 0; +} + +template <class T, int nBlockSize, int nMaxBlocks> +BlockArray<T,BlockSize,nMaxBlocks>::~BlockArray() +{ + GetBlocks(0); // free blocks +} +*/ + +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(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 + int nNewBlocks = (nObjects / nBlockSize) + 1; + if(nNewBlocks != nBlocks) + GetBlocks(nNewBlocks); + nCount = nObjects; +} + +template <class T, int nBlockSize, int nMaxBlocks> +T& BlockArray<T,nBlockSize,nMaxBlocks>::operator[] (int iIndex) +{ + if(iIndex >= nCount) + SetCount(iIndex+1); + return Blocks[iIndex / nBlockSize][iIndex % nBlockSize]; +} + +typedef struct +{ + char Name[128]; + int iValue; +} Buffy; + +void main(void) +{ + BlockArray<Buffy, 16, 16> Buffies; + + for(int i = 0; i < 256; i++) + { + Buffies[i].iValue = i; + strcpy(Buffies[i].Name, "Buk bUk buK"); + } + + for(i = 0; i < 256; i++) + { + printf("%d: %s\n", Buffies[i].iValue, Buffies[i].Name); + } + + Buffies.SetCount(10); +}
\ No newline at end of file |