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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
#include <cstdlib>
#include <cstdio>
#include <string>
#include <memory.h>
template <class datatype> class CArray
{
public:
CArray(int i) : pObjs(0) // One Dimensional Array Constructor
{
this->pObjs=new(std::nothrow) datatype[i]();
d1=i, d2=0, d3=0, d4=0;
iNumObjects=i;
}
CArray(int i, int j) : pObjs(0) // Two Dimensional Array Constructor
{
int iNumElements=i*j;
this->pObjs=new(std::nothrow) datatype[iNumElements]();
d1=i, d2=j, d3=0, d4=0;
iNumObjects=i*j;
}
CArray(int i, int j, int k) : pObjs() // Three Dimensional Array Constructor
{
int iNumElements=i*j*k;
this->pObjs=new(std::nothrow) datatype[iNumElements]();
d1=i, d2=j, d3=k, d4=0;
iNumObjects=i*j*k;
}
CArray(int i, int j, int k, int l) : pObjs(0) // Four Dimensional Array Constructor
{
int iNumElements=i*j*k*l;
this->pObjs=new(std::nothrow) datatype[iNumElements]();
d1=i, d2=j, d3=k, d4=l;
iNumObjects=i*j*k*l;
}
datatype& operator()(int i) // One Dimensional Accessor
{
return pObjs[i];
}
datatype& operator()(int i, int j) // Two Dimensional Accessor
{
return pObjs[i*d2 + j];
}
datatype& operator()(int i, int j, int k) // Three Dimensional Accessor
{
return pObjs[i*d2 + j + k*d1*d2];
}
datatype& operator()(int i, int j, int k, int l) // Four Dimensional Accessor
{
return pObjs[i*d2 + j + k*d1*d2 + l*d1*d2*d3];
}
bool blnMemoryIsGood()
{
return !!pObjs;
}
int UBound(int iDim)
{
if(iDim==1)
return d1-1;
if(iDim==2)
return d2-1;
if(iDim==3)
return d3-1;
if(iDim==4)
return d4-1;
else
return 0;
}
void ClearMemory() // Non-class types like ints, doubles, etc., need to be initialized
{ // to zero. This must not be done for class types.
memset(this->pObjs, 0, sizeof(datatype)*this->iNumObjects);
}
~CArray()
{
delete [] this->pObjs;
}
private:
int iNumObjects; // We'll need this to zero memory for non-class types
datatype* pObjs; // pointer to the base memory allocation for array
int d1; // Dimension #1
int d2; // Dimension #2
int d3; // Dimension #3
int d4; // Dimension #4
};
template<typename t2> void Output(CArray<t2>& Ar2)
{
int i,j;
for(i=0; i<=Ar2.UBound(1); i++)
{
for(j=0; j<=Ar2.UBound(2); j++)
{
printf("%s\t",Ar2(i,j).c_str());
}
printf("\n");
}
printf("\n\n");
}
int main(void)
{
int c=4,r=3;
CArray<std::string> ar_2(r,c);
if(ar_2.blnMemoryIsGood())
{
char szBuffer[16];
printf("ar_2() Allocated OK!\n");
printf("ar_2.UBound(1) = %d\n",ar_2.UBound(1));
printf("ar_2.UBound(2) = %d\n\n",ar_2.UBound(2));
for(int i=0; i<=ar_2.UBound(1); i++)
{
for(int j=0; j<=ar_2.UBound(2); j++)
{
ar_2(i,j)="(";
sprintf(szBuffer,"%d",i);
ar_2(i,j)=ar_2(i,j)+szBuffer+",";
sprintf(szBuffer,"%d",j);
ar_2(i,j)=ar_2(i,j)+szBuffer+")";
}
}
}
Output(ar_2);
getchar();
return 0;
}
#if 0
ar_2() Allocated OK!
ar_2.UBound(1) = 2
ar_2.UBound(2) = 3
(0,0) (0,1) (0,2) (0,3)
(1,0) (1,1) (1,2) (1,3)
(2,0) (2,1) (2,2) (2,3)
#endif
|