forked from perilouswithadollarsign/cstrike15_src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockarray.cpp
111 lines (94 loc) · 2.02 KB
/
blockarray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//========= Copyright © 1996-2005, 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);
}